private bool AddToDb(int id, IRestResponse response, double mseconds) { //todo использую три таблицы с одинаковыми данными, своего рода шардинг предполагала, //можно было бы сделать одну таблицу, тогда код уменьшился бы в три раза try { using (ServicesDbContext dbContext = new ServicesDbContext()) { if (id == 0) { Refdata refdata = new Refdata() { CreatedDate = DateTime.UtcNow, IsAvailable = response.IsSuccessful, ResponseDuration = mseconds, LastHourErrors = dbContext.Refdatas.Count(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60) && r.IsAvailable == false), LastDayErrors = dbContext.Refdatas.Count(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1) && r.IsAvailable == false) }; dbContext.Refdatas.Add(refdata); dbContext.SaveChanges(); } else if (id == 1) { Ibonus ibonus = new Ibonus() { CreatedDate = DateTime.UtcNow, IsAvailable = response.IsSuccessful, ResponseDuration = mseconds, LastHourErrors = dbContext.Ibonuses.Count(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60) && r.IsAvailable == false), LastDayErrors = dbContext.Ibonuses.Count(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1) && r.IsAvailable == false) }; dbContext.Ibonuses.Add(ibonus); dbContext.SaveChanges(); } else { Catalog catalog = new Catalog() { CreatedDate = DateTime.UtcNow, IsAvailable = response.IsSuccessful, ResponseDuration = mseconds, LastHourErrors = dbContext.Catalogs.Count(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60) && r.IsAvailable == false), LastDayErrors = dbContext.Catalogs.Count(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1) && r.IsAvailable == false) }; dbContext.Catalogs.Add(catalog); dbContext.SaveChanges(); } return(true); } } catch (Exception e) { _logger.LogError($"Wasn't able to save objects to databse, ex: {e}"); return(false); } }
public string ReturnFullInfo(int serviceId) { //todo использую три таблицы с одинаковыми данными, своего рода шардинг предполагала, //можно было бы сделать одну таблицу, тогда код уменьшился бы в три раза try { using (ServicesDbContext dbContext = new ServicesDbContext()) { if (serviceId == 0) { double RefdataBestTime = _config.GetSection("ResponseBestTime:MaxRefdataResponseDuration").Get <double>(); Refdata refdata = dbContext.Refdatas.Last(); StatusRm statusRm = new StatusRm() { IsAvailable = refdata.IsAvailable, LastHourAvgResponseDuration = dbContext.Refdatas.Where(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60)).Select(x => x.ResponseDuration).Average(), LastDayAvgResponseDuration = dbContext.Refdatas.Where(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1)).Select(x => x.ResponseDuration).Average(), ResponseDuration = refdata.ResponseDuration, LastDayErrors = refdata.LastDayErrors, LastHourErrors = refdata.LastHourErrors, BestResponseTime = RefdataBestTime, AvgResponseDuration = dbContext.Refdatas.Select(x => x.ResponseDuration).Average(), LastHourMaxResponseDuration = dbContext.Refdatas.Where(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60)).Select(x => x.ResponseDuration).Max(), LastDayMaxResponseDuration = dbContext.Refdatas.Where(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1)).Select(x => x.ResponseDuration).Max() }; statusRm.LastHourResponseDeviationTime = statusRm.LastHourAvgResponseDuration - RefdataBestTime; statusRm.LastDayResponseDeviationTime = statusRm.LastDayAvgResponseDuration - RefdataBestTime; string serializedDto = JsonConvert.SerializeObject(statusRm); return(serializedDto); } else if (serviceId == 1) { double IbonusBestTime = _config.GetSection("ResponseBestTime:MaxIbonusResponseDuration").Get <double>(); Ibonus ibonus = dbContext.Ibonuses.Last(); StatusRm statusRm = new StatusRm() { IsAvailable = ibonus.IsAvailable, LastHourAvgResponseDuration = dbContext.Ibonuses.Where(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60)).Select(x => x.ResponseDuration).Average(), LastDayAvgResponseDuration = dbContext.Ibonuses.Where(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1)).Select(x => x.ResponseDuration).Average(), ResponseDuration = ibonus.ResponseDuration, LastDayErrors = ibonus.LastDayErrors, LastHourErrors = ibonus.LastHourErrors, BestResponseTime = IbonusBestTime, AvgResponseDuration = dbContext.Ibonuses.Select(x => x.ResponseDuration).Average(), LastHourMaxResponseDuration = dbContext.Ibonuses.Where(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60)).Select(x => x.ResponseDuration).Max(), LastDayMaxResponseDuration = dbContext.Ibonuses.Where(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1)).Select(x => x.ResponseDuration).Max() }; statusRm.LastHourResponseDeviationTime = statusRm.LastHourAvgResponseDuration - IbonusBestTime; statusRm.LastDayResponseDeviationTime = statusRm.LastDayAvgResponseDuration - IbonusBestTime; string serializedDto = JsonConvert.SerializeObject(statusRm); return(serializedDto); } else { double CatalogBestTime = _config.GetSection("ResponseBestTime:MaxCatalogResponseDuration").Get <double>(); Catalog catalog = dbContext.Catalogs.Last(); StatusRm statusRm = new StatusRm() { IsAvailable = catalog.IsAvailable, LastHourAvgResponseDuration = dbContext.Catalogs.Where(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60)).Select(x => x.ResponseDuration).Average(), LastDayAvgResponseDuration = dbContext.Catalogs.Where(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1)).Select(x => x.ResponseDuration).Average(), ResponseDuration = catalog.ResponseDuration, LastDayErrors = catalog.LastDayErrors, LastHourErrors = catalog.LastHourErrors, BestResponseTime = CatalogBestTime, AvgResponseDuration = dbContext.Catalogs.Select(x => x.ResponseDuration).Average(), LastHourMaxResponseDuration = dbContext.Catalogs.Where(r => r.CreatedDate > DateTime.UtcNow.AddMinutes(-60)).Select(x => x.ResponseDuration).Max(), LastDayMaxResponseDuration = dbContext.Catalogs.Where(r => r.CreatedDate > DateTime.UtcNow.AddDays(-1)).Select(x => x.ResponseDuration).Max() }; statusRm.LastHourResponseDeviationTime = statusRm.LastHourAvgResponseDuration - CatalogBestTime; statusRm.LastDayResponseDeviationTime = statusRm.LastDayAvgResponseDuration - CatalogBestTime; string serializedDto = JsonConvert.SerializeObject(statusRm); return(serializedDto); } } } catch { _logger.LogError("Wasn't able to get objects from databse"); return(string.Empty); } }