123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using Azure.Storage.Blobs;
- using Azure.Storage.Blobs.Models;
- 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.DI;
- 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; //读取文件流
- private readonly AzureStorageFactory _azureStorage;
- public AnalyseFileController(IWebHostEnvironment environment, AzureStorageFactory azureStorage)
- {
- _environment = environment;
- _azureStorage = azureStorage;
- }
- [HttpPost("get-visitjson")]
- public async Task<IActionResult> 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<object> 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()},");
- }
- visits.Append("]");
- streamReader.Close();
- string input = visits.ToString();
- List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
- DateTimeOffset dtime = DateTimeOffset.UtcNow;
- string cHour = dtime.ToString("yyyyMMddHH");
- string cDay = dtime.ToString("yyyyMMdd");
- if (aGInfos.Count > 0)
- {
- cHour = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMddHH")).First();
- cDay = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMdd")).First();
- }
- RecCnt saveCnts = new();
- //var ipGroup = aGInfos.GroupBy(g => g.properties.clientIp).ToDictionary(k => k.Key, k => k.Count()).ToList();
- 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();
- 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();
- saveCnts.apiCnt= apiCnt;
- 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();
- saveCnts.ipCnt = ipCnt;
- ////保存存至Blob文件
- var url = await _azureStorage.UploadFileByContainer("0-public", saveCnts.ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
- var blob = _azureStorage.GetBlobContainerClient($"0-public");
- List<RecCnt> recCnts = new();
- await foreach (BlobItem blobItem in blob.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{cDay}"))
- {
- BlobClient blobClient = blob.GetBlobClient(blobItem.Name);
- if (await blobClient.ExistsAsync())
- {
- using (var meomoryStream = new MemoryStream())
- {
- var response = blob.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
- //var response = await blob.GetBlobClient($"{blobItem.Name}").DownloadToAsync(meomoryStream);
- var temps = meomoryStream.ToString();
- var temp1 = Encoding.UTF8.GetString(meomoryStream.ToArray());
- var temp = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToObject<RecCnt>();
- RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<RecCnt>();
- recCnts.Add(recCnt);
- }
- }
- }
- //var url = await _azureStorage.UploadFileByContainer("0-public", kvList.ToList().ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
- 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 }); ;
- //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 });;
- }
- public record RecCnt
- {
- public List<RecApiCnt> apiCnt { get; set; }
- public List<RecIpCnt> ipCnt { get; set; }
- }
- public record RecCntBas
- {
- public int count { get; set; }
- public string hour { get; set; }
- public List<string> hostName { get; set; }
- }
- public record RecIpCnt : RecCntBas
- {
- public string ip { get; set; }
- public List<string> api { get; set; }
- }
- public record RecApiCnt : RecCntBas
- {
- public string api { get; set; }
- public List<string> ip { get; set; }
- }
- 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; }
- }
- }
- }
|