MonitorServicesBus.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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("active-task", "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. /* keyValuePairs.TryGetValue("id", out object id);
  45. keyValuePairs.TryGetValue("name", out object name);
  46. keyValuePairs.TryGetValue("code", out object code);*/
  47. //keyValuePairs.TryGetValue("status", out object progress);
  48. /*if (name.ToString().Equals("Exam", StringComparison.OrdinalIgnoreCase))
  49. {
  50. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  51. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0)
  52. {
  53. exam.progress = "going";
  54. }
  55. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0)
  56. {
  57. exam.progress = "finish";
  58. }
  59. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  60. }*/
  61. }
  62. catch (Exception ex)
  63. {
  64. await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  65. }
  66. }
  67. [FunctionName("Vote")]
  68. public async Task Vote([ServiceBusTrigger("active-task", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  69. {
  70. try
  71. {
  72. var jsonMsg = JsonDocument.Parse(msg);
  73. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  74. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  75. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  76. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  77. var client = _azureCosmos.GetCosmosClient();
  78. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  79. vote.progress = progress.ToString();
  80. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  81. /*keyValuePairs.TryGetValue("id", out object id);
  82. keyValuePairs.TryGetValue("name", out object name);
  83. keyValuePairs.TryGetValue("code", out object code);*/
  84. //keyValuePairs.TryGetValue("status", out object progress);
  85. /*if (name.ToString().Equals("Vote", StringComparison.OrdinalIgnoreCase))
  86. {
  87. Vote vote;
  88. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  89. if (sresponse.Status == 200)
  90. {
  91. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  92. vote = json.ToObject<Vote>();
  93. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
  94. {
  95. vote.progress = "going";
  96. }
  97. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
  98. {
  99. vote.progress = "finish";
  100. }
  101. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  102. }
  103. }*/
  104. }
  105. catch (Exception ex)
  106. {
  107. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  108. }
  109. }
  110. [FunctionName("Survey")]
  111. public async Task Survey([ServiceBusTrigger("active-task", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  112. {
  113. try
  114. {
  115. var jsonMsg = JsonDocument.Parse(msg);
  116. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  117. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  118. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  119. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  120. var client = _azureCosmos.GetCosmosClient();
  121. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  122. survey.progress = progress.ToString();
  123. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  124. /*keyValuePairs.TryGetValue("id", out object id);
  125. keyValuePairs.TryGetValue("name", out object name);
  126. keyValuePairs.TryGetValue("code", out object code);*/
  127. //keyValuePairs.TryGetValue("status", out object progress);
  128. /*if (name.ToString().Equals("Survey", StringComparison.OrdinalIgnoreCase))
  129. {
  130. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  131. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
  132. {
  133. survey.progress = "going";
  134. }
  135. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
  136. {
  137. survey.progress = "finish";
  138. }
  139. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  140. }*/
  141. }
  142. catch (Exception ex)
  143. {
  144. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  145. }
  146. }
  147. [FunctionName("Blob")]
  148. public async Task Blob([ServiceBusTrigger("active-task", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
  149. try
  150. {
  151. // await _dingDing.SendBotMsg($"ServiceBus,Blob(){msg}", GroupNames.醍摩豆服務運維群組);
  152. var jsonMsg = JsonDocument.Parse(msg);
  153. if(jsonMsg.RootElement.TryGetProperty("name", out JsonElement name)&& name.ValueKind==JsonValueKind.String)
  154. {
  155. var client = _azureStorage.GetBlobContainerClient($"{name}");
  156. var size = await client.GetBlobsCatalogSize();
  157. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{long.Parse($"{size.Item1}")}"));
  158. foreach (var key in size.Item2.Keys)
  159. {
  160. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  161. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  162. }
  163. await _dingDing.SendBotMsg($"ServiceBus,Blob() 容器:{name}使用:{size.Item1},文件分类:{size.Item2.ToJsonString()}",
  164. GroupNames.醍摩豆服務運維群組);
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. await _dingDing.SendBotMsg($"ServiceBus,Blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  170. }
  171. }
  172. }
  173. }