Split current conditions condition code querying
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherapi/internal/app"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||
)
|
||||
|
||||
func (r *Repository) CurrentConditions(ctx context.Context, observationWindowMinutes int) (*app.CurrentConditions, error) {
|
||||
@@ -25,7 +26,6 @@ func (r *Repository) CurrentConditions(ctx context.Context, observationWindowMin
|
||||
&row.RelativeHumidityPercent,
|
||||
&row.WindSpeedKmh,
|
||||
&row.WindDirectionDegrees,
|
||||
&row.ConditionCode,
|
||||
&row.IsDay,
|
||||
)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
@@ -35,5 +35,42 @@ func (r *Repository) CurrentConditions(ctx context.Context, observationWindowMin
|
||||
return nil, fmt.Errorf("query current conditions: %w", err)
|
||||
}
|
||||
|
||||
return mapCurrentConditionsRow(row), nil
|
||||
if row.SampleCount == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
candidates, err := r.currentConditionsConditionCodeCandidates(ctx, observationWindowMinutes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mapCurrentConditionsRow(row, selectCurrentConditionsConditionCode(candidates)), nil
|
||||
}
|
||||
|
||||
func (r *Repository) currentConditionsConditionCodeCandidates(ctx context.Context, observationWindowMinutes int) ([]currentConditionsConditionCodeCandidate, error) {
|
||||
rows, err := r.db.QueryContext(ctx, queryCurrentConditionsConditionCodeCandidates, observationWindowMinutes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query current conditions condition code candidates: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var candidates []currentConditionsConditionCodeCandidate
|
||||
for rows.Next() {
|
||||
var (
|
||||
eventSource string
|
||||
conditionCode int64
|
||||
)
|
||||
if err := rows.Scan(&eventSource, &conditionCode); err != nil {
|
||||
return nil, fmt.Errorf("scan current conditions condition code candidate: %w", err)
|
||||
}
|
||||
candidates = append(candidates, currentConditionsConditionCodeCandidate{
|
||||
EventSource: eventSource,
|
||||
ConditionCode: model.WMOCode(conditionCode),
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate current conditions condition code candidates: %w", err)
|
||||
}
|
||||
|
||||
return candidates, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user