IndexController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Azure.Storage.Blobs.Models;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using static TEAMModelOS.SDK.Models.Service.SystemService;
  5. using System.Collections.Concurrent;
  6. using System.Text.Json;
  7. using System.Text;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Models.Service;
  10. using TEAMModelOS.SDK;
  11. using TEAMModelOS.SDK.Extension;
  12. using Azure.Storage.Blobs.Specialized;
  13. namespace HTEX.Complex.Controllers
  14. {
  15. [ApiController]
  16. [Route("api")]
  17. public class IndexController : ControllerBase
  18. {
  19. private readonly DingDing _dingDing;
  20. private readonly IHttpClientFactory _httpClient;
  21. private readonly IConfiguration _configuration;
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly AzureRedisFactory _azureRedis;
  24. private readonly IPSearcher _ipSearcher;
  25. private readonly Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator;
  26. public IndexController(AzureRedisFactory azureRedis, Region2LongitudeLatitudeTranslator longitudeLatitudeTranslator, IHttpClientFactory httpClient, IConfiguration configuration, AzureStorageFactory azureStorage, IPSearcher searcher, DingDing dingDing)
  27. {
  28. _httpClient = httpClient;
  29. _configuration = configuration;
  30. _azureStorage = azureStorage;
  31. _ipSearcher = searcher;
  32. _dingDing = dingDing;
  33. _azureRedis=azureRedis;
  34. _longitudeLatitudeTranslator = longitudeLatitudeTranslator;
  35. }
  36. /// <summary>
  37. /// 上传到blob
  38. /// </summary>
  39. /// <param name="request"></param>
  40. /// <returns></returns>
  41. [HttpPost("http-log")]
  42. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  43. public async Task<IActionResult> HttpLog(JsonElement json)
  44. {
  45. try
  46. {
  47. string data = json.ToJsonString();
  48. var gmt8Time = DateTimeOffset.Now.GetGMTTime(8);
  49. var appendBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/{gmt8Time:HH}.log");
  50. if (!await appendBlob.ExistsAsync())
  51. {
  52. await appendBlob.CreateAsync();
  53. }
  54. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n")))
  55. {
  56. await appendBlob.AppendBlockAsync(stream);
  57. }
  58. return Ok(new { code = 1 });
  59. }
  60. catch (Exception ex)
  61. {
  62. return Ok(new { code = 1 });
  63. }
  64. }
  65. [HttpPost("report-api-settle")]
  66. [AllowAnonymous]
  67. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  68. public async Task<IActionResult> ReportApiSettle(JsonElement json)
  69. {
  70. List<string> times = json.GetProperty("times").ToObject<List<string>>();
  71. return Ok(new { });
  72. }
  73. [HttpPost("report-api-reload")]
  74. [AllowAnonymous]
  75. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  76. public async Task<IActionResult> ReportApiTest(JsonElement json)
  77. {
  78. try
  79. {
  80. List<string> times = json.GetProperty("times").ToObject<List<string>>();
  81. foreach (var timeDate in times)
  82. {
  83. if (DateTimeOffset.TryParse(timeDate, out DateTimeOffset date))
  84. {
  85. var gmt8Time = date.GetGMTTime(8);
  86. var nowGmt8Time = DateTimeOffset.Now.GetGMTTime(8);
  87. List<string> logs = await _azureStorage.GetBlobContainerClient("0-service-log").List($"http-log/{date:yyyy}/{date:MM}/{date:dd}");
  88. List<HttpLog> httpLogs = new List<HttpLog>();
  89. foreach (var log in logs)
  90. {
  91. string nowPath = $"{nowGmt8Time:yyyy/MM/dd/HH}.log";
  92. if (!log.Contains("index.log") && !log.Contains(".json") &&!log.Contains(nowPath))
  93. {
  94. BlobDownloadResult result = await _azureStorage.GetBlobContainerClient("0-service-log").GetBlobBaseClient(log).DownloadContentAsync();
  95. var content = result.Content.ToString();
  96. content= content.Substring(0, content.Length-2);
  97. if (content.EndsWith("}"))
  98. {
  99. content=$"[{content}]";
  100. }
  101. else
  102. {
  103. content=$"[{content}}}]";
  104. }
  105. httpLogs.AddRange(content.ToObject<List<HttpLog>>());
  106. }
  107. }
  108. (ConcurrentBag<ApiVisit> visits, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo) =
  109. await SystemService.ConvertHttpLog(httpLogs, _azureRedis, _ipSearcher, _longitudeLatitudeTranslator, gmt8Time, true);
  110. if (visits!=null && visits.Count>0)
  111. {
  112. var appendDayBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/index.log");
  113. if (!await appendDayBlob.ExistsAsync())
  114. {
  115. await appendDayBlob.CreateAsync();
  116. }
  117. else
  118. {
  119. await appendDayBlob.DeleteAsync();
  120. await appendDayBlob.CreateAsync();
  121. }
  122. int maxSize = 4*1024 * 1024; // 1M
  123. List<string> parts = new List<string>();
  124. StringBuilder sb = new StringBuilder();
  125. foreach (var item in visits)
  126. {
  127. string jsonString = $"{item.ToJsonString()},\n";
  128. int currentSize = Encoding.UTF8.GetByteCount(sb.ToString());
  129. if (currentSize + Encoding.UTF8.GetByteCount(jsonString) > maxSize)
  130. {
  131. parts.Add(sb.ToString());
  132. sb.Clear();
  133. }
  134. sb.Append(jsonString);
  135. }
  136. if (sb.Length > 0)
  137. {
  138. parts.Add(sb.ToString());
  139. }
  140. foreach (string part in parts)
  141. {
  142. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(part)))
  143. {
  144. await appendDayBlob.AppendBlockAsync(stream);
  145. }
  146. }
  147. //using (var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{sb}")))
  148. //{
  149. // await appendDayBlob.AppendBlockAsync(stream);
  150. //}
  151. }
  152. }
  153. }
  154. //全网:用户访问数,学校访问数,学生访问数,不同业务访问量,
  155. return Ok(new { });
  156. }
  157. catch (Exception ex)
  158. {
  159. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  160. }
  161. return Ok();
  162. }
  163. /// <summary>
  164. /// 使用时长,一天内累计
  165. /// </summary>
  166. /// <param name="json"></param>
  167. /// <returns></returns>
  168. [HttpPost("report-api")]
  169. [AllowAnonymous]
  170. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  171. public async Task<IActionResult> ReportApi(JsonElement json)
  172. {
  173. var force = json.GetProperty("force").GetString();
  174. List<string> times = json.GetProperty("times").ToObject<List<string>>();
  175. await SystemService.VisitSettle(times, _azureStorage, _azureRedis, _longitudeLatitudeTranslator, _ipSearcher, $"{force}");
  176. return Ok(new { code = 200 });
  177. }
  178. }
  179. }