| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961 |
- using molilian.core;
- using dodohold.core;
- using Dapper;
- using Microsoft.AspNetCore.Mvc;
- using Org.BouncyCastle.Ocsp;
- using System.Text.Json;
- using System.Runtime.InteropServices;
- using System.Net;
- using System.Threading;
- using System.Data;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using TencentCloud.Soe.V20180724.Models;
- using static dodohold.core.ZTOExpress.CreateOrderArgs;
- using static Spire.Xls.Core.Spreadsheet.HTMLOptions;
- using System.Net.Http.Json;
- using System.Text.RegularExpressions;
- using System.Text;
- using TencentCloud.Oceanus.V20190422.Models;
- using YunhuiKit;
- using TencentCloud.Cdn.V20180606.Models;
- namespace molilian.api.Controllers
- {
- [ApiController]
- [Route("[controller]/[action]")]
- public class TestController : ControllerBase
- {
- protected IHttpContextAccessor _accessor;
- public TestController(IHttpContextAccessor accessor)
- {
- _accessor = accessor;
- }
- [HttpGet]
- public async Task<ActionResult> ecs_list_test()
- {
- var list = AliyunPoolCore.EcsList();
- return new APIResult(new
- {
- success = true,
- msg = "ok",
- list
- });
- }
-
- [HttpGet]
- public async Task<ActionResult> testReconnectionRedis()
- {
- string cacheKey = "test";
- RedisKit.SetAsync(cacheKey, 1, 3600);
- string val = await RedisKit.GetAsync<string>(cacheKey);
- return new APIResult(new { success = "ok", val });
- }
- [HttpGet]
- public async Task<ActionResult> ChangePublicIpByName(string nodeName)
- {
- var proxy_node = new DBContext.Table("proxy_nodes").Get<dynamic>("nodeName=@nodeName", new { nodeName });
- if (proxy_node == null) return new APIResult(new { success = false, msg = "没有匹配的 proxy_nodes 记录" });
- int aliyun_id = proxy_node.aliyun_id;
- string proxy_server = proxy_node.server;
- var account = new DBContext.Table("aliyun_pool").Get<AliyunPoolDTO>(aliyun_id);
- if (account == null) return new APIResult(new { success = false, msg = "没有匹配的 aliyun_pool 记录" });
- var uri = new Uri(proxy_server);
- string privateIp = uri.Host;
- AliyunCore core = new AliyunCore(account);
- var success = await core.ChangePublicIpAsync(privateIp);
- return new APIResult(new { success = "ok" });
- }
- [HttpGet]
- public async Task<ActionResult> ChangePublicIp(int id)
- {
- id = id >= 20000 ? id - 20000 : id;
- var proxy_node = new DBContext.Table("proxy_nodes").Get<dynamic>(id);
- if (proxy_node == null) return new APIResult(new { success = false, msg = "没有匹配的 proxy_nodes 记录" });
- int aliyun_id = proxy_node.aliyun_id;
- string proxy_server = proxy_node.server;
- var account = new DBContext.Table("aliyun_pool").Get<AliyunPoolDTO>(aliyun_id);
- if (account == null) return new APIResult(new { success = false, msg = "没有匹配的 aliyun_pool 记录" });
- var uri = new Uri(proxy_server);
- string privateIp = uri.Host;
- AliyunCore core = new AliyunCore(account);
- var success = await core.ChangePublicIpAsync(privateIp);
- return new APIResult(new { success = "ok" });
- }
- [HttpGet]
- //第一部 先加一个网卡,
- public async Task<ActionResult> CreateNetworkInterface(int id, string ecsid)
- {
- AliyunCore core = new AliyunCore(id);
- //创建弹性网卡并绑定公网IP
- var success = core.EcsCreateNetworkInterface("cn-beijing", ecsid);
- return new APIResult(new { success });
- }
- [HttpGet]
- //第二部 给网卡绑定很多个辅助ip
- public async Task<ActionResult> EcsAssignPrivateIpAddresses(int id, string ecsid, int count, bool isSecondary = true)
- {
- AliyunCore core = new AliyunCore(id);
- //创建弹性网卡并绑定公网IP
- var success = core.EcsAssignPrivateIpAddresses("cn-beijing", ecsid, count, !isSecondary);
- return new APIResult(new { success });
- }
- [HttpGet]
- public async Task<ActionResult> QueryAccountBalance()
- {
- AliyunPlus plus = new AliyunPlus("LTAI5tQbkTjtULQcrWGaw2VJ", "UIkkolVVEddooOKOIUByCqymZkK6ZA");
- var response = plus.QueryAccountBalance();
- var balance = response.Body.Data.AvailableAmount;
- return new APIResult(new { response });
- }
- //[HttpGet]
- //public async Task<ActionResult> test(string table = "tk_parse_logs_shop", int count = 100)
- //{
- // //table = "tk_parse_logs_shop";
- // //table = "tk_parse_logs_live";
- // //table = "tk_parse_logs_video";
- // string filter = "success=0 AND message='放弃转链' AND reason='非标准链接'";
- // for (int i = 0; i < 100; i++)
- // {
- // try
- // {
- // var list = new DBContext.Table(table)
- // .Where(filter, new { })
- // .Limit(count).Select<TkDataDTO>();
- // if (!list.Any())
- // {
- // return new APIResult(new
- // {
- // success = false,
- // message = "所有任务完成"
- // });
- // }
- // foreach (var item in list)
- // {
- // TkPoolDTO? account = TkPoolCore.GetOne(TkPoolCore.TkAction.parse);
- // if (account == null)
- // {
- // return new APIResult(new
- // {
- // success = false,
- // message = "没有工作账号"
- // });
- // }
- // var result = item.Convert2Json().Convert2Object<TkDataDTO>();
- // result.accountId = account.id;
- // result.accountName = account.company;
- // var alimama = new AlimamaPlus(account);
- // int timeout = alimama._config.rt_max;
- // using var cts = new CancellationTokenSource();
- // cts.CancelAfter(timeout);
- // result = await alimama.UnionParseAsync(item.content, result);
- // string reason = "非标准链接".Equals(result.reason) ? "非标准链接ok" : result.reason;
- // new DBContext.Table(table)
- // .Add("linkType", (int)result.link_type)
- // .Add("rawContent", result.rawContent)
- // .Add("success", result.success)
- // .Add("message", result.message)
- // .Add("reason", reason)
- // .Add("content", result.content)
- // .Add("itemId", result.itemId)
- // .Add("itemName", result.itemName)
- // .Add("pic", result.pic)
- // .Add("couponAmount", result.couponAmount)
- // .Add("promotionPrice", result.promotionPrice)
- // .Add("taoToken", result.taoToken)
- // .Add("shortLinkurl", result.shortLinkurl)
- // .Add("deeplink_url", result.deeplink_url)
- // .Add("num_iid", result.num_iid)
- // .Add("elapsedTime", result.elapsedTime)
- // .Add("elapsedTime2", result.elapsedTime2)
- // .Add("elapsedTime3", result.elapsedTime3)
- // .Add("subCode", result.subCode)
- // .Where("id=@id", new { item.id })
- // .Update();
- // }
- // }
- // catch
- // {
- // }
- // }
- // return new APIResult(new
- // {
- // success = true,
- // message = "ok"
- // });
- //}
- [HttpGet]
- public async Task<ActionResult> redu_douyin(string command)
- {
- ReduPlus plus = new ReduPlus("", "", "");
- var result = await plus.DouyinParse(command);
- return Content(result.Convert2Json());
- }
- [HttpGet]
- public async Task<ActionResult> redu_kuaishou(string command)
- {
- ReduPlus plus = new ReduPlus("", "", "");
- var result = await plus.KuaishouParse(command);
- return Content(result.Convert2Json());
- }
- [HttpPost]
- public async Task<ActionResult> test2([FromForm] string jsonContent, [FromForm] string testText)
- {
- // 用于存储输出结果的StringBuilder
- StringBuilder outputBuilder = new StringBuilder();
- // 解析JSON数据
- JsonDocument jsonDoc = JsonDocument.Parse(jsonContent);
- // 获取根元素
- JsonElement root = jsonDoc.RootElement;
- // 遍历规则
- foreach (JsonElement ruleSet in root.EnumerateArray())
- {
- string platformType = ruleSet.Read("platformType", string.Empty);
- string supplier = ruleSet.Read("supplier", string.Empty);
- outputBuilder.AppendLine($"平台类型: {platformType}, 供应商: {supplier}");
- JsonElement pwdRules = ruleSet.GetProperty("pwdRules");
- int idx = 0;
- foreach (JsonElement patternElement in pwdRules.EnumerateArray())
- {
- string pattern = patternElement.GetString();
- try
- {
- // 使用Regex类来编译正则表达式
- Regex compiledPattern = new Regex(pattern);
- if (compiledPattern.IsMatch(testText))
- {
- outputBuilder.AppendLine($"规则 {idx + 1}: 匹配\t{pattern}");
- }
- else
- {
- outputBuilder.AppendLine($"规则 {idx + 1}: 不匹配");
- }
- }
- catch (Exception e)
- {
- outputBuilder.AppendLine($"规则 {idx + 1}: 正则表达式错误 - {e.Message}\t{pattern}");
- }
- idx++;
- }
- }
- outputBuilder.AppendLine("测试完成");
- return Content(outputBuilder.ToString());
- }
- [HttpGet]
- public async Task<ActionResult> ip(string ip)
- {
- string result = IP2RegionPlus.Search(ip);
- return new APIResult(new
- {
- success = true,
- message = result
- });
- }
- [HttpPost]
- public async Task<ActionResult> testreg([FromBody] JsonElement form)
- {
- var content = form.Read("s", string.Empty);
- string shortLinkurl = AlimamaPlus.GetTaobaoLink(content);
- bool is_tao_token = AlimamaPlus.MatchRegexes(content, []);
- bool is_other = AlimamaPlus.MatchOtherInfo(content, []);
- return new APIResult(new
- {
- shortLinkurl,
- is_tao_token,
- is_other
- });
- }
- //[HttpGet]
- //public async Task<ActionResult> comparison_tk([FromQuery] int count = 100)
- //{
- // string cacheKey = ":lock_key:comparison_tk_logs";
- // int last_id = RedisHelper.Get<int>(cacheKey);
- // string filter = "id>@last_id";
- // var result = new DBContext.Table("comparison_tk_logs")
- // .Where(filter, new { last_id })
- // .Page(count, 1)
- // .Order("ID")
- // .PageList<TkDataDTO>(false);
- // var ip = "127.0.0.1";
- // var oaid = "test-comparison_tk_logs";
- // foreach (var item in result.List)
- // {
- // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
- // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
- // }
- // if (result.Count < count)
- // {
- // cacheKey = ":lock_key:start_comparison_tk";
- // RedisHelper.Set(cacheKey, 1, 600);
- // }
- // return new APIResult(new
- // {
- // success = true,
- // message = "ok"
- // });
- //}
- //[HttpGet]
- //public async Task<ActionResult> comparison_tk_raw([FromQuery] int count = 100)
- //{
- // string cacheKey = ":lock_key:comparison_tk_logs_raw";
- // int last_id = RedisHelper.Get<int>(cacheKey);
- // string filter = "id>@last_id";
- // var result = new DBContext.Table("comparison_tk_logs")
- // .Where(filter, new { last_id })
- // .Page(count, 1)
- // .Order("ID")
- // .PageList<TkDataDTO>(false);
- // var ip = "127.0.0.1";
- // var oaid = "test-comparison_tk_logs_raw";
- // foreach (var item in result.List)
- // {
- // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
- // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
- // }
- // if (result.Count < count)
- // {
- // cacheKey = ":lock_key:start_comparison_tk";
- // RedisHelper.Set(cacheKey, 1, 600);
- // }
- // return new APIResult(new
- // {
- // success = true,
- // message = "ok"
- // });
- //}
- [HttpGet]
- public async Task<ActionResult> testTask()
- {
- string url = "https://www.taobao.com";
- TkDataDTO result = new TkDataDTO();
- result.message = "init";
- result.success = true;
- int timeout = 1000;
- using var cts = new CancellationTokenSource();
- cts.CancelAfter(timeout);
- var requestTask = Task.Run(() => AlimamaPlus.testTaskAsync(result, cts.Token), cts.Token);
- var delayTask = Task.Delay(timeout, cts.Token);
- var completedTask = await Task.WhenAny(requestTask, delayTask);
- if (completedTask == requestTask)
- {
- result = await requestTask;
- }
- else
- {
- cts.Cancel();
- result.success = false;
- result.message = "放弃转链";
- result.reason = "请求超时";
- result.itemName = "点击打开淘宝APP";
- }
- return new APIResult(new
- {
- success = true,
- message = "ok",
- result,
- });
- }
- [HttpGet]
- public async Task<ActionResult> testThread()
- {
- // 查看默认的最小和最大线程数
- ThreadPool.GetMinThreads(out int defaultMinWorker, out int defaultMinIOC);
- ThreadPool.GetMaxThreads(out int defaultMaxWorker, out int defaultMaxIOC);
- string tmp = $"Default Min worker threads: {defaultMinWorker}, Min I/O completion threads: {defaultMinIOC}";
- string tmp2 = $"Default Max worker threads: {defaultMaxWorker}, Max I/O completion threads: {defaultMaxIOC}";
- // 获取当前线程池中可用的工作线程数和 I/O 完成端口线程数
- ThreadPool.GetAvailableThreads(out int availableWorkerThreads, out int availableIOCompletionThreads);
- string tmp3 = $"Current available worker threads: {availableWorkerThreads}, available I/O completion threads: {availableIOCompletionThreads}";
- return new APIResult(new
- {
- tmp,
- tmp2,
- tmp3,
- });
- }
- [HttpPost]
- public async Task<ActionResult> test1([FromBody] JsonElement form)
- {
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【淘宝联盟:test】cookie 掉线",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- return new APIResult(new
- {
- success = true,
- message = "ok"
- });
- }
- [HttpGet]
- public async Task<ActionResult> RepairTkDailyAccountStats(
- DateTime startDate = default,
- DateTime endDate = default,
- string accountIds = "129,140",
- string extraNames = "搜同款_楚颜_128众杰科技,搜同款_广哲2",
- bool dryRun = false,
- bool repairDailyLogs = true,
- bool repairRedis = true,
- bool repairRedisNameKeys = true,
- int commandTimeoutSeconds = 600)
- {
- if (startDate == default) startDate = new DateTime(2026, 6, 16);
- if (endDate == default) endDate = DateTime.Now.Date;
- commandTimeoutSeconds = Math.Clamp(commandTimeoutSeconds, 30, 3600);
- startDate = startDate.Date;
- endDate = endDate.Date;
- if (endDate < startDate)
- {
- return new APIResult(new { success = false, message = "endDate 不能早于 startDate" });
- }
- var ids = (accountIds ?? string.Empty)
- .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
- .Select(item => int.TryParse(item, out int id) ? id : 0)
- .Where(id => id > 0)
- .Distinct()
- .ToArray();
- if (ids.Length == 0)
- {
- return new APIResult(new { success = false, message = "accountIds 不能为空" });
- }
- using var conn = CenterHub.GetOpenConnection();
- if (conn.State != ConnectionState.Open) conn.Open();
- var errors = new List<object>();
- var affectedNames = new HashSet<string>(StringComparer.Ordinal);
- var accounts = SqlMapper.Query<TkDailyRepairCountRow>(
- conn,
- "SELECT id accountId, company accountName FROM tk_pool WHERE id IN @ids",
- new { ids },
- commandTimeout: commandTimeoutSeconds).ToList();
- foreach (string name in accounts.Select(item => item.accountName).Where(item => !string.IsNullOrWhiteSpace(item)))
- {
- affectedNames.Add(name);
- }
- extraNames ??= string.Empty;
- foreach (string name in extraNames
- .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
- .Where(item => !string.IsNullOrWhiteSpace(item)))
- {
- affectedNames.Add(name);
- }
- foreach (string name in SqlMapper.Query<string>(
- conn,
- @"
- SELECT DISTINCT accountName
- FROM center_daily_logs
- WHERE channel = 0
- AND accountId IN @ids
- AND log_date BETWEEN @startDate AND @endDate
- AND COALESCE(accountName,'')<>''",
- new { ids, startDate, endDate },
- commandTimeout: commandTimeoutSeconds))
- {
- affectedNames.Add(name);
- }
- var days = EachDay(startDate, endDate).ToList();
- var dailyReports = new List<object>();
- var endpointCountsByDate = new Dictionary<string, List<TkDailyRepairEndpointCountRow>>();
- foreach (var date in days)
- {
- try
- {
- string tableName = $"tk_parse_logs_{date:yyyyMMdd}";
- if (!TableExists(conn, tableName, commandTimeoutSeconds))
- {
- dailyReports.Add(new { date = date.ToString("yyyy-MM-dd"), tableName, skipped = true, reason = "table not exists" });
- continue;
- }
- var endpointCounts = SqlMapper.Query<TkDailyRepairEndpointCountRow>(
- conn,
- $@"
- SELECT
- l.accountId,
- COALESCE(p.company, MAX(l.accountName), '') accountName,
- COALESCE(l.end_point, '') end_point,
- COUNT(*) total_count,
- CAST(COALESCE(SUM(l.success = 1), 0) AS SIGNED) success_count,
- CAST(COALESCE(SUM(l.success = 0), 0) AS SIGNED) fail_count,
- CAST(COALESCE(SUM(l.message = '放弃转链' OR l.reason = '放弃转链'), 0) AS SIGNED) abandon_count
- FROM {tableName} l
- LEFT JOIN tk_pool p ON p.id = l.accountId
- WHERE l.accountId IN @ids
- GROUP BY l.accountId, p.company, l.end_point
- ORDER BY l.accountId, l.end_point",
- new { ids },
- commandTimeout: commandTimeoutSeconds).ToList();
- endpointCountsByDate[date.ToString("yyyyMMdd")] = endpointCounts;
- var accountCounts = endpointCounts
- .GroupBy(item => item.accountId)
- .Select(group => new TkDailyRepairCountRow
- {
- accountId = group.Key,
- accountName = accounts.FirstOrDefault(item => item.accountId == group.Key)?.accountName
- ?? group.FirstOrDefault()?.accountName
- ?? string.Empty,
- total_count = group.Sum(item => item.total_count),
- success_count = group.Sum(item => item.success_count),
- fail_count = group.Sum(item => item.fail_count),
- abandon_count = group.Sum(item => item.abandon_count)
- })
- .ToList();
- int dailyLogRows = 0;
- var allAccountCounts = ids
- .Select(accountId => accountCounts.FirstOrDefault(item => item.accountId == accountId)
- ?? new TkDailyRepairCountRow
- {
- accountId = accountId,
- accountName = accounts.FirstOrDefault(item => item.accountId == accountId)?.accountName ?? string.Empty
- })
- .ToList();
- foreach (var counts in allAccountCounts)
- {
- if (!dryRun && repairDailyLogs)
- {
- dailyLogRows += UpsertCenterDailyLog(conn, date, counts, commandTimeoutSeconds);
- }
- }
- dailyReports.Add(new
- {
- date = date.ToString("yyyy-MM-dd"),
- tableName,
- skipped = false,
- dailyLogRows,
- source = "mysql:tk_parse_logs_yyyyMMdd",
- counts = allAccountCounts,
- endpointCounts
- });
- }
- catch (Exception ex)
- {
- var error = new { scope = "daily_log", date = date.ToString("yyyy-MM-dd"), error = FormatRepairError(ex) };
- errors.Add(error);
- dailyReports.Add(new { date = date.ToString("yyyy-MM-dd"), skipped = true, reason = "error", error });
- }
- }
- var endpointReports = new List<object>();
- if (repairRedis || repairRedisNameKeys)
- {
- var endpoints = EndPointCore.List(true)
- .Where(node => node.status && node.is_public_api && !string.IsNullOrEmpty(EndPointCore.GetRedisServer(node)))
- .ToList();
- foreach (var endpoint in endpoints)
- {
- var redisServer = EndPointCore.GetRedisServer(endpoint);
- var endpointReport = new List<object>();
- try
- {
- await using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
- var redis = scope.Client;
- foreach (var date in days)
- {
- string dateKey = date.ToString("yyyyMMdd");
- if (!endpointCountsByDate.TryGetValue(dateKey, out var endpointCounts))
- {
- continue;
- }
- foreach (int accountId in ids)
- {
- var counts = endpointCounts.FirstOrDefault(item =>
- item.accountId == accountId &&
- string.Equals(item.end_point, endpoint.name, StringComparison.Ordinal))
- ?? new TkDailyRepairEndpointCountRow
- {
- accountId = accountId,
- accountName = accounts.FirstOrDefault(item => item.accountId == accountId)?.accountName ?? string.Empty,
- end_point = endpoint.name
- };
- if (!dryRun && repairRedis)
- {
- await WriteParseCountKeysAsync(redis, $"tb_{accountId}", dateKey, counts);
- }
- endpointReport.Add(new
- {
- date = date.ToString("yyyy-MM-dd"),
- bucket = $"tb_{accountId}",
- counts
- });
- }
- if (repairRedisNameKeys)
- {
- foreach (string name in affectedNames)
- {
- var before = await ReadParseCountKeysAsync(redis, name, dateKey);
- if (!dryRun)
- {
- await DeleteParseBucketKeysAsync(redis, name, dateKey);
- }
- endpointReport.Add(new
- {
- date = date.ToString("yyyy-MM-dd"),
- deletedBucket = name,
- before
- });
- }
- }
- }
- }
- catch (Exception ex)
- {
- errors.Add(new { scope = "redis_endpoint", endpoint = endpoint.name, error = FormatRepairError(ex) });
- }
- endpointReports.Add(new
- {
- endpoint = endpoint.name,
- endpoint.description,
- dryRun,
- deletedBuckets = endpointReport
- });
- }
- }
- return new APIResult(new
- {
- success = errors.Count == 0,
- dryRun,
- accountIds = ids,
- extraNames,
- repairDailyLogs,
- repairRedis,
- repairRedisNameKeys,
- commandTimeoutSeconds,
- startDate = startDate.ToString("yyyy-MM-dd"),
- endDate = endDate.ToString("yyyy-MM-dd"),
- affectedNames = affectedNames.OrderBy(item => item).ToList(),
- mysql = dailyReports,
- redis = endpointReports,
- errors
- });
- }
- private static IEnumerable<DateTime> EachDay(DateTime startDate, DateTime endDate)
- {
- for (var date = startDate.Date; date <= endDate.Date; date = date.AddDays(1))
- {
- yield return date;
- }
- }
- private static bool TableExists(IDbConnection conn, string tableName, int commandTimeoutSeconds)
- {
- const string sql = @"
- SELECT COUNT(*)
- FROM information_schema.tables
- WHERE table_schema = DATABASE()
- AND table_name = @tableName";
- return SqlMapper.ExecuteScalar<int>(
- conn,
- sql,
- new { tableName },
- commandTimeout: commandTimeoutSeconds) > 0;
- }
- private static int UpsertCenterDailyLog(IDbConnection conn, DateTime date, TkDailyRepairCountRow counts, int commandTimeoutSeconds)
- {
- if (string.IsNullOrWhiteSpace(counts.accountName)) return 0;
- string successPercentage = counts.total_count > 0 ? $"{counts.success_count / (double)counts.total_count * 100:f2}%" : string.Empty;
- string abandonPercentage = counts.total_count > 0 ? $"{counts.abandon_count / (double)counts.total_count * 100:f2}%" : string.Empty;
- var existingIds = SqlMapper.Query<int>(
- conn,
- @"
- SELECT id
- FROM center_daily_logs
- WHERE channel = 0
- AND log_date = @date
- AND accountId = @accountId
- ORDER BY id",
- new { date, counts.accountId },
- commandTimeout: commandTimeoutSeconds).ToList();
- if (existingIds.Count > 0)
- {
- int affectedRows = SqlMapper.Execute(
- conn,
- @"
- UPDATE center_daily_logs
- SET accountName = @accountName,
- parse_total_count = @totalCount,
- parse_success_count = @successCount,
- parse_abandon_count = @abandonCount,
- parse_success_percentage = @successPercentage,
- parse_abandon_percentage = @abandonPercentage,
- last_time = NOW()
- WHERE id = @id",
- new
- {
- id = existingIds[0],
- counts.accountName,
- totalCount = counts.total_count,
- successCount = counts.success_count,
- abandonCount = counts.abandon_count,
- successPercentage,
- abandonPercentage
- },
- commandTimeout: commandTimeoutSeconds);
- if (existingIds.Count > 1)
- {
- affectedRows += SqlMapper.Execute(
- conn,
- "DELETE FROM center_daily_logs WHERE id IN @ids",
- new { ids = existingIds.Skip(1).ToArray() },
- commandTimeout: commandTimeoutSeconds);
- }
- return affectedRows;
- }
- if (counts.total_count <= 0) return 0;
- return SqlMapper.Execute(
- conn,
- @"
- INSERT INTO center_daily_logs
- (channel, accountId, accountName, log_date, create_time, last_time,
- parse_total_count, parse_success_count, parse_abandon_count,
- parse_success_percentage, parse_abandon_percentage)
- VALUES
- (0, @accountId, @accountName, @date, NOW(), NOW(),
- @totalCount, @successCount, @abandonCount,
- @successPercentage, @abandonPercentage)",
- new
- {
- date,
- counts.accountId,
- counts.accountName,
- totalCount = counts.total_count,
- successCount = counts.success_count,
- abandonCount = counts.abandon_count,
- successPercentage,
- abandonPercentage
- },
- commandTimeout: commandTimeoutSeconds);
- }
- private static async Task<TkDailyRepairCountRow> ReadParseCountKeysAsync(YunhuiKit.RedisClient redis, string bucket, string dateKey)
- {
- return new TkDailyRepairCountRow
- {
- accountName = bucket,
- total_count = await redis.GetAsync<int>($":parse_total:{bucket}:{dateKey}"),
- success_count = await redis.GetAsync<int>($":parse_total:{bucket}:success:{dateKey}"),
- fail_count = await redis.GetAsync<int>($":parse_total:{bucket}:fail:{dateKey}"),
- abandon_count = await redis.GetAsync<int>($":parse_total:{bucket}:放弃转链:{dateKey}")
- };
- }
- private static async Task DeleteParseBucketKeysAsync(YunhuiKit.RedisClient redis, string bucket, string dateKey)
- {
- var keys = new List<string>
- {
- $":parse_total:{bucket}:{dateKey}",
- $":parse_total:{bucket}:success:{dateKey}",
- $":parse_total:{bucket}:fail:{dateKey}",
- $":parse_total:{bucket}:放弃转链:{dateKey}",
- $":parse_total:{bucket}:message:{dateKey}",
- $":parse_total:{bucket}:reason:{dateKey}"
- };
- foreach (string dpBucket in new[] { "dp_none", "dp_home", "dp_success", "dp_fail" })
- {
- keys.Add($":parse_total:{dpBucket}:{bucket}:{dateKey}");
- keys.Add($":parse_total:{dpBucket}:{bucket}:success:{dateKey}");
- keys.Add($":parse_total:{dpBucket}:{bucket}:fail:{dateKey}");
- keys.Add($":parse_total:{dpBucket}:{bucket}:放弃转链:{dateKey}");
- keys.Add($":parse_total:{dpBucket}:{bucket}:message:{dateKey}");
- keys.Add($":parse_total:{dpBucket}:{bucket}:reason:{dateKey}");
- }
- await redis.DelAsync(keys.ToArray());
- }
- private static string FormatRepairError(Exception ex)
- {
- return ex.InnerException == null ? ex.Message : $"{ex.Message} | {ex.InnerException.Message}";
- }
- private static async Task WriteParseCountKeysAsync(YunhuiKit.RedisClient redis, string bucket, string dateKey, TkDailyRepairCountRow counts)
- {
- var keys = new[]
- {
- $":parse_total:{bucket}:{dateKey}",
- $":parse_total:{bucket}:success:{dateKey}",
- $":parse_total:{bucket}:fail:{dateKey}",
- $":parse_total:{bucket}:放弃转链:{dateKey}"
- };
- if (counts.total_count <= 0)
- {
- await redis.DelAsync(keys);
- return;
- }
- await redis.SetAsync(keys[0], counts.total_count, 90 * 86400);
- await redis.SetAsync(keys[1], counts.success_count, 90 * 86400);
- await redis.SetAsync(keys[2], counts.fail_count, 90 * 86400);
- await redis.SetAsync(keys[3], counts.abandon_count, 90 * 86400);
- }
- private class TkDailyRepairCountRow
- {
- public int accountId { get; set; }
- public string accountName { get; set; } = string.Empty;
- public long total_count { get; set; }
- public long success_count { get; set; }
- public long fail_count { get; set; }
- public long abandon_count { get; set; }
- }
- private sealed class TkDailyRepairEndpointCountRow : TkDailyRepairCountRow
- {
- public string end_point { get; set; } = string.Empty;
- }
- [HttpGet]
- public async Task<ActionResult> xxx()
- {
- var list = await TkPoolCore.ListAsync();
- if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
- string message = string.Empty;
- foreach (var account in list)
- {
- try
- {
- var alimama = new AlimamaPlus(account);
- alimama.RenewCookie();
- }
- catch (Exception ex)
- {
- message = $"【cookie续期】xxxx\n{ex.Message}\n{ex.StackTrace}";
- NotifyCore.Notify(new NifyMessage
- {
- message = message,
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- continue;
- }
- }
- return new APIResult(new { success = true, message = "ok" });
- }
- }
- }
|