AnalyseFileController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 TEAMModelBI.Models.RecordM;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models.Cosmos.BI;
  17. namespace TEAMModelBI.Controllers.BIHome
  18. {
  19. [Route("analyse")]
  20. [ApiController]
  21. public class AnalyseFileController : ControllerBase
  22. {
  23. private readonly IWebHostEnvironment _environment; //读取文件流
  24. private readonly AzureStorageFactory _azureStorage;
  25. public AnalyseFileController(IWebHostEnvironment environment, AzureStorageFactory azureStorage)
  26. {
  27. _environment = environment;
  28. _azureStorage = azureStorage;
  29. }
  30. [HttpPost("get-visitjson")]
  31. public async Task<IActionResult> GetVisitJson(JsonElement jsonElement)
  32. {
  33. jsonElement.TryGetProperty("path", out JsonElement _path);
  34. jsonElement.TryGetProperty("time", out JsonElement _time);
  35. var path = $"{_environment.ContentRootPath}/JsonFile/TempFile/PT1H.json";
  36. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  37. List<object> objs = new();
  38. StringBuilder visits = new StringBuilder("[");
  39. string text;
  40. while ((text = streamReader.ReadLine()) != null)
  41. {
  42. if (streamReader.EndOfStream)
  43. visits.Append($"{text.ToString()}");
  44. else
  45. visits.Append($"{text.ToString()},");
  46. }
  47. visits.Append("]");
  48. streamReader.Close();
  49. string input = visits.ToString();
  50. List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
  51. DateTimeOffset dtime = DateTimeOffset.UtcNow;
  52. string cHour = dtime.ToString("yyyyMMddHH");
  53. string cDay = dtime.ToString("yyyyMMdd");
  54. if (aGInfos.Count > 0)
  55. {
  56. cHour = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMddHH")).First();
  57. cDay = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMdd")).First();
  58. }
  59. RecCnt saveCnts = new();
  60. //var ipGroup = aGInfos.GroupBy(g => g.properties.clientIp).ToDictionary(k => k.Key, k => k.Count()).ToList();
  61. List<RecAppGWInfo> recInfo = aGInfos.Select(s => new RecAppGWInfo { 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();
  62. 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();
  63. saveCnts.apiCnt= apiCnt;
  64. 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();
  65. saveCnts.ipCnt = ipCnt;
  66. ////保存存至Blob文件
  67. var url = await _azureStorage.UploadFileByContainer("0-public", saveCnts.ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
  68. var blob = _azureStorage.GetBlobContainerClient($"0-public");
  69. List<RecCnt> recCnts = new();
  70. await foreach (BlobItem blobItem in blob.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{cDay}"))
  71. {
  72. BlobClient blobClient = blob.GetBlobClient(blobItem.Name);
  73. if (await blobClient.ExistsAsync())
  74. {
  75. using (var meomoryStream = new MemoryStream())
  76. {
  77. var response = blob.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
  78. //var response = await blob.GetBlobClient($"{blobItem.Name}").DownloadToAsync(meomoryStream);
  79. var temps = meomoryStream.ToString();
  80. var temp1 = Encoding.UTF8.GetString(meomoryStream.ToArray());
  81. var temp = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToObject<RecCnt>();
  82. RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<RecCnt>();
  83. recCnts.Add(recCnt);
  84. }
  85. }
  86. }
  87. //var url = await _azureStorage.UploadFileByContainer("0-public", kvList.ToList().ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
  88. 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 }); ;
  89. //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 });;
  90. }
  91. public record StatisNameCnt
  92. {
  93. public string name { get; set; }
  94. public int cnt { get; set; }
  95. }
  96. public record AGInfo
  97. {
  98. //public string resourceId { get; set; }
  99. public string operationName { get; set; }
  100. public string time { get; set; }
  101. public string category { get; set; }
  102. public Properties properties { get; set; }
  103. }
  104. public record Properties
  105. {
  106. //public string instanceId { get; set; }
  107. public string clientIp { get; set; }
  108. public string clientPort { get; set; }
  109. public string requestUri { get; set; }
  110. public string ruleSetType { get; set; }
  111. public string ruleSetVersion { get; set; }
  112. public string ruleId { get; set;}
  113. public string ruleGroup { get; set; }
  114. //public string message { get; set; }
  115. public string action { get; set; }
  116. public string site { get; set; }
  117. //public Datails datails { get; set; }
  118. public string hostname { get; set; }
  119. public string transactionId { get; set; }
  120. }
  121. public record Datails
  122. {
  123. public string message { get; set; }
  124. public string data { get; set; }
  125. public string file { get; set; }
  126. public string line { get; set; }
  127. }
  128. }
  129. }