IESTimerTrigger.cs 7.1 KB

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