refact GetAsync

This commit is contained in:
eugeniy_ivanov 2023-04-14 00:36:11 +05:00
parent c989e449c2
commit 55e17f6955

View File

@ -52,10 +52,7 @@ namespace AsbCloudInfrastructure.Repository
}
if (dateBeginUtc == default)
dateBeginUtc = DateTime.UtcNow.AddSeconds(-intervalSec);
//var cacheData = telemetryDataCache.GetOrDefault(telemetry.Id, dateBeginUtc.ToRemoteDateTime(timezone.Hours), intervalSec, approxPointsCount);
//if (cacheData is not null)
// return cacheData;
dateBeginUtc = DateTime.UtcNow.AddSeconds(-intervalSec);
var dateEnd = dateBeginUtc.AddSeconds(intervalSec);
var queryWitsInt = db.Set<WitsItemInt>()
@ -68,22 +65,26 @@ namespace AsbCloudInfrastructure.Repository
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
var recordAllInt = await GetEntityAsync(queryWitsInt, dateEnd, filterByDateEnd, approxPointsCount, token);
var recordAllFloat = await GetEntityAsync(queryWitsFloat, dateEnd, filterByDateEnd, approxPointsCount, token);
var recordAllString = await GetEntityAsync(queryWitsString, dateEnd, filterByDateEnd, approxPointsCount, token);
var dtos = new List<WitsRecordDto>();
if (recordAllInt.Any())
var recordAllInt = await GetItemsOrDefaultAsync(queryWitsInt, dateEnd, filterByDateEnd, approxPointsCount, timezone.Hours, token);
var recordAllFloat = await GetItemsOrDefaultAsync(queryWitsFloat, dateEnd, filterByDateEnd, approxPointsCount,timezone.Hours, token);
var recordAllString = await GetItemsOrDefaultAsync(queryWitsString, dateEnd, filterByDateEnd, approxPointsCount, timezone.Hours, token);
var groupRecordDate = (recordAllFloat.Union(recordAllInt)).Union(recordAllString)
.GroupBy(g => new
{
IdRecord = g.IdRecord,
Date = g.Date
}).ToList();
var dtos = groupRecordDate.Select(g => new WitsRecordDto
{
WriteItem(dtos, recordAllInt, timezone.Hours);
}
if (recordAllString.Any())
{
WriteItem(dtos, recordAllString, timezone.Hours);
}
if (recordAllFloat.Any())
{
WriteItem(dtos, recordAllFloat, timezone.Hours);
}
Id = g.Key.IdRecord,
Date = g.Key.Date,
IdTelemetry = g.First().IdTelemetry,
Items = g.Select(r => new {
Key = r.IdItem,
Value = r.Item
}).ToList().ToDictionary(x => x.Key, x => x.Value)
});
return dtos;
}
@ -97,25 +98,25 @@ namespace AsbCloudInfrastructure.Repository
var jsonValue = item.Value;
if(jsonValue.Value is string valueString)
{
var entity = GetEntity(dto, valueString, item.Key, timezoneHours);
var entity = ConvertToEntity(dto, valueString, item.Key, timezoneHours);
db.WitsItemString.Add(entity.Adapt<WitsItemString>());
}
if (jsonValue.Value is float valueFloat)
{
var entity = GetEntity(dto, valueFloat, item.Key, timezoneHours);
var entity = ConvertToEntity(dto, valueFloat, item.Key, timezoneHours);
db.WitsItemFloat.Add(entity.Adapt<WitsItemFloat>());
}
if (jsonValue.Value is int valueInt)
{
var entity = GetEntity(dto, valueInt, item.Key, timezoneHours);
var entity = ConvertToEntity(dto, valueInt, item.Key, timezoneHours);
db.WitsItemInt.Add(entity.Adapt<WitsItemInt>());
}
}
await db.SaveChangesAsync(token);
}
private static async Task<IEnumerable<WitsItemBase<T>>> GetEntityAsync<T>(IQueryable<WitsItemBase<T>> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
private static async Task<IEnumerable<ItemRecord>> GetItemsOrDefaultAsync<T>(IQueryable<WitsItemBase<T>> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount, double timezoneHours
, CancellationToken token)
{
//if (filterByDateEnd)
@ -139,9 +140,19 @@ namespace AsbCloudInfrastructure.Repository
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities;
if (entities is null)
return Enumerable.Empty<ItemRecord>();
var items = entities.Select(e => new ItemRecord
{
IdRecord = e.IdRecord,
IdTelemetry = e.IdTelemetry,
Date = e.DateTime.ToRemoteDateTime(timezoneHours),
IdItem = e.IdItem,
Item = new JsonValue(e.Value)
});
return items;
}
private static WitsItemBase<Tvalue> GetEntity<Tvalue>(WitsRecordDto record, Tvalue value, int idItems, double timezoneHours)
private static WitsItemBase<Tvalue> ConvertToEntity<Tvalue>(WitsRecordDto record, Tvalue value, int idItems, double timezoneHours)
{
var entity = new WitsItemBase<Tvalue>
{
@ -154,31 +165,14 @@ namespace AsbCloudInfrastructure.Repository
return entity;
}
private static void WriteItem<T> (List<WitsRecordDto> dtos, IEnumerable<WitsItemBase<T>> recordAll, double timezoneHours)
internal class ItemRecord
{
foreach (var record in recordAll)
{
var existingDto = dtos.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezoneHours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezoneHours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto);
}
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
}
public int IdRecord { get; set; }
public int IdTelemetry { get; set; }
public DateTime Date { get; set; }
public int IdItem { get; set; }
public JsonValue Item { get; set; }
}
}
#nullable disable
}