/// <summary> /// Returns the RedisSensorValuesRows for the ship with the given ShipId, /// and whose timestamps are between the given Unix timestamps (in milliseconds since Jan 1, 1970). /// </summary> public List <RedisSensorValuesRow> GetRange(long shipId, long startMinuteUnixMilliTs, long endMinuteUnixMilliTs) { List <string> keys = new List <string>(); for (long currMinuteInUnixMillis = startMinuteUnixMilliTs; currMinuteInUnixMillis < endMinuteUnixMilliTs; currMinuteInUnixMillis += 60000) { keys.Add(SensorValuesRowKeyFormatter.GetKey(shipId, currMinuteInUnixMillis)); } return(RedisDatabaseApi.Search <RedisSensorValuesRow>(keys)); }
/// <summary> /// Returns the RedisKpiValues for the given KpiEnums which have the given ShipId, /// and whose timestamps are between the given startDate and endDate. /// </summary> public List <RedisKpiValue> GetRange(long shipId, List <EKpi> kpiEnums, DateTime startDate, DateTime endDate) { List <string> keys = new List <string>(); for (var currDate = startDate; currDate < endDate; currDate = currDate.AddDays(1)) { foreach (var kpi in kpiEnums) { keys.Add(KpiValueKeyFormatter.GetKey(shipId, kpi, currDate)); } } return(RedisDatabaseApi.Search <RedisKpiValue>(keys)); }
public IActionResult UpdatingAllSensorValues(long shipId) { var firstAndLastImportMillis = GetFirstAndLastImportUnixTs(shipId); long firstImportUnixTimeMillis = firstAndLastImportMillis.Item1; long lastImportUnixTimeMillis = firstAndLastImportMillis.Item2; // Redis var sw = new Stopwatch(); sw.Start(); var allRedisSensorValuesForShip = _sensorValuesRowRetriever.GetRange(shipId, firstImportUnixTimeMillis, lastImportUnixTimeMillis) .Take(25000) .ToList(); allRedisSensorValuesForShip.ForEach(x => x.SensorValues[ESensor.sensor1] = 100.0); RedisDatabaseApi.Update(allRedisSensorValuesForShip); sw.Stop(); RedisUpdatingTimesMillis.Add(sw.ElapsedMilliseconds); AverageRedisUpdatingTimeMillis = RedisUpdatingTimesMillis.Average(); // Entity Framework sw = new Stopwatch(); sw.Start(); using (var context = new PrototypeContext()) { var allEfSensorValuesForShip = context.SensorValuesRows.Where(x => x.ShipId == shipId && x.RowTimestamp >= firstImportUnixTimeMillis && x.RowTimestamp <= lastImportUnixTimeMillis) .Take(25000) .ToList(); allEfSensorValuesForShip.ForEach(sv => sv.sensor1 += 100); context.SaveChanges(); } sw.Stop(); EfUpdatingTimesMillis.Add(sw.ElapsedMilliseconds); AverageEfUpdatingTimeMillis = EfUpdatingTimesMillis.Average(); return(Ok()); }
/// <summary> /// Calculates the KpiValue for each Kpi and saves them to the Redis DB using the Redis API for the given /// RedisSensorValuesRows. Makes use of the KpiCalculatorFactory in order to get the appropriate KpiCalculator. /// The DateOfImport and shipId parameters are used for the creation of the Redis keys for each KpiValue. /// </summary> public void Handle(List <RedisSensorValuesRow> importedRows, long shipId, DateTime DateOfImport) { List <RedisKpiValue> KpiValuesToSave = new List <RedisKpiValue>(); using (var ctx = new PrototypeContext()) { foreach (var kpi in ctx.Kpis) { var calculatorForKpi = _kpiCalculatorFactory.GetCalculator(shipId, kpi); var calculatedKpiValue = calculatorForKpi.Calculate(importedRows, DateOfImport); KpiValuesToSave.Add(calculatedKpiValue); } } RedisDatabaseApi.Create <RedisKpiValue>(KpiValuesToSave); }
private void SaveSensorValues(List <RedisSensorValuesRow> rows) { RedisDatabaseApi.Create <RedisSensorValuesRow>(rows); }