Code refactoring + made anomaly detection system more stable.
Some checks failed
Docker / docker (push) Has been cancelled
Tests / build (push) Has been cancelled

This commit is contained in:
2025-11-24 12:14:54 +01:00
parent bdc4d40d4a
commit e26f7ff164
18 changed files with 357 additions and 329 deletions

View File

@@ -5,37 +5,23 @@ import (
"slices"
"strconv"
"github.com/ceticamarco/zephyr/cache"
"github.com/ceticamarco/zephyr/statistics"
"github.com/ceticamarco/zephyr/types"
)
func GetStatistics(cityName string, statDB *types.StatDB) (types.StatResult, error) {
func GetStatistics(cityName string, statCache *cache.StatCache) (types.StatResult, error) {
// Check whether there are sufficient and updated records for the given location
if statDB.IsKeyInvalid(cityName) {
if statCache.IsKeyInvalid(cityName) {
return types.StatResult{}, errors.New("insufficient or outdated data to perform statistical analysis")
}
extractTemps := func(weatherArr []types.Weather) ([]float64, error) {
temps := make([]float64, 0, len(weatherArr))
for _, weather := range weatherArr {
temperature, err := strconv.ParseFloat(weather.Temperature, 64)
if err != nil {
return nil, err
}
temps = append(temps, temperature)
}
return temps, nil
}
// Extract records from the database
stats := statDB.GetCityStatistics(cityName)
// Extract temperatures from weather statistics
temps, err := extractTemps(stats)
if err != nil {
return types.StatResult{}, err
stats := statCache.GetCityStatistics(cityName)
// Extract temperatures from statistics
temps := make([]float64, len(stats))
for idx, stat := range stats {
temps[idx] = stat.Temperature
}
// Detect anomalies

View File

@@ -42,10 +42,10 @@ func GetEmoji(condition string, isNight bool) string {
return "❓"
}
func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
func GetWeather(city *types.City, apiKey string) (types.Weather, float64, error) {
url, err := url.Parse(WTR_URL)
if err != nil {
return types.Weather{}, err
return types.Weather{}, 0, err
}
params := url.Query()
@@ -59,7 +59,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
res, err := http.Get(url.String())
if err != nil {
return types.Weather{}, err
return types.Weather{}, 0, err
}
defer res.Body.Close()
@@ -77,8 +77,9 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
} `json:"current"`
Daily []struct {
Temp struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Daily float64 `json:"day"`
Min float64 `json:"min"`
Max float64 `json:"max"`
} `json:"temp"`
} `json:"daily"`
Alerts []struct {
@@ -91,7 +92,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
var weather WeatherRes
if err := json.NewDecoder(res.Body).Decode(&weather); err != nil {
return types.Weather{}, err
return types.Weather{}, 0, err
}
// Format UNIX timestamp as 'YYYY-MM-DD'
@@ -143,5 +144,5 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
Condition: weather.Current.Weather[0].Title,
Emoji: emoji,
Alerts: alerts,
}, nil
}, weather.Daily[0].Temp.Daily, nil
}