using System; using System.Collections.Generic; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; using Azure.Cosmos; using Microsoft.Azure.Documents; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; using TEAMModelOS.Models.CommonInfo; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; namespace TEAMModelFunction { public class MonitorCosmosDB { private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; public MonitorCosmosDB( IHttpClientFactory clientFactory,AzureCosmosFactory azureCosmos,DingDing dingDing) { _azureCosmos = azureCosmos; _dingDing = dingDing; } [FunctionName("School")] public async Task School([CosmosDBTrigger( databaseName: "TEAMModelOS", collectionName: "Common", ConnectionStringSetting = "CosmosConnection", LeaseCollectionName = "leases")]IReadOnlyList input, ILogger log) { try { var client = _azureCosmos.GetCosmosClient(); List exams = new List(); string pk = input[0].GetPropertyValue("pk"); if (!string.IsNullOrEmpty(pk) && pk.Equals("Exam", StringComparison.OrdinalIgnoreCase)) { string code = input[0].GetPropertyValue("code"); 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}") })) { using var json = await JsonDocument.ParseAsync(item.ContentStream); if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0) { foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) { exams.Add(obj.ToObject()); } } } for (int i = 0; i < exams.Count; i++) { List examClassResults = new List(); 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}") })) { using var json = await JsonDocument.ParseAsync(item.ContentStream); if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0) { foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) { examClassResults.Add(obj.ToObject()); } } } /*if (exams[i].startTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) < 0 && exams[i].endTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) > 0 ) {*/ if (examClassResults.Count < 0) { if (exams[i].progress.Equals("going", StringComparison.OrdinalIgnoreCase)) { //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exams[i], exams[i].id.ToString(), new Azure.Cosmos.PartitionKey($"{exams[i].code}")); for (int j = 0; j < exams[i].subjects.Count; j++) { for (int k = 0; k < exams[i].targetClassIds.Count; k++) { ExamClassResult result = new ExamClassResult(); result.code = "ExamClassResult-" + exams[i].school; result.examId = exams[i].id; result.id = Guid.NewGuid().ToString(); result.subjectId = exams[i].subjects[j].id; result.year = exams[i].year; result.ttl = -1; result.scope = exams[i].scope; result.pk = typeof(ExamClassResult).Name; result.info.id = exams[i].targetClassIds[k]; var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(exams[i].targetClassIds[k], new Azure.Cosmos.PartitionKey($"Class-{exams[i].school}")); if (sresponse.Status == 200) { using var json = await JsonDocument.ParseAsync(sresponse.ContentStream); Classroom classroom = json.ToObject(); result.info.name = classroom.name; List> ans = new List>(); List ansPoint = new List(); foreach (double p in exams[i].papers[j].point) { ans.Add(new List()); ansPoint.Add(0); } foreach (StudentSimple stu in classroom.students) { result.studentIds.Add(stu.id); result.studentAnswers.Add(ans); result.studentScores.Add(ansPoint); } } result.progress = exams[i].progress; result.school = exams[i].school; await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}")); } } } } if (exams[i].progress.Equals("finish", StringComparison.OrdinalIgnoreCase)) { for (int j = 0; j < exams[i].subjects.Count; j++) { ExamResult result = new ExamResult(); result.ttl = -1; result.pk = typeof(ExamResult).Name; result.code = "ExamResult-" + exams[i].school; result.school = exams[i].school; result.id = Guid.NewGuid().ToString(); result.examId = exams[i].id; result.subjectId = exams[i].subjects[j].id; result.year = exams[i].year; result.paper = exams[i].papers[j]; result.point = exams[i].papers[j].point; result.scope = exams[i].scope; result.name = exams[i].name; //result.time //人数总和 int Count = 0; int m = 0; List classRanges = new List(); foreach (ExamClassResult classResult in examClassResults) { //处理班级信息 ClassRange range = new ClassRange(); range.id = classResult.info.id; range.name = classResult.info.name; List ran = new List(); int stuCount = classResult.studentIds.Count; Count += stuCount; if (m == 0) { ran.Add(0); ran.Add(stuCount - 1); } else { ran.Add(Count - stuCount); ran.Add(Count - 1); } m++; range.range = ran; classRanges.Add(range); //处理学生ID foreach (string id in classResult.studentIds) { result.studentIds.Add(id); } foreach (List scores in classResult.studentScores) { result.studentScores.Add(scores); } } result.classes = classRanges; await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{result.school}")); } } } } } catch (Exception e) { await _dingDing.SendBotMsg($"CosmosDB_common,School()\n{e.Message}", GroupNames.醍摩豆服務運維群組); return; } } } }