оптимизация метода Get

This commit is contained in:
eugeniy_ivanov 2023-04-11 13:49:51 +05:00
parent 44f1b08f90
commit 84a783a76b

View File

@ -61,91 +61,28 @@ namespace AsbCloudInfrastructure.Repository
var queryWitsInt = db.WitsItemInt
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
var queryWitsStr = db.WitsItemString
var queryWitsString = db.WitsItemString
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
var queryWitsFloat = db.WitsItemFloat
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
var recordAllInt = await GetEntityIntAsync(queryWitsInt, dateEnd, filterByDateEnd, approxPointsCount, token);
var recordAllFloat = await GetEntityFloatAsync(queryWitsFloat, dateEnd, filterByDateEnd, approxPointsCount, token);
var recordAllStr = await GetEntityStrAsync(queryWitsStr, dateEnd, filterByDateEnd, approxPointsCount, token);
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())
{
foreach (var record in recordAllInt)
{
var existingDto = dtos.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezone.Hours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezone.Hours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto);
}
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
WriteItem(dtos, recordAllInt, timezone.Hours);
}
if (recordAllStr.Any())
if (recordAllString.Any())
{
foreach (var record in recordAllStr)
{
var existingDto = dtos.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezone.Hours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezone.Hours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto);
}
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
WriteItem(dtos, recordAllString, timezone.Hours);
}
if (recordAllFloat.Any())
{
foreach (var record in recordAllFloat)
{
var existingDto = dtos
.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezone.Hours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezone.Hours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto);
}
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
WriteItem(dtos, recordAllFloat, timezone.Hours);
}
return dtos;
}
@ -177,35 +114,7 @@ namespace AsbCloudInfrastructure.Repository
await db.SaveChangesAsync(token);
}
private static async Task<IEnumerable<WitsItemInt>> GetEntityIntAsync(IQueryable<WitsItemInt> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
, CancellationToken token)
{
//if (filterByDateEnd)
// query = query.Where(d => d.DateTime <= dateEnd);
//var fullDataCount = await query.CountAsync(token)
// .ConfigureAwait(false);
//if (fullDataCount == 0)
// return Enumerable.Empty<WitsItemInt>();
//if (fullDataCount > 1.75 * approxPointsCount)
//{
// var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
// if (m > 1)
// query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
//}
var entities = await query
.OrderBy(d => d.DateTime)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities;
}
private static async Task<IEnumerable<WitsItemFloat>> GetEntityFloatAsync(IQueryable<WitsItemFloat> query,
private static async Task<IEnumerable<WitsItemBase<T>>> GetEntityAsync<T>(IQueryable<WitsItemBase<T>> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
, CancellationToken token)
{
@ -232,35 +141,6 @@ namespace AsbCloudInfrastructure.Repository
.ConfigureAwait(false);
return entities;
}
private static async Task<IEnumerable<WitsItemString>> GetEntityStrAsync(IQueryable<WitsItemString> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
, CancellationToken token)
{
//if (filterByDateEnd)
// query = query.Where(d => d.DateTime <= dateEnd);
//var fullDataCount = await query.CountAsync(token)
// .ConfigureAwait(false);
//if (fullDataCount == 0)
// return Enumerable.Empty<WitsItemString>();
//if (fullDataCount > 1.75 * approxPointsCount)
//{
// var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
// if (m > 1)
// query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
//}
var entities = await query
.OrderBy(d => d.DateTime)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities;
}
private static WitsItemBase<Tvalue> GetEntity<Tvalue>(WitsRecordDto record, Tvalue value, int idItems, double timezoneHours)
{
var entity = new WitsItemBase<Tvalue>
@ -273,6 +153,32 @@ namespace AsbCloudInfrastructure.Repository
};
return entity;
}
private static void WriteItem<T> (List<WitsRecordDto> dtos, IEnumerable<WitsItemBase<T>> recordAll, double timezoneHours)
{
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!));
}
}
}
}
#nullable disable
}