MonitorCosmosDB.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Text.Json;
  5. using System.Threading.Tasks;
  6. using Azure.Cosmos;
  7. using Microsoft.Azure.Documents;
  8. using Microsoft.Azure.WebJobs;
  9. using Microsoft.Azure.WebJobs.Host;
  10. using Microsoft.Extensions.Logging;
  11. using TEAMModelOS.Models.CommonInfo;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. namespace TEAMModelFunction
  15. {
  16. public class MonitorCosmosDB
  17. {
  18. private readonly AzureCosmosFactory _azureCosmos;
  19. private readonly DingDing _dingDing;
  20. public MonitorCosmosDB( IHttpClientFactory clientFactory,AzureCosmosFactory azureCosmos,DingDing dingDing)
  21. {
  22. _azureCosmos = azureCosmos;
  23. _dingDing = dingDing;
  24. }
  25. [FunctionName("School")]
  26. public async Task School([CosmosDBTrigger(
  27. databaseName: "TEAMModelOS",
  28. collectionName: "Common",
  29. ConnectionStringSetting = "CosmosConnection",
  30. LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
  31. {
  32. try {
  33. var client = _azureCosmos.GetCosmosClient();
  34. List<ExamInfo> exams = new List<ExamInfo>();
  35. string pk = input[0].GetPropertyValue<string>("pk");
  36. if (!string.IsNullOrEmpty(pk) && pk.Equals("Exam", StringComparison.OrdinalIgnoreCase))
  37. {
  38. string code = input[0].GetPropertyValue<string>("code");
  39. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id = '{input[0].Id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{code}") }))
  40. {
  41. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  42. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  43. {
  44. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  45. {
  46. exams.Add(obj.ToObject<ExamInfo>());
  47. }
  48. }
  49. }
  50. for (int i = 0; i < exams.Count; i++)
  51. {
  52. List<ExamClassResult> examClassResults = new List<ExamClassResult>();
  53. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{exams[i].id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{code}") }))
  54. {
  55. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  56. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  57. {
  58. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  59. {
  60. examClassResults.Add(obj.ToObject<ExamClassResult>());
  61. }
  62. }
  63. }
  64. /*if (exams[i].startTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) < 0
  65. && exams[i].endTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) > 0 )
  66. {*/
  67. if (examClassResults.Count < 0)
  68. {
  69. if (exams[i].progress.Equals("going", StringComparison.OrdinalIgnoreCase))
  70. {
  71. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exams[i], exams[i].id.ToString(), new Azure.Cosmos.PartitionKey($"{exams[i].code}"));
  72. for (int j = 0; j < exams[i].subjects.Count; j++)
  73. {
  74. for (int k = 0; k < exams[i].targetClassIds.Count; k++)
  75. {
  76. ExamClassResult result = new ExamClassResult();
  77. result.code = "ExamClassResult-" + exams[i].school;
  78. result.examId = exams[i].id;
  79. result.id = Guid.NewGuid().ToString();
  80. result.subjectId = exams[i].subjects[j].id;
  81. result.year = exams[i].year;
  82. result.ttl = -1;
  83. result.scope = exams[i].scope;
  84. result.pk = typeof(ExamClassResult).Name;
  85. result.info.id = exams[i].targetClassIds[k];
  86. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(exams[i].targetClassIds[k], new Azure.Cosmos.PartitionKey($"Class-{exams[i].school}"));
  87. if (sresponse.Status == 200)
  88. {
  89. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  90. Classroom classroom = json.ToObject<Classroom>();
  91. result.info.name = classroom.name;
  92. List<List<string>> ans = new List<List<string>>();
  93. List<double> ansPoint = new List<double>();
  94. foreach (double p in exams[i].papers[j].point)
  95. {
  96. ans.Add(new List<string>());
  97. ansPoint.Add(0);
  98. }
  99. foreach (StudentSimple stu in classroom.students)
  100. {
  101. result.studentIds.Add(stu.id);
  102. result.studentAnswers.Add(ans);
  103. result.studentScores.Add(ansPoint);
  104. }
  105. }
  106. result.progress = exams[i].progress;
  107. result.school = exams[i].school;
  108. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  109. }
  110. }
  111. }
  112. }
  113. if (exams[i].progress.Equals("finish", StringComparison.OrdinalIgnoreCase))
  114. {
  115. for (int j = 0; j < exams[i].subjects.Count; j++)
  116. {
  117. ExamResult result = new ExamResult();
  118. result.ttl = -1;
  119. result.pk = typeof(ExamResult).Name;
  120. result.code = "ExamResult-" + exams[i].school;
  121. result.school = exams[i].school;
  122. result.id = Guid.NewGuid().ToString();
  123. result.examId = exams[i].id;
  124. result.subjectId = exams[i].subjects[j].id;
  125. result.year = exams[i].year;
  126. result.paper = exams[i].papers[j];
  127. result.point = exams[i].papers[j].point;
  128. result.scope = exams[i].scope;
  129. result.name = exams[i].name;
  130. //result.time
  131. //人数总和
  132. int Count = 0;
  133. int m = 0;
  134. List<ClassRange> classRanges = new List<ClassRange>();
  135. foreach (ExamClassResult classResult in examClassResults)
  136. {
  137. //处理班级信息
  138. ClassRange range = new ClassRange();
  139. range.id = classResult.info.id;
  140. range.name = classResult.info.name;
  141. List<int> ran = new List<int>();
  142. int stuCount = classResult.studentIds.Count;
  143. Count += stuCount;
  144. if (m == 0)
  145. {
  146. ran.Add(0);
  147. ran.Add(stuCount - 1);
  148. }
  149. else
  150. {
  151. ran.Add(Count - stuCount);
  152. ran.Add(Count - 1);
  153. }
  154. m++;
  155. range.range = ran;
  156. classRanges.Add(range);
  157. //处理学生ID
  158. foreach (string id in classResult.studentIds)
  159. {
  160. result.studentIds.Add(id);
  161. }
  162. foreach (List<double> scores in classResult.studentScores)
  163. {
  164. result.studentScores.Add(scores);
  165. }
  166. }
  167. result.classes = classRanges;
  168. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{result.school}"));
  169. }
  170. }
  171. }
  172. }
  173. } catch (Exception e) {
  174. await _dingDing.SendBotMsg($"CosmosDB_common,School()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  175. return;
  176. }
  177. }
  178. }
  179. }