Optimization of last value call
ci/woodpecker/push/build Pipeline failed
Details
ci/woodpecker/push/build Pipeline failed
Details
This commit is contained in:
parent
8a52975adb
commit
caa95f52a0
|
|
@ -61,7 +61,7 @@ func unitToSeconds(unit string) float64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
|
func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
|
||||||
condition := map[string]interface{}{}
|
condition := map[string]any{}
|
||||||
err := json.Unmarshal([]byte(alert.Condition), &condition)
|
err := json.Unmarshal([]byte(alert.Condition), &condition)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -86,7 +86,7 @@ func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
|
||||||
|
|
||||||
sensorId = int64(sensorValueCondition.SensorId)
|
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 != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
lastValue = float64(0)
|
lastValue = float64(0)
|
||||||
|
|
@ -112,7 +112,7 @@ func (s *AlertsEvaluatorService) EvaluateAlert(alert *models.AlertItem) error {
|
||||||
|
|
||||||
sensorId = sensorLastContactCondition.SensorId
|
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 != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,19 @@ func (s *SensorValuesService) GetLatest(sensorId int64, to int64) (*sensorValue,
|
||||||
return &value, nil
|
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 {
|
func (s *SensorValuesService) Cleanup(retentionInDays int64) error {
|
||||||
if retentionInDays <= 0 {
|
if retentionInDays <= 0 {
|
||||||
return fmt.Errorf("retentionInDays must be greater than 0")
|
return fmt.Errorf("retentionInDays must be greater than 0")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue