Skip to content

Commit

Permalink
⚡ refactor: Adjust query sorting (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 authored Jan 6, 2025
1 parent 1540cc7 commit 0b8789a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
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.Id).FirstOrDefault();
return query.Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.CreationTime).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.Id).Skip(offsetPeriod - 1).FirstOrDefault();
var offsetRecord = query.Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.CreationTime).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 @@ -10,5 +10,6 @@ public void Configure(EntityTypeBuilder<AlarmRuleRecord> builder)
builder.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleRecords", AlertConsts.DB_SCHEMA);
builder.Property(x => x.AggregateResult).HasConversion(new JsonValueConverter<ConcurrentDictionary<string, long>>());
builder.Property(x => x.RuleResultItems).HasConversion(new JsonValueConverter<List<RuleResultItem>>());
builder.HasIndex(u => u.CreationTime);
}
}
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.Id).FirstOrDefaultAsync();
return await Context.Set<AlarmHistory>().Where(x => x.AlarmRuleId == alarmRuleId).OrderByDescending(x => x.CreationTime).FirstOrDefaultAsync();
}
}

0 comments on commit 0b8789a

Please sign in to comment.