123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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.Extensions.Logging;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelFunction
- {
- public class MonitorCosmosDB
- {
- private readonly IHttpClientFactory _clientFactory;
- private readonly AzureCosmosFactory _azureCosmos;
- public MonitorCosmosDB(IHttpClientFactory clientFactory, AzureCosmosFactory azureCosmos)
- {
- _clientFactory = clientFactory;
- _azureCosmos = azureCosmos;
- }
- [FunctionName("School")]
- public async Task School([CosmosDBTrigger(
- databaseName: "TEAMModelOS",
- collectionName: "Common",
- ConnectionStringSetting = "CosmosConnection",
- LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
- {
- if (input != null && input.Count > 0)
- {
- log.LogInformation("Documents modified " + input.Count);
- log.LogInformation("First document Id " + input[0].Id);
- }
- //input[0]
- var client = _azureCosmos.GetCosmosClient();
- List<ExamInfo> exams = new List<ExamInfo>();
- string pk = input[0].GetPropertyValue<string>("pk");
- if (!string.IsNullOrEmpty(pk) && pk.Equals("Exam"))
- {
- string code = input[0].GetPropertyValue<string>("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<ExamInfo>());
- }
- }
- }
- for (int i = 0; i < exams.Count; i++)
- {
- List<ExamClassResult> examClassResults = new List<ExamClassResult>();
- 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<ExamClassResult>());
- }
- }
- }
- /*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"))
- {
- //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<Classroom>();
- result.info.name = classroom.name;
- List<List<string>> ans = new List<List<string>>();
- List<double> ansPoint = new List<double>();
- foreach (double p in exams[i].papers[j].point)
- {
- ans.Add(new List<string>());
- 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"))
- {
- 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<ClassRange> classRanges = new List<ClassRange>();
- foreach (ExamClassResult classResult in examClassResults)
- {
- //处理班级信息
- ClassRange range = new ClassRange();
- range.id = classResult.info.id;
- range.name = classResult.info.name;
- List<int> ran = new List<int>();
- 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<double> 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}"));
- }
- }
- }
- }
- }
- }
- }
|