MonitorServicesBus.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Threading.Tasks;
  6. using Azure.Cosmos;
  7. using Azure.Messaging.ServiceBus;
  8. using Microsoft.Azure.WebJobs;
  9. using Microsoft.Azure.WebJobs.Host;
  10. using Microsoft.Extensions.Logging;
  11. using StackExchange.Redis;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  15. using TEAMModelOS.SDK.Models;
  16. using TEAMModelOS.SDK.Models.Cosmos;
  17. using TEAMModelOS.SDK.Models.Cosmos.Common;
  18. namespace TEAMModelFunction
  19. {
  20. public class MonitorServicesBus
  21. {
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly DingDing _dingDing;
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly AzureRedisFactory _azureRedis;
  26. private readonly AzureServiceBusFactory _serviceBus;
  27. public MonitorServicesBus(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis, AzureServiceBusFactory serviceBus)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _dingDing = dingDing;
  31. _azureStorage = azureStorage;
  32. _azureRedis = azureRedis;
  33. _serviceBus = serviceBus;
  34. }
  35. [FunctionName("Exam")]
  36. public async Task Exam([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "exam", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  37. {
  38. try
  39. {
  40. var json = JsonDocument.Parse(msg);
  41. json.RootElement.TryGetProperty("id", out JsonElement id);
  42. json.RootElement.TryGetProperty("progress", out JsonElement progress);
  43. json.RootElement.TryGetProperty("code", out JsonElement code);
  44. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  45. var client = _azureCosmos.GetCosmosClient();
  46. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  47. exam.progress = progress.ToString();
  48. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  49. }
  50. catch (Exception ex)
  51. {
  52. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  53. }
  54. }
  55. [FunctionName("Vote")]
  56. public async Task Vote([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  57. {
  58. try
  59. {
  60. var jsonMsg = JsonDocument.Parse(msg);
  61. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  62. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  63. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  64. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  65. var client = _azureCosmos.GetCosmosClient();
  66. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  67. vote.progress = progress.ToString();
  68. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  69. }
  70. catch (Exception ex)
  71. {
  72. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  73. }
  74. }
  75. [FunctionName("Correct")]
  76. public async Task Correct([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "correct", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  77. {
  78. try
  79. {
  80. var jsonMsg = JsonDocument.Parse(msg);
  81. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  82. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  83. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  84. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  85. var client = _azureCosmos.GetCosmosClient();
  86. Correct correct = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Correct>(id.ToString(), new PartitionKey($"{code}"));
  87. correct.progress = progress.ToString();
  88. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(correct, id.ToString(), new PartitionKey($"{code}"));
  89. }
  90. catch (Exception ex)
  91. {
  92. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  93. }
  94. }
  95. [FunctionName("Survey")]
  96. public async Task Survey([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  97. {
  98. try
  99. {
  100. var jsonMsg = JsonDocument.Parse(msg);
  101. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  102. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  103. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  104. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  105. var client = _azureCosmos.GetCosmosClient();
  106. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  107. survey.progress = progress.ToString();
  108. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  109. }
  110. catch (Exception ex)
  111. {
  112. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,SurveyBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  113. }
  114. }
  115. [FunctionName("Blob")]
  116. public async Task Blob([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
  117. try
  118. {
  119. // await _dingDing.SendBotMsg($"ServiceBus,Blob(){msg}", GroupNames.醍摩豆服務運維群組);
  120. var jsonMsg = JsonDocument.Parse(msg);
  121. if(jsonMsg.RootElement.TryGetProperty("name", out JsonElement name)&& name.ValueKind==JsonValueKind.String)
  122. {
  123. var client = _azureStorage.GetBlobContainerClient($"{name}");
  124. var size = await client.GetBlobsCatalogSize();
  125. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{long.Parse($"{size.Item1}")}"));
  126. foreach (var key in size.Item2.Keys)
  127. {
  128. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  129. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  130. }
  131. #if DEBUG
  132. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:{name}使用:{size.Item1},文件分类:{size.Item2.ToJsonString()}",
  133. GroupNames.成都开发測試群組);
  134. #endif
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  140. }
  141. }
  142. /// <summary>
  143. /// 根据容器的根目录刷新redis并获取redis的最新使用情况
  144. /// </summary>
  145. /// <param name="msg"></param>
  146. /// <returns></returns>
  147. [FunctionName("BlobRoot")]
  148. public async Task BlobRoot([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blobroot", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  149. {
  150. try
  151. {
  152. var jsonMsg = JsonDocument.Parse(msg);
  153. if (jsonMsg.RootElement.TryGetProperty("name", out JsonElement name) && name.ValueKind == JsonValueKind.String
  154. && jsonMsg.RootElement.TryGetProperty("root", out JsonElement root) && root.ValueKind == JsonValueKind.String)
  155. {
  156. List<Dictionary<string, double?>> list = new List<Dictionary<string, double?>>();
  157. string u = System.Web.HttpUtility.UrlDecode($"{root}", Encoding.UTF8).Split("/")[0];
  158. var client = _azureStorage.GetBlobContainerClient($"{name}");
  159. var size = await client.GetBlobsSize(u);
  160. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", u);
  161. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", u, size.HasValue ? size.Value : 0);
  162. var scores = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{name}");
  163. double blobsize = 0;
  164. if (scores != default && scores != null)
  165. {
  166. foreach (var score in scores)
  167. {
  168. blobsize = blobsize + score.Score;
  169. }
  170. }
  171. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{blobsize}"));
  172. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:{name}使用:{root},文件分类:{list.ToJsonString()}",
  173. GroupNames.成都开发測試群組);
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  179. }
  180. }
  181. /// <summary>
  182. /// 完善课程变更,StuListChange, originCode是学校编码 则表示名单是学校自定义名单,如果是tmdid则表示醍摩豆的私有名单,scope=school,private。
  183. /// </summary>
  184. /// <data msg>
  185. /// CourseChange
  186. ///// </data>
  187. /// <param name="msg"></param>
  188. /// <returns></returns>
  189. [FunctionName("StuList")]
  190. public async Task StuList([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "stulist", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  191. {
  192. var client = _azureCosmos.GetCosmosClient();
  193. try
  194. {
  195. await _dingDing.SendBotMsg($"ServiceBus,StuList:{msg}", GroupNames.醍摩豆服務運維群組);
  196. var jsonMsg = JsonDocument.Parse(msg);
  197. StuListChange stuListChange = msg.ToObject<StuListChange>();
  198. //名单变动修改学生课程关联信息
  199. await StuListService.FixStuCourse(client, stuListChange);
  200. //Vote投票 Survey问卷 Exam评测 Learn学习活动 Homework作业活动
  201. //名单变动修改学生问卷关联信息
  202. await StuListService.FixActivity(client, stuListChange, "Survey");
  203. //名单变动修改学生投票关联信息
  204. await StuListService.FixActivity(client, stuListChange, "Vote");
  205. //名单变动修改学生评测关联信息
  206. await StuListService.FixActivity(client, stuListChange, "Exam");
  207. //TODO学习活动
  208. //await FixActivity(client, stuListChange, "Learn");
  209. //TODO作业活动
  210. // await FixActivity(client, stuListChange, "Homework");
  211. }
  212. catch (Exception ex)
  213. {
  214. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListServiceBus-StuList\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  215. }
  216. }
  217. }
  218. }