MonitorServicesBus.cs 7.0 KB

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