MonitorServicesBus.cs 8.1 KB

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