MonitorCosmosDB.cs 9.6 KB

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