IESTimerTrigger.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using DinkToPdf.Contracts;
  3. using Microsoft.Azure.Functions.Worker;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Logging;
  6. using TEAMModelOS.SDK.DI;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.SDK.Extension;
  9. using TEAMModelOS.SDK.Models.Service.BI;
  10. using System.Globalization;
  11. using TEAMModelOS.SDK.Models;
  12. using HTEX.Lib.ETL.Lesson;
  13. using System.Linq;
  14. namespace TEAMModelOS.Function
  15. {
  16. public class IESTimerTrigger
  17. {
  18. private readonly ILogger _logger;
  19. /// <summary>
  20. /// 文档。https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
  21. /// Timer 在线测试 https://ncrontab.swimburger.net/
  22. /// </summary>
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly DingDing _dingDing;
  25. private readonly AzureStorageFactory _azureStorage;
  26. private readonly AzureRedisFactory _azureRedis;
  27. //private readonly IConverter _converter;
  28. private readonly SnowflakeId _snowflakeId;
  29. private readonly IHttpClientFactory _httpClient;
  30. private readonly CoreAPIHttpService _coreAPIHttpService;
  31. //private IPSearcher _ipSearcher;
  32. private readonly IConfiguration _configuration;
  33. public IESTimerTrigger(ILoggerFactory loggerFactory, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, SnowflakeId snowflakeId, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
  34. {
  35. _logger = loggerFactory.CreateLogger<IESTimerTrigger>();
  36. _azureCosmos = azureCosmos;
  37. _dingDing = dingDing;
  38. _azureStorage = azureStorage;
  39. _azureRedis = azureRedis;
  40. // _converter = converter;
  41. _snowflakeId=snowflakeId;
  42. _httpClient = httpClient;
  43. // _ipSearcher = ipSearcher;
  44. _coreAPIHttpService = coreAPIHttpService;
  45. _configuration= configuration;
  46. }
  47. //0 0 20 * * 0
  48. [Function("WeeklyReport")]
  49. public async Task WeeklyReport([TimerTrigger("0 0 20 * * 0")] TimerInfo myTimer)
  50. {
  51. await TEAMModelOS.SDK.Models.Service.SystemService.LessonWeekly(_azureRedis,_azureCosmos,_coreAPIHttpService,_dingDing,_configuration);
  52. //var server = _azureRedis.GetGetServer();
  53. //// 使用Keys方法获取所有匹配前缀的key
  54. //var pattern = "LessonWeek:*";
  55. //var keys = server.Keys(8, pattern);
  56. //foreach (var key in keys)
  57. //{
  58. // await _azureRedis.GetRedisClient(8).KeyDeleteAsync(key);
  59. //}
  60. }
  61. /// <summary>
  62. /// //0 1 * * * * 一天中每小时的第 1 分钟
  63. ///0 */10 * * * * 每五分钟一次
  64. /// </summary>
  65. /// <param name="myTimer"></param>
  66. /// <returns></returns>
  67. [Function("DailyReport")]
  68. public async Task DailyReport([TimerTrigger("0 1 * * * *")] TimerInfo myTimer)
  69. {
  70. _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
  71. try
  72. {
  73. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location},定时任务{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}", GroupNames.成都开发測試群組);
  74. string? local = Environment.GetEnvironmentVariable("Option:Location");
  75. // if (local.Contains("Test")||local.Contains("Dep"))
  76. {
  77. int am = 8;
  78. int pm = 20;
  79. //int am = DateTimeOffset.Now.Hour;
  80. //int pm = DateTimeOffset.Now.Hour;
  81. var url = Environment.GetEnvironmentVariable("HaBookAuth:CoreAPI");
  82. var clientID = Environment.GetEnvironmentVariable("HaBookAuth:CoreService:clientID");
  83. var clientSecret = Environment.GetEnvironmentVariable("HaBookAuth:CoreService:clientSecret");
  84. string location = "";
  85. if (local!.Contains("China"))
  86. {
  87. location = "China";
  88. return;
  89. }
  90. else if (local.Contains("Global"))
  91. {
  92. location = "Global";
  93. }
  94. var client = _httpClient.CreateClient();
  95. var token = CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location).Result;
  96. if (client.DefaultRequestHeaders.Contains("Authorization"))
  97. {
  98. client.DefaultRequestHeaders.Remove("Authorization");
  99. client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
  100. }
  101. else
  102. {
  103. client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
  104. }
  105. var data = await TEAMModelOS.SDK.Models.Service.SystemService.AccumulateDaily(_configuration, _azureRedis, _azureCosmos,_azureStorage, _coreAPIHttpService, _dingDing, client, _snowflakeId, url, am, pm);
  106. //await _dingDing.SendBotMsg($"返回数据{data.ToJsonString()}", GroupNames.成都开发測試群組);
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. await _dingDing.SendBotMsg($"{DailyReport}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  112. }
  113. if (myTimer.ScheduleStatus is not null)
  114. {
  115. _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
  116. }
  117. }
  118. /// <summary>
  119. /// 每天執行 取得IOT TeachingData 並統計昨日每校Redis資料 執行時間:每日凌晨1時1分
  120. /// </summary>
  121. [Function("BICrtDailyAnal")]
  122. //0 1 0 * * * 一天中00的第 1 分钟
  123. //0 1 * * * * 一天中每小时的第 1 分钟
  124. //0 */10 * * * * 每五分钟一次
  125. public async Task BICreatDailyAnalData([TimerTrigger("0 1 1 * * *")] TimerInfo myTimer, ILogger log)
  126. {
  127. var _azureCosmosClient = _azureCosmos.GetCosmosClient();
  128. var _azureCosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2");
  129. var _azureCosmosClientCsv2CnRead = _azureCosmos.GetCosmosClient(name: "CoreServiceV2CnRead");
  130. var datetime = DateTimeOffset.UtcNow.AddHours(-12); //統計昨天的數據
  131. var y = $"{datetime.Year}";
  132. var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}";
  133. var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}";
  134. //生成學校IOT數據
  135. await BIProdAnalysis.BICreatDailyAnalData(_azureRedis, _azureCosmosClient, _azureCosmosClientCsv2, _azureCosmosClientCsv2CnRead, _dingDing, y, m, d);
  136. //刪除三個月以前的Redis數據 [待做]
  137. }
  138. }
  139. }