2023-02-15 01:52:11 +00:00
|
|
|
package execution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-22 10:49:22 +00:00
|
|
|
"database/sql"
|
2023-09-15 14:58:45 +00:00
|
|
|
"errors"
|
|
|
|
"math"
|
2023-02-15 01:52:11 +00:00
|
|
|
"time"
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/logstore"
|
2023-09-15 14:58:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/logstore/record"
|
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
|
|
|
)
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
var _ logstore.UsageStorer[*record.ExecutionLog] = (*databaseLogStorage)(nil)
|
2023-02-15 01:52:11 +00:00
|
|
|
|
|
|
|
type databaseLogStorage struct {
|
2023-02-27 21:36:43 +00:00
|
|
|
dbClient *database.DB
|
2023-09-15 14:58:45 +00:00
|
|
|
commands *command.Commands
|
|
|
|
queries *query.Queries
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func NewDatabaseLogStorage(dbClient *database.DB, commands *command.Commands, queries *query.Queries) *databaseLogStorage {
|
|
|
|
return &databaseLogStorage{dbClient: dbClient, commands: commands, queries: queries}
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *databaseLogStorage) QuotaUnit() quota.Unit {
|
|
|
|
return quota.ActionsAllRunsSeconds
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (l *databaseLogStorage) Emit(ctx context.Context, bulk []*record.ExecutionLog) error {
|
2023-03-01 17:05:12 +00:00
|
|
|
if len(bulk) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
return l.incrementUsage(ctx, bulk)
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (l *databaseLogStorage) incrementUsage(ctx context.Context, bulk []*record.ExecutionLog) (err error) {
|
|
|
|
byInstance := make(map[string][]*record.ExecutionLog)
|
|
|
|
for _, r := range bulk {
|
|
|
|
if r.InstanceID != "" {
|
|
|
|
byInstance[r.InstanceID] = append(byInstance[r.InstanceID], r)
|
|
|
|
}
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
for instanceID, instanceBulk := range byInstance {
|
|
|
|
q, getQuotaErr := l.queries.GetQuota(ctx, instanceID, quota.ActionsAllRunsSeconds)
|
|
|
|
if errors.Is(getQuotaErr, sql.ErrNoRows) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = errors.Join(err, getQuotaErr)
|
|
|
|
if getQuotaErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sum, incrementErr := l.incrementUsageFromExecutionLogs(ctx, instanceID, q.CurrentPeriodStart, instanceBulk)
|
|
|
|
err = errors.Join(err, incrementErr)
|
|
|
|
if incrementErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-15 01:52:11 +00:00
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
notifications, getNotificationErr := l.queries.GetDueQuotaNotifications(ctx, instanceID, quota.ActionsAllRunsSeconds, q, q.CurrentPeriodStart, sum)
|
|
|
|
err = errors.Join(err, getNotificationErr)
|
|
|
|
if getNotificationErr != nil || len(notifications) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ctx = authz.WithInstanceID(ctx, instanceID)
|
|
|
|
reportErr := l.commands.ReportQuotaUsage(ctx, notifications)
|
|
|
|
err = errors.Join(err, reportErr)
|
|
|
|
if reportErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
return err
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (l *databaseLogStorage) incrementUsageFromExecutionLogs(ctx context.Context, instanceID string, periodStart time.Time, records []*record.ExecutionLog) (sum uint64, err error) {
|
|
|
|
var total time.Duration
|
|
|
|
for _, r := range records {
|
|
|
|
total += r.Took
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
return projection.QuotaProjection.IncrementUsage(ctx, quota.ActionsAllRunsSeconds, instanceID, periodStart, uint64(math.Floor(total.Seconds())))
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|