Code refactoring + made anomaly detection system more stable.
Some checks failed
Docker / docker (push) Has been cancelled
Tests / build (push) Has been cancelled
Some checks failed
Docker / docker (push) Has been cancelled
Tests / build (push) Has been cancelled
This commit is contained in:
80
cache/master.go
vendored
Normal file
80
cache/master.go
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ceticamarco/zephyr/types"
|
||||
)
|
||||
|
||||
// cacheType, representing the abstract value of a CacheEntity
|
||||
type cacheType interface {
|
||||
types.Weather | types.Metrics | types.Wind | types.DailyForecast | types.HourlyForecast | types.Moon
|
||||
}
|
||||
|
||||
// CacheEntity, representing the value of the cache
|
||||
type CacheEntity[T cacheType] struct {
|
||||
element T
|
||||
timestamp time.Time
|
||||
}
|
||||
|
||||
// MasterCache, representing a mapping between a key(str) and a CacheEntity
|
||||
type MasterCache[T cacheType] struct {
|
||||
mu sync.RWMutex
|
||||
Data map[string]CacheEntity[T]
|
||||
}
|
||||
|
||||
// MasterCaches, representing a grouping of the various caches
|
||||
type MasterCaches struct {
|
||||
WeatherCache MasterCache[types.Weather]
|
||||
MetricsCache MasterCache[types.Metrics]
|
||||
WindCache MasterCache[types.Wind]
|
||||
DailyForecastCache MasterCache[types.DailyForecast]
|
||||
HourlyForecastCache MasterCache[types.HourlyForecast]
|
||||
MoonCache MasterCache[types.Moon]
|
||||
}
|
||||
|
||||
func InitMasterCache() *MasterCaches {
|
||||
return &MasterCaches{
|
||||
WeatherCache: MasterCache[types.Weather]{Data: make(map[string]CacheEntity[types.Weather])},
|
||||
MetricsCache: MasterCache[types.Metrics]{Data: make(map[string]CacheEntity[types.Metrics])},
|
||||
WindCache: MasterCache[types.Wind]{Data: make(map[string]CacheEntity[types.Wind])},
|
||||
DailyForecastCache: MasterCache[types.DailyForecast]{Data: make(map[string]CacheEntity[types.DailyForecast])},
|
||||
HourlyForecastCache: MasterCache[types.HourlyForecast]{Data: make(map[string]CacheEntity[types.HourlyForecast])},
|
||||
MoonCache: MasterCache[types.Moon]{Data: make(map[string]CacheEntity[types.Moon])},
|
||||
}
|
||||
}
|
||||
|
||||
func (cache *MasterCache[T]) GetEntry(cityName string, ttl int8) (T, bool) {
|
||||
cache.mu.RLock()
|
||||
defer cache.mu.RUnlock()
|
||||
|
||||
val, isPresent := cache.Data[strings.ToUpper(cityName)]
|
||||
|
||||
// If key is not present, return a zero value
|
||||
if !isPresent {
|
||||
return val.element, false
|
||||
}
|
||||
|
||||
// Otherwise check whether cache element is expired
|
||||
currentTime := time.Now()
|
||||
expired := currentTime.Sub(val.timestamp) > (time.Duration(ttl) * time.Hour)
|
||||
if expired {
|
||||
return val.element, false
|
||||
}
|
||||
|
||||
return val.element, true
|
||||
}
|
||||
|
||||
func (cache *MasterCache[T]) AddEntry(entry T, cityName string) {
|
||||
cache.mu.Lock()
|
||||
defer cache.mu.Unlock()
|
||||
|
||||
currentTime := time.Now()
|
||||
|
||||
cache.Data[strings.ToUpper(cityName)] = CacheEntity[T]{
|
||||
element: entry,
|
||||
timestamp: currentTime,
|
||||
}
|
||||
}
|
||||
93
cache/statistics.go
vendored
Normal file
93
cache/statistics.go
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ceticamarco/zephyr/types"
|
||||
)
|
||||
|
||||
// statistic cache data type, representing a mapping between a location+date and its daily average temperature
|
||||
type StatCache struct {
|
||||
mu sync.RWMutex
|
||||
db map[string]float64
|
||||
}
|
||||
|
||||
func InitStatCache() *StatCache {
|
||||
return &StatCache{
|
||||
db: make(map[string]float64),
|
||||
}
|
||||
}
|
||||
|
||||
func (cache *StatCache) AddStatistic(cityName string, statDate string, dailyTemp float64) {
|
||||
cache.mu.Lock()
|
||||
defer cache.mu.Unlock()
|
||||
|
||||
// Format key as '<DATE>@<LOCATION>
|
||||
key := fmt.Sprintf("%s@%s", statDate, cityName)
|
||||
|
||||
// Insert weather statistic into the database if it doesn't already exist
|
||||
if _, exists := cache.db[key]; exists {
|
||||
return
|
||||
}
|
||||
|
||||
cache.db[key] = dailyTemp
|
||||
}
|
||||
|
||||
func (cache *StatCache) IsKeyInvalid(key string) bool {
|
||||
cache.mu.RLock()
|
||||
defer cache.mu.RUnlock()
|
||||
|
||||
// A key is invalid if it has less than 2 entries within the last 2 days
|
||||
threshold := time.Now().AddDate(0, 0, -2)
|
||||
|
||||
var validEntries uint = 0
|
||||
for storedKey := range cache.db {
|
||||
if !strings.HasSuffix(storedKey, key) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get <DATE> from <DATE>@<LOCATION>
|
||||
keyDate, err := time.Parse("2006-01-02", strings.Split(storedKey, "@")[0])
|
||||
if err != nil {
|
||||
keyDate = time.Now() // Add a fallback date if parsing fails
|
||||
}
|
||||
|
||||
if !keyDate.Before(threshold) {
|
||||
validEntries++
|
||||
|
||||
// Early skip if we already found two valid entries
|
||||
if validEntries >= 2 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (cache *StatCache) GetCityStatistics(cityName string) []types.StatElement {
|
||||
cache.mu.RLock()
|
||||
defer cache.mu.RUnlock()
|
||||
|
||||
result := make([]types.StatElement, 0)
|
||||
|
||||
for key, record := range cache.db {
|
||||
if strings.HasSuffix(key, cityName) {
|
||||
// Get <DATE> from <DATE>@<LOCATION>
|
||||
keyDate, err := time.Parse("2006-01-02", strings.Split(key, "@")[0])
|
||||
if err != nil {
|
||||
keyDate = time.Now() // Add a fallback date if parsing fails
|
||||
}
|
||||
|
||||
result = append(result, types.StatElement{
|
||||
Temperature: record,
|
||||
Date: keyDate,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user