|
@@ -8,6 +8,7 @@ using StackExchange.Redis;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
using System.Text;
|
|
|
using System.Text.Json;
|
|
|
using System.Text.RegularExpressions;
|
|
@@ -38,7 +39,8 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
var redisClinet8 = _azureRedis.GetRedisClient(8);
|
|
|
var datetime = DateTimeOffset.UtcNow;
|
|
|
var ynow = datetime.Year;
|
|
|
- //取得CS TeachingData (IOT紀錄只有一年分)
|
|
|
+ List<string> calPropList = new List<string>() { "deviceNoAuth", "deviceAuth", "lessonRecord", "useIES", "useIES5Resource", "useWebIrs", "useDeviceIrs", "useHaboard", "useHita", "lessonLengMin", "stuShow", "stuLessonLengMin", "tGreen", "lTypeCoop", "lTypeIact", "lTypeMis", "lTypeTst", "lTypeDif", "lessonCnt928", "lessonCntId", "lessonCntDevice", "lessonCntIdDevice", "mission", "missionFin", "item", "interact" }; //要計算的ProdAnalysis欄位列表
|
|
|
+ //取得CS Redis TeachingData (IOT紀錄只有三個月分)
|
|
|
List<IotTeachingData> IotTeachingDataList = new List<IotTeachingData>();
|
|
|
if (y.Equals(ynow.ToString()))
|
|
|
{
|
|
@@ -80,7 +82,10 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //統計各校產品分析數據
|
|
|
+ //生成各校每日產品分析數據
|
|
|
+ //1.生成 Redis ProdAnalysis:Month
|
|
|
+ //2.生成 CosmosDB Month
|
|
|
+ //3.生成 CosmosDB 系統所有學校數值加總
|
|
|
List<string> crtVirtualSchoolId = new List<string>(); //虛擬學校創建ID列表
|
|
|
List<ProdAnalysis> ProdAnalysisList = new List<ProdAnalysis>();
|
|
|
if (IotTeachingDataList.Count > 0)
|
|
@@ -88,7 +93,7 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
foreach (IotTeachingData IotTeachingDatRow in IotTeachingDataList)
|
|
|
{
|
|
|
string schoolId = (!string.IsNullOrWhiteSpace(IotTeachingDatRow.schoolId)) ? IotTeachingDatRow.schoolId.ToLower() : "noschoolid"; //noschoolid:無任何學校ID
|
|
|
- if(!schoolId.Equals("noschoolid") && !crtVirtualSchoolId.Contains(schoolId))
|
|
|
+ if(!schoolId.Equals("noschoolid") && !schoolId.Equals("allschool") && !crtVirtualSchoolId.Contains(schoolId))
|
|
|
{
|
|
|
crtVirtualSchoolId.Add(schoolId);
|
|
|
}
|
|
@@ -113,11 +118,11 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
addFlg = true;
|
|
|
}
|
|
|
//欄位加總
|
|
|
- if (!prodAnalysisRow.deviceList.Contains(IotTeachingDatRow.deviceId))
|
|
|
+ if (!string.IsNullOrWhiteSpace(IotTeachingDatRow.deviceId) && !prodAnalysisRow.deviceList.Contains(IotTeachingDatRow.deviceId))
|
|
|
{
|
|
|
prodAnalysisRow.deviceList.Add(IotTeachingDatRow.deviceId);
|
|
|
- prodAnalysisRow.deviceCnt++;
|
|
|
}
|
|
|
+ prodAnalysisRow.deviceCnt = prodAnalysisRow.deviceList.Count;
|
|
|
switch (IotTeachingDatRow.authType)
|
|
|
{
|
|
|
case "0": //928授權
|
|
@@ -140,6 +145,10 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
prodAnalysisRow.lessonCntIdDevice++;
|
|
|
break;
|
|
|
}
|
|
|
+ if (!string.IsNullOrWhiteSpace(IotTeachingDatRow.tmid) && !prodAnalysisRow.tmidList.Contains(IotTeachingDatRow.tmid))
|
|
|
+ {
|
|
|
+ prodAnalysisRow.tmidList.Add(IotTeachingDatRow.tmid);
|
|
|
+ }
|
|
|
prodAnalysisRow.lessonRecord++;
|
|
|
if (IotTeachingDatRow.useIES.Equals("1")) prodAnalysisRow.useIES++;
|
|
|
prodAnalysisRow.useIES5Resource += IotTeachingDatRow.useIES5Resource;
|
|
@@ -167,28 +176,46 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //記入IES5 ProdAnalysis:Day
|
|
|
+ //1.記入IES5 Redis ProdAnalysis:Day
|
|
|
+ //2.記入IES5 CosmosDB Day
|
|
|
if (ProdAnalysisList.Count > 0)
|
|
|
{
|
|
|
//資料整形
|
|
|
Dictionary<string, Dictionary<string, string>> redisSchFieldDic = new Dictionary<string, Dictionary<string, string>>(); //架構: key => schId => schJsonContent
|
|
|
+ List<ProdAnalysisCosmos> cosmosSchList = new List<ProdAnalysisCosmos>();
|
|
|
+ Dictionary <string,ProdAnalysisCosmos> cosmosAllSchSumDayDic = new Dictionary<string, ProdAnalysisCosmos>();
|
|
|
foreach (ProdAnalysis prodAnalysisRow in ProdAnalysisList)
|
|
|
{
|
|
|
+ //Redis整形
|
|
|
string toolType = prodAnalysisRow.toolType;
|
|
|
string schoolId = prodAnalysisRow.schoolId;
|
|
|
string hkey = $"ProdAnalysis:Day:{toolType}:{y}{m}{d}";
|
|
|
string fieldContent = prodAnalysisRow.ToJsonString();
|
|
|
if (redisSchFieldDic.ContainsKey(hkey)) redisSchFieldDic[hkey].Add(schoolId, fieldContent);
|
|
|
else redisSchFieldDic.Add(hkey, new Dictionary<string, string>() { { schoolId, fieldContent } });
|
|
|
+ //CosmosDB整形
|
|
|
+ ProdAnalysisCosmos ProdAnalysisCosmosRow = prodAnalysisRow.ToJsonString().ToObject<ProdAnalysisCosmos>();
|
|
|
+ ProdAnalysisCosmosRow.date = $"{y}{m}{d}";
|
|
|
+ ProdAnalysisCosmosRow.year = Convert.ToInt32(y, 10);
|
|
|
+ ProdAnalysisCosmosRow.month = Convert.ToInt32(m, 10);
|
|
|
+ ProdAnalysisCosmosRow.day = Convert.ToInt32(d, 10);
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(ProdAnalysisCosmosRow.year, ProdAnalysisCosmosRow.month, ProdAnalysisCosmosRow.day, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ ProdAnalysisCosmosRow.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ ProdAnalysisCosmosRow.id = $"{ProdAnalysisCosmosRow.toolType}-{ProdAnalysisCosmosRow.date}-{schoolId}";
|
|
|
+ ProdAnalysisCosmosRow.dateUnit = "day";
|
|
|
+ ProdAnalysisCosmosRow.createDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ cosmosSchList.Add(ProdAnalysisCosmosRow);
|
|
|
}
|
|
|
//記入Redis
|
|
|
- if(redisSchFieldDic.Count > 0) {
|
|
|
+ if (redisSchFieldDic.Count > 0)
|
|
|
+ {
|
|
|
foreach (KeyValuePair<string, Dictionary<string, string>> hkeyItem in redisSchFieldDic)
|
|
|
{
|
|
|
+ //記入Redis
|
|
|
string hkey = hkeyItem.Key;
|
|
|
List<HashEntry> hvalList = new List<HashEntry>();
|
|
|
Dictionary<string, string> fieldDic = hkeyItem.Value;
|
|
|
- foreach(KeyValuePair<string, string> schItem in fieldDic)
|
|
|
+ foreach (KeyValuePair<string, string> schItem in fieldDic)
|
|
|
{
|
|
|
string schoolId = schItem.Key;
|
|
|
string fieldContent = schItem.Value;
|
|
@@ -197,9 +224,62 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
await redisClinet8.HashSetAsync(hkey, hvalList.ToArray());
|
|
|
}
|
|
|
}
|
|
|
+ //記入CosmosDB
|
|
|
+ if (cosmosSchList.Count > 0)
|
|
|
+ {
|
|
|
+ foreach (ProdAnalysisCosmos cosmosSchRow in cosmosSchList)
|
|
|
+ {
|
|
|
+ //各校每日CosmosDB記入
|
|
|
+ await _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ProdAnalysisCosmos>(cosmosSchRow);
|
|
|
+ //所有學校數值加總 數值生成 cosmosAllSchSumDayDic -> key:toolType val:cosmosAllSchSumDay
|
|
|
+ string toolType = cosmosSchRow.toolType;
|
|
|
+ string schoolId = "allschool";
|
|
|
+ ProdAnalysisCosmos cosmosAllSchSumDay = new ProdAnalysisCosmos();
|
|
|
+ if (cosmosAllSchSumDayDic.ContainsKey(toolType)) cosmosAllSchSumDay = cosmosAllSchSumDayDic[toolType];
|
|
|
+ foreach (PropertyInfo propertyInfo in cosmosAllSchSumDay.GetType().GetProperties()) //累加項目
|
|
|
+ {
|
|
|
+ if (calPropList.Contains(propertyInfo.Name))
|
|
|
+ {
|
|
|
+ var propType = propertyInfo.PropertyType;
|
|
|
+ var valNow = propertyInfo.GetValue(cosmosAllSchSumDay);
|
|
|
+ var valTodo = cosmosSchRow.GetType().GetProperty(propertyInfo.Name).GetValue(cosmosSchRow);
|
|
|
+ if (propType.Equals(typeof(long))) propertyInfo.SetValue(cosmosAllSchSumDay, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
|
|
|
+ else propertyInfo.SetValue(cosmosAllSchSumDay, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cosmosAllSchSumDay.schoolId = schoolId;
|
|
|
+ cosmosAllSchSumDay.toolType = cosmosSchRow.toolType;
|
|
|
+ cosmosAllSchSumDay.date = cosmosSchRow.date;
|
|
|
+ cosmosAllSchSumDay.id = $"{cosmosAllSchSumDay.toolType}-{cosmosAllSchSumDay.date}-{schoolId}";
|
|
|
+ cosmosAllSchSumDay.dateUnit = "day";
|
|
|
+ cosmosAllSchSumDay.year = cosmosSchRow.year;
|
|
|
+ cosmosAllSchSumDay.month = cosmosSchRow.month;
|
|
|
+ cosmosAllSchSumDay.day = cosmosSchRow.day;
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(cosmosAllSchSumDay.year, cosmosAllSchSumDay.month, cosmosAllSchSumDay.day, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ cosmosAllSchSumDay.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ cosmosAllSchSumDay.deviceList = cosmosAllSchSumDay.deviceList.Union(cosmosSchRow.deviceList).ToList();
|
|
|
+ cosmosAllSchSumDay.deviceCnt = cosmosAllSchSumDay.deviceList.Count;
|
|
|
+ cosmosAllSchSumDay.tmidList = cosmosAllSchSumDay.tmidList.Union(cosmosSchRow.tmidList).ToList();
|
|
|
+ if (cosmosAllSchSumDayDic.ContainsKey(toolType)) cosmosAllSchSumDayDic[toolType] = cosmosAllSchSumDay;
|
|
|
+ else cosmosAllSchSumDayDic.Add(toolType, cosmosAllSchSumDay);
|
|
|
+ }
|
|
|
+ //每日所有學校數據總計CosmosDB記入
|
|
|
+ foreach (KeyValuePair<string, ProdAnalysisCosmos> schItem in cosmosAllSchSumDayDic)
|
|
|
+ {
|
|
|
+ string toolType = schItem.Key;
|
|
|
+ ProdAnalysisCosmos cosmosAllSchSumDay = schItem.Value;
|
|
|
+ cosmosAllSchSumDay.createDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ await _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ProdAnalysisCosmos>(cosmosAllSchSumDay);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- //取該月所有 ProdAnalysis:Day 生成 ProdAnalysis:Month
|
|
|
+
|
|
|
+ //取 Redis 該月所有 ProdAnalysis:Day
|
|
|
+ //1.生成 Redis ProdAnalysis:Month
|
|
|
+ //2.生成 CosmosDB Month
|
|
|
Dictionary<string, Dictionary<string, ProdAnalysis>> ProdAnalysisListMonth = new Dictionary<string, Dictionary<string, ProdAnalysis>>();
|
|
|
+ Dictionary<string, ProdAnalysisCosmos> cosmosAllSchSumMonthDic = new Dictionary<string, ProdAnalysisCosmos>();
|
|
|
+ List<ProdAnalysisCosmos> cosmosSchListMonth = new List<ProdAnalysisCosmos>();
|
|
|
string patternD = $"ProdAnalysis:Day:HiT*:{y}{m}*";
|
|
|
List<string> keysDayList = ScanRedisKeysByPattern(_azureRedis, patternD);
|
|
|
if (keysDayList.Count > 0)
|
|
@@ -228,40 +308,26 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
string keySchId = hset.Name;
|
|
|
string valSchDataJson = hset.Value;
|
|
|
ProdAnalysis SchDataTodo = valSchDataJson.ToObject<ProdAnalysis>();
|
|
|
- //月Dic已有此key => 分校累加
|
|
|
- if (ProdAnalysisListMonth.ContainsKey(prodAnalMonthKey))
|
|
|
+ //Redis Month 資料生成
|
|
|
+ if (ProdAnalysisListMonth.ContainsKey(prodAnalMonthKey)) //月Dic已有此key => 分校累加
|
|
|
{
|
|
|
if (ProdAnalysisListMonth[$"{prodAnalMonthKey}"].ContainsKey($"{keySchId}"))
|
|
|
{
|
|
|
ProdAnalysis SchDataNow = ProdAnalysisListMonth[$"{prodAnalMonthKey}"][$"{keySchId}"];
|
|
|
- SchDataNow.deviceCnt += SchDataTodo.deviceCnt;
|
|
|
- SchDataNow.deviceNoAuth += SchDataTodo.deviceNoAuth;
|
|
|
- SchDataNow.deviceAuth += SchDataTodo.deviceAuth;
|
|
|
- SchDataNow.lessonRecord += SchDataTodo.lessonRecord;
|
|
|
- SchDataNow.useIES += SchDataTodo.useIES;
|
|
|
- SchDataNow.useIES5Resource += SchDataTodo.useIES5Resource;
|
|
|
- SchDataNow.useWebIrs += SchDataTodo.useWebIrs;
|
|
|
- SchDataNow.useDeviceIrs += SchDataTodo.useDeviceIrs;
|
|
|
- SchDataNow.useHaboard += SchDataTodo.useHaboard;
|
|
|
- SchDataNow.useHita += SchDataTodo.useHita;
|
|
|
- SchDataNow.lessonLengMin += SchDataTodo.lessonLengMin;
|
|
|
- SchDataNow.stuShow += SchDataTodo.stuShow;
|
|
|
- SchDataNow.stuLessonLengMin += SchDataTodo.stuLessonLengMin;
|
|
|
- SchDataNow.tGreen += SchDataTodo.tGreen;
|
|
|
- SchDataNow.lTypeCoop += SchDataTodo.lTypeCoop;
|
|
|
- SchDataNow.lTypeIact += SchDataTodo.lTypeIact;
|
|
|
- SchDataNow.lTypeMis += SchDataTodo.lTypeMis;
|
|
|
- SchDataNow.lTypeTst += SchDataTodo.lTypeTst;
|
|
|
- SchDataNow.lTypeDif += SchDataTodo.lTypeDif;
|
|
|
- SchDataNow.lessonCnt928 += SchDataTodo.lessonCnt928;
|
|
|
- SchDataNow.lessonCntId += SchDataTodo.lessonCntId;
|
|
|
- SchDataNow.lessonCntDevice += SchDataTodo.lessonCntDevice;
|
|
|
- SchDataNow.lessonCntIdDevice += SchDataTodo.lessonCntIdDevice;
|
|
|
- SchDataNow.mission += SchDataTodo.mission;
|
|
|
- SchDataNow.missionFin += SchDataTodo.missionFin;
|
|
|
- SchDataNow.item += SchDataTodo.item;
|
|
|
- SchDataNow.interact += SchDataTodo.interact;
|
|
|
+ foreach (PropertyInfo propertyInfo in SchDataNow.GetType().GetProperties())
|
|
|
+ {
|
|
|
+ if (calPropList.Contains(propertyInfo.Name))
|
|
|
+ {
|
|
|
+ var propType = propertyInfo.PropertyType;
|
|
|
+ var valNow = propertyInfo.GetValue(SchDataNow);
|
|
|
+ var valTodo = SchDataTodo.GetType().GetProperty(propertyInfo.Name).GetValue(SchDataTodo);
|
|
|
+ if (propType.Equals(typeof(long))) propertyInfo.SetValue(SchDataNow, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
|
|
|
+ else propertyInfo.SetValue(SchDataNow, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
|
|
|
+ }
|
|
|
+ }
|
|
|
SchDataNow.deviceList = SchDataNow.deviceList.Union(SchDataTodo.deviceList).ToList();
|
|
|
+ SchDataNow.deviceCnt = SchDataNow.deviceList.Count;
|
|
|
+ SchDataNow.tmidList = SchDataNow.tmidList.Union(SchDataTodo.tmidList).ToList();
|
|
|
}
|
|
|
//無此校資料 => 該校資料放入
|
|
|
else
|
|
@@ -269,13 +335,13 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
ProdAnalysisListMonth[$"{prodAnalMonthKey}"][$"{keySchId}"] = SchDataTodo;
|
|
|
}
|
|
|
}
|
|
|
- //無此月資料 => 所有學校資料放入
|
|
|
- else
|
|
|
+ else //無此月資料 => 所有學校資料放入
|
|
|
{
|
|
|
ProdAnalysisListMonth.Add(prodAnalMonthKey, new Dictionary<string, ProdAnalysis>() { { keySchId, SchDataTodo } });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
if (ProdAnalysisListMonth.Count > 0)
|
|
@@ -289,104 +355,173 @@ namespace TEAMModelOS.SDK.Models.Service.BI
|
|
|
{
|
|
|
string monthRedisSchId = monthRedisSchItem.Key;
|
|
|
ProdAnalysis monthRedisSchData = monthRedisSchItem.Value;
|
|
|
+ //Redis資料製作
|
|
|
hvalList.Add(new HashEntry(monthRedisSchId, monthRedisSchData.ToJsonString()));
|
|
|
+ //CosmosDB資料製作
|
|
|
+ ProdAnalysisCosmos cosmosSchRow = monthRedisSchData.ToJsonString().ToObject<ProdAnalysisCosmos>();
|
|
|
+ cosmosSchRow.date = $"{y}{m}";
|
|
|
+ cosmosSchRow.year = Convert.ToInt32(y, 10);
|
|
|
+ cosmosSchRow.month = Convert.ToInt32(m, 10);
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(cosmosSchRow.year, cosmosSchRow.month, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ cosmosSchRow.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ cosmosSchRow.id = $"{monthRedisSchData.toolType}-{cosmosSchRow.date}-{monthRedisSchId}";
|
|
|
+ cosmosSchRow.dateUnit = "month";
|
|
|
+ cosmosSchRow.createDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ cosmosSchListMonth.Add(cosmosSchRow);
|
|
|
}
|
|
|
+ //記入Redis
|
|
|
await redisClinet8.HashSetAsync($"{monthRedisKey}", hvalList.ToArray());
|
|
|
}
|
|
|
- }
|
|
|
- //取該年所有 ProdAnalysis:Month 生成 ProdAnalysis:Year
|
|
|
- Dictionary<string, Dictionary<string, ProdAnalysis>> ProdAnalysisListYear = new Dictionary<string, Dictionary<string, ProdAnalysis>>();
|
|
|
- string patternM = $"ProdAnalysis:Month:HiT*:{y}*";
|
|
|
- List<string> keysMonthList = ScanRedisKeysByPattern(_azureRedis, patternM);
|
|
|
- if (keysMonthList.Count > 0)
|
|
|
- {
|
|
|
- foreach (string keyMonth in keysMonthList)
|
|
|
+ //記入CosmosDB
|
|
|
+ if (cosmosSchListMonth.Count > 0)
|
|
|
{
|
|
|
- string[] keyItemList = keyMonth.Split(':'); //ProdAnalysis:Month:HiTeach:202303
|
|
|
- string toolType = keyItemList[2];
|
|
|
- string dateStrD = keyItemList[3];
|
|
|
- string dateStrD_y = string.Empty;
|
|
|
- string dateStrD_m = string.Empty;
|
|
|
- Regex rgx = new Regex(@"[0-9]{6}");
|
|
|
- if (rgx.IsMatch(dateStrD))
|
|
|
+ foreach (ProdAnalysisCosmos cosmosSchRow in cosmosSchListMonth)
|
|
|
{
|
|
|
- dateStrD_y = dateStrD.Substring(0, 4);
|
|
|
- dateStrD_m = dateStrD.Substring(4, 2);
|
|
|
+ await _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ProdAnalysisCosmos>(cosmosSchRow);
|
|
|
+ //所有學校數值加總 數值生成 cosmosAllSchSumDayDic -> key:toolType val:cosmosAllSchSumDay
|
|
|
+ string toolType = cosmosSchRow.toolType;
|
|
|
+ string schoolId = "allschool";
|
|
|
+ ProdAnalysisCosmos cosmosAllSchSumMonth = new ProdAnalysisCosmos();
|
|
|
+ if (cosmosAllSchSumMonthDic.ContainsKey(toolType)) cosmosAllSchSumMonth = cosmosAllSchSumMonthDic[toolType];
|
|
|
+ foreach (PropertyInfo propertyInfo in cosmosAllSchSumMonth.GetType().GetProperties()) //累加項目
|
|
|
+ {
|
|
|
+ if (calPropList.Contains(propertyInfo.Name))
|
|
|
+ {
|
|
|
+ var propType = propertyInfo.PropertyType;
|
|
|
+ var valNow = propertyInfo.GetValue(cosmosAllSchSumMonth);
|
|
|
+ var valTodo = cosmosSchRow.GetType().GetProperty(propertyInfo.Name).GetValue(cosmosSchRow);
|
|
|
+ if (propType.Equals(typeof(long))) propertyInfo.SetValue(cosmosAllSchSumMonth, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
|
|
|
+ else propertyInfo.SetValue(cosmosAllSchSumMonth, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cosmosAllSchSumMonth.schoolId = schoolId;
|
|
|
+ cosmosAllSchSumMonth.toolType = cosmosSchRow.toolType;
|
|
|
+ cosmosAllSchSumMonth.date = cosmosSchRow.date;
|
|
|
+ cosmosAllSchSumMonth.id = $"{cosmosAllSchSumMonth.toolType}-{cosmosAllSchSumMonth.date}-{schoolId}";
|
|
|
+ cosmosAllSchSumMonth.dateUnit = "month";
|
|
|
+ cosmosAllSchSumMonth.year = cosmosSchRow.year;
|
|
|
+ cosmosAllSchSumMonth.month = cosmosSchRow.month;
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(cosmosAllSchSumMonth.year, cosmosAllSchSumMonth.month, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ cosmosAllSchSumMonth.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ cosmosAllSchSumMonth.deviceList = cosmosAllSchSumMonth.deviceList.Union(cosmosSchRow.deviceList).ToList();
|
|
|
+ cosmosAllSchSumMonth.deviceCnt = cosmosAllSchSumMonth.deviceList.Count;
|
|
|
+ cosmosAllSchSumMonth.tmidList = cosmosAllSchSumMonth.tmidList.Union(cosmosSchRow.tmidList).ToList();
|
|
|
+ if (cosmosAllSchSumMonthDic.ContainsKey(toolType)) cosmosAllSchSumMonthDic[toolType] = cosmosAllSchSumMonth;
|
|
|
+ else cosmosAllSchSumMonthDic.Add(toolType, cosmosAllSchSumMonth);
|
|
|
}
|
|
|
- string prodAnalYearKey = $"ProdAnalysis:Year:{toolType}:{dateStrD_y}";
|
|
|
- bool ProdAnalysisMonthExist = await redisClinet8.KeyExistsAsync(keyMonth);
|
|
|
- if (ProdAnalysisMonthExist && !string.IsNullOrWhiteSpace(dateStrD_y) && !string.IsNullOrWhiteSpace(dateStrD_m))
|
|
|
+ //每月所有學校數據總計CosmosDB記入
|
|
|
+ foreach (KeyValuePair<string, ProdAnalysisCosmos> schItem in cosmosAllSchSumMonthDic)
|
|
|
{
|
|
|
- HashEntry[] hsetMonth = redisClinet8.HashGetAll(keyMonth); //某月 ProdAnalysis:Month所有學校資料
|
|
|
- foreach (HashEntry hset in hsetMonth)
|
|
|
+ string toolType = schItem.Key;
|
|
|
+ ProdAnalysisCosmos cosmosAllSchSumMonth = schItem.Value;
|
|
|
+ cosmosAllSchSumMonth.createDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ await _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ProdAnalysisCosmos>(cosmosAllSchSumMonth);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //取該年所有 CosmosDB 該年所有月份 生成CosmosDB年資料
|
|
|
+ Dictionary<string, Dictionary<string, ProdAnalysisCosmos>> ProdAnalysisListYear = new Dictionary<string, Dictionary<string, ProdAnalysisCosmos>>();
|
|
|
+ var query = $"SELECT * FROM c WHERE c.year = {y} AND c.dateUnit = 'month' AND c.schoolId != 'allschool'";
|
|
|
+ await foreach (var itemcr in _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("ProdAnalysis") }))
|
|
|
+ {
|
|
|
+ var json = await JsonDocument.ParseAsync(itemcr.ContentStream);
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ {
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ {
|
|
|
+ ProdAnalysisCosmos SchDataTodo = obj.ToObject<ProdAnalysisCosmos>();
|
|
|
+ string toolType = SchDataTodo.toolType;
|
|
|
+ string schId = SchDataTodo.schoolId;
|
|
|
+ //年Dic已有此key => 分校累加
|
|
|
+ if (ProdAnalysisListYear.ContainsKey(toolType))
|
|
|
{
|
|
|
- string keySchId = hset.Name;
|
|
|
- string valSchDataJson = hset.Value;
|
|
|
- ProdAnalysis SchDataTodo = valSchDataJson.ToObject<ProdAnalysis>();
|
|
|
- //年Dic已有此key => 分校累加
|
|
|
- if (ProdAnalysisListYear.ContainsKey(prodAnalYearKey))
|
|
|
+ if (ProdAnalysisListYear[$"{toolType}"].ContainsKey($"{schId}"))
|
|
|
{
|
|
|
- if (ProdAnalysisListYear[$"{prodAnalYearKey}"].ContainsKey($"{keySchId}"))
|
|
|
+ ProdAnalysisCosmos SchDataNow = ProdAnalysisListYear[$"{toolType}"][$"{schId}"];
|
|
|
+ foreach (PropertyInfo propertyInfo in SchDataNow.GetType().GetProperties())
|
|
|
{
|
|
|
- ProdAnalysis SchDataNow = ProdAnalysisListYear[$"{prodAnalYearKey}"][$"{keySchId}"];
|
|
|
- SchDataNow.deviceCnt += SchDataTodo.deviceCnt;
|
|
|
- SchDataNow.deviceNoAuth += SchDataTodo.deviceNoAuth;
|
|
|
- SchDataNow.deviceAuth += SchDataTodo.deviceAuth;
|
|
|
- SchDataNow.lessonRecord += SchDataTodo.lessonRecord;
|
|
|
- SchDataNow.useIES += SchDataTodo.useIES;
|
|
|
- SchDataNow.useIES5Resource += SchDataTodo.useIES5Resource;
|
|
|
- SchDataNow.useWebIrs += SchDataTodo.useWebIrs;
|
|
|
- SchDataNow.useDeviceIrs += SchDataTodo.useDeviceIrs;
|
|
|
- SchDataNow.useHaboard += SchDataTodo.useHaboard;
|
|
|
- SchDataNow.useHita += SchDataTodo.useHita;
|
|
|
- SchDataNow.lessonLengMin += SchDataTodo.lessonLengMin;
|
|
|
- SchDataNow.stuShow += SchDataTodo.stuShow;
|
|
|
- SchDataNow.stuLessonLengMin += SchDataTodo.stuLessonLengMin;
|
|
|
- SchDataNow.tGreen += SchDataTodo.tGreen;
|
|
|
- SchDataNow.lTypeCoop += SchDataTodo.lTypeCoop;
|
|
|
- SchDataNow.lTypeIact += SchDataTodo.lTypeIact;
|
|
|
- SchDataNow.lTypeMis += SchDataTodo.lTypeMis;
|
|
|
- SchDataNow.lTypeTst += SchDataTodo.lTypeTst;
|
|
|
- SchDataNow.lTypeDif += SchDataTodo.lTypeDif;
|
|
|
- SchDataNow.lessonCnt928 += SchDataTodo.lessonCnt928;
|
|
|
- SchDataNow.lessonCntId += SchDataTodo.lessonCntId;
|
|
|
- SchDataNow.lessonCntDevice += SchDataTodo.lessonCntDevice;
|
|
|
- SchDataNow.lessonCntIdDevice += SchDataTodo.lessonCntIdDevice;
|
|
|
- SchDataNow.mission += SchDataTodo.mission;
|
|
|
- SchDataNow.missionFin += SchDataTodo.missionFin;
|
|
|
- SchDataNow.item += SchDataTodo.item;
|
|
|
- SchDataNow.interact += SchDataTodo.interact;
|
|
|
- SchDataNow.deviceList = SchDataNow.deviceList.Union(SchDataTodo.deviceList).ToList();
|
|
|
- }
|
|
|
- //無此校資料 => 該校資料放入
|
|
|
- else
|
|
|
- {
|
|
|
- ProdAnalysisListYear[$"{prodAnalYearKey}"][$"{keySchId}"] = SchDataTodo;
|
|
|
+ if (calPropList.Contains(propertyInfo.Name))
|
|
|
+ {
|
|
|
+ var propType = propertyInfo.PropertyType;
|
|
|
+ var valNow = propertyInfo.GetValue(SchDataNow);
|
|
|
+ var valTodo = SchDataTodo.GetType().GetProperty(propertyInfo.Name).GetValue(SchDataTodo);
|
|
|
+ if (propType.Equals(typeof(long))) propertyInfo.SetValue(SchDataNow, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
|
|
|
+ else propertyInfo.SetValue(SchDataNow, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
|
|
|
+ }
|
|
|
}
|
|
|
+ SchDataNow.deviceList = SchDataNow.deviceList.Union(SchDataTodo.deviceList).ToList();
|
|
|
+ SchDataNow.deviceCnt = SchDataNow.deviceList.Count;
|
|
|
+ SchDataNow.tmidList = SchDataNow.tmidList.Union(SchDataTodo.tmidList).ToList();
|
|
|
}
|
|
|
- //無此年資料 => 所有學校資料放入
|
|
|
+ //無此校資料 => 該校資料放入
|
|
|
else
|
|
|
{
|
|
|
- ProdAnalysisListYear.Add(prodAnalYearKey, new Dictionary<string, ProdAnalysis>() { { keySchId, SchDataTodo } });
|
|
|
+ ProdAnalysisCosmos SchDataNow = SchDataTodo;
|
|
|
+ SchDataNow.date = $"{y}";
|
|
|
+ SchDataNow.year = Convert.ToInt32(y, 10);
|
|
|
+ SchDataNow.month = 0;
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(SchDataNow.year, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ SchDataNow.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ SchDataNow.id = $"{toolType}-{y}-{schId}";
|
|
|
+ SchDataNow.dateUnit = "year";
|
|
|
+ ProdAnalysisListYear[$"{toolType}"][$"{schId}"] = SchDataNow;
|
|
|
}
|
|
|
}
|
|
|
+ //無此年資料 => 所有學校資料放入
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ProdAnalysisCosmos SchDataNow = SchDataTodo;
|
|
|
+ SchDataNow.date = $"{y}";
|
|
|
+ SchDataNow.year = Convert.ToInt32(y, 10);
|
|
|
+ SchDataNow.month = 0;
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(SchDataNow.year, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ SchDataNow.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ SchDataNow.id = $"{toolType}-{y}-{schId}";
|
|
|
+ SchDataNow.dateUnit = "year";
|
|
|
+ ProdAnalysisListYear.Add(toolType, new Dictionary<string, ProdAnalysisCosmos>() { { schId, SchDataNow } });
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (ProdAnalysisListYear.Count > 0)
|
|
|
{
|
|
|
- foreach (KeyValuePair<string, Dictionary<string, ProdAnalysis>> item in ProdAnalysisListYear)
|
|
|
+ foreach (KeyValuePair<string, Dictionary<string, ProdAnalysisCosmos>> item in ProdAnalysisListYear)
|
|
|
{
|
|
|
- string yearRedisKey = item.Key;
|
|
|
- List<HashEntry> hvalList = new List<HashEntry>();
|
|
|
- Dictionary<string, ProdAnalysis> yearRedisSchDIc = item.Value;
|
|
|
- foreach (KeyValuePair<string, ProdAnalysis> yearRedisSchItem in yearRedisSchDIc)
|
|
|
+ string toolType = item.Key;
|
|
|
+ ProdAnalysisCosmos SchDataNow = new ProdAnalysisCosmos(); //當年某產品所有學校總和
|
|
|
+ Dictionary<string, ProdAnalysisCosmos> yearCosmosSchDIc = item.Value;
|
|
|
+ foreach (KeyValuePair<string, ProdAnalysisCosmos> yearCosmosSchItem in yearCosmosSchDIc)
|
|
|
{
|
|
|
- string yearRedisSchId = yearRedisSchItem.Key;
|
|
|
- ProdAnalysis yearRedisSchData = yearRedisSchItem.Value;
|
|
|
- hvalList.Add(new HashEntry(yearRedisSchId, yearRedisSchData.ToJsonString()));
|
|
|
+ string schId = yearCosmosSchItem.Key;
|
|
|
+ ProdAnalysisCosmos yearCosmosSchData = yearCosmosSchItem.Value;
|
|
|
+ await _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ProdAnalysisCosmos>(yearCosmosSchData);
|
|
|
+ //每年所有學校數據總計CosmosDB記入
|
|
|
+ foreach (PropertyInfo propertyInfo in SchDataNow.GetType().GetProperties())
|
|
|
+ {
|
|
|
+ if (calPropList.Contains(propertyInfo.Name))
|
|
|
+ {
|
|
|
+ var propType = propertyInfo.PropertyType;
|
|
|
+ var valNow = propertyInfo.GetValue(SchDataNow);
|
|
|
+ var valTodo = yearCosmosSchData.GetType().GetProperty(propertyInfo.Name).GetValue(yearCosmosSchData);
|
|
|
+ if (propType.Equals(typeof(long))) propertyInfo.SetValue(SchDataNow, Convert.ToInt64(valNow.ToString(), 10) + Convert.ToInt64(valTodo.ToString(), 10));
|
|
|
+ else propertyInfo.SetValue(SchDataNow, Convert.ToInt32(valNow.ToString(), 10) + Convert.ToInt32(valTodo.ToString(), 10));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ SchDataNow.schoolId = "allschool";
|
|
|
+ SchDataNow.toolType = yearCosmosSchData.toolType;
|
|
|
+ SchDataNow.date = yearCosmosSchData.date;
|
|
|
+ SchDataNow.id = $"{yearCosmosSchData.toolType}-{yearCosmosSchData.date}-allschool";
|
|
|
+ SchDataNow.dateUnit = "year";
|
|
|
+ SchDataNow.year = yearCosmosSchData.year;
|
|
|
+ SchDataNow.month = 0;
|
|
|
+ DateTimeOffset dateTime = new DateTimeOffset(SchDataNow.year, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
+ SchDataNow.dateTime = dateTime.ToUnixTimeSeconds();
|
|
|
+ SchDataNow.deviceList = SchDataNow.deviceList.Union(yearCosmosSchData.deviceList).ToList();
|
|
|
+ SchDataNow.deviceCnt = SchDataNow.deviceList.Count;
|
|
|
+ SchDataNow.tmidList = SchDataNow.tmidList.Union(yearCosmosSchData.tmidList).ToList();
|
|
|
}
|
|
|
- await redisClinet8.HashSetAsync($"{yearRedisKey}", hvalList.ToArray());
|
|
|
+ await _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ProdAnalysisCosmos>(SchDataNow);
|
|
|
}
|
|
|
}
|
|
|
|