Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reactor : Optimize Query #339

Merged
merged 9 commits into from
Jan 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ public async Task UpdateAsync(AlarmRule alarmRule, bool isEnabled, CheckFrequenc
public async Task<AlarmRuleRecord?> GetLatest(Guid alarmRuleId)
{
var query = await _alarmRuleRecordRepository.GetQueryableAsync();
return query.Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.ExcuteTime).FirstOrDefault();
return query.Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.Id).FirstOrDefault();
}

public async Task<long?> GetOffsetResult(Guid alarmRuleId, int offsetPeriod, string alias)
{
var query = await _alarmRuleRecordRepository.GetQueryableAsync();
var offsetRecord = query.Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.ExcuteTime).Skip(offsetPeriod - 1).FirstOrDefault();
var offsetRecord = query.Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.Id).Skip(offsetPeriod - 1).FirstOrDefault();

return offsetRecord?.AggregateResult.FirstOrDefault(x => x.Key == alias).Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public async Task<IQueryable<AlarmHistory>> GetQueryableAsync()

public async Task<AlarmHistory?> GetLastAsync(Guid alarmRuleId)
{
return await Context.Set<AlarmHistory>().Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.CreationTime).FirstOrDefaultAsync();
return await Context.Set<AlarmHistory>().Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.Id).FirstOrDefaultAsync();
}
}