zhouj1203@hotmail.com 1 year ago
parent
commit
8c8fee8fa6

+ 133 - 128
TEAMModelOS.FunctionV4/CosmosDB/TriggerExam.cs

@@ -1472,11 +1472,12 @@ namespace TEAMModelOS.FunctionV4
             }*/
             //处理人员变更时被移除的人员
             result.lostStus = lostStu;
+            List<string> stuIds = examClassResults.SelectMany(x => x.studentIds).ToList();
             if (result.lostStus.Count > 0)
             {
                 for (int i = 0; i < result.lostStus.Count; i++)
                 {
-                    if (!examClassResults[0].studentIds.Contains(result.lostStus[i]))
+                    if (!stuIds.Contains(result.lostStus[i]))
                     {
                         result.lostStus.RemoveAt(i);
                     }
@@ -1518,154 +1519,158 @@ namespace TEAMModelOS.FunctionV4
             {
                 errorItems.Add(item);
             }
-            if (errorItems.Count == 0)
-            {
-                // 新增逻辑 收集错题内容
-                BlobDownloadResult index_json;
-                if (info.scope.Equals("school"))
-                {
-                    index_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{info.papers[no].blob}/index.json").DownloadContentAsync();
-                }
-                else
+            try {
+                if (errorItems.Count == 0)
                 {
-                    index_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{info.papers[no].blob}/index.json").DownloadContentAsync();
-                }
-                //BlobDownloadResult index_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{info.papers[no].blob}/index.json").DownloadContentAsync();
-                JObject jo = JObject.Parse(index_json.Content.ToString());
-                JArray array = jo.Value<JArray>("slides");
-                List<string> urls = new();
-                foreach (var ne in array)
-                {
-                    if (!ne["type"].ToString().Equals("compose"))
+                    // 新增逻辑 收集错题内容
+                    BlobDownloadResult index_json;
+                    if (info.scope.Equals("school"))
                     {
-                        urls.Add(ne["url"].ToString());
+                        index_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{info.papers[no].blob}/index.json").DownloadContentAsync();
                     }
-                }
-                string code = string.Empty;
-                string pk = string.Empty;
-                string table = "School";
-                if (info.scope.Equals("school"))
-                {
-                    code = $"ErrorItems-{info.school}";
-                    pk = $"Item-{info.school}";
-                }
-                else
-                {
-                    code = "ErrorItems";
-                    pk = $"Item-{info.creatorId}";
-                    table = "Teacher";
-                }
-                // 获取整体的题目ID集合
-                List<string> ids = new();
-                List<(string id, string pid, int level, string type, List<string> knows)> itemInfos = new();
-                foreach (string url in urls)
-                {
-                    string id = url.Replace(".json", "");
-                    BlobDownloadResult index_item_json;
+                    else
+                    {
+                        index_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{info.papers[no].blob}/index.json").DownloadContentAsync();
+                    }
+                    //BlobDownloadResult index_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{info.papers[no].blob}/index.json").DownloadContentAsync();
+                    JObject jo = JObject.Parse(index_json.Content.ToString());
+                    JArray array = jo.Value<JArray>("slides");
+                    List<string> urls = new();
+                    foreach (var ne in array)
+                    {
+                        if (!ne["type"].ToString().Equals("compose"))
+                        {
+                            urls.Add(ne["url"].ToString());
+                        }
+                    }
+                    string code = string.Empty;
+                    string pk = string.Empty;
+                    string table = "School";
                     if (info.scope.Equals("school"))
                     {
-                        index_item_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{info.papers[no].blob}/{url}").DownloadContentAsync();
+                        code = $"ErrorItems-{info.school}";
+                        pk = $"Item-{info.school}";
                     }
                     else
                     {
-                        index_item_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{info.papers[no].blob}/{url}").DownloadContentAsync();
+                        code = "ErrorItems";
+                        pk = $"Item-{info.creatorId}";
+                        table = "Teacher";
+                    }
+                    // 获取整体的题目ID集合
+                    List<string> ids = new();
+                    List<(string id, string pid, int level, string type, List<string> knows)> itemInfos = new();
+                    foreach (string url in urls)
+                    {
+                        string id = url.Replace(".json", "");
+                        BlobDownloadResult index_item_json;
+                        if (info.scope.Equals("school"))
+                        {
+                            index_item_json = await _azureStorage.GetBlobContainerClient($"{info.school}").GetBlobClient($"{info.papers[no].blob}/{url}").DownloadContentAsync();
+                        }
+                        else
+                        {
+                            index_item_json = await _azureStorage.GetBlobContainerClient($"{info.creatorId}").GetBlobClient($"{info.papers[no].blob}/{url}").DownloadContentAsync();
+                        }
+                        JObject keys = JObject.Parse(index_item_json.Content.ToString());
+                        string type = keys["exercise"].Value<string>("type");
+                        int level = keys["exercise"].Value<int>("level");
+                        var knowledge = keys["exercise"].Value<JArray>("knowledge")?.ToObject<List<string>>();
+                        string pid = keys.Value<string>("pid");
+                        itemInfos.Add((id, pid, level, type, knowledge));
                     }
-                    JObject keys = JObject.Parse(index_item_json.Content.ToString());
-                    string type = keys["exercise"].Value<string>("type");
-                    int level = keys["exercise"].Value<int>("level");
-                    var knowledge = keys["exercise"].Value<JArray>("knowledge")?.ToObject<List<string>>();
-                    string pid = keys.Value<string>("pid");
-                    itemInfos.Add((id, pid, level, type, knowledge));
-                }
 
 
-                /*await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, table).GetItemQueryStreamIterator(
-                                    queryText: $"select c.id,c.pid,c.level,c.type from c where c.id in ({string.Join(",", ids.Select(o => $"'{o}'"))})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{pk}") }))
-                {
-                    using var json = await JsonDocument.ParseAsync(item.ContentStream);
-                    if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                    /*await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, table).GetItemQueryStreamIterator(
+                                        queryText: $"select c.id,c.pid,c.level,c.type from c where c.id in ({string.Join(",", ids.Select(o => $"'{o}'"))})", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{pk}") }))
                     {
-                        var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
-                        while (accounts.MoveNext())
+                        using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                         {
-                            JsonElement account = accounts.Current;
-                            string pid = "";
-                            if (account.TryGetProperty("pid",out JsonElement element)) {
-                                pid = element.GetString();
+                            var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
+                            while (accounts.MoveNext())
+                            {
+                                JsonElement account = accounts.Current;
+                                string pid = "";
+                                if (account.TryGetProperty("pid",out JsonElement element)) {
+                                    pid = element.GetString();
+                                }
+                                itemInfos.Add((account.GetProperty("id").GetString(), pid, account.GetProperty("level").GetInt32(), account.GetProperty("type").GetString()));
                             }
-                            itemInfos.Add((account.GetProperty("id").GetString(), pid, account.GetProperty("level").GetInt32(), account.GetProperty("type").GetString()));
                         }
-                    }
-                }*/
+                    }*/
 
-                double[] point = StringHelper.ListTodouble(result.paper.point);
-                double[,] res = StringHelper.ListToDouble(result.studentScores);
-                var cdm = new ClouDASMatrix(res, point);
-                //需要努力的题目
-                var ss = cdm.StriveTopic;
-                int n = 0;
-                List<Task<ItemResponse<ErrorItems>>> task_error = new();
-                List<ErrorItems> errors = new();
-                foreach (var sIds in result.studentIds)
-                {
-                    ErrorItems error = new()
+                    double[] point = StringHelper.ListTodouble(result.paper.point);
+                    double[,] res = StringHelper.ListToDouble(result.studentScores);
+                    var cdm = new ClouDASMatrix(res, point);
+                    //需要努力的题目
+                    var ss = cdm.StriveTopic;
+                    int n = 0;
+                    List<Task<ItemResponse<ErrorItems>>> task_error = new();
+                    List<ErrorItems> errors = new();
+                    foreach (var sIds in result.studentIds)
                     {
-                        ttl = -1,
-                        code = code,
-                        id = Guid.NewGuid().ToString(),
-                        stuId = sIds,
-                        school = info.school,
-                        activityId = info.id,
-                        subjectId = result.subjectId,
-                        time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
-                    };
-                    //顺序学生错题的索引
-                    int[] item_index = ss[n];
-                    foreach (var item in item_index)
-                    {
-                        Items items = new()
+                        ErrorItems error = new()
                         {
-                            id = urls[item - 1].Replace(".json", ""),
-                            blob = info.papers[no].blob
-
+                            ttl = -1,
+                            code = code,
+                            id = Guid.NewGuid().ToString(),
+                            stuId = sIds,
+                            school = info.school,
+                            activityId = info.id,
+                            subjectId = result.subjectId,
+                            time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                         };
-                        items.level = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().level;
-                        items.type = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().type;
-                        items.pId = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().pid;
-                        items.knowledge = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().knows;
-                        error.its.Add(items);
+                        //顺序学生错题的索引
+                        int[] item_index = ss[n];
+                        foreach (var item in item_index)
+                        {
+                            Items items = new()
+                            {
+                                id = urls[item - 1].Replace(".json", ""),
+                                blob = info.papers[no].blob
+
+                            };
+                            items.level = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().level;
+                            items.type = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().type;
+                            items.pId = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().pid;
+                            items.knowledge = itemInfos.Where(c => c.id.Equals(items.id)).FirstOrDefault().knows;
+                            error.its.Add(items);
+                        }
+                        errors.Add(error);
+                        n++;
+                        task_error.Add(_azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(error, new PartitionKey($"{error.code}")));
                     }
-                    errors.Add(error);
-                    n++;
-                    task_error.Add(_azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(error, new PartitionKey($"{error.code}")));
-                }
-                //string connect = _configuration.GetValue<string>("HaBookAuth:Crtmabank") + "https://malearnfunction.chinacloudsites.cn/api/crtmabank";
-                string location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
-                string urlAction = string.Empty;
-                if (location.Contains("China") || location.Contains("China-Dep"))
-                {
-                    urlAction = "https://malearnfunction.chinacloudsites.cn";
-                }
-                else if (location.Contains("Global"))
-                {
-                    urlAction = "https://malearnfunction.azurewebsites.net";
-                }
-                else if (location.Contains("Global-Test"))
-                {
-                    urlAction = "https://malearn-test.azurewebsites.net";
-                }
-                string connect = $"{urlAction}/api/crtmabank";
-                var htc = _httpClient.CreateClient();
-                if (htc.DefaultRequestHeaders.Contains("x-functions-key"))
-                {
-                    htc.DefaultRequestHeaders.Remove("x-functions-key");
+                    //string connect = _configuration.GetValue<string>("HaBookAuth:Crtmabank") + "https://malearnfunction.chinacloudsites.cn/api/crtmabank";
+                    string location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
+                    string urlAction = string.Empty;
+                    if (location.Contains("China") || location.Contains("China-Dep") || location.Contains("China-Test"))
+                    {
+                        urlAction = "https://malearnfunction.chinacloudsites.cn";
+                    }
+                    else if (location.Contains("Global"))
+                    {
+                        urlAction = "https://malearnfunction.azurewebsites.net";
+                    }
+                    else if (location.Contains("Global-Test"))
+                    {
+                        urlAction = "https://malearn-test.azurewebsites.net";
+                    }
+                    string connect = $"{urlAction}/api/crtmabank";
+                    var htc = _httpClient.CreateClient();
+                    if (htc.DefaultRequestHeaders.Contains("x-functions-key"))
+                    {
+                        htc.DefaultRequestHeaders.Remove("x-functions-key");
+                    }
+                    htc.DefaultRequestHeaders.Add("x-functions-key", "QUzQqbqbnLsTDTeaJy4Br6wUuqPoAlKpzRK2S6PGImRHAzFuySGAeA==");
+                    string paramJson = JsonConvert.SerializeObject(errors);
+                    var content = new StringContent(paramJson, Encoding.UTF8, "application/json");
+                    await htc.PostAsync(connect, content);
+                    await task_error.TaskPage(10);
                 }
-                htc.DefaultRequestHeaders.Add("x-functions-key", "QUzQqbqbnLsTDTeaJy4Br6wUuqPoAlKpzRK2S6PGImRHAzFuySGAeA==");
-                string paramJson = JsonConvert.SerializeObject(errors);
-                var content = new StringContent(paramJson, Encoding.UTF8, "application/json");
-                await htc.PostAsync(connect, content);
-                await task_error.TaskPage(10);
-            }
+            } catch (Exception e) {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测错题异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
+            }           
         }
 
         public class Settlement

+ 17 - 5
TEAMModelOS/Controllers/Analysis/AnalysisController.cs

@@ -431,7 +431,7 @@ namespace TEAMModelOS.Controllers.Analysis
                           x.csRate = totalAll > 0 ? Math.Round(totalAverage / totalAll,2) : 0;*/
                         //标准差 介于缺考人员的判定逻辑 多科考试必须多科同时缺考才能算整个评测缺考
                         int inx = examClassResults.FirstOrDefault(e => e.info.id.Equals(rGroup.id)).studentIds.IndexOf(x.id);
-                        var statu = examClassResults.Select(x => x.status).ToList();
+                        var statu = examClassResults.Where(e => e.info.id.Equals(rGroup.id)).Select(x => x.status).ToList();
                         bool flag = false;
                         foreach (var ets in statu) {
                             if (ets[inx] == 0) {
@@ -1530,7 +1530,17 @@ namespace TEAMModelOS.Controllers.Analysis
                 List<Dictionary<string, object>> averageMap = new List<Dictionary<string, object>>();
                 List<Dictionary<string, object>> averageTotal = new List<Dictionary<string, object>>();
                 var client = _azureCosmos.GetCosmosClient();
-                ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"Exam-{code}"));
+                //todo  需要检查数据是否存在
+                ExamInfo info = new();
+                var response = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Exam-{code}"));
+                if (response.Status == 200)
+                {
+                    using var cJson = await JsonDocument.ParseAsync(response.ContentStream);
+                    info = cJson.ToObject<ExamInfo>();
+                }
+                else {
+                    return Ok(new { averageMap, averageTotal });
+                }
                 List<ExamResult> examResults = new List<ExamResult>();
                 var query = $"select c.id,c.name,c.subjectId,c.studentScores,c.studentIds,c.paper,c.classes,c.afp,c.akp,c.lostStus from c where c.examId =  '{id}' ";
                 await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamResult>(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamResult-{id}") }))
@@ -1566,9 +1576,11 @@ namespace TEAMModelOS.Controllers.Analysis
                     mapClass.Add("subjectId", result.subjectId);
                     foreach (ClassRange range in result.classes)
                     {
-                        ClassSimple classSimple = new ClassSimple();
-                        classSimple.id = range.id;
-                        classSimple.name = range.name;
+                        ClassSimple classSimple = new()
+                        {
+                            id = range.id,
+                            name = range.name
+                        };
                         ClassName.Add(classSimple);
                         double totalClass = 0;
                         for (int i = range.range[0]; i <= range.range[1]; i++)