105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package meteo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type MeteoDataFromServiceA struct {
|
|
Timestamp time.Time `csv:"fecha" json:"timestamp"`
|
|
Location string `csv:"ciudad" json:"location"`
|
|
MaxTemp float32 `csv:"temperatura maxima" json:"max_temp"`
|
|
MinTemp float32 `csv:"temperatura minima" json:"min_temp"`
|
|
Rainfall float32 `csv:"precipitacion" json:"rainfall"`
|
|
Cloudiness int `csv:"nubosidad" json:"cloudiness"`
|
|
}
|
|
|
|
type MeteoDataPerDay struct {
|
|
MaxTemp float32 `json:"max_temp"`
|
|
MinTemp float32 `json:"min_temp"`
|
|
AvgTemp float32 `json:"avg_temp"`
|
|
Rainfall float32 `json:"rainfall"`
|
|
Cloudiness int `json:"cloudiness"`
|
|
}
|
|
|
|
func (mtpd *MeteoDataPerDay) ConvertValue() {
|
|
mtpd.MaxTemp = mtpd.MaxTemp*9/5 + 32
|
|
mtpd.MinTemp = mtpd.MinTemp*9/5 + 32
|
|
mtpd.AvgTemp = mtpd.AvgTemp*9/5 + 32
|
|
}
|
|
|
|
type Rolling7Data struct {
|
|
AvgTemp float32 `json:"avg_temp"`
|
|
AvgCloudiness int `json:"avg_cloudiness"`
|
|
SumRainfall float32 `json:"sum_rainfall"`
|
|
}
|
|
|
|
type MeteoData struct {
|
|
Location string `json:"location"`
|
|
Unit Unit `json:"unit"`
|
|
From string `json:"from"`
|
|
Days *[]MeteoDataPerDay `json:"days,omitempty"`
|
|
Rolling7 *Rolling7Data `json:"rolling7,omitempty"`
|
|
}
|
|
|
|
func (mt *MeteoData) ComputeCacheKey() string {
|
|
return fmt.Sprintf("meteo:%s:%s", mt.Location, mt.From)
|
|
}
|
|
|
|
type Unit string
|
|
|
|
const (
|
|
UnitC Unit = "C"
|
|
UnitF Unit = "F"
|
|
)
|
|
|
|
type Agg string
|
|
|
|
const (
|
|
AggDaily Agg = "daily"
|
|
AggRolling7 Agg = "rolling7"
|
|
)
|
|
|
|
type GetMeteoData struct {
|
|
Location string
|
|
Date string
|
|
Days int
|
|
Unit Unit
|
|
Agg Agg
|
|
}
|
|
|
|
func (mt *GetMeteoData) Validate() error {
|
|
if mt.Date == "" {
|
|
mt.Date = time.Now().Format("2006-01-02")
|
|
}
|
|
|
|
if _, err := time.Parse("2006-01-02", mt.Date); err != nil {
|
|
return ErrMissingOrInvalidDate
|
|
}
|
|
|
|
if mt.Days == 0 {
|
|
mt.Days = 5
|
|
}
|
|
if mt.Days < 1 || mt.Days > 10 {
|
|
return ErrInvalidDays
|
|
}
|
|
|
|
if mt.Unit == "" {
|
|
mt.Unit = UnitC
|
|
}
|
|
|
|
if mt.Agg == "" {
|
|
mt.Agg = AggDaily
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
ErrCityNotFound = errors.New("city not found")
|
|
ErrReadingData = errors.New("error reading data")
|
|
ErrMissingOrInvalidDate = errors.New("missing or invalid date")
|
|
ErrInvalidDays = errors.New("days must be between 1 and 10")
|
|
)
|