IESTimerTrigger.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using DinkToPdf;
  4. using DinkToPdf.Contracts;
  5. using HTEXLib.COMM.Helpers;
  6. using Microsoft.Azure.Cosmos.Table;
  7. using Microsoft.Azure.Functions.Worker;
  8. using Microsoft.Azure.Functions.Worker.Http;
  9. using Microsoft.Extensions.Logging;
  10. using StackExchange.Redis;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Net.Http;
  17. using System.Reflection;
  18. using System.Text;
  19. using System.Text.Json;
  20. using System.Threading.Tasks;
  21. using System.Web;
  22. using TEAMModelOS.SDK;
  23. using TEAMModelOS.SDK.DI;
  24. using TEAMModelOS.SDK.Extension;
  25. using TEAMModelOS.SDK.Models;
  26. using TEAMModelOS.SDK.Models.Cosmos.Teacher;
  27. using TEAMModelOS.SDK.Models.Service;
  28. using TEAMModelOS.SDK.Models.Service.BI;
  29. using TEAMModelOS.SDK.Models.Table;
  30. using static TEAMModelOS.SDK.Models.Teacher;
  31. namespace TEAMModelOS.FunctionV4.TimeTrigger
  32. {
  33. public class IESTimerTrigger
  34. {
  35. /// <summary>
  36. /// 文档。https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
  37. /// Timer 在线测试 https://ncrontab.swimburger.net/
  38. /// </summary>
  39. private readonly AzureCosmosFactory _azureCosmos;
  40. private readonly DingDing _dingDing;
  41. private readonly AzureStorageFactory _azureStorage;
  42. private readonly AzureRedisFactory _azureRedis;
  43. private readonly IConverter _converter;
  44. private readonly SnowflakeId _snowflakeId;
  45. private readonly IHttpClientFactory _httpClient;
  46. private IPSearcher _ipSearcher;
  47. public IESTimerTrigger(IPSearcher ipSearcher, IHttpClientFactory httpClient,SnowflakeId snowflakeId,IConverter converter, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
  48. {
  49. _azureCosmos = azureCosmos;
  50. _dingDing = dingDing;
  51. _azureStorage = azureStorage;
  52. _azureRedis = azureRedis;
  53. _converter = converter;
  54. _snowflakeId=snowflakeId;
  55. _httpClient = httpClient;
  56. _ipSearcher = ipSearcher;
  57. }
  58. /// <summary>
  59. /// 防火墙日志记录文件
  60. /// </summary>
  61. /// <param name="req"></param>
  62. /// <param name="log"></param>
  63. /// <returns></returns>
  64. [Function("FireWallFileLog")]
  65. //https://docs.azure.cn/zh-cn/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
  66. //0 1 * * * * 一天中每小时的第 1 分钟
  67. //0 */10 * * * * 每五分钟一次
  68. public async Task FireWallFileLog([TimerTrigger("0 1 * * * *")] TimerInfo myTimer, ILogger log)
  69. {
  70. try
  71. {
  72. string location = Environment.GetEnvironmentVariable("Option:Location");
  73. var datetime = DateTimeOffset.Now.AddHours(-1);
  74. var y = datetime.Year;
  75. var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}";
  76. var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}";
  77. var h = datetime.Hour >= 10 ? $"{datetime.Hour}" : $"0{datetime.Hour}";
  78. #if DEBUG
  79. if (location.Equals("China-Dep"))
  80. #else
  81. if (location.Equals("China"))
  82. #endif
  83. {
  84. //string path = $"resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.NETWORK/APPLICATIONGATEWAYS/OSFIREWARE/y={y}/m={m}/d={d}/h={h}/m=00/PT1H.json";
  85. string path = $"resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.WEB/SITES/TEAMMODELOS/y={y}/m={m}/d={d}/h={h}/m=00/PT1H.json";
  86. var retn = await BILogAnalyseService.GetPathAnalyse(_azureStorage, _ipSearcher, _dingDing, path, "LogStorage");
  87. if (retn.recCnts.IsNotEmpty())
  88. {
  89. //https://teammodelos.blob.core.chinacloudapi.cn/0-public/pie-borderRadius.html
  90. string publishUrl = $"https://teammodelos.blob.core.chinacloudapi.cn/0-public/api-count.html?url={HttpUtility.UrlEncode(retn.saveUrls.First(), Encoding.UTF8)}&time={HttpUtility.UrlEncode(datetime.ToString("yyyy年MM月dd日 HH时"), Encoding.UTF8)}";
  91. string ulrs = $"https://teammodelos.blob.core.chinacloudapi.cn/0-public/api-count.html?url={retn.saveUrls.First()}&time={datetime.ToString("yyyy年MM月dd日 HH时")}";
  92. string ulr = $"http://cdhabook.teammodel.cn:8805/screen/screenshot-png?width=1920&height=1450&url={HttpUtility.UrlEncode(ulrs, Encoding.UTF8)}&delay=5000";
  93. string image = "";
  94. try
  95. {
  96. string strs = await _httpClient.CreateClient().GetStringAsync(ulr);
  97. if (!string.IsNullOrWhiteSpace(strs))
  98. {
  99. JsonElement json = strs.ToObject<JsonElement>();
  100. json.TryGetProperty("url", out JsonElement base64);
  101. using (MemoryStream ms = new MemoryStream(Convert.FromBase64String($"{base64}")))
  102. {
  103. image = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(ms, $"visitCnt/{y}{m}{d}", $"{y}{m}{d}{h}.png", false);
  104. }
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. }
  110. await _dingDing.SendBotMarkdown("防火墙日志记录", $"#### 防火墙日志记录(小时)\n> 记录时间:{datetime.ToString("yyyy-MM-dd HH")}\n> ![screenshot]({image})\n> ###### 发布时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}" +
  111. $" [发布地址]({publishUrl}) \n", GroupNames.醍摩豆服務運維群組);
  112. }
  113. //处理昨天的防火墙日志
  114. if (h.Equals("00"))
  115. {
  116. var pastTime = datetime.AddHours(-1);
  117. var ptY = pastTime.Year;
  118. var ptM = pastTime.Month >= 10 ? $"{pastTime.Month}" : $"0{pastTime.Month}";
  119. var ptD = pastTime.Day >= 10 ? $"{pastTime.Day}" : $"0{pastTime.Day}";
  120. string dayPath = $"resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.WEB/SITES/TEAMMODELOS/y={ptY}/m={ptM}/d={ptD}";
  121. var retnDay = await BILogAnalyseService.GetPathAnalyse(_azureStorage, _ipSearcher, _dingDing, dayPath, "LogStorage", timeType: "Day");
  122. if (retn.recCnts.IsNotEmpty())
  123. {
  124. //一天的统计
  125. string dayPublishUrl = $"https://teammodelos.blob.core.chinacloudapi.cn/0-public/api-count.html?url={HttpUtility.UrlEncode(retnDay.saveUrls.First(), Encoding.UTF8)}&time={HttpUtility.UrlEncode(pastTime.ToString("yyyy年MM月dd日"), Encoding.UTF8)}";
  126. string dayUrls = $"https://teammodelos.blob.core.chinacloudapi.cn/0-public/api-count.html?url={retnDay.saveUrls.First()}&time={pastTime.ToString("yyyy年MM月dd日")}";
  127. string dayUrl = $"http://cdhabook.teammodel.cn:8805/screen/screenshot-png?width=1920&height=1450&url={HttpUtility.UrlEncode(dayUrls, Encoding.UTF8)}&delay=6000";
  128. string dayImage = "";
  129. try
  130. {
  131. string dayStr = await _httpClient.CreateClient().GetStringAsync(dayUrl);
  132. if (!string.IsNullOrWhiteSpace(dayStr))
  133. {
  134. JsonElement dayJson = dayStr.ToObject<JsonElement>();
  135. dayJson.TryGetProperty("url", out JsonElement dayBase64);
  136. using (MemoryStream dayMs = new(Convert.FromBase64String($"{dayBase64}")))
  137. {
  138. dayImage = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(dayMs, $"visitCnt/{ptY}{ptM}{ptD}", "days.png", false);
  139. }
  140. }
  141. }
  142. catch (Exception ex) { }
  143. await _dingDing.SendBotMarkdown("防火墙日志记录", $"#### 防火墙日志记录(天)\n> 记录时间:{pastTime.ToString("yyyy-MM-dd")}\n> ![screenshot]({dayImage})\n> ###### 发布时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}" +
  144. $" [发布地址]({dayPublishUrl}) \n", GroupNames.醍摩豆服務運維群組);
  145. }
  146. }
  147. }
  148. else if (location.Contains("Global"))
  149. {
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. // await _dingDing.SendBotMsg($"FireWallFileLog 防火墙日志记录: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  155. }
  156. }
  157. /// <summary>
  158. /// 每天执行 一次清零动作
  159. /// </summary>
  160. /// <param name="myTimer"></param>
  161. /// <param name="log"></param>
  162. /// <returns></returns>
  163. [Function("BIStatsDayDefault")]
  164. //https://docs.azure.cn/zh-cn/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
  165. //0 1 0 * * * 一天中00的第 1 分钟
  166. //0 1 * * * * 一天中每小时的第 1 分钟
  167. //0 */10 * * * * 每五分钟一次
  168. public async Task BIStatsDayDefault([TimerTrigger("0 1 0 * * *")] TimerInfo myTimer, ILogger log)
  169. {
  170. try
  171. {
  172. _ = BIStats.SetStatsZeroPoint(_azureCosmos, _dingDing);
  173. }
  174. catch (Exception ex)
  175. {
  176. await _dingDing.SendBotMsg($"BIStatsDayDefault 定时清理每天的数据: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  177. }
  178. }
  179. /// <summary>
  180. /// 清理HiTeach教研类型的课例文件上传在Blob空间的文件。 0 */10 22-23 * * *
  181. /// 该代码执行之后 需要删除或者取消Timer任务。
  182. /// </summary>
  183. /// <param name="myTimer"></param>
  184. /// <param name="log"></param>
  185. /// <returns></returns>
  186. //[Function("CleanUnusedLessonRecord")]
  187. //5分钟一次
  188. public async Task CleanUnusedLessonRecord(
  189. //[TimerTrigger("0 */10 15-23 * * *")] TimerInfo myTimer, ILogger log
  190. ) {
  191. string location = Environment.GetEnvironmentVariable("Option:Location");
  192. try {
  193. if (location.Equals("China") || location.Equals("Global"))
  194. {
  195. bool lockKey = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"LessonRecord:Unused:Lock:{location}");
  196. bool keyLock = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"LessonRecord:Unused:Lock:Lock");
  197. if (keyLock)
  198. {
  199. return;
  200. }
  201. //key不存在的时候 。
  202. if (!lockKey)
  203. {
  204. var schoolKeys = new List<IdCodeCount>();
  205. var teacherKeys = new List<IdCodeCount>();
  206. string sqlT = "select c.id,c.name, 'Teacher' as code from c where c.code='Base'";
  207. string sqlS = "select c.id,c.name, 'School' as code from c where c.code='Base'";
  208. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).
  209. GetItemQueryIterator<IdCodeCount>(queryText: sqlT, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  210. {
  211. teacherKeys.Add(item);
  212. }
  213. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).
  214. GetItemQueryIterator<IdCodeCount>(queryText: sqlS, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  215. {
  216. schoolKeys.Add(item);
  217. }
  218. string sqlcount = "select value count(1) from c where c.pk='LessonRecord'";
  219. foreach (var key in schoolKeys)
  220. {
  221. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).
  222. GetItemQueryIterator<int>(queryText: sqlcount, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"LessonRecord-{key.id}") }))
  223. {
  224. key.count = item;
  225. break;
  226. }
  227. }
  228. foreach (var key in teacherKeys)
  229. {
  230. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).
  231. GetItemQueryIterator<int>(queryText: $"select value count(1) from c where c.pk='LessonRecord' and c.tmdid='{key.id}'",
  232. requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"LessonRecord") }))
  233. {
  234. key.count = item;
  235. break;
  236. }
  237. }
  238. schoolKeys.AddRange(teacherKeys);
  239. //以下代码作用,用于根据当前拥有的课例数判断区分学校和个人使用课例频率越多的,
  240. //并均分每次需要处理的学校和个人容器个数,避免每次处理批次花费时间相差太大。
  241. //将每个批次处理数量级控制在500-1000左右。
  242. List<IEnumerable<IdCodeCount>> counts = new List<IEnumerable<IdCodeCount>>();
  243. //501-∞
  244. var count501_ = schoolKeys.Where(x => x.count >= 501); //至少500条
  245. counts.AddRange(count501_.Page(1));//1个学校或个人
  246. //201-500
  247. var count201_500 = schoolKeys.Where(x => x.count >= 201 && x.count <= 500); //至少603条-1500条
  248. counts.AddRange(count201_500.Page(3));//2个学校或个人
  249. //100-200
  250. var count100_200 = schoolKeys.Where(x => x.count >= 100 && x.count <= 200);//至少500条-1000条
  251. counts.AddRange(count100_200.Page(10));//5个学校或个人
  252. //51-99
  253. var count51_99 = schoolKeys.Where(x => x.count >= 51 && x.count <= 99);//至少500条-990条
  254. counts.AddRange(count51_99.Page(10));//10个学校或个人
  255. //21-50
  256. var count21_50 = schoolKeys.Where(x => x.count >= 21 && x.count <= 50);//至少410条-1000条
  257. counts.AddRange(count21_50.Page(20));//20个学校或个人
  258. //10-20
  259. var count10_20 = schoolKeys.Where(x => x.count >= 10 && x.count <= 20);//至少500条-1000条
  260. counts.AddRange(count10_20.Page(50));//50个学校或个人
  261. //5-9
  262. var count0_9 = schoolKeys.Where(x => x.count >= 5 && x.count <= 9);//至少500条-900条
  263. counts.AddRange(count0_9.Page(100));//100个学校或个人
  264. //2-4
  265. var count2_4 = schoolKeys.Where(x => x.count >= 2 && x.count <= 4);//至少600条-1200条
  266. counts.AddRange(count2_4.Page(300));
  267. //0-1,已经处理过一次,不用再处理
  268. var count0_1 = schoolKeys.Where(x => x.count ==1);//至少500,就算没有课例也算一次。
  269. counts.AddRange(count0_1.Page(1000));
  270. int field = 1;
  271. foreach (var item in counts)
  272. {
  273. if (item.Any())
  274. {
  275. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"LessonRecord:Unused:Lock:{location}", field,
  276. new UnusedLock { field = field, status = 0, item = item }.ToJsonString());
  277. field += 1;
  278. }
  279. }
  280. await _dingDing.SendBotMsg($"{location},获取到:{schoolKeys.Count} 个学校和个人的容器\n" +
  281. $"大概要处理:{schoolKeys.Sum(x => x.count) + schoolKeys.Where(x => x.count >= 0).Count()} 条数据\n" +
  282. $"将数据分为:{field - 1} 次处理,每次处理500-1000条数据。", GroupNames.醍摩豆服務運維群組);
  283. }
  284. //key存在的时候 开始进行数据处理
  285. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"LessonRecord:Unused:Lock:{location}");
  286. List<UnusedLock> unuseds = new List<UnusedLock>();
  287. foreach (var rcd in records)
  288. {
  289. var value = rcd.Value.ToString().ToObject<UnusedLock>();
  290. unuseds.Add(value);
  291. }
  292. int max = unuseds.Max(x => x.field);
  293. int index = max + 1;
  294. var unusedLock = unuseds.Where(s => s.status == 0)?.OrderBy(x => x.field)?.First();
  295. if (unusedLock != null)
  296. {
  297. var stime = DateTimeOffset.UtcNow;
  298. long s = stime.ToUnixTimeMilliseconds();
  299. string sdata = stime.ToString("yyyy-MM-dd HH:mm:ss");
  300. await _dingDing.SendBotMsg($"{location}-开始第{unusedLock.field}轮清理冗余的课例记录文件...\n开始时间:{sdata}\n清理容器有{unusedLock.item.Select(x => $"{x.name}-{x.id}").ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  301. //将当前的标记为1锁定。
  302. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"LessonRecord:Unused:Lock:{location}", unusedLock.field,
  303. new UnusedLock { field = unusedLock.field, status = 1, item = unusedLock.item }.ToJsonString());
  304. List<KeyValuePair<string, List<string>>> deleteUrls = new List<KeyValuePair<string, List<string>>>();
  305. foreach (IdCodeCount idCode in unusedLock.item)
  306. {
  307. try
  308. {
  309. List<string> urls = new List<string>();
  310. var ContainerClient = _azureStorage.GetBlobContainerClient(idCode.id);
  311. List<string> items = await ContainerClient.List("records");
  312. if (items.IsNotEmpty())
  313. {
  314. HashSet<string> set = new HashSet<string>();
  315. items.ForEach(z => {
  316. var uri = z.Split("/");
  317. if (uri.Length > 1)
  318. {
  319. set.Add(uri[1]);
  320. }
  321. });
  322. if (set.Any())
  323. {
  324. string tbname = idCode.code.Equals("Teacher") ? Constant.Teacher : Constant.School;
  325. string pk = idCode.code.Equals("Teacher") ? "LessonRecord" : $"LessonRecord-{idCode.id}";
  326. string sql = $"select value c.id from c where c.pk='LessonRecord' and c.id in ({string.Join(",", set.Select(m => $"'{m}'"))})";
  327. List<string> ids = new List<string>();
  328. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  329. .GetItemQueryIterator<string>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey(pk) }))
  330. {
  331. ids.Add(item);
  332. }
  333. var notin = set.Except(ids);
  334. if (notin.Any())
  335. {
  336. foreach (var not in notin)
  337. {
  338. if (!string.IsNullOrWhiteSpace(not)) {
  339. string url = $"records/{not}";
  340. long id = -1;
  341. long.TryParse(not, out id);
  342. if (id > -1)
  343. {
  344. id = _snowflakeId.ParseIdToTimeStamp(id);
  345. }
  346. if (id > -1)
  347. {
  348. //72小时之外的数据。需要被删除 72 * 60 * 60 *1000= 259200000
  349. //当前时间-课例产生的时间戳
  350. if (s - id > 259200000)
  351. {
  352. urls.Add(url);
  353. await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, idCode.id, new List<string> { url });
  354. }
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. if (urls.IsNotEmpty()) {
  362. deleteUrls.Add(new KeyValuePair<string, List<string>>($"{idCode.name}-{idCode.id}", urls));
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. //异常的学校,需要将数据放回redis 重新处理。
  368. await _azureRedis.GetRedisClient(8).HashSetAsync($"LessonRecord:Unused:Lock:{location}", index,
  369. new UnusedLock { field = index, status = 0, item = new List<IdCodeCount>() { idCode } }.ToJsonString());
  370. index += 1;
  371. await _dingDing.SendBotMsg($"{location}-第{unusedLock.field}轮清理时容器:{idCode.id},类型:{idCode.code}出现异常。\n将{idCode.id}重新加入队列,队列编号:{index}\n" +
  372. $"异常信息:{ex.Message},{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  373. }
  374. }
  375. await _azureRedis.GetRedisClient(8).HashDeleteAsync($"LessonRecord:Unused:Lock:{location}", unusedLock.field);
  376. var etime = DateTimeOffset.UtcNow;
  377. long e = etime.ToUnixTimeMilliseconds();
  378. string edata = etime.ToString("yyyy-MM-dd HH:mm:ss");
  379. long d = (e - s) / 1000;
  380. string timeStr = d >= 60 ? Math.Round(d / 60.0) + "分" : d + "秒";
  381. List<string> content = new List<string>();
  382. foreach (var url in deleteUrls)
  383. {
  384. if (url.Value.IsNotEmpty()) {
  385. content.Add($"{url.Key}\n {string.Join(" \t\n", url.Value)}");
  386. }
  387. }
  388. if (max == unusedLock.field)
  389. {
  390. //锁定15至少小时。
  391. await _azureRedis.GetRedisClient(8).StringSetAsync($"LessonRecord:Unused:Lock:Lock", "用于在清理完所有Blob容器后,锁定不再进行多次清理。");
  392. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"LessonRecord:Unused:Lock:Lock", DateTime.UtcNow.AddHours(15) );
  393. }
  394. if (content.IsNotEmpty())
  395. {
  396. string str = string.Join("\n", content);
  397. await _dingDing.SendBotMsg($"{location}-结束第{unusedLock.field}轮清理冗余的课例记录文件...\n结束时间:{edata}\n耗时:{timeStr}\n清理内容:\n{str}", GroupNames.醍摩豆服務運維群組);
  398. }
  399. else {
  400. // await _dingDing.SendBotMsg($"{location}-结束第{unusedLock.field}轮清理冗余的课例记录文件...\n结束时间:{edata}\n耗时:{timeStr}\n清理内容:暂无", GroupNames.醍摩豆服務運維群組);
  401. }
  402. }
  403. }
  404. } catch (Exception ex) {
  405. await _dingDing.SendBotMsg($"{location} 清理时容器出现异常。\n异常信息:{ex.Message},{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  406. }
  407. }
  408. /// <summary>
  409. /// 每天執行 取得IOT TeachingData 並統計昨日每校Redis資料 執行時間:每日凌晨1時1分
  410. /// </summary>
  411. [Function("BICrtDailyAnal")]
  412. //0 1 0 * * * 一天中00的第 1 分钟
  413. //0 1 * * * * 一天中每小时的第 1 分钟
  414. //0 */10 * * * * 每五分钟一次
  415. public async Task BICreatDailyAnalData([TimerTrigger("0 1 1 * * *")] TimerInfo myTimer, ILogger log)
  416. {
  417. var _azureCosmosClient = _azureCosmos.GetCosmosClient();
  418. var _azureCosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2");
  419. var _azureCosmosClientCsv2CnRead = _azureCosmos.GetCosmosClient(name: "CoreServiceV2CnRead");
  420. var datetime = DateTimeOffset.UtcNow.AddHours(-12); //統計昨天的數據
  421. var y = $"{datetime.Year}";
  422. var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}";
  423. var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}";
  424. //生成學校IOT數據
  425. await BIProdAnalysis.BICreatDailyAnalData(_azureRedis, _azureCosmosClient, _azureCosmosClientCsv2, _azureCosmosClientCsv2CnRead, _dingDing, y, m, d);
  426. //刪除三個月以前的Redis數據 [待做]
  427. }
  428. /// <summary>
  429. /// 每天執行 計算各學生各科錯題庫的數量,記入Redis 執行時間:每日2時1分
  430. /// 「新增錯題數」取得方法改由智慧錯題的時間戳記判斷,每日統計錯題數已不需要
  431. /// </summary>
  432. //[Function("CntStuErrorItems")]
  433. //public async Task CntStuErrorItems([TimerTrigger("0 1 2 * * *")] TimerInfo myTimer, ILogger log)
  434. //{
  435. // var _azureCosmosClient = _azureCosmos.GetCosmosClient();
  436. // await ErrorItemsService.cntStuErrorItemsAsync(_azureRedis, _azureCosmosClient, _dingDing);
  437. //}
  438. public class UnusedLock {
  439. public int field { get; set; }
  440. /// <summary>
  441. /// 0默认未被执行,1 正在执行中
  442. /// </summary>
  443. public int status { get; set; }
  444. public IEnumerable<IdCodeCount> item { get; set; } = new List<IdCodeCount>();
  445. }
  446. }
  447. }