2020-05-18 12:06:36 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-03-28 10:05:09 +02:00
|
|
|
"context"
|
2020-07-28 09:42:21 +02:00
|
|
|
"time"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/auth/repository/eventsourcing/view"
|
|
|
|
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
|
2022-07-22 12:08:39 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/query"
|
|
|
|
query2 "github.com/zitadel/zitadel/internal/query"
|
2022-07-22 12:08:39 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2020-05-18 12:06:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Configs map[string]*Config
|
|
|
|
|
|
|
|
type Config struct {
|
2022-02-14 17:22:30 +01:00
|
|
|
MinimumCycleDuration time.Duration
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
view *view.View
|
|
|
|
bulkLimit uint64
|
|
|
|
cycleDuration time.Duration
|
|
|
|
errorCountUntilSkip uint64
|
2020-12-18 16:47:45 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
es v1.Eventstore
|
2020-12-18 16:47:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
func (h *handler) Eventstore() v1.Eventstore {
|
2020-12-18 16:47:45 +01:00
|
|
|
return h.es
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 07:46:12 +02:00
|
|
|
func Register(ctx context.Context, configs Configs, bulkLimit, errorCount uint64, view *view.View, es v1.Eventstore, queries *query2.Queries) []query.Handler {
|
2020-07-28 09:42:21 +02:00
|
|
|
return []query.Handler{
|
2022-11-22 07:36:48 +01:00
|
|
|
newUser(ctx,
|
2022-02-16 16:49:17 +01:00
|
|
|
handler{view, bulkLimit, configs.cycleDuration("User"), errorCount, es}, queries),
|
2022-11-22 07:36:48 +01:00
|
|
|
newUserSession(ctx,
|
2022-06-03 14:37:24 +02:00
|
|
|
handler{view, bulkLimit, configs.cycleDuration("UserSession"), errorCount, es}, queries),
|
2022-11-22 07:36:48 +01:00
|
|
|
newToken(ctx,
|
2021-02-22 14:08:47 +01:00
|
|
|
handler{view, bulkLimit, configs.cycleDuration("Token"), errorCount, es}),
|
2022-11-22 07:36:48 +01:00
|
|
|
newRefreshToken(ctx, handler{view, bulkLimit, configs.cycleDuration("RefreshToken"), errorCount, es}),
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (configs Configs) cycleDuration(viewModel string) time.Duration {
|
|
|
|
c, ok := configs[viewModel]
|
|
|
|
if !ok {
|
2020-12-18 16:47:45 +01:00
|
|
|
return 3 * time.Minute
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
2022-02-14 17:22:30 +01:00
|
|
|
return c.MinimumCycleDuration
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
2020-07-28 09:42:21 +02:00
|
|
|
|
|
|
|
func (h *handler) MinimumCycleDuration() time.Duration {
|
|
|
|
return h.cycleDuration
|
|
|
|
}
|
|
|
|
|
2020-12-22 12:27:55 +01:00
|
|
|
func (h *handler) LockDuration() time.Duration {
|
|
|
|
return h.cycleDuration / 3
|
|
|
|
}
|
|
|
|
|
2020-07-28 09:42:21 +02:00
|
|
|
func (h *handler) QueryLimit() uint64 {
|
|
|
|
return h.bulkLimit
|
|
|
|
}
|
2022-03-28 10:05:09 +02:00
|
|
|
|
|
|
|
func withInstanceID(ctx context.Context, instanceID string) context.Context {
|
2022-03-29 11:53:19 +02:00
|
|
|
return authz.WithInstanceID(ctx, instanceID)
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
2022-07-22 12:08:39 +02:00
|
|
|
|
|
|
|
func newSearchQuery(sequences []*repository.CurrentSequence, aggregateTypes []models.AggregateType, instanceIDs []string) *models.SearchQuery {
|
|
|
|
searchQuery := models.NewSearchQuery()
|
2022-11-22 07:36:48 +01:00
|
|
|
for _, instanceID := range instanceIDs {
|
2022-07-22 12:08:39 +02:00
|
|
|
var seq uint64
|
2022-11-22 07:36:48 +01:00
|
|
|
for _, sequence := range sequences {
|
2022-07-22 12:08:39 +02:00
|
|
|
if sequence.InstanceID == instanceID {
|
|
|
|
seq = sequence.CurrentSequence
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
searchQuery.AddQuery().
|
|
|
|
AggregateTypeFilter(aggregateTypes...).
|
|
|
|
LatestSequenceFilter(seq).
|
2022-11-22 07:36:48 +01:00
|
|
|
InstanceIDFilter(instanceID)
|
2022-07-22 12:08:39 +02:00
|
|
|
}
|
|
|
|
return searchQuery
|
|
|
|
}
|