IESTimerTrigger.cs 6.2 KB

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