MonitorServicesBus.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using System.Threading.Tasks;
  5. using Azure.Cosmos;
  6. using Microsoft.Azure.WebJobs;
  7. using Microsoft.Azure.WebJobs.Host;
  8. using Microsoft.Extensions.Logging;
  9. using StackExchange.Redis;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models;
  13. namespace TEAMModelFunction
  14. {
  15. public class MonitorServicesBus
  16. {
  17. private readonly AzureCosmosFactory _azureCosmos;
  18. private readonly DingDing _dingDing;
  19. private readonly AzureStorageFactory _azureStorage;
  20. private readonly AzureRedisFactory _azureRedis;
  21. public MonitorServicesBus(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  22. , AzureRedisFactory azureRedis
  23. )
  24. {
  25. _azureCosmos = azureCosmos;
  26. _dingDing = dingDing;
  27. _azureStorage = azureStorage;
  28. _azureRedis = azureRedis;
  29. }
  30. [FunctionName("Exam")]
  31. public async Task Exam([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "exam", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  32. {
  33. try
  34. {
  35. var json = JsonDocument.Parse(msg);
  36. json.RootElement.TryGetProperty("id", out JsonElement id);
  37. json.RootElement.TryGetProperty("progress", out JsonElement progress);
  38. json.RootElement.TryGetProperty("code", out JsonElement code);
  39. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  40. var client = _azureCosmos.GetCosmosClient();
  41. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  42. exam.progress = progress.ToString();
  43. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  44. }
  45. catch (Exception ex)
  46. {
  47. await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  48. }
  49. }
  50. [FunctionName("Vote")]
  51. public async Task Vote([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  52. {
  53. try
  54. {
  55. var jsonMsg = JsonDocument.Parse(msg);
  56. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  57. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  58. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  59. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  60. var client = _azureCosmos.GetCosmosClient();
  61. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  62. vote.progress = progress.ToString();
  63. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  64. }
  65. catch (Exception ex)
  66. {
  67. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  68. }
  69. }
  70. [FunctionName("Survey")]
  71. public async Task Survey([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  72. {
  73. try
  74. {
  75. var jsonMsg = JsonDocument.Parse(msg);
  76. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  77. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  78. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  79. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  80. var client = _azureCosmos.GetCosmosClient();
  81. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  82. survey.progress = progress.ToString();
  83. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  84. }
  85. catch (Exception ex)
  86. {
  87. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  88. }
  89. }
  90. [FunctionName("Blob")]
  91. public async Task Blob([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
  92. try
  93. {
  94. // await _dingDing.SendBotMsg($"ServiceBus,Blob(){msg}", GroupNames.醍摩豆服務運維群組);
  95. var jsonMsg = JsonDocument.Parse(msg);
  96. if(jsonMsg.RootElement.TryGetProperty("name", out JsonElement name)&& name.ValueKind==JsonValueKind.String)
  97. {
  98. var client = _azureStorage.GetBlobContainerClient($"{name}");
  99. var size = await client.GetBlobsCatalogSize();
  100. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{long.Parse($"{size.Item1}")}"));
  101. foreach (var key in size.Item2.Keys)
  102. {
  103. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  104. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  105. }
  106. #if DEBUG
  107. await _dingDing.SendBotMsg($"ServiceBus,Blob() 容器:{name}使用:{size.Item1},文件分类:{size.Item2.ToJsonString()}",
  108. GroupNames.成都开发測試群組);
  109. #endif
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. await _dingDing.SendBotMsg($"ServiceBus,Blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  115. }
  116. }
  117. }
  118. }