IndexController.cs 8.8 KB

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