Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 7 additions & 55 deletions internal/server/postgres/.sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,30 @@ sql:
emit_interface: true
emit_exported_queries: true
overrides:
- column: "winning_predictions.p02_sip"
- column: "*.p02_sip"
go_type:
type: "int16"
pointer: true
- column: "winning_predictions.p10_sip"
- column: "*.p10_sip"
go_type:
type: "int16"
pointer: true
- column: "winning_predictions.p25_sip"
- column: "*.p25_sip"
go_type:
type: "int16"
pointer: true
- column: "winning_predictions.p75_sip"
- column: "*.p50_sip"
go_type:
type: "int16"
pointer: true
- column: "winning_predictions.p90_sip"
go_type:
type: "int16"
pointer: true
- column: "winning_predictions.p98_sip"
go_type:
type: "int16"
pointer: true
- column: "expanded.p02_sip"
go_type:
type: "int16"
pointer: true
- column: "expanded.p10_sip"
go_type:
type: "int16"
pointer: true
- column: "expanded.p25_sip"
go_type:
type: "int16"
pointer: true
- column: "expanded.p50_sip"
go_type:
type: "int16"
- column: "expanded.p75_sip"
go_type:
type: "int16"
pointer: true
- column: "expanded.p90_sip"
go_type:
type: "int16"
pointer: true
- column: "expanded.p98_sip"
go_type:
type: "int16"
pointer: true
- column: "ListPredictionsAtTimeForLocations.p02_sip"
go_type:
type: "int16"
pointer: true
- column: "ListPredictionsAtTimeForLocations.p10_sip"
go_type:
type: "int16"
pointer: true
- column: "ListPredictionsAtTimeForLocations.p25_sip"
go_type:
type: "int16"
pointer: true
- column: "ListPredictionsAtTimeForLocations.p75_sip"
- column: "*.p75_sip"
go_type:
type: "int16"
pointer: true
- column: "ListPredictionsAtTimeForLocations.p90_sip"
- column: "*.p90_sip"
go_type:
type: "int16"
pointer: true
- column: "ListPredictionsAtTimeForLocations.p98_sip"
- column: "*.p98_sip"
go_type:
type: "int16"
pointer: true
Expand Down
51 changes: 0 additions & 51 deletions internal/server/postgres/dataserverimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,6 @@ func (s *DataPlatformDataServiceServerImpl) CreateForecast(
return nil, fmt.Errorf("invalid forecast: %w", err)
}

// Create the forecast data
paramsList := make([]db.CreatePredictedValuesParams, len(req.Values))
for i, value := range req.Values {
paramsList[i] = db.CreatePredictedValuesParams{
HorizonMins: int16(value.HorizonMins),
P02Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p02"),
P10Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p10"),
P25Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p25"),
P50Sip: int16(value.P50Fraction * 30000.0),
P75Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p75"),
P90Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p90"),
P98Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p98"),
ForecastUuid: fParams.ForecastUuid,
}
}

count, err := querier.CreatePredictedValues(ctx, paramsList)
if err != nil || count < int64(len(req.Values)) {
if err == nil {
err = errors.New("inserted count less than requested")
}

return nil, fmt.Errorf("invalid predicted generation values: %w", err)
}

l.Debug().
Str("dp.forecast.uuid", fParams.ForecastUuid.String()).
Str("dp.geometry.uuid", fParams.GeometryUuid.String()).
Expand Down Expand Up @@ -1481,7 +1456,6 @@ func (s *DataPlatformDataServiceServerImpl) StreamCreateForecasts(

var (
forecastParams []db.CreateForecastsParams
valueParams []db.CreatePredictedValuesParams
createdUuids []string
batchUuids []string
)
Expand Down Expand Up @@ -1519,20 +1493,10 @@ func (s *DataPlatformDataServiceServerImpl) StreamCreateForecasts(
return fmt.Errorf("failed to insert forecasts batch: %w", err)
}

countV, err := querier.CreatePredictedValues(ctx, valueParams)
if err != nil || countV < int64(len(valueParams)) {
if err == nil {
err = errors.New("inserted predicted values count less than requested")
}

return fmt.Errorf("failed to insert predicted values batch: %w", err)
}

createdUuids = append(createdUuids, batchUuids...)

// Reset batch buffers
forecastParams = forecastParams[:0]
valueParams = valueParams[:0]
batchUuids = batchUuids[:0]

batchesProcessed++
Expand Down Expand Up @@ -1633,21 +1597,6 @@ func (s *DataPlatformDataServiceServerImpl) StreamCreateForecasts(
}

forecastParams = append(forecastParams, fParams)

for _, value := range req.Values {
valueParams = append(valueParams, db.CreatePredictedValuesParams{
HorizonMins: int16(value.HorizonMins),
P02Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p02"),
P10Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p10"),
P25Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p25"),
P50Sip: int16(value.P50Fraction * 30000.0),
P75Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p75"),
P90Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p90"),
P98Sip: extractSIPStatPtrFromMap(value.OtherStatisticsFractions, "p98"),
ForecastUuid: fParams.ForecastUuid,
})
}

batchUuids = append(batchUuids, fParams.ForecastUuid.String())

// Flush if we hit the batch size limit
Expand Down
13 changes: 0 additions & 13 deletions internal/server/postgres/mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ func timeptrToPgTimestamp(t *timestamppb.Timestamp) pgtype.Timestamp {
return pgtype.Timestamp{Time: t.AsTime().UTC(), Valid: true}
}

// extractSIPStatPtrFromMap gets a key's value from a map as a pointer, and converts it
// to a smallint percentage. If it doesn't exist, it returns nil.
func extractSIPStatPtrFromMap(m map[string]float32, key string) *int16 {
val, exists := m[key]
if !exists {
return nil
}

sip_val := int16(val * 30000.0)

return &sip_val
}

// extractSIPStatSlice builds the array for a single p-level from a forecast's values.
// Returns nil if no value in the series carries this statistic, so the column is stored as a
// NULL array rather than a materialised array of nulls (~3 bytes per forecast against ~130).
Expand Down
42 changes: 0 additions & 42 deletions internal/server/postgres/mappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,48 +83,6 @@ func Test_timeptrToPgTimestamp(t *testing.T) {
}
}

func Test_extractSIPStatPtrFromMap(t *testing.T) {
m := map[string]float32{"p10": 0.1, "p90": 0.9}

tests := []struct {
name string
inputMap map[string]float32
key string
expected *int16
}{
{
name: "nil map returns nil",
inputMap: nil,
key: "p10",
expected: nil,
},
{
name: "missing key returns nil",
inputMap: m,
key: "p50",
expected: nil,
},
{
name: "existing key returns sip value",
inputMap: m,
key: "p10",
expected: func() *int16 { v := int16(3000); return &v }(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := extractSIPStatPtrFromMap(tt.inputMap, tt.key)
if tt.expected == nil {
require.Nil(t, res)
} else {
require.NotNil(t, res)
require.Equal(t, *tt.expected, *res)
}
})
}
}

func Test_sipToFraction(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading
Loading