Added StatDB data type and IsKeyInvalid, AddStatistics methods

This commit is contained in:
2025-06-18 16:58:52 +02:00
parent e46ce3e2b2
commit 2db2c57ce2
5 changed files with 131 additions and 11 deletions

52
types/statDB.go Normal file
View File

@@ -0,0 +1,52 @@
package types
import (
"fmt"
"strings"
"time"
)
// StatDB data type, representing a mapping between a location and its weather
type StatDB struct {
db map[string]Weather
}
func InitDB() *StatDB {
return &StatDB{
db: make(map[string]Weather),
}
}
func (statDB *StatDB) AddStatistic(cityName string, weather Weather) {
key := fmt.Sprintf("%s@%s", weather.Date.Date, cityName)
// Insert weather statistic into the database only if it isn't present
if _, isPresent := statDB.db[key]; isPresent {
return
}
statDB.db[key] = weather
}
func (statDB *StatDB) IsKeyInvalid(key string) bool {
// A key is invalid if it has less than 2 entries within the last 2 days
threshold := time.Now().AddDate(0, 0, -2)
var validKeys uint = 0
for storedKey, record := range statDB.db {
if !strings.HasSuffix(storedKey, key) {
continue
}
if !record.Date.Date.Before(threshold) {
validKeys++
// Early skip if we already found two valid keys
if validKeys >= 2 {
return false
}
}
}
return true
}

21
types/statistics.go Normal file
View File

@@ -0,0 +1,21 @@
package types
// The WeatherAnomaly data type, representing
// skewed meteorological events
type WeatherAnomaly struct {
Date ZephyrDate `json:"date"`
Temp float64 `json:"temperature"`
}
// The StatResult data type, representing weather statistics
// of past meteorological events
type StatResult struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
Count int `json:"count"`
Mean float64 `json:"mean"`
StdDev float64 `json:"stdDev"`
Median float64 `json:"median"`
Mode float64 `json:"mode"`
Anomaly WeatherAnomaly `json:"anomaly"`
}