MonitorServicesBus.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. namespace TEAMModelFunction
  13. {
  14. public class MonitorServicesBus
  15. {
  16. private readonly AzureCosmosFactory _azureCosmos;
  17. private readonly DingDing _dingDing;
  18. private readonly AzureStorageFactory _azureStorage;
  19. private readonly AzureRedisFactory _azureRedis;
  20. public MonitorServicesBus(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
  21. {
  22. _azureCosmos = azureCosmos;
  23. _dingDing = dingDing;
  24. _azureStorage = azureStorage;
  25. _azureRedis = azureRedis;
  26. }
  27. [FunctionName("Exam")]
  28. public async Task Exam([ServiceBusTrigger("active-task", "exam", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  29. {
  30. try
  31. {
  32. var json = JsonDocument.Parse(msg);
  33. json.RootElement.TryGetProperty("id", out JsonElement id);
  34. json.RootElement.TryGetProperty("progress", out JsonElement progress);
  35. json.RootElement.TryGetProperty("code", out JsonElement code);
  36. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  37. var client = _azureCosmos.GetCosmosClient();
  38. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  39. exam.progress = progress.ToString();
  40. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  41. /* keyValuePairs.TryGetValue("id", out object id);
  42. keyValuePairs.TryGetValue("name", out object name);
  43. keyValuePairs.TryGetValue("code", out object code);*/
  44. //keyValuePairs.TryGetValue("status", out object progress);
  45. /*if (name.ToString().Equals("Exam", StringComparison.OrdinalIgnoreCase))
  46. {
  47. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  48. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0)
  49. {
  50. exam.progress = "going";
  51. }
  52. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0)
  53. {
  54. exam.progress = "finish";
  55. }
  56. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  57. }*/
  58. }
  59. catch (Exception ex)
  60. {
  61. await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  62. }
  63. }
  64. [FunctionName("Vote")]
  65. public async Task Vote([ServiceBusTrigger("active-task", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  66. {
  67. try
  68. {
  69. var jsonMsg = JsonDocument.Parse(msg);
  70. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  71. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  72. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  73. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  74. var client = _azureCosmos.GetCosmosClient();
  75. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  76. vote.progress = progress.ToString();
  77. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  78. /*keyValuePairs.TryGetValue("id", out object id);
  79. keyValuePairs.TryGetValue("name", out object name);
  80. keyValuePairs.TryGetValue("code", out object code);*/
  81. //keyValuePairs.TryGetValue("status", out object progress);
  82. /*if (name.ToString().Equals("Vote", StringComparison.OrdinalIgnoreCase))
  83. {
  84. Vote vote;
  85. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  86. if (sresponse.Status == 200)
  87. {
  88. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  89. vote = json.ToObject<Vote>();
  90. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
  91. {
  92. vote.progress = "going";
  93. }
  94. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
  95. {
  96. vote.progress = "finish";
  97. }
  98. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  99. }
  100. }*/
  101. }
  102. catch (Exception ex)
  103. {
  104. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  105. }
  106. }
  107. [FunctionName("Survey")]
  108. public async Task Survey([ServiceBusTrigger("active-task", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  109. {
  110. try
  111. {
  112. var jsonMsg = JsonDocument.Parse(msg);
  113. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  114. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  115. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  116. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  117. var client = _azureCosmos.GetCosmosClient();
  118. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  119. survey.progress = progress.ToString();
  120. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  121. /*keyValuePairs.TryGetValue("id", out object id);
  122. keyValuePairs.TryGetValue("name", out object name);
  123. keyValuePairs.TryGetValue("code", out object code);*/
  124. //keyValuePairs.TryGetValue("status", out object progress);
  125. /*if (name.ToString().Equals("Survey", StringComparison.OrdinalIgnoreCase))
  126. {
  127. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  128. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
  129. {
  130. survey.progress = "going";
  131. }
  132. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
  133. {
  134. survey.progress = "finish";
  135. }
  136. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  137. }*/
  138. }
  139. catch (Exception ex)
  140. {
  141. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  142. }
  143. }
  144. [FunctionName("Blob")]
  145. public async Task Blob([ServiceBusTrigger("active-task", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
  146. var jsonMsg = JsonDocument.Parse(msg);
  147. jsonMsg.RootElement.TryGetProperty("code", out JsonElement name);
  148. var client = _azureStorage.GetBlobContainerClient($"{name}");
  149. var size = await client.GetBlobsSize();
  150. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name.GetString(), size);
  151. await _dingDing.SendBotMsg($"ServiceBus,Blob() 容器:{name}更新大小:{size}", GroupNames.醍摩豆服務運維群組);
  152. }
  153. }
  154. }