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()); } }
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); } }