zitadel/internal/query/quota_period.go
Silvan e38abdcdf3
perf: query data AS OF SYSTEM TIME (#5231)
Queries the data in the storage layser at the timestamp when the call hit the API layer
2023-02-27 22:36:43 +01:00

31 lines
922 B
Go

package query
import (
"context"
"time"
"github.com/zitadel/zitadel/internal/repository/quota"
)
func (q *Queries) GetCurrentQuotaPeriod(ctx context.Context, instanceID string, unit quota.Unit) (*quota.AddedEvent, time.Time, error) {
rm, err := q.getQuotaReadModel(ctx, instanceID, instanceID, unit)
if err != nil || !rm.active {
return nil, time.Time{}, err
}
return rm.config, pushPeriodStart(rm.config.From, rm.config.ResetInterval, time.Now()), nil
}
func pushPeriodStart(from time.Time, interval time.Duration, now time.Time) time.Time {
next := from.Add(interval)
if next.After(now) {
return from
}
return pushPeriodStart(next, interval, now)
}
func (q *Queries) getQuotaReadModel(ctx context.Context, instanceId, resourceOwner string, unit quota.Unit) (*quotaReadModel, error) {
rm := newQuotaReadModel(instanceId, resourceOwner, unit)
return rm, q.eventstore.FilterToQueryReducer(ctx, rm)
}