using Azure.Cosmos;
using Azure.Storage.Blobs.Models;
using DinkToPdf;
using DinkToPdf.Contracts;
using HTEXLib.COMM.Helpers;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models;
using TEAMModelOS.SDK.Models.Cosmos.Teacher;
using TEAMModelOS.SDK.Models.Service.BI;
using TEAMModelOS.SDK.Models.Table;
using static TEAMModelOS.SDK.Models.Teacher;
namespace TEAMModelOS.FunctionV4.TimeTrigger
{
public class IESTimerTrigger
{
private readonly AzureCosmosFactory _azureCosmos;
private readonly DingDing _dingDing;
private readonly AzureStorageFactory _azureStorage;
private readonly AzureRedisFactory _azureRedis;
private readonly IConverter _converter;
public IESTimerTrigger(IConverter converter, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
{
_azureCosmos = azureCosmos;
_dingDing = dingDing;
_azureStorage = azureStorage;
_azureRedis = azureRedis;
_converter = converter;
} ///
///
///
///
///
[Function("FireWallFileLog")]
//https://docs.azure.cn/zh-cn/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
//0 1 * * * * 一天中每小时的第 1 分钟
//0 */1 * * * * 每五分钟一次
public async Task FireWallFileLog([TimerTrigger("0 1 * * * *")] TimerInfo myTimer, ILogger log)
{
try
{
string location = Environment.GetEnvironmentVariable("Option:Location");
var datetime = DateTimeOffset.UtcNow.AddHours(-1);
var y = datetime.Year;
var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}";
var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}";
var h = datetime.Hour >= 10 ? $"{datetime.Hour}" : $"0{datetime.Hour}";
if (location.Equals("China"))
{
string path = $"resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.NETWORK/APPLICATIONGATEWAYS/OSFIREWARE/y={y}/m={m}/d={d}/h={h}/m=00/PT1H.json";
var retn = await BILogAnalyseService.GetPathAnalyse(_azureStorage, path, "LogStorage");
if (retn.recCnts.IsNotEmpty())
{
var topApi = retn.recCnts.SelectMany(x => x.apiCnt).OrderByDescending(o => o.count).Take(5).Select(x => new { x.api, x.ip, x.count });
var topIp = retn.recCnts.SelectMany(x => x.ipCnt).OrderByDescending(o => o.count).Take(5).Select(x => new { x.api, x.ip, x.count });
//https://teammodelos.blob.core.chinacloudapi.cn/0-public/pie-borderRadius.html
await _dingDing.SendBotMarkdown("防火墙日志记录", $"#### 防火墙日志记录\n> 记录时间:{datetime.AddHours(8).ToString("yyyy-MM-dd HH")}\n> ![screenshot]()\n> ###### 发布时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} [地址]({retn.saveUrls.First()}) \n", GroupNames.成都开发測試群組);
}
}
else if (location.Contains("Global"))
{
}
}
catch (Exception ex)
{
// await _dingDing.SendBotMsg($"FireWallFileLog 防火墙日志记录: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
}
}