refact GetAsync

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

View File

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