|
@@ -0,0 +1,207 @@
|
|
|
+using Azure.Storage.Blobs.Models;
|
|
|
+using Azure.Storage.Blobs.Specialized;
|
|
|
+using Microsoft.AspNetCore.Authorization;
|
|
|
+using Microsoft.AspNetCore.Cors;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using System.Collections.Concurrent;
|
|
|
+using System.Configuration;
|
|
|
+using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Net;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using System.ServiceModel.Channels;
|
|
|
+using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
+using System.Web;
|
|
|
+using TEAMModelOS.SDK;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using static TEAMModelOS.SDK.Models.Service.SystemService;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK.Models.Service;
|
|
|
+namespace HTEX.Screen.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 });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|