AnalyseFileController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Azure.Storage.Blobs;
  2. using Azure.Storage.Blobs.Models;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Models.Cosmos.BI;
  16. namespace TEAMModelBI.Controllers.BIHome
  17. {
  18. [Route("analyse")]
  19. [ApiController]
  20. public class AnalyseFileController : ControllerBase
  21. {
  22. private readonly IWebHostEnvironment _environment; //读取文件流
  23. private readonly AzureStorageFactory _azureStorage;
  24. public AnalyseFileController(IWebHostEnvironment environment, AzureStorageFactory azureStorage)
  25. {
  26. _environment = environment;
  27. _azureStorage = azureStorage;
  28. }
  29. [HttpPost("get-visitjson")]
  30. public async Task<IActionResult> GetVisitJson(JsonElement jsonElement)
  31. {
  32. jsonElement.TryGetProperty("path", out JsonElement _path);
  33. jsonElement.TryGetProperty("time", out JsonElement _time);
  34. var path = $"{_environment.ContentRootPath}/JsonFile/TempFile/PT1H.json";
  35. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  36. List<object> objs = new();
  37. StringBuilder visits = new StringBuilder("[");
  38. string text;
  39. while ((text = streamReader.ReadLine()) != null)
  40. {
  41. if (streamReader.EndOfStream)
  42. visits.Append($"{text.ToString()}");
  43. else
  44. visits.Append($"{text.ToString()},");
  45. }
  46. visits.Append("]");
  47. streamReader.Close();
  48. string input = visits.ToString();
  49. List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
  50. DateTimeOffset dtime = DateTimeOffset.UtcNow;
  51. string cHour = dtime.ToString("yyyyMMddHH");
  52. string cDay = dtime.ToString("yyyyMMdd");
  53. if (aGInfos.Count > 0)
  54. {
  55. cHour = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMddHH")).First();
  56. cDay = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMdd")).First();
  57. }
  58. RecCnt saveCnts = new();
  59. //var ipGroup = aGInfos.GroupBy(g => g.properties.clientIp).ToDictionary(k => k.Key, k => k.Count()).ToList();
  60. List<RecGWInfo> recInfo = aGInfos.Select(s => new RecGWInfo { hour = cHour, ip = s.properties.clientIp, api = s.properties.requestUri.Split("?").ToList().Count() > 1 ? s.properties.requestUri.Split("?").ToList()[0] : s.properties.requestUri, hostName = s.properties.hostname }).ToList();
  61. List<RecApiCnt> apiCnt = recInfo.GroupBy(a => a.api).Select(g => new RecApiCnt { api = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), ip = g.Select(i => i.ip).Distinct().ToList() }).ToList();
  62. saveCnts.apiCnt= apiCnt;
  63. List<RecIpCnt> ipCnt = recInfo.GroupBy(a => a.ip).Select(g => new RecIpCnt { ip = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), api = g.Select(i => i.api).Distinct().ToList() }).ToList();
  64. saveCnts.ipCnt = ipCnt;
  65. ////保存存至Blob文件
  66. var url = await _azureStorage.UploadFileByContainer("0-public", saveCnts.ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
  67. var blob = _azureStorage.GetBlobContainerClient($"0-public");
  68. List<RecCnt> recCnts = new();
  69. await foreach (BlobItem blobItem in blob.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{cDay}"))
  70. {
  71. BlobClient blobClient = blob.GetBlobClient(blobItem.Name);
  72. if (await blobClient.ExistsAsync())
  73. {
  74. using (var meomoryStream = new MemoryStream())
  75. {
  76. var response = blob.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
  77. //var response = await blob.GetBlobClient($"{blobItem.Name}").DownloadToAsync(meomoryStream);
  78. var temps = meomoryStream.ToString();
  79. var temp1 = Encoding.UTF8.GetString(meomoryStream.ToArray());
  80. var temp = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToObject<RecCnt>();
  81. RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<RecCnt>();
  82. recCnts.Add(recCnt);
  83. }
  84. }
  85. }
  86. //var url = await _azureStorage.UploadFileByContainer("0-public", kvList.ToList().ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
  87. return Ok(new { state = 200, recCnts, apiCon = apiCnt.Count(), apiSum = apiCnt.Select(ap => ap.count).Sum(), ipCount = ipCnt.Count, ipSum = ipCnt.Select(ip => ip.count).Sum(), ipCnt, apiCnt }); ;
  88. //return Ok(new { state = 200, cnt = recInfo.Count, apiCon = apiCnt.Count(), apiSum = apiCnt.Select(ap=> ap.count).Sum(), ipCount = ipCnt.Count, ipSum = ipCnt.Select(ip=>ip.count).Sum(), ipCnt,apiCnt, recInfo });;
  89. }
  90. public record RecCnt
  91. {
  92. public List<RecApiCnt> apiCnt { get; set; }
  93. public List<RecIpCnt> ipCnt { get; set; }
  94. }
  95. public record RecCntBas
  96. {
  97. public int count { get; set; }
  98. public string hour { get; set; }
  99. public List<string> hostName { get; set; }
  100. }
  101. public record RecIpCnt : RecCntBas
  102. {
  103. public string ip { get; set; }
  104. public List<string> api { get; set; }
  105. }
  106. public record RecApiCnt : RecCntBas
  107. {
  108. public string api { get; set; }
  109. public List<string> ip { get; set; }
  110. }
  111. public record RecGWInfo
  112. {
  113. public string hour { get; set; }
  114. public string ip { get; set; }
  115. public string api { get; set; }
  116. public string hostName { get; set; }
  117. }
  118. public record StatisNameCnt
  119. {
  120. public string name { get; set; }
  121. public int cnt { get; set; }
  122. }
  123. public record AGInfo
  124. {
  125. //public string resourceId { get; set; }
  126. public string operationName { get; set; }
  127. public string time { get; set; }
  128. public string category { get; set; }
  129. public Properties properties { get; set; }
  130. }
  131. public record Properties
  132. {
  133. //public string instanceId { get; set; }
  134. public string clientIp { get; set; }
  135. public string clientPort { get; set; }
  136. public string requestUri { get; set; }
  137. public string ruleSetType { get; set; }
  138. public string ruleSetVersion { get; set; }
  139. public string ruleId { get; set;}
  140. public string ruleGroup { get; set; }
  141. //public string message { get; set; }
  142. public string action { get; set; }
  143. public string site { get; set; }
  144. //public Datails datails { get; set; }
  145. public string hostname { get; set; }
  146. public string transactionId { get; set; }
  147. }
  148. public record Datails
  149. {
  150. public string message { get; set; }
  151. public string data { get; set; }
  152. public string file { get; set; }
  153. public string line { get; set; }
  154. }
  155. }
  156. }