MonitorServicesBus.cs 10 KB

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