| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package customcost
- import (
- "context"
- "fmt"
- "sync"
- "time"
- "github.com/opencost/opencost/core/pkg/opencost"
- "github.com/opencost/opencost/core/pkg/util/timeutil"
- )
- type RepositoryQuerier struct {
- hourlyRepo Repository
- dailyRepo Repository
- hourlyDuration time.Duration
- dailyDuration time.Duration
- }
- func NewRepositoryQuerier(hourlyRepo, dailyRepo Repository, hourlyDuration, dailyDuration time.Duration) *RepositoryQuerier {
- return &RepositoryQuerier{
- hourlyRepo: hourlyRepo,
- dailyRepo: dailyRepo,
- hourlyDuration: hourlyDuration,
- dailyDuration: dailyDuration,
- }
- }
- func (rq *RepositoryQuerier) QueryTotal(ctx context.Context, request CostTotalRequest) (*CostResponse, error) {
- repo := rq.dailyRepo
- step := timeutil.Day
- if request.Accumulate == opencost.AccumulateOptionHour {
- repo = rq.hourlyRepo
- step = time.Hour
- }
- domains, err := repo.Keys()
- if err != nil {
- return nil, fmt.Errorf("QueryTotal: %w", err)
- }
- requestWindow := opencost.NewClosedWindow(request.Start, request.End)
- ccs := NewCustomCostSet(requestWindow)
- queryStart := request.Start
- for queryStart.Before(request.End) {
- queryEnd := queryStart.Add(step)
- for _, domain := range domains {
- ccResponse, err := repo.Get(queryStart, domain)
- if err != nil {
- return nil, fmt.Errorf("QueryTotal: %w", err)
- } else if ccResponse == nil || ccResponse.Start == nil || ccResponse.End == nil {
- continue
- }
- customCosts := ParseCustomCostResponse(ccResponse)
- ccs.Add(customCosts)
- }
- queryStart = queryEnd
- }
- err = ccs.Aggregate(request.AggregateBy)
- if err != nil {
- return nil, err
- }
- return NewCostResponse(ccs), nil
- }
- func (rq *RepositoryQuerier) QueryTimeseries(ctx context.Context, request CostTimeseriesRequest) (*CostTimeseriesResponse, error) {
- window, _ := opencost.NewClosedWindow(request.Start, request.End).GetAccumulateWindow(request.Accumulate)
- windows, err := window.GetAccumulateWindows(request.Accumulate)
- if err != nil {
- return nil, fmt.Errorf("error getting timeseries windows: %w", err)
- }
- totals := make([]*CostResponse, len(windows))
- errors := make([]error, len(windows))
- // Query concurrently for each result, error
- var wg sync.WaitGroup
- wg.Add(len(windows))
- for i, w := range windows {
- go func(i int, window opencost.Window, res []*CostResponse) {
- defer wg.Done()
- totals[i], errors[i] = rq.QueryTotal(ctx, CostTotalRequest{
- Start: *window.Start(),
- End: *window.End(),
- AggregateBy: request.AggregateBy,
- Filter: request.Filter,
- Accumulate: request.Accumulate,
- })
- }(i, w, totals)
- }
- wg.Wait()
- // Return an error if any errors occurred
- for i, err := range errors {
- if err != nil {
- return nil, fmt.Errorf("one of %d errors: error querying costs for %s: %w", numErrors(errors), windows[i], err)
- }
- }
- result := &CostTimeseriesResponse{
- Window: window,
- Timeseries: totals,
- }
- return result, nil
- }
- func numErrors(errors []error) int {
- numErrs := 0
- for i := range errors {
- if errors[i] != nil {
- numErrs++
- }
- }
- return numErrs
- }
|