// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var config = new MongoDBConfig(); Configuration.Bind(config); #region Statistics var statisticsContext = new StatisticsContext(config); var statisticsRepository = new StatisticsRepository(statisticsContext); services.AddSingleton <StatisticsRepository>(statisticsRepository); #endregion var statistics = statisticsRepository.GetAll().GetAwaiter().GetResult(); if (statistics.Count() == 0) { this.seedDefaultStatisticsObject(statisticsRepository); } services.AddControllers(); services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod(); }); }); }
public TransactionalContext(AuthenticationDetails auth, ICreateSendOptions options) { classicEmail = new ClassicEmailContext(auth, options); smartEmail = new SmartEmailContext(auth, options); messages = new MessagesContext(auth, options); statistics = new StatisticsContext(auth, options); }
private void saveBuffer() // 保存缓存。 懒的进行集合运算了, 所以直接清空表 // 清空表 { Logger.Debug("开始保存缓存。"); var context = new StatisticsContext(); var buffer = context.Buffer.ToList(); Logger.Debug("保存缓存 -> 删除所有记录"); context.Buffer.RemoveRange(buffer); if (downServerIds.ToArray().Length > 0) { // 有数据可以缓存 Logger.Debug("保存缓存 -> 存在数据可以缓存"); foreach (var data in downServerList.ToArray()) { Logger.Debug("保存缓存 -> 缓存 id: " + data.id + ", 开始时间戳: " + data.startTS); var child = new buffer() { identification = data.id, startTS = data.startTS.ToString() }; context.Buffer.Add(child); } Logger.Debug("保存缓存 -> 保存变更"); context.SaveChanges(); Logger.Debug("保存缓存 -> 完成"); } }
public CountEmployeeWaitTimeAtKitchen(GameContext game, StatisticsContext statistics) { employeesAtKitchen = game.GetGroup(GameMatcher .AllOf(GameMatcher.Employee, GameMatcher.AtKitchen)); this.statistics = statistics; }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { using (var db = new StatisticsContext()) db.Database.EnsureCreated(); services.AddOptions <Settings>() .Bind(Configuration); services.AddHostedService <StatisticsCollector>(); }
private void addLog(string id, string type, long ts) // 添加事件, 记录故障与恢复; { var context = new StatisticsContext(); var child = new log() { identification = id, type = type, ts = ts }; context.Log.Add(child); context.SaveChanges(); }
static public string gernerateDataStatistics() // 读取数据库, 分析监控数据 { Logger.Debug("开始生成数据统计。"); var context = new StatisticsContext(); var result = ""; DateTime now = DateTime.Now; // 首先, 检测是否当前有服务器宕机 Logger.Debug("生成数据统计 -> 检测是否服务器存在故障。"); if (downServerIds.ToArray().Length > 0) { result += "当前有节点故障, 故障列表如下:\r\n"; Logger.Debug("生成数据统计 -> 有服务器存在故障。"); foreach (var downServer in downServerList.ToArray()) { // 计算持续时间 DateTime startTime = ConvertTimeStampToDateTime(downServer.startTS); TimeSpan distance = now - startTime; string last = distance.Days + " 天 " + distance.Hours + " 时 " + distance.Minutes + " 分 " + distance.Seconds + " 秒 "; string part = "标识: " + downServer.id + " -> 触发时间: " + startTime.ToString("s") + ", 已持续: " + last; result += part + "\r\n"; Logger.Debug("生成数据统计 -> " + part); } result += "\r\n"; // 换行 } // 生成统计报告 Logger.Debug("生成数据统计 -> 生成统计报告"); result += "统计报告: \r\n"; var status = context.Status.ToList(); foreach (var child in status) { var identification = child.identification; var total = child.totol; var up = child.up; var down = child.down; var onlineRate = Math.Round((double)(up / total) * 100, 2); var part = "标识: " + identification + " -> 总计:" + total + " 次, 正常: " + up + " 次, 故障: " + down + " 次; 在线率: " + onlineRate + "%"; result += part + "\r\n"; Logger.Debug("生成数据统计 -> " + part); } result += "\r\n"; // 换行 result += fetchHitokotoText(); Logger.Debug("生成数据统计 -> 完成."); // 返回结果 return(result); }
public Contexts() { game = new GameContext(); settings = new SettingsContext(); statistics = new StatisticsContext(); var postConstructors = System.Linq.Enumerable.Where( GetType().GetMethods(), method => System.Attribute.IsDefined(method, typeof(Entitas.CodeGeneration.Attributes.PostConstructorAttribute)) ); foreach (var postConstructor in postConstructors) { postConstructor.Invoke(this, null); } }
public async Task <IActionResult> Post(RenderingParameters parameters, bool renderCompleteHtmlPage = false) { // If we can record statistics if (this.context != null) { try { StatisticsContext context = this.context !; Statistics statistics = new Statistics { AccessedAt = DateTime.UtcNow, ForwardedFor = this.Request.Headers["HTTP_X_FORWARDED_FOR"].FirstOrDefault(), IpAddress = this.Request.HttpContext.Connection.RemoteIpAddress?.ToString(), Passage = parameters.PassageReference.ChapterReference.ToString(), PrimaryProvider = parameters.PrimaryProvider, PrimaryTranslation = parameters.PrimaryTranslation, SecondaryProvider = parameters.SecondaryProvider, SecondaryTranslation = parameters.SecondaryTranslation, }; await context.Statistics.AddAsync(statistics); await context.SaveChangesAsync(); } catch (Exception ex) { if (ex is not DbException) { throw; } } } try { return(this.Ok(await this.renderer.RenderAsync(parameters, renderCompleteHtmlPage))); } catch (Exception ex) { // Log the URL, with details to help us debug RequestHeaders header = this.Request.GetTypedHeaders(); string renderingParameters = JsonSerializer.Serialize(parameters); this.logger.LogError(ex, $"URL: {this.Request.GetDisplayUrl()}{Environment.NewLine}Referer: {header.Referer}{Environment.NewLine}RenderingParameters: {renderingParameters}"); return(this.Problem()); } }
static void restoreData() // 从 SQLite 数据库中恢复数据 // 从缓存库中获取数据 { var context = new StatisticsContext(); var buffer = context.Buffer.ToList().ToArray(); if (buffer.Length > 0) { // 既然有数据, 那就有缓存。 执行操作恢复缓存 foreach (var data in buffer) { downServerIds.Add(data.identification); // 将标识添加到列表 var child = new down() { id = data.identification, startTS = long.Parse(data.startTS) }; downServerList.Add(child); } } }
public SimulationFeature(GameContext game, SettingsContext settings, StatisticsContext statistics) { Add(new SetInitialTimeScale(settings, timeScale: 1)); Add(new CorrectTooSmallTimeScale(settings)); Add(new ApplyTimeScale(settings)); Add(new CreateTeapots(game, count: 2)); Add(new CreateEmployees(game, count: 40)); Add(new MarkEmptyTeapot(game)); Add(new MarkFullTeapot(game)); Add(new MarkHotTeapot(game)); Add(new MarkBoilingTeapot(game)); Add(new SetRandomTimeoutToNotThirstyEmployee(game)); Add(new ReduceTimeout(game)); Add(new RemoveTimeoutIfZero(game)); Add(new MakeEmployeeThirstyIfNoTimeout(game)); Add(new GetThirstyEmployeeToKitchen(game)); Add(new DrinkHotTea(game)); Add(new GetNotThirstyEmployeeBackToWork(game)); Add(new FillTeapot(game)); Add(new HeatTeapot(game)); Add(new StartFillingEmptyTeapotIfThirstyAndNoFilledTeapots(game)); Add(new StopFillingFullTeapot(game)); Add(new StartHeatingNotHotTeapot(game)); Add(new StopHeatingBoilingTeapot(game)); Add(new CoolTeapot(game)); Add(new IncreaseWorkTime(statistics)); Add(new CountEmployeeWaitTimeAtKitchen(game, statistics)); }
public IncreaseWorkTime(StatisticsContext statistics) => this.statistics = statistics;
public StatisticsService(StatisticsContext context) { _context = context; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseDefaultFiles(); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/stats.json", async context => { using (var db = new StatisticsContext()) { var stats = await db.Statistics.Where(x => x.Date > DateTime.UtcNow.AddDays(-2)) .ToListAsync(); var window = TimeSpan.FromMinutes(5).Ticks; var data = stats .GroupBy(x => new DateTime(window * (x.Date.Ticks / window), DateTimeKind.Utc)) .Select(x => new { t = x.Key, Latancies = x.Where(x => x.Type == DataType.Latancy), DownloadSpeeds = x.Where(x => x.Type == DataType.DownloadSpeed), UploadSpeeds = x.Where(x => x.Type == DataType.UploadSpeed), }) .Select(x => new { t = x.t, Latancy = x.Latancies.Any() ? x.Latancies.Average(x => x.Value) : (double?)null, DownloadSpeed = x.DownloadSpeeds.Any() ? x.DownloadSpeeds.Average(x => x.Value) : (double?)null, UploadSpeed = x.UploadSpeeds.Any() ? x.UploadSpeeds.Average(x => x.Value) : (double?)null }); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(JsonConvert.SerializeObject(data)); } }); endpoints.MapGet("/stats.csv", async context => { var sb = new StringBuilder(); using (var db = new StatisticsContext()) { var stats = await db.Statistics.OrderByDescending(x => x.Date) .Take(10000) .ToListAsync(); sb.AppendLine("Date,Latancy(ms),Download(Mbps),Upload(Mbps)"); foreach (var g in stats.GroupBy(x => x.Date).OrderByDescending(x => x.Key)) { sb.Append(g.Key); sb.Append(','); var lD = g.Where(x => x.Type == DataType.Latancy); sb.Append(lD.Any() ? lD.Average(x => x.Value).ToString() : ""); sb.Append(','); var d = g.Where(x => x.Type == DataType.DownloadSpeed); sb.Append(d.Any() ? d.Average(x => x.Value) .FromBytesPerSecondTo(SpeedTest.Net.Enums.SpeedTestUnit.MegaBitsPerSecond) .ToString() : ""); sb.Append(','); var u = g.Where(x => x.Type == DataType.UploadSpeed); sb.Append(u.Any() ? u.Average(x => x.Value) .FromBytesPerSecondTo(SpeedTest.Net.Enums.SpeedTestUnit.MegaBitsPerSecond) .ToString() : ""); sb.AppendLine(); } } context.Response.Headers.Add("Content-Disposition", new StringValues("attachment; filename=stats.csv")); context.Response.ContentType = "text/csv"; await context.Response.WriteAsync(sb.ToString()); }); endpoints.MapGet("/stats.db", async context => { await context.Response.SendFileAsync(StatisticsContext.Path); }); }); }
public SatisticsRepository(StatisticsContext _context) { Context = _context; }
public FeedbackController(StatisticsContext db) { _db = db; }
public StatisticController(StatisticsContext db) { _db = db; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { SpeedTest.Net.Models.Server?server = null; while (!stoppingToken.IsCancellationRequested) { try { server ??= await SpeedTest.Net.SpeedTestClient.GetServer(); using (var db = new StatisticsContext()) { var dbServer = await db.FindAsync <Server>(server.Id); var now = DateTime.UtcNow; if (dbServer == null) { dbServer = new Server { Id = server.Id, Host = server.Host, Latitude = server.Latitude, Longitude = server.Longitude }; db.Add(dbServer); } if (!stoppingToken.IsCancellationRequested) { var speed = await SpeedTest.Net.SpeedTestClient.GetDownloadSpeed(server); db.Add(new Statistic { Date = now, Type = DataType.DownloadSpeed, Value = speed.Speed }); await db.SaveChangesAsync(); } if (!stoppingToken.IsCancellationRequested) { var speed = await SpeedTest.Net.SpeedTestClient.GetUploadSpeed(server); db.Add(new Statistic { Date = now, Type = DataType.UploadSpeed, Value = speed.Speed }); await db.SaveChangesAsync(); } if (!stoppingToken.IsCancellationRequested) { var latancy = await SpeedTest.Net.SpeedTestClient.GetLatancy(server); db.Add(new Statistic { Date = now, Type = DataType.Latancy, Value = latancy }); await db.SaveChangesAsync(); } } } catch (Exception ex) { server = null; using (var db = new StatisticsContext()) { db.Events.Add(new Event { Date = DateTime.UtcNow, Title = ex.Message, Details = ex.ToString() }); await db.SaveChangesAsync(); } } // should be able to change this setting!!! await Task.Delay(options.Value.Frequancy); } }
public RulesEngineStatistics(string name, string description) : base(name, description) { Context = new StatisticsContext(name); }
private void saveStatus() // 保存数据到状态监控 { Logger.Debug("开始保存状态统计"); var context = new StatisticsContext(); if (downServerIds.ToArray().Length > 0) // 缓存存在故障数据 { Logger.Debug("保存数据统计 -> 发现缓存中存在故障数据"); foreach (var id in downServerIds.ToArray()) // 对缓存数组进行一次遍历, 然后更新对应数组的状态 { var statusHasId = context.Status .Where(c => c.identification.Contains(id)); if (statusHasId.ToList().ToArray().Length > 0) // 对于该标识有记录, 我们直接更新状态 { Logger.Debug("保存数据统计 -> 故障 id: " + id + " 在数据库中发现记录"); var identification = statusHasId.First(); identification.totol++; identification.down++; } else // 初始化 { Logger.Debug("保存数据统计 -> 故障 id: " + id + "未在数据库中发现记录"); var row = new status() { identification = id, totol = 1, down = 1, up = 0 }; context.Status.Add(row); } } } // 计算补集, 确认正常的服务 Logger.Debug("保存数据统计 -> 计算补给, 获得正常标识数组"); var diff = serverIds.ToArray().Where(c => !downServerIds.ToArray().Contains(c)).ToArray(); foreach (var id in diff) { var statusHasId = context.Status .Where(c => c.identification.Contains(id)); if (statusHasId.ToList().ToArray().Length > 0) // 对于该标识有记录, 我们直接更新状态 { Logger.Debug("保存数据统计 -> 正常 id: " + id + " 在数据库中发现记录"); var identification = statusHasId.First(); identification.totol++; identification.up++; } else // 初始化 { Logger.Debug("保存数据统计 -> 正常 id: " + id + " 未在数据库中发现记录"); var row = new status() { identification = id, totol = 1, down = 0, up = 1 }; context.Status.Add(row); } } Logger.Debug("保存数据统计 -> 保存变更"); context.SaveChanges(); Logger.Debug("保存数据统计 -> 完成"); }