Skip to content

Commit

Permalink
Merge pull request #195 from alphagov/2023-statements-fix
Browse files Browse the repository at this point in the history
2023 slow statements fix
  • Loading branch information
fearoffish committed Dec 7, 2023
2 parents a6c08c3 + 047af3e commit 3c83877
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 21 deletions.
88 changes: 87 additions & 1 deletion apiserver/server_billable_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package apiserver

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"

"github.com/alphagov/paas-billing/apiserver/auth"
"github.com/alphagov/paas-billing/eventio"
Expand Down Expand Up @@ -49,12 +53,13 @@ func BillableEventsHandler(store eventio.BillableEventReader, consolidatedStore

delim := ""
for _, monthFilter := range months {
err := func () error { // so we can use defer in-loop
err := func() error { // so we can use defer in-loop
isConsolidated, err := consolidatedStore.IsRangeConsolidated(monthFilter)
if err != nil {
return err
}
var rows eventio.BillableEventRows

if isConsolidated {
rows, err = consolidatedStore.GetConsolidatedBillableEventRows(storeCtx, monthFilter)
} else {
Expand All @@ -65,9 +70,70 @@ func BillableEventsHandler(store eventio.BillableEventReader, consolidatedStore
}
defer rows.Close()

// Assume rows is a slice of event data
taskEvents := make(map[string]*eventio.BillableEvent)

next := rows.Next()
for next {
b, err := rows.EventJSON()

// Check if the resource type is "task"
row, err := rows.Event()

if row != nil && row.ResourceType == "task" {
// Set the key as a combination of Org GUID and Space GUID
key := fmt.Sprintf("%s-%s", row.OrgGUID, row.SpaceGUID)

// Convert the price values to float
priceInc, _ := strconv.ParseFloat(row.Price.IncVAT, 64)
priceEx, _ := strconv.ParseFloat(row.Price.ExVAT, 64)

event, exists := taskEvents[key]
if !exists {
const layout = "2006-01-02"

rangeStart, _ := time.Parse(layout, c.QueryParam("range_start"))
rangeStop, _ := time.Parse(layout, c.QueryParam("range_stop"))

event = &eventio.BillableEvent{
EventGUID: row.EventGUID,
EventStart: rangeStart.Format("2006-01-02T00:00:00+00:00"),
EventStop: rangeStop.Format("2006-01-02T00:00:00+00:00"),
ResourceGUID: row.ResourceGUID,
ResourceName: "Total Task Events",
ResourceType: "task",
OrgGUID: row.OrgGUID,
OrgName: row.OrgName,
SpaceGUID: row.SpaceGUID,
SpaceName: row.SpaceName,
PlanGUID: row.PlanGUID,
PlanName: row.PlanName,
QuotaDefinitionGUID: row.QuotaDefinitionGUID,
Price: eventio.Price{
Details: []eventio.PriceComponent{{
Name: "All tasks aggregated",
PlanName: "tasks",
Start: rangeStart.Format("2006-01-02T00:00:00+00:00"),
Stop: rangeStop.Format("2006-01-02T00:00:00+00:00"),
CurrencyCode: "USD",
VatRate: "0.2",
}},
FloatIncVAT: priceInc,
FloatExVAT: priceEx,
},
}
taskEvents[key] = event
} else {
// Add this priceInc to event.Price.IncVAT
event.Price.FloatIncVAT = event.Price.FloatIncVAT + priceInc
event.Price.FloatExVAT = event.Price.FloatExVAT + priceEx
}

// Skip the event as we will group them all into one event at the end
next = rows.Next()
continue
}

if err != nil {
return err
}
Expand All @@ -88,6 +154,26 @@ func BillableEventsHandler(store eventio.BillableEventReader, consolidatedStore
next = rows.Next()
c.Response().Flush()
}
// loop over each task event and send it
for _, event := range taskEvents {
event.Price.IncVAT = fmt.Sprintf("%.16f", event.Price.FloatIncVAT)
event.Price.ExVAT = fmt.Sprintf("%.16f", event.Price.FloatExVAT)
event.Price.Details[0].IncVAT = fmt.Sprintf("%.16f", event.Price.FloatIncVAT)
event.Price.Details[0].ExVAT = fmt.Sprintf("%.16f", event.Price.FloatExVAT)
b, err := json.Marshal(event)
if err != nil {
return err
}
// send the delimiter
if _, err := c.Response().Write([]byte(",\n")); err != nil {
return err
}
if _, err := c.Response().Write(b); err != nil {
return err
}
c.Response().Flush()
}

return nil
}()
if err != nil {
Expand Down
Loading

0 comments on commit 3c83877

Please sign in to comment.