123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using Azure.Storage.Blobs.Models;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using static TEAMModelOS.SDK.Models.Service.SystemService;
- using System.Collections.Concurrent;
- using System.Text.Json;
- using System.Text;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models.Service;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Extension;
- using Azure.Storage.Blobs.Specialized;
- namespace HTEX.Complex.Controllers
- {
- [ApiController]
- [Route("api")]
- public class IndexController : ControllerBase
- {
- private readonly DingDing _dingDing;
- private readonly IHttpClientFactory _httpClient;
- private readonly IConfiguration _configuration;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly IPSearcher _ipSearcher;
- private readonly Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator;
- public IndexController(AzureRedisFactory azureRedis, Region2LongitudeLatitudeTranslator longitudeLatitudeTranslator, IHttpClientFactory httpClient, IConfiguration configuration, AzureStorageFactory azureStorage, IPSearcher searcher, DingDing dingDing)
- {
- _httpClient = httpClient;
- _configuration = configuration;
- _azureStorage = azureStorage;
- _ipSearcher = searcher;
- _dingDing = dingDing;
- _azureRedis=azureRedis;
- _longitudeLatitudeTranslator = longitudeLatitudeTranslator;
- }
- /// <summary>
- /// 上传到blob
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("http-log")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> HttpLog(JsonElement json)
- {
- try
- {
- string data = json.ToJsonString();
- var gmt8Time = DateTimeOffset.Now.GetGMTTime(8);
- var appendBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/{gmt8Time:HH}.log");
- if (!await appendBlob.ExistsAsync())
- {
- await appendBlob.CreateAsync();
- }
- using (var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n")))
- {
- await appendBlob.AppendBlockAsync(stream);
- }
- return Ok(new { code = 1 });
- }
- catch (Exception ex)
- {
- return Ok(new { code = 1 });
- }
- }
- [HttpPost("report-api-settle")]
- [AllowAnonymous]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> ReportApiSettle(JsonElement json)
- {
- List<string> times = json.GetProperty("times").ToObject<List<string>>();
- return Ok(new { });
- }
- [HttpPost("report-api-reload")]
- [AllowAnonymous]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> ReportApiTest(JsonElement json)
- {
- try
- {
- List<string> times = json.GetProperty("times").ToObject<List<string>>();
- foreach (var timeDate in times)
- {
- if (DateTimeOffset.TryParse(timeDate, out DateTimeOffset date))
- {
- var gmt8Time = date.GetGMTTime(8);
- var nowGmt8Time = DateTimeOffset.Now.GetGMTTime(8);
- List<string> logs = await _azureStorage.GetBlobContainerClient("0-service-log").List($"http-log/{date:yyyy}/{date:MM}/{date:dd}");
- List<HttpLog> httpLogs = new List<HttpLog>();
- foreach (var log in logs)
- {
- string nowPath = $"{nowGmt8Time:yyyy/MM/dd/HH}.log";
- if (!log.Contains("index.log") && !log.Contains(".json") &&!log.Contains(nowPath))
- {
- BlobDownloadResult result = await _azureStorage.GetBlobContainerClient("0-service-log").GetBlobBaseClient(log).DownloadContentAsync();
- var content = result.Content.ToString();
- content= content.Substring(0, content.Length-2);
- if (content.EndsWith("}"))
- {
- content=$"[{content}]";
- }
- else
- {
- content=$"[{content}}}]";
- }
- httpLogs.AddRange(content.ToObject<List<HttpLog>>());
- }
- }
- (ConcurrentBag<ApiVisit> visits, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo) =
- await SystemService.ConvertHttpLog(httpLogs, _azureRedis, _ipSearcher, _longitudeLatitudeTranslator, gmt8Time, true);
- if (visits!=null && visits.Count>0)
- {
- var appendDayBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/index.log");
- if (!await appendDayBlob.ExistsAsync())
- {
- await appendDayBlob.CreateAsync();
- }
- else
- {
- await appendDayBlob.DeleteAsync();
- await appendDayBlob.CreateAsync();
- }
- int maxSize = 4*1024 * 1024; // 1M
- List<string> parts = new List<string>();
- StringBuilder sb = new StringBuilder();
- foreach (var item in visits)
- {
- string jsonString = $"{item.ToJsonString()},\n";
- int currentSize = Encoding.UTF8.GetByteCount(sb.ToString());
- if (currentSize + Encoding.UTF8.GetByteCount(jsonString) > maxSize)
- {
- parts.Add(sb.ToString());
- sb.Clear();
- }
- sb.Append(jsonString);
- }
- if (sb.Length > 0)
- {
- parts.Add(sb.ToString());
- }
- foreach (string part in parts)
- {
- using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(part)))
- {
- await appendDayBlob.AppendBlockAsync(stream);
- }
- }
- //using (var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{sb}")))
- //{
- // await appendDayBlob.AppendBlockAsync(stream);
- //}
- }
- }
- }
- //全网:用户访问数,学校访问数,学生访问数,不同业务访问量,
- return Ok(new { });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- return Ok();
- }
- /// <summary>
- /// 使用时长,一天内累计
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- [HttpPost("report-api")]
- [AllowAnonymous]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> ReportApi(JsonElement json)
- {
- var force = json.GetProperty("force").GetString();
- List<string> times = json.GetProperty("times").ToObject<List<string>>();
- await SystemService.VisitSettle(times, _azureStorage, _azureRedis, _longitudeLatitudeTranslator, _ipSearcher, $"{force}");
- return Ok(new { code = 200 });
- }
- }
- }
|