Optimization of last value call
ci/woodpecker/push/build Pipeline failed Details

This commit is contained in:
Jan Zípek 2026-04-11 20:21:27 +02:00
parent 8a52975adb
commit caa95f52a0
Signed by: kamen
GPG Key ID: A17882625B33AC31
2 changed files with 16 additions and 3 deletions

View File

@ -61,7 +61,7 @@ func unitToSeconds(unit string) float64 {
}
func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
condition := map[string]interface{}{}
condition := map[string]any{}
err := json.Unmarshal([]byte(alert.Condition), &condition)
if err != nil {
return err
@ -86,7 +86,7 @@ func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
sensorId = int64(sensorValueCondition.SensorId)
value, err := s.ctx.Services.SensorValues.GetLatest(sensorValueCondition.SensorId, time.Now().Unix())
value, err := s.ctx.Services.SensorValues.GetLatestToNow(sensorValueCondition.SensorId)
if err != nil {
if err == sql.ErrNoRows {
lastValue = float64(0)
@ -112,7 +112,7 @@ func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
sensorId = sensorLastContactCondition.SensorId
value, err := s.ctx.Services.SensorValues.GetLatest(sensorLastContactCondition.SensorId, time.Now().Unix())
value, err := s.ctx.Services.SensorValues.GetLatestToNow(sensorLastContactCondition.SensorId)
if err != nil {
if err == sql.ErrNoRows {

View File

@ -48,6 +48,19 @@ func (s *SensorValuesService) GetLatest(sensorId int64, to int64) (*sensorValue,
return &value, nil
}
func (s *SensorValuesService) GetLatestToNow(sensorId int64) (*sensorValue, error) {
var value = sensorValue{}
row := s.ctx.DB.QueryRow("SELECT timestamp, value FROM sensor_values WHERE sensor_id = ? ORDER BY timestamp DESC LIMIT 1", sensorId)
err := row.Scan(&value.Timestamp, &value.Value)
if err != nil {
return nil, err
}
return &value, nil
}
func (s *SensorValuesService) Cleanup(retentionInDays int64) error {
if retentionInDays <= 0 {
return fmt.Errorf("retentionInDays must be greater than 0")