MonitorCosmosDB.cs 9.9 KB

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