123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using DinkToPdf.Contracts;
- using Microsoft.Azure.Functions.Worker;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Service.BI;
- using System.Globalization;
- using TEAMModelOS.SDK.Models;
- using HTEX.Lib.ETL.Lesson;
- using System.Linq;
- using TEAMModelOS.SDK.DI.IPIP;
- using static TEAMModelOS.SDK.Extension.GeoRegion;
- using System.Text.Json;
- namespace TEAMModelOS.Function
- {
- public class IESTimerTrigger
- {
- private readonly ILogger _logger;
- /// <summary>
- /// 文档。https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
- /// Timer 在线测试 https://ncrontab.swimburger.net/
- /// </summary>
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- //private readonly IConverter _converter;
- private readonly SnowflakeId _snowflakeId;
- private readonly IHttpClientFactory _httpClient;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- //private IPSearcher _ipSearcher;
- private readonly IConfiguration _configuration;
- private readonly City _city;
- public IESTimerTrigger(ILoggerFactory loggerFactory, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, SnowflakeId snowflakeId, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, City city)
- {
- _logger = loggerFactory.CreateLogger<IESTimerTrigger>();
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _azureRedis = azureRedis;
- // _converter = converter;
- _snowflakeId=snowflakeId;
- _httpClient = httpClient;
- // _ipSearcher = ipSearcher;
- _coreAPIHttpService = coreAPIHttpService;
- _configuration= configuration;
- _city = city;
- }
- //0 0 20 * * 0
- [Function("WeeklyReport")]
- public async Task WeeklyReport([TimerTrigger("0 0 20 * * 0")] TimerInfo myTimer)
- {
- await TEAMModelOS.SDK.Models.Service.SystemService.LessonWeekly(_azureRedis,_azureCosmos,_coreAPIHttpService,_dingDing,_configuration);
- //var server = _azureRedis.GetGetServer();
- //// 使用Keys方法获取所有匹配前缀的key
- //var pattern = "LessonWeek:*";
- //var keys = server.Keys(8, pattern);
- //foreach (var key in keys)
- //{
- // await _azureRedis.GetRedisClient(8).KeyDeleteAsync(key);
- //}
- }
-
- /// <summary>
- /// //0 1 * * * * 一天中每小时的第 1 分钟
- ///0 */10 * * * * 每五分钟一次
- /// </summary>
- /// <param name="myTimer"></param>
- /// <returns></returns>
- [Function("DailyReport")]
- public async Task DailyReport([TimerTrigger("0 1 * * * *")] TimerInfo myTimer)
- {
- _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
- try
- {
- await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location},定时任务{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}", GroupNames.成都开发測試群組);
- string? local = Environment.GetEnvironmentVariable("Option:Location");
- // if (local.Contains("Test")||local.Contains("Dep"))
- {
- int am = 8;
- int pm = 20;
- //int am = DateTimeOffset.Now.Hour;
- //int pm = DateTimeOffset.Now.Hour;
- var url = Environment.GetEnvironmentVariable("HaBookAuth:CoreAPI");
- var clientID = Environment.GetEnvironmentVariable("HaBookAuth:CoreService:clientID");
- var clientSecret = Environment.GetEnvironmentVariable("HaBookAuth:CoreService:clientSecret");
- string location = "";
- if (local!.Contains("China"))
- {
- location = "China";
- return;
- }
- else if (local.Contains("Global"))
- {
- location = "Global";
- }
- var client = _httpClient.CreateClient();
- var token = CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location).Result;
- if (client.DefaultRequestHeaders.Contains("Authorization"))
- {
- client.DefaultRequestHeaders.Remove("Authorization");
- client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
- }
- else
- {
- client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
- }
- var data = await TEAMModelOS.SDK.Models.Service.SystemService.AccumulateDaily(_configuration, _azureRedis, _azureCosmos,_azureStorage, _coreAPIHttpService, _dingDing, client, _snowflakeId, url, am, pm);
- //await _dingDing.SendBotMsg($"返回数据{data.ToJsonString()}", GroupNames.成都开发測試群組);
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{DailyReport}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- if (myTimer.ScheduleStatus is not null)
- {
- _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
- }
- }
- /// <summary>
- /// 每天執行 取得IOT TeachingData 並統計昨日每校Redis資料 執行時間:每日凌晨1時1分
- /// </summary>
- [Function("BICrtDailyAnal")]
- //0 1 0 * * * 一天中00的第 1 分钟
- //0 1 * * * * 一天中每小时的第 1 分钟
- //0 */10 * * * * 每五分钟一次
- public async Task BICreatDailyAnalData([TimerTrigger("0 1 1 * * *")] TimerInfo myTimer, ILogger log)
- {
- var _azureCosmosClient = _azureCosmos.GetCosmosClient();
- var _azureCosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2");
- var _azureCosmosClientCsv2CnRead = _azureCosmos.GetCosmosClient(name: "CoreServiceV2CnRead");
- var datetime = DateTimeOffset.UtcNow.AddHours(-12); //統計昨天的數據
- var y = $"{datetime.Year}";
- var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}";
- var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}";
- string? local = Environment.GetEnvironmentVariable("Option:Location");
- List<regionrow> region_gl = new List<regionrow>();
- using (StreamReader r = new StreamReader("JsonFile/Region/region_gl.json"))
- {
- string json_g = r.ReadToEnd();
- region_gl = JsonSerializer.Deserialize<List<regionrow>>(json_g);
- }
- List<regionrow> region_cn = new List<regionrow>();
- using (StreamReader r = new StreamReader("JsonFile/Region/region_cn.json"))
- {
- string json_c = r.ReadToEnd();
- region_cn = JsonSerializer.Deserialize<List<regionrow>>(json_c);
- }
- //生成學校IOT數據
- await BIProdAnalysis.BICreatDailyAnalData(_azureRedis, _azureCosmosClient, _azureCosmosClientCsv2, _azureCosmosClientCsv2CnRead, _dingDing, _city, local, y, m, d, region_gl, region_cn);
- //刪除三個月以前的Redis數據 [待做]
- }
- }
- }
|