VisitSettleJob.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Azure.Storage.Blobs.Models;
  2. using Azure.Storage.Blobs.Specialized;
  3. using HTEX.Complex.Service.AzureRedis;
  4. using System.Collections.Concurrent;
  5. using System.Text;
  6. using TEAMModelOS.SDK;
  7. using TEAMModelOS.SDK.DI;
  8. using static HTEX.Complex.Service.SystemService;
  9. namespace HTEX.Complex.Service.CoreHangfire
  10. {
  11. public class VisitSettleJob
  12. {
  13. private readonly IConfiguration _configuration;
  14. private readonly AzureRedisFactory _azureRedis;
  15. private readonly AzureStorageFactory _azureStorage;
  16. private readonly DingDing _dingDing;
  17. private readonly IPSearcher _ipSearcher;
  18. private readonly Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator;
  19. public VisitSettleJob(Region2LongitudeLatitudeTranslator longitudeLatitudeTranslator,IPSearcher ipSearcher, DingDing dingDing,IConfiguration configuration, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
  20. {
  21. _azureRedis = azureRedis;
  22. _configuration = configuration;
  23. _azureStorage = azureStorage;
  24. _dingDing=dingDing;
  25. _ipSearcher=ipSearcher;
  26. _longitudeLatitudeTranslator=longitudeLatitudeTranslator;
  27. }
  28. public async Task Run()
  29. {
  30. var gmt8Time = DateTimeOffset.Now.GetGMTTime(8).AddHours(-1);
  31. await _dingDing.SendBotMsg($"{DateTimeOffset.Now.GetGMTTime(8):yyyy-MM-dd HH:mm:ss}Http日志访问统计开始,统计时间:{gmt8Time:yyyy-MM-dd HH}", GroupNames.成都开发測試群組);
  32. try
  33. {
  34. // string location = _configuration.GetValue<string>("Option:Location");
  35. //获取上一个小时的数据
  36. var appendBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/{gmt8Time:HH}.log");
  37. if (await appendBlob.ExistsAsync())
  38. {
  39. BlobDownloadResult result = await appendBlob.DownloadContentAsync();
  40. var content = result.Content.ToString();
  41. content= content.Substring(0, content.Length-2);
  42. if (content.EndsWith("}"))
  43. {
  44. content=$"[{content}]";
  45. }
  46. else
  47. {
  48. content=$"[{content}}}]";
  49. }
  50. var httpLogList = content.ToObject<List<HttpLog>>();
  51. (ConcurrentBag<ApiVisit> vists, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo) =
  52. await SystemService.ConvertHttpLog(httpLogList, _azureRedis, _ipSearcher, _longitudeLatitudeTranslator, gmt8Time);
  53. if (vists!=null && vists.Count>0)
  54. {
  55. var appendDayBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/index.log");
  56. if (!await appendDayBlob.ExistsAsync())
  57. {
  58. await appendDayBlob.CreateAsync();
  59. }
  60. StringBuilder sb = new StringBuilder();
  61. foreach (var item in vists)
  62. {
  63. sb.Append($"{item.ToJsonString()},\n");
  64. }
  65. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{sb}")))
  66. {
  67. await appendDayBlob.AppendBlockAsync(stream);
  68. }
  69. }
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. await _dingDing.SendBotMsg($"日志访问统计异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  75. }
  76. await _dingDing.SendBotMsg($"{DateTimeOffset.Now.GetGMTTime(8):yyyy-MM-dd HH:mm:ss}Http日志访问统计结束,统计时间:{gmt8Time:yyyy-MM-dd HH}", GroupNames.成都开发測試群組);
  77. }
  78. }
  79. }