using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace FastGithub.Scanner.ScanMiddlewares
{
    /// 
    /// 扫描统计中间件
    /// 
    [Service(ServiceLifetime.Singleton)]
    sealed class StatisticsMiddleware : IMiddleware
    {
        /// 
        /// 记录扫描结果
        /// 
        /// 
        /// 
        /// 
        public async Task InvokeAsync(GithubContext context, Func next)
        {
            var stopwatch = new Stopwatch();
            try
            {
                stopwatch.Start();
                await next();
            }
            finally
            {
                stopwatch.Stop();
            }
            context.History.Add(context.Available, stopwatch.Elapsed);
        }
    }
}