2023-02-27 22:36:43 +01:00
|
|
|
package query
|
2023-02-15 02:52:11 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
|
|
|
)
|
|
|
|
|
2023-03-29 00:09:06 +02:00
|
|
|
func (q *Queries) GetDueQuotaNotifications(ctx context.Context, config *quota.AddedEvent, periodStart time.Time, usedAbs uint64) ([]*quota.NotificationDueEvent, error) {
|
2023-02-15 02:52:11 +01:00
|
|
|
if len(config.Notifications) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
aggregate := config.Aggregate()
|
2023-02-27 22:36:43 +01:00
|
|
|
wm, err := q.getQuotaNotificationsReadModel(ctx, aggregate, periodStart)
|
2023-02-15 02:52:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
usedRel := uint16(math.Floor(float64(usedAbs*100) / float64(config.Amount)))
|
|
|
|
|
2023-03-29 00:09:06 +02:00
|
|
|
var dueNotifications []*quota.NotificationDueEvent
|
2023-02-15 02:52:11 +01:00
|
|
|
for _, notification := range config.Notifications {
|
|
|
|
if notification.Percent > usedRel {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold := notification.Percent
|
|
|
|
if notification.Repeat {
|
2023-03-29 00:09:06 +02:00
|
|
|
threshold = uint16(math.Max(1, math.Floor(float64(usedRel)/float64(notification.Percent)))) * notification.Percent
|
2023-02-15 02:52:11 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 00:09:06 +02:00
|
|
|
if wm.latestDueThresholds[notification.ID] < threshold {
|
2023-02-15 02:52:11 +01:00
|
|
|
dueNotifications = append(
|
|
|
|
dueNotifications,
|
2023-03-29 00:09:06 +02:00
|
|
|
quota.NewNotificationDueEvent(
|
2023-02-15 02:52:11 +01:00
|
|
|
ctx,
|
|
|
|
&aggregate,
|
|
|
|
config.Unit,
|
|
|
|
notification.ID,
|
|
|
|
notification.CallURL,
|
|
|
|
periodStart,
|
|
|
|
threshold,
|
|
|
|
usedAbs,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dueNotifications, nil
|
|
|
|
}
|
|
|
|
|
2023-02-27 22:36:43 +01:00
|
|
|
func (q *Queries) getQuotaNotificationsReadModel(ctx context.Context, aggregate eventstore.Aggregate, periodStart time.Time) (*quotaNotificationsReadModel, error) {
|
|
|
|
wm := newQuotaNotificationsReadModel(aggregate.ID, aggregate.InstanceID, aggregate.ResourceOwner, periodStart)
|
|
|
|
return wm, q.eventstore.FilterToQueryReducer(ctx, wm)
|
2023-02-15 02:52:11 +01:00
|
|
|
}
|