From b1a4a79d64585a43199ea54baaac6576583dc473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 17 Jun 2021 09:36:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AA=E7=BB=9F=E8=AE=A1=E8=BF=912=E5=B0=8F?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E6=89=AB=E6=8F=8F=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Scanner/GithubContext.cs | 22 +++++- FastGithub.Scanner/GithubContextCollection.cs | 4 +- FastGithub.Scanner/GithubContextHistory.cs | 74 +++++++++++++++++++ FastGithub.Scanner/GithubContextStatistics.cs | 58 --------------- FastGithub.Scanner/GithubScanService.cs | 2 - .../Middlewares/ScanOkLogMiddleware.cs | 39 ---------- .../Middlewares/StatisticsMiddleware.cs | 17 ++++- 7 files changed, 109 insertions(+), 107 deletions(-) create mode 100644 FastGithub.Scanner/GithubContextHistory.cs delete mode 100644 FastGithub.Scanner/GithubContextStatistics.cs delete mode 100644 FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs diff --git a/FastGithub.Scanner/GithubContext.cs b/FastGithub.Scanner/GithubContext.cs index 4273e49..e70356e 100644 --- a/FastGithub.Scanner/GithubContext.cs +++ b/FastGithub.Scanner/GithubContext.cs @@ -5,6 +5,12 @@ namespace FastGithub.Scanner { sealed class GithubContext : IEquatable { + private record Github( + string Domain, + IPAddress Address, + double SuccessRate, + TimeSpan AvgElapsed); + /// /// 获取域名 /// @@ -21,16 +27,16 @@ namespace FastGithub.Scanner public bool Available { get; set; } /// - /// 获取统计信息 + /// 获取扫描历史信息 /// - public GithubContextStatistics Statistics { get; } = new(); + public GithubContextHistory History { get; } = new(); public GithubContext(string domain, IPAddress address) { this.Domain = domain; this.Address = address; - } + } public override bool Equals(object? obj) { @@ -46,5 +52,15 @@ namespace FastGithub.Scanner { return HashCode.Combine(this.Domain, this.Address); } + + public override string ToString() + { + return new Github( + this.Domain, + this.Address, + this.History.GetSuccessRate(), + this.History.GetAvgElapsed() + ).ToString(); + } } } diff --git a/FastGithub.Scanner/GithubContextCollection.cs b/FastGithub.Scanner/GithubContextCollection.cs index 11206c7..a968643 100644 --- a/FastGithub.Scanner/GithubContextCollection.cs +++ b/FastGithub.Scanner/GithubContextCollection.cs @@ -55,8 +55,8 @@ namespace FastGithub.Scanner { return this.contextList .Where(item => item.Available && item.Domain == domain) - .OrderByDescending(item => item.Statistics.GetSuccessRate()) - .ThenBy(item => item.Statistics.GetAvgElapsed()) + .OrderByDescending(item => item.History.GetSuccessRate()) + .ThenBy(item => item.History.GetAvgElapsed()) .Select(item => item.Address) .FirstOrDefault(); } diff --git a/FastGithub.Scanner/GithubContextHistory.cs b/FastGithub.Scanner/GithubContextHistory.cs new file mode 100644 index 0000000..228be76 --- /dev/null +++ b/FastGithub.Scanner/GithubContextHistory.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; + +namespace FastGithub.Scanner +{ + sealed class GithubContextHistory + { + private record ScanLog(DateTime ScanTime, TimeSpan Elapsed); + + private readonly Queue successLogs = new(); + + private readonly Queue failureLogs = new(); + + private static readonly TimeSpan keepLogsTimeSpan = TimeSpan.FromHours(2d); + + + public void AddSuccess(TimeSpan elapsed) + { + ClearStaleData(this.successLogs, keepLogsTimeSpan); + this.successLogs.Enqueue(new ScanLog(DateTime.Now, elapsed)); + } + + public void AddFailure() + { + ClearStaleData(this.failureLogs, keepLogsTimeSpan); + this.failureLogs.Enqueue(new ScanLog(DateTime.Now, TimeSpan.Zero)); + } + + static void ClearStaleData(Queue logs, TimeSpan timeSpan) + { + var time = DateTime.Now.Subtract(timeSpan); + while (logs.TryPeek(out var log)) + { + if (log.ScanTime < time) + { + logs.TryDequeue(out _); + } + break; + } + } + + /// + /// 获取成功率 + /// + /// + public double GetSuccessRate() + { + var successCount = this.successLogs.Count; + var totalScanCount = successCount + this.failureLogs.Count; + return totalScanCount == 0 ? 0d : (double)successCount / totalScanCount; + } + + /// + /// 获取平均耗时 + /// + /// + public TimeSpan GetAvgElapsed() + { + var totalScanCount = this.successLogs.Count + this.failureLogs.Count; + if (totalScanCount == 0) + { + return TimeSpan.MaxValue; + } + + var totalSuccessElapsed = TimeSpan.Zero; + foreach (var item in this.successLogs) + { + totalSuccessElapsed = totalSuccessElapsed.Add(item.Elapsed); + } + + return totalSuccessElapsed / totalScanCount; + } + } +} diff --git a/FastGithub.Scanner/GithubContextStatistics.cs b/FastGithub.Scanner/GithubContextStatistics.cs deleted file mode 100644 index 9df0788..0000000 --- a/FastGithub.Scanner/GithubContextStatistics.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; - -namespace FastGithub.Scanner -{ - sealed class GithubContextStatistics - { - /// - /// 扫描总次数 - /// - public int TotalScanCount { get; private set; } - - - /// - /// 扫描总成功次数 - /// - public int TotalSuccessCount { get; private set; } - - /// - /// 扫描总耗时 - /// - public TimeSpan TotalSuccessElapsed { get; private set; } - - - public void SetScanFailure() - { - this.TotalScanCount += 1; - } - - public void SetScanSuccess(TimeSpan elapsed) - { - this.TotalScanCount += 1; - this.TotalSuccessCount += 1; - this.TotalSuccessElapsed = this.TotalSuccessElapsed.Add(elapsed); - } - - /// - /// 获取成功率 - /// - /// - public double GetSuccessRate() - { - return this.TotalScanCount > 0 ? - (double)this.TotalSuccessCount / this.TotalScanCount - : 0d; - } - - /// - /// 获取平均耗时 - /// - /// - public TimeSpan GetAvgElapsed() - { - return this.TotalScanCount > 0 - ? this.TotalSuccessElapsed / this.TotalScanCount - : TimeSpan.MaxValue; - } - } -} diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs index 437c397..3488df1 100644 --- a/FastGithub.Scanner/GithubScanService.cs +++ b/FastGithub.Scanner/GithubScanService.cs @@ -30,14 +30,12 @@ namespace FastGithub.Scanner ; this.fullScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) .Use() - .Use() .Use() .Use() .Use() .Build(); this.resultScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) - .Use() .Use() .Use() .Use() diff --git a/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs b/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs deleted file mode 100644 index 62c68a0..0000000 --- a/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using System; -using System.Net; -using System.Threading.Tasks; - -namespace FastGithub.Scanner.Middlewares -{ - [Service(ServiceLifetime.Singleton)] - sealed class ScanOkLogMiddleware : IMiddleware - { - private readonly ILogger logger; - - private record ScanOk(string Domain, IPAddress Address, int TotalScanCount, double SuccessRate, TimeSpan AvgElapsed); - - public ScanOkLogMiddleware(ILogger logger) - { - this.logger = logger; - } - - public async Task InvokeAsync(GithubContext context, Func next) - { - await next(); - - if (context.Available) - { - var mesage = new ScanOk( - context.Domain, - context.Address, - context.Statistics.TotalScanCount, - context.Statistics.GetSuccessRate(), - context.Statistics.GetAvgElapsed() - ); - - this.logger.LogInformation(mesage.ToString()); - } - } - } -} diff --git a/FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs b/FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs index 2aecd10..b70db7f 100644 --- a/FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs +++ b/FastGithub.Scanner/Middlewares/StatisticsMiddleware.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using System; using System.Diagnostics; using System.Threading.Tasks; @@ -8,24 +9,34 @@ namespace FastGithub.Scanner.Middlewares [Service(ServiceLifetime.Singleton)] sealed class StatisticsMiddleware : IMiddleware { + private readonly ILogger logger; + + public StatisticsMiddleware(ILogger logger) + { + this.logger = logger; + } + public async Task InvokeAsync(GithubContext context, Func next) { var stopwatch = new Stopwatch(); + stopwatch.Start(); + try { - stopwatch.Start(); await next(); } finally { stopwatch.Stop(); + if (context.Available) { - context.Statistics.SetScanSuccess(stopwatch.Elapsed); + context.History.AddSuccess(stopwatch.Elapsed); + this.logger.LogInformation(context.ToString()); } else { - context.Statistics.SetScanFailure(); + context.History.AddFailure(); } } }