AnalyseFileController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI;
  13. namespace TEAMModelBI.Controllers.BIHome
  14. {
  15. [Route("analyse")]
  16. [ApiController]
  17. public class AnalyseFileController : ControllerBase
  18. {
  19. private readonly IWebHostEnvironment _environment; //读取文件流
  20. public AnalyseFileController(IWebHostEnvironment environment)
  21. {
  22. _environment = environment;
  23. }
  24. [HttpPost("get-visitjson")]
  25. public async Task<IActionResult> GetVisitJson(JsonElement jsonElement)
  26. {
  27. jsonElement.TryGetProperty("path", out JsonElement _path);
  28. jsonElement.TryGetProperty("time", out JsonElement _time);
  29. var path = $"{_environment.ContentRootPath}/JsonFile/TempFile/PT1H.json";
  30. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  31. List<object> objs = new();
  32. StringBuilder visits = new StringBuilder("[");
  33. string text;
  34. while ((text = streamReader.ReadLine()) != null)
  35. {
  36. if (streamReader.EndOfStream)
  37. {
  38. visits.Append($"{text.ToString()}");
  39. }
  40. else
  41. {
  42. visits.Append($"{text.ToString()},");
  43. }
  44. objs.Add(text.ToJsonString());
  45. }
  46. visits.Append("]");
  47. streamReader.Close();
  48. string input = visits.ToString();
  49. List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
  50. //var ipGroup = aGInfos.GroupBy(g => g.properties.clientIp).ToDictionary(k => k.Key, k => k.Count()).ToList();
  51. List<RecGWInfo> recInfo = aGInfos.Select(x => new RecGWInfo { hour = DateTimeOffset.Parse(x.time).ToString("yyyyMMddHH"),hostName=x.properties.hostname }).ToList();
  52. List<StatisNameCnt> ipCnt = aGInfos.GroupBy(g => g.properties.clientIp).Select(x => new StatisNameCnt { name = x.Key, cnt = x.Count() }).ToList();
  53. ipCnt.Sort((x, y) => y.cnt.CompareTo(x.cnt));
  54. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  55. var apiCnt = aGInfos.Select(s => s.properties.requestUri.Split("?").ToList().Count()>1 ? s.properties.requestUri.Split("?").ToList()[0]: s.properties.requestUri).GroupBy(g => g).Select(x=>new { key = x.Key,cnt =x.Count()}).ToList();
  56. return Ok(new { state = 200, apiCnt, ipCnt });
  57. }
  58. public record RecGWInfo
  59. {
  60. public string hour { get; set; }
  61. public string ip { get; set; }
  62. public string api { get; set; }
  63. public string hostName { get; set; }
  64. }
  65. public record StatisNameCnt
  66. {
  67. public string name { get; set; }
  68. public int cnt { get; set; }
  69. }
  70. public record AGInfo
  71. {
  72. //public string resourceId { get; set; }
  73. public string operationName { get; set; }
  74. public string time { get; set; }
  75. public string category { get; set; }
  76. public Properties properties { get; set; }
  77. }
  78. public record Properties
  79. {
  80. //public string instanceId { get; set; }
  81. public string clientIp { get; set; }
  82. public string clientPort { get; set; }
  83. public string requestUri { get; set; }
  84. public string ruleSetType { get; set; }
  85. public string ruleSetVersion { get; set; }
  86. public string ruleId { get; set;}
  87. public string ruleGroup { get; set; }
  88. //public string message { get; set; }
  89. public string action { get; set; }
  90. public string site { get; set; }
  91. //public Datails datails { get; set; }
  92. public string hostname { get; set; }
  93. public string transactionId { get; set; }
  94. }
  95. public record Datails
  96. {
  97. public string message { get; set; }
  98. public string data { get; set; }
  99. public string file { get; set; }
  100. public string line { get; set; }
  101. }
  102. }
  103. }