using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models.Cosmos.BI; namespace TEAMModelBI.Controllers.BIHome { [Route("analyse")] [ApiController] public class AnalyseFileController : ControllerBase { private readonly IWebHostEnvironment _environment; //读取文件流 public AnalyseFileController(IWebHostEnvironment environment) { _environment = environment; } [HttpPost("get-visitjson")] public async Task GetVisitJson(JsonElement jsonElement) { jsonElement.TryGetProperty("path", out JsonElement _path); jsonElement.TryGetProperty("time", out JsonElement _time); var path = $"{_environment.ContentRootPath}/JsonFile/TempFile/PT1H.json"; StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8); List objs = new(); StringBuilder visits = new StringBuilder("["); string text; while ((text = streamReader.ReadLine()) != null) { if (streamReader.EndOfStream) { visits.Append($"{text.ToString()}"); } else { visits.Append($"{text.ToString()},"); } objs.Add(text.ToJsonString()); } visits.Append("]"); streamReader.Close(); string input = visits.ToString(); List aGInfos = input.ToObject>(); //var ipGroup = aGInfos.GroupBy(g => g.properties.clientIp).ToDictionary(k => k.Key, k => k.Count()).ToList(); List recInfo = aGInfos.Select(x => new RecGWInfo { hour = DateTimeOffset.Parse(x.time).ToString("yyyyMMddHH"),hostName=x.properties.hostname }).ToList(); List ipCnt = aGInfos.GroupBy(g => g.properties.clientIp).Select(x => new StatisNameCnt { name = x.Key, cnt = x.Count() }).ToList(); ipCnt.Sort((x, y) => y.cnt.CompareTo(x.cnt)); DateTimeOffset dateTime = DateTimeOffset.UtcNow; 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(); return Ok(new { state = 200, apiCnt, ipCnt }); } public record RecGWInfo { public string hour { get; set; } public string ip { get; set; } public string api { get; set; } public string hostName { get; set; } } public record StatisNameCnt { public string name { get; set; } public int cnt { get; set; } } public record AGInfo { //public string resourceId { get; set; } public string operationName { get; set; } public string time { get; set; } public string category { get; set; } public Properties properties { get; set; } } public record Properties { //public string instanceId { get; set; } public string clientIp { get; set; } public string clientPort { get; set; } public string requestUri { get; set; } public string ruleSetType { get; set; } public string ruleSetVersion { get; set; } public string ruleId { get; set;} public string ruleGroup { get; set; } //public string message { get; set; } public string action { get; set; } public string site { get; set; } //public Datails datails { get; set; } public string hostname { get; set; } public string transactionId { get; set; } } public record Datails { public string message { get; set; } public string data { get; set; } public string file { get; set; } public string line { get; set; } } } }