Files
zephyr/model/statisticsModel.go
Marco Cetica e26f7ff164
Some checks failed
Docker / docker (push) Has been cancelled
Tests / build (push) Has been cancelled
Code refactoring + made anomaly detection system more stable.
2025-11-24 12:14:54 +01:00

45 lines
1.3 KiB
Go

package model
import (
"errors"
"slices"
"strconv"
"github.com/ceticamarco/zephyr/cache"
"github.com/ceticamarco/zephyr/statistics"
"github.com/ceticamarco/zephyr/types"
)
func GetStatistics(cityName string, statCache *cache.StatCache) (types.StatResult, error) {
// Check whether there are sufficient and updated records for the given location
if statCache.IsKeyInvalid(cityName) {
return types.StatResult{}, errors.New("insufficient or outdated data to perform statistical analysis")
}
// Extract records from the database
stats := statCache.GetCityStatistics(cityName)
// Extract temperatures from statistics
temps := make([]float64, len(stats))
for idx, stat := range stats {
temps[idx] = stat.Temperature
}
// Detect anomalies
anomalies := statistics.DetectAnomalies(stats)
if len(anomalies) == 0 {
anomalies = nil
}
// Compute statistics
return types.StatResult{
Min: strconv.FormatFloat(slices.Min(temps), 'f', -1, 64),
Max: strconv.FormatFloat(slices.Max(temps), 'f', -1, 64),
Count: len(stats),
Mean: strconv.FormatFloat(statistics.Mean(temps), 'f', -1, 64),
StdDev: strconv.FormatFloat(statistics.StdDev(temps), 'f', -1, 64),
Median: strconv.FormatFloat(statistics.Median(temps), 'f', -1, 64),
Mode: strconv.FormatFloat(statistics.Mode(temps), 'f', -1, 64),
Anomaly: &anomalies,
}, nil
}