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.Text.RegularExpressions;
using System.Threading.Tasks;
using TEAMModelBI.Tool;
using TEAMModelBI.Tool.CosmosBank;
using TEAMModelOS.SDK.Context.BI;
using TEAMModelOS.SDK.Context.Constant;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models.Cosmos.BI;
namespace TEAMModelBI.Controllers.BIBlob
{
[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;
}
///
/// 查询一天的分析防火墙分析 已对接
///
///
///
[ProducesDefaultResponseType]
[HttpPost("get-dayapi")]
public async Task GetDayApi(JsonElement jsonElement)
{
jsonElement.TryGetProperty("dateTime", out JsonElement dateTime);
jsonElement.TryGetProperty("site", out JsonElement site);
DateTimeOffset dtOff = DateTimeOffset.UtcNow;
var blobCilent = _azureStorage.GetBlobContainerClient(containerName: "0-public");
if ($"{site}".Equals(BIConst.Global))
blobCilent = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
if (!string.IsNullOrEmpty($"{dateTime}"))
dtOff = TimeHelper.GetDateTime(dateTime.GetInt64());
string day = dtOff.ToString("yyyyMMdd");
Dictionary days = new();
await foreach (BlobItem blobItem in blobCilent.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{day}"))
{
if (blobItem.Name.EndsWith(".json") && !blobItem.Name.EndsWith("/days.json"))
{
BlobClient tempClient = blobCilent.GetBlobClient(blobItem.Name);
if (await tempClient.ExistsAsync())
{
using (var meomoryStream = new MemoryStream())
{
var response = blobCilent.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
//var response = await blob.GetBlobClient($"{blobItem.Name}").DownloadToAsync(meomoryStream);
RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject();
string name = stringSuffix.MidStrEx(blobItem.Name, "/", ".");
days.Add(name, recCnt);
}
}
}
}
return Ok(new { state = RespondCode.Ok, days = days.ToList() });
}
///
/// 查询七天的数据
///
///
///
[ProducesDefaultResponseType]
[HttpPost("get-sevendayapi")]
public async Task GetSevenDayApi(JsonElement jsonElement)
{
jsonElement.TryGetProperty("site", out JsonElement site);
var blobCilent = _azureStorage.GetBlobContainerClient(containerName: "0-public");
if ($"{site}".Equals(BIConst.Global))
blobCilent = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
List savenDayApis = new();
List day7 = TimeHelper.GetNearDay(DateTimeOffset.UtcNow, 7);
foreach (var item in day7)
{
await foreach (BlobItem blobItem in blobCilent.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{item}"))
{
BlobClient tempClient = blobCilent.GetBlobClient(blobItem.Name);
if (await tempClient.ExistsAsync())
{
using (var meomoryStream = new MemoryStream())
{
var response = blobCilent.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
//var response = await blob.GetBlobClient($"{blobItem.Name}").DownloadToAsync(meomoryStream);
RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject();
savenDayApis.AddRange(recCnt.apiCnt);
}
}
}
}
return Ok(new { state = RespondCode.Ok, savenDayApis });
}
///
/// 查询三十天的数据
///
///
///
[ProducesDefaultResponseType]
[HttpPost("get-thirtydayapi")]
public async Task GetThirtyDayApi(JsonElement jsonElement)
{
jsonElement.TryGetProperty("site", out JsonElement site);
var blobCilent = _azureStorage.GetBlobContainerClient(containerName: "0-public");
if ($"{site}".Equals(BIConst.Global))
blobCilent = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
List thirtyDayApi = new();
List day30 = TimeHelper.GetNearDay(DateTimeOffset.UtcNow, 30);
foreach (var item in day30)
{
await foreach (BlobItem blobItem in blobCilent.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{item}"))
{
BlobClient tempClient = blobCilent.GetBlobClient(blobItem.Name);
if (await tempClient.ExistsAsync())
{
using (var meomoryStream = new MemoryStream())
{
var response = blobCilent.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
//var response = await blob.GetBlobClient($"{blobItem.Name}").DownloadToAsync(meomoryStream);
RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject();
thirtyDayApi.AddRange(recCnt.apiCnt);
}
}
}
}
return Ok(new { state = RespondCode.Ok, thirtyDayApi });
}
///
/// 本地Json
///
///
///
[ProducesDefaultResponseType]
[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