All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/ejr/weatherapi/internal/core/ports"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type ObservationRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewObservationRepository(pool *pgxpool.Pool) *ObservationRepository {
|
|
return &ObservationRepository{pool: pool}
|
|
}
|
|
|
|
func (r *ObservationRepository) GetCurrentSummary(ctx context.Context, window time.Duration) (ports.ObservationSummaryMetric, error) {
|
|
var temperature sql.NullFloat64
|
|
var apparent sql.NullFloat64
|
|
|
|
if err := r.pool.QueryRow(ctx, queryObservationSummary, windowMinutes(window)).Scan(&temperature, &apparent); err != nil {
|
|
return ports.ObservationSummaryMetric{}, fmt.Errorf("query observation summary: %w", err)
|
|
}
|
|
|
|
return ports.ObservationSummaryMetric{
|
|
TemperatureC: ptrFloat64(temperature),
|
|
ApparentTemperatureC: ptrFloat64(apparent),
|
|
}, nil
|
|
}
|
|
|
|
func (r *ObservationRepository) ListCurrentConditions(ctx context.Context, window time.Duration) ([]ports.ObservationConditionMetric, error) {
|
|
rows, err := r.pool.Query(ctx, queryObservationConditions, windowMinutes(window))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query observation conditions: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := make([]ports.ObservationConditionMetric, 0)
|
|
for rows.Next() {
|
|
var stationID sql.NullString
|
|
var observedAt time.Time
|
|
var temperature sql.NullFloat64
|
|
var textDescription sql.NullString
|
|
var providerRawDescription sql.NullString
|
|
var conditionText sql.NullString
|
|
|
|
if err := rows.Scan(
|
|
&stationID,
|
|
&observedAt,
|
|
&temperature,
|
|
&textDescription,
|
|
&providerRawDescription,
|
|
&conditionText,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("scan observation condition row: %w", err)
|
|
}
|
|
|
|
out = append(out, ports.ObservationConditionMetric{
|
|
StationID: ptrString(stationID),
|
|
ObservedAt: observedAt,
|
|
TemperatureC: ptrFloat64(temperature),
|
|
TextDescription: ptrString(textDescription),
|
|
ProviderRawDescription: ptrString(providerRawDescription),
|
|
ConditionText: ptrString(conditionText),
|
|
})
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate observation conditions rows: %w", err)
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func (r *ObservationRepository) ListCurrentPrecipitationEvents(ctx context.Context, window time.Duration) ([]string, error) {
|
|
rows, err := r.pool.Query(ctx, queryObservationPrecipitation, windowMinutes(window))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query observation precipitation events: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := make([]string, 0)
|
|
for rows.Next() {
|
|
var rawText sql.NullString
|
|
if err := rows.Scan(&rawText); err != nil {
|
|
return nil, fmt.Errorf("scan observation precipitation row: %w", err)
|
|
}
|
|
if rawText.Valid {
|
|
out = append(out, rawText.String)
|
|
}
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate observation precipitation rows: %w", err)
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func windowMinutes(window time.Duration) int {
|
|
return int(window / time.Minute)
|
|
}
|