Browse Source

Merge branch 'develop3.0-tmd' of http://106.12.23.251:10080/TEAMMODEL/TEAMModelOS into develop3.0-tmd

OnePsycho 4 years ago
parent
commit
d91838ce1d
2 changed files with 127 additions and 132 deletions
  1. 124 126
      TEAMModelFunction/MonitorCosmosDB.cs
  2. 3 6
      TEAMModelFunction/ServiceBusTopic.cs

+ 124 - 126
TEAMModelFunction/MonitorCosmosDB.cs

@@ -6,9 +6,7 @@ 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;
 
@@ -16,14 +14,14 @@ namespace TEAMModelFunction
 {
     public class MonitorCosmosDB
     {
+        private readonly IHttpClientFactory _clientFactory;
         private readonly AzureCosmosFactory _azureCosmos;
-        private readonly DingDing _dingDing;
 
-        public MonitorCosmosDB( IHttpClientFactory clientFactory,AzureCosmosFactory azureCosmos,DingDing dingDing)
+        public MonitorCosmosDB(IHttpClientFactory clientFactory, AzureCosmosFactory azureCosmos)
         {
+            _clientFactory = clientFactory;
             _azureCosmos = azureCosmos;
-            _dingDing = dingDing;
-    }
+        }
 
         [FunctionName("School")]
         public async Task School([CosmosDBTrigger(
@@ -32,156 +30,156 @@ namespace TEAMModelFunction
             ConnectionStringSetting = "CosmosConnection",
             LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
         {
-            try {
-                var client = _azureCosmos.GetCosmosClient();
-                List<ExamInfo> exams = new List<ExamInfo>();
-                string pk = input[0].GetPropertyValue<string>("pk");
-                if (!string.IsNullOrEmpty(pk) && pk.Equals("Exam", StringComparison.OrdinalIgnoreCase))
+            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}") }))
                 {
-                    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)
                     {
-                        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())
                         {
-                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
-                            {
 
-                                exams.Add(obj.ToObject<ExamInfo>());
-                            }
+                            exams.Add(obj.ToObject<ExamInfo>());
                         }
                     }
-                    for (int i = 0; i < exams.Count; i++)
+                }
+                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}") }))
                     {
-                        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)
                         {
-                            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())
                             {
-                                foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
-                                {
-                                    examClassResults.Add(obj.ToObject<ExamClassResult>());
-                                }
+                                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].startTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) < 0 
+                        && exams[i].endTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) > 0 )
+                    {*/
+                    if (examClassResults.Count < 0)
+                    {
+                        if (exams[i].progress.Equals("going"))
                         {
-                            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++)
+                            //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++)
                                 {
-                                    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)
                                     {
-                                        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)
                                         {
-                                            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);
-                                            }
+                                            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}"));
-
                                     }
+                                    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))
+                    }
+                    if (exams[i].progress.Equals("finish"))
+                    {
+                        for (int j = 0; j < exams[i].subjects.Count; j++)
                         {
-                            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
+                            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)
+                            //人数总和
+                            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)
                                 {
-                                    //处理班级信息
-                                    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);
-                                    }
+                                    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}"));
                             }
+                            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;
             }
-                     
         }
     }
 }

+ 3 - 6
TEAMModelFunction/ServiceBusTopic.cs

@@ -45,8 +45,7 @@ namespace TEAMModelFunction
                     await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
                 }
             } catch (Exception ex) {
-                await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
-                return;
+                await _dingDing.SendBotMsg($"ServiceBusㄛExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);               
             }
                               
         }
@@ -79,8 +78,7 @@ namespace TEAMModelFunction
                     }
                 }
             } catch (Exception ex){
-                await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
-                return;
+                await _dingDing.SendBotMsg($"ServiceBusㄛVoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);               
             }
                          
         }
@@ -108,8 +106,7 @@ namespace TEAMModelFunction
                 }
             }
             catch (Exception ex){
-                await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
-                return;
+                await _dingDing.SendBotMsg($"ServiceBusㄛVoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);                
             }                       
         }
     }