Add a /conditions/current endpoint
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -8,6 +8,10 @@ const (
|
||||
UnitsUS Units = "us"
|
||||
)
|
||||
|
||||
const (
|
||||
ObservationWindowMinutesDefault = 30
|
||||
)
|
||||
|
||||
const (
|
||||
celsiusToFahrenheitScale = 9.0 / 5.0
|
||||
celsiusToFahrenheitOffset = 32.0
|
||||
|
||||
15
internal/core/current_conditions.go
Normal file
15
internal/core/current_conditions.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package core
|
||||
|
||||
import "gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||
|
||||
// CurrentConditions is an averaged current-conditions aggregate over a recent window.
|
||||
type CurrentConditions struct {
|
||||
TemperatureC *float64
|
||||
ApparentTemperatureC *float64
|
||||
DewpointC *float64
|
||||
RelativeHumidityPercent *float64
|
||||
WindSpeedKmh *float64
|
||||
WindDirectionDegrees *float64
|
||||
ConditionCode model.WMOCode
|
||||
IsDay *bool
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||
)
|
||||
|
||||
// WeatherObservationUS is the US-customary response shape for observations.
|
||||
@@ -69,6 +70,24 @@ type WeatherForecastPeriodUS struct {
|
||||
UVIndex *float64 `json:"uvIndex,omitempty" xml:"uvIndex,omitempty"`
|
||||
}
|
||||
|
||||
// CurrentConditionsResponse is the response shape for /conditions/current.
|
||||
// Unit-bearing fields are populated according to the requested unit mode.
|
||||
type CurrentConditionsResponse struct {
|
||||
TemperatureC *float64 `json:"temperatureC,omitempty" xml:"temperatureC,omitempty"`
|
||||
ApparentTemperatureC *float64 `json:"apparentTemperatureC,omitempty" xml:"apparentTemperatureC,omitempty"`
|
||||
DewpointC *float64 `json:"dewpointC,omitempty" xml:"dewpointC,omitempty"`
|
||||
WindSpeedKmh *float64 `json:"windSpeedKmh,omitempty" xml:"windSpeedKmh,omitempty"`
|
||||
TemperatureF *float64 `json:"temperatureF,omitempty" xml:"temperatureF,omitempty"`
|
||||
ApparentTemperatureF *float64 `json:"apparentTemperatureF,omitempty" xml:"apparentTemperatureF,omitempty"`
|
||||
DewpointF *float64 `json:"dewpointF,omitempty" xml:"dewpointF,omitempty"`
|
||||
WindSpeedMph *float64 `json:"windSpeedMph,omitempty" xml:"windSpeedMph,omitempty"`
|
||||
RelativeHumidityPercent *float64 `json:"relativeHumidityPercent,omitempty" xml:"relativeHumidityPercent,omitempty"`
|
||||
WindDirectionDegrees *float64 `json:"windDirectionDegrees,omitempty" xml:"windDirectionDegrees,omitempty"`
|
||||
ConditionText string `json:"conditionText,omitempty" xml:"conditionText,omitempty"`
|
||||
IsDay *bool `json:"isDay,omitempty" xml:"isDay,omitempty"`
|
||||
IsDayText string `json:"-" xml:"-"`
|
||||
}
|
||||
|
||||
func ObservationPayload(obs *model.WeatherObservation, units Units) any {
|
||||
if obs == nil {
|
||||
return nil
|
||||
@@ -155,6 +174,34 @@ func AlertsPayload(run *model.WeatherAlertRun, _ Units) any {
|
||||
return run
|
||||
}
|
||||
|
||||
func CurrentConditionsPayload(conditions *CurrentConditions, units Units) any {
|
||||
if conditions == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
out := CurrentConditionsResponse{
|
||||
RelativeHumidityPercent: copyFloat64Ptr(conditions.RelativeHumidityPercent),
|
||||
WindDirectionDegrees: copyFloat64Ptr(conditions.WindDirectionDegrees),
|
||||
ConditionText: standards.WMOText(conditions.ConditionCode, conditions.IsDay),
|
||||
IsDay: copyBoolPtr(conditions.IsDay),
|
||||
IsDayText: boolText(conditions.IsDay),
|
||||
}
|
||||
|
||||
if units == UnitsUS {
|
||||
out.TemperatureF = celsiusToFahrenheitPtr(conditions.TemperatureC)
|
||||
out.ApparentTemperatureF = celsiusToFahrenheitPtr(conditions.ApparentTemperatureC)
|
||||
out.DewpointF = celsiusToFahrenheitPtr(conditions.DewpointC)
|
||||
out.WindSpeedMph = scalePtr(conditions.WindSpeedKmh, kmhToMphFactor)
|
||||
return out
|
||||
}
|
||||
|
||||
out.TemperatureC = copyFloat64Ptr(conditions.TemperatureC)
|
||||
out.ApparentTemperatureC = copyFloat64Ptr(conditions.ApparentTemperatureC)
|
||||
out.DewpointC = copyFloat64Ptr(conditions.DewpointC)
|
||||
out.WindSpeedKmh = copyFloat64Ptr(conditions.WindSpeedKmh)
|
||||
return out
|
||||
}
|
||||
|
||||
func celsiusToFahrenheitPtr(v *float64) *float64 {
|
||||
if v == nil {
|
||||
return nil
|
||||
@@ -194,3 +241,13 @@ func copyTimePtr(v *time.Time) *time.Time {
|
||||
out := *v
|
||||
return &out
|
||||
}
|
||||
|
||||
func boolText(v *bool) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
if *v {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
}
|
||||
|
||||
@@ -102,12 +102,73 @@ func TestMetricPassthroughAndNilHandling(t *testing.T) {
|
||||
if AlertsPayload(nil, UnitsUS) != nil {
|
||||
t.Fatalf("expected nil alerts input to return nil payload")
|
||||
}
|
||||
if CurrentConditionsPayload(nil, UnitsUS) != nil {
|
||||
t.Fatalf("expected nil current conditions input to return nil payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentConditionsPayloadMetricAndUS(t *testing.T) {
|
||||
conditions := &CurrentConditions{
|
||||
TemperatureC: float64Ptr(20),
|
||||
ApparentTemperatureC: float64Ptr(18),
|
||||
DewpointC: float64Ptr(10),
|
||||
RelativeHumidityPercent: float64Ptr(55),
|
||||
WindSpeedKmh: float64Ptr(100),
|
||||
WindDirectionDegrees: float64Ptr(225),
|
||||
ConditionCode: 0,
|
||||
IsDay: boolPtr(true),
|
||||
}
|
||||
|
||||
metricPayload := CurrentConditionsPayload(conditions, UnitsMetric)
|
||||
metric, ok := metricPayload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
t.Fatalf("expected CurrentConditionsResponse metric payload, got %T", metricPayload)
|
||||
}
|
||||
assertApprox(t, metric.TemperatureC, 20, 0.0001)
|
||||
assertApprox(t, metric.WindSpeedKmh, 100, 0.0001)
|
||||
if metric.TemperatureF != nil || metric.WindSpeedMph != nil {
|
||||
t.Fatalf("expected US fields omitted for metric payload")
|
||||
}
|
||||
if metric.ConditionText != "Sunny" {
|
||||
t.Fatalf("expected condition text Sunny, got %q", metric.ConditionText)
|
||||
}
|
||||
|
||||
usPayload := CurrentConditionsPayload(conditions, UnitsUS)
|
||||
us, ok := usPayload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
t.Fatalf("expected CurrentConditionsResponse US payload, got %T", usPayload)
|
||||
}
|
||||
assertApprox(t, us.TemperatureF, 68, 0.0001)
|
||||
assertApprox(t, us.WindSpeedMph, 62.1371192237, 0.0001)
|
||||
if us.TemperatureC != nil || us.WindSpeedKmh != nil {
|
||||
t.Fatalf("expected metric fields omitted for US payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentConditionsPayloadUsesNightConditionText(t *testing.T) {
|
||||
night := false
|
||||
payload := CurrentConditionsPayload(&CurrentConditions{
|
||||
ConditionCode: 0,
|
||||
IsDay: &night,
|
||||
}, UnitsMetric)
|
||||
|
||||
metric, ok := payload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
t.Fatalf("expected CurrentConditionsResponse payload, got %T", payload)
|
||||
}
|
||||
if metric.ConditionText != "Clear" {
|
||||
t.Fatalf("expected condition text Clear, got %q", metric.ConditionText)
|
||||
}
|
||||
}
|
||||
|
||||
func float64Ptr(v float64) *float64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
|
||||
func assertApprox(t *testing.T, got *float64, want, eps float64) {
|
||||
t.Helper()
|
||||
if got == nil {
|
||||
|
||||
@@ -11,6 +11,7 @@ type Repository interface {
|
||||
LatestObservation(ctx context.Context) (*model.WeatherObservation, error)
|
||||
LatestHourlyForecast(ctx context.Context) (*model.WeatherForecastRun, error)
|
||||
LatestActiveAlerts(ctx context.Context) (*model.WeatherAlertRun, error)
|
||||
CurrentConditions(ctx context.Context, observationWindowMinutes int) (*CurrentConditions, error)
|
||||
}
|
||||
|
||||
// Service provides weather read use-cases.
|
||||
@@ -33,3 +34,7 @@ func (s *Service) LatestHourlyForecast(ctx context.Context) (*model.WeatherForec
|
||||
func (s *Service) LatestActiveAlerts(ctx context.Context) (*model.WeatherAlertRun, error) {
|
||||
return s.repo.LatestActiveAlerts(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) CurrentConditions(ctx context.Context) (*CurrentConditions, error) {
|
||||
return s.repo.CurrentConditions(ctx, ObservationWindowMinutesDefault)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user