mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-26 06:18:46 +00:00
* project quota added
* project quota removed
* add periods table
* make log record generic
* accumulate usage
* query usage
* count action run seconds
* fix filter in ReportQuotaUsage
* fix existing tests
* fix logstore tests
* fix typo
* fix: add quota unit tests command side
* fix: add quota unit tests command side
* fix: add quota unit tests command side
* move notifications into debouncer and improve limit querying
* cleanup
* comment
* fix: add quota unit tests command side
* fix remaining quota usage query
* implement InmemLogStorage
* cleanup and linting
* improve test
* fix: add quota unit tests command side
* fix: add quota unit tests command side
* fix: add quota unit tests command side
* fix: add quota unit tests command side
* action notifications and fixes for notifications query
* revert console prefix
* fix: add quota unit tests command side
* fix: add quota integration tests
* improve accountable requests
* improve accountable requests
* fix: add quota integration tests
* fix: add quota integration tests
* fix: add quota integration tests
* comment
* remove ability to store logs in db and other changes requested from review
* changes requested from review
* changes requested from review
* Update internal/api/http/middleware/access_interceptor.go
Co-authored-by: Silvan <[email protected]>
* tests: fix quotas integration tests
* improve incrementUsageStatement
* linting
* fix: delete e2e tests as intergation tests cover functionality
* Update internal/api/http/middleware/access_interceptor.go
Co-authored-by: Silvan <[email protected]>
* backup
* fix conflict
* create rc
* create prerelease
* remove issue release labeling
* fix tracing
---------
Co-authored-by: Livio Spring <[email protected]>
Co-authored-by: Stefan Benz <[email protected]>
Co-authored-by: adlerhurst <[email protected]>
(cherry picked from commit 1a49b7d298)
87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
|
zitadel_errors "github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
)
|
|
|
|
var (
|
|
quotaPeriodsTable = table{
|
|
name: projection.QuotaPeriodsProjectionTable,
|
|
instanceIDCol: projection.QuotaColumnInstanceID,
|
|
}
|
|
QuotaPeriodColumnInstanceID = Column{
|
|
name: projection.QuotaPeriodColumnInstanceID,
|
|
table: quotaPeriodsTable,
|
|
}
|
|
QuotaPeriodColumnUnit = Column{
|
|
name: projection.QuotaPeriodColumnUnit,
|
|
table: quotaPeriodsTable,
|
|
}
|
|
QuotaPeriodColumnStart = Column{
|
|
name: projection.QuotaPeriodColumnStart,
|
|
table: quotaPeriodsTable,
|
|
}
|
|
QuotaPeriodColumnUsage = Column{
|
|
name: projection.QuotaPeriodColumnUsage,
|
|
table: quotaPeriodsTable,
|
|
}
|
|
)
|
|
|
|
func (q *Queries) GetRemainingQuotaUsage(ctx context.Context, instanceID string, unit quota.Unit) (remaining *uint64, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
stmt, scan := prepareRemainingQuotaUsageQuery(ctx, q.client)
|
|
query, args, err := stmt.Where(
|
|
sq.And{
|
|
sq.Eq{
|
|
QuotaPeriodColumnInstanceID.identifier(): instanceID,
|
|
QuotaPeriodColumnUnit.identifier(): unit,
|
|
QuotaColumnLimit.identifier(): true,
|
|
},
|
|
sq.Expr("age(" + QuotaPeriodColumnStart.identifier() + ") < " + QuotaColumnInterval.identifier()),
|
|
sq.Expr(QuotaPeriodColumnStart.identifier() + " < now()"),
|
|
}).
|
|
ToSql()
|
|
if err != nil {
|
|
return nil, zitadel_errors.ThrowInternal(err, "QUERY-FSA3g", "Errors.Query.SQLStatement")
|
|
}
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
remaining, err = scan(row)
|
|
return err
|
|
}, query, args...)
|
|
if zitadel_errors.IsNotFound(err) {
|
|
return nil, nil
|
|
}
|
|
return remaining, err
|
|
}
|
|
|
|
func prepareRemainingQuotaUsageQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*uint64, error)) {
|
|
return sq.
|
|
Select(
|
|
"greatest(0, " + QuotaColumnAmount.identifier() + "-" + QuotaPeriodColumnUsage.identifier() + ")",
|
|
).
|
|
From(quotaPeriodsTable.identifier()).
|
|
Join(join(QuotaColumnUnit, QuotaPeriodColumnUnit) + db.Timetravel(call.Took(ctx))).
|
|
PlaceholderFormat(sq.Dollar), func(row *sql.Row) (*uint64, error) {
|
|
usage := new(uint64)
|
|
err := row.Scan(usage)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, zitadel_errors.ThrowNotFound(err, "QUERY-quiowi2", "Errors.Internal")
|
|
}
|
|
return nil, zitadel_errors.ThrowInternal(err, "QUERY-81j1jn2", "Errors.Internal")
|
|
}
|
|
return usage, nil
|
|
}
|
|
}
|