From 1d0b4c9ef575d120070990712902eca38bb4e5a5 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 14:44:27 +0300 Subject: [PATCH 01/25] test version early test event --- internal/cli/event.go | 1 + internal/service/event/main.go | 1 + 2 files changed, 2 insertions(+) create mode 100644 internal/cli/event.go create mode 100644 internal/service/event/main.go diff --git a/internal/cli/event.go b/internal/cli/event.go new file mode 100644 index 0000000..7f1e458 --- /dev/null +++ b/internal/cli/event.go @@ -0,0 +1 @@ +package cli diff --git a/internal/service/event/main.go b/internal/service/event/main.go new file mode 100644 index 0000000..0e4b82e --- /dev/null +++ b/internal/service/event/main.go @@ -0,0 +1 @@ +package event From 4796ccb3b79da47dd14add4cabdc6232309b4d67 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 14:55:03 +0300 Subject: [PATCH 02/25] first test version --- internal/cli/event.go | 14 +++++ internal/cli/main.go | 6 +++ internal/data/balances.go | 2 + internal/data/evtypes/models/extra.go | 1 + internal/data/pg/balances.go | 8 +++ internal/service/event/main.go | 54 +++++++++++++++++++ internal/service/workers/leveljustice/main.go | 1 - 7 files changed, 85 insertions(+), 1 deletion(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index 7f1e458..98d921a 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -1 +1,15 @@ package cli + +import ( + "log" + + "github.com/rarimo/geo-points-svc/internal/config" + "github.com/rarimo/geo-points-svc/internal/service/event" +) + +func eventStart(cfg config.Config, data int) { + err := event.Run(cfg, data) + if err != nil { + log.Fatalf("Error starting event: %v", err) + } +} diff --git a/internal/cli/main.go b/internal/cli/main.go index 4d7a177..4b09860 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -29,6 +29,10 @@ func Run(args []string) bool { migrateCmd = app.Command("migrate", "migrate command") migrateUpCmd = migrateCmd.Command("up", "migrate db up") migrateDownCmd = migrateCmd.Command("down", "migrate db down") + + event = app.Command("event", "claim event command") + eventCmd = event.Command("start", "claim event command") + data = eventCmd.Arg("data", "data after ...").Required().Int() ) cmd, err := app.Parse(args[1:]) @@ -48,6 +52,8 @@ func Run(args []string) bool { err = MigrateUp(cfg) case migrateDownCmd.FullCommand(): err = MigrateDown(cfg) + case eventCmd.FullCommand(): + eventStart(cfg, *data) default: log.Errorf("unknown command %s", cmd) return false diff --git a/internal/data/balances.go b/internal/data/balances.go index 76bf4e4..6556109 100644 --- a/internal/data/balances.go +++ b/internal/data/balances.go @@ -54,6 +54,8 @@ type BalancesQ interface { FilterByInternalAID(aid string) BalancesQ FilterByExternalAID(aid string) BalancesQ FilterBySharedHash(hash string) BalancesQ + FilterByCreatedBefore(time int) BalancesQ + FilterVerified() BalancesQ } type WithoutPassportEventBalance struct { diff --git a/internal/data/evtypes/models/extra.go b/internal/data/evtypes/models/extra.go index 3b794e8..3b7991f 100644 --- a/internal/data/evtypes/models/extra.go +++ b/internal/data/evtypes/models/extra.go @@ -21,6 +21,7 @@ const ( TypeInternalPassportScan = "internal_passport_scan" TypeExternalPassportScan = "external_passport_scan" TypePollParticipation = "poll_participation" + TypeEarlyTest = "early_test" ) const ( diff --git a/internal/data/pg/balances.go b/internal/data/pg/balances.go index 44a0172..c6f4d1a 100644 --- a/internal/data/pg/balances.go +++ b/internal/data/pg/balances.go @@ -222,6 +222,10 @@ func (q *balances) FilterBySharedHash(hash string) data.BalancesQ { return q.applyCondition(squirrel.Eq{"shared_hash": hash}) } +func (q *balances) FilterByCreatedBefore(data int) data.BalancesQ { + return q.applyCondition(squirrel.Lt{"created_at": data}) +} + func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ { q.selector = q.selector.Where(cond) q.updater = q.updater.Where(cond) @@ -229,3 +233,7 @@ func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ { q.counter = q.counter.Where(cond) return q } + +func (q *balances) FilterVerified() data.BalancesQ { + return q.applyCondition(squirrel.Gt{"verified": true}) +} diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 0e4b82e..e36f922 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -1 +1,55 @@ package event + +import ( + "fmt" + + "github.com/rarimo/geo-points-svc/internal/config" + "github.com/rarimo/geo-points-svc/internal/data" + "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" + "github.com/rarimo/geo-points-svc/internal/data/pg" +) + +func Run(cfg config.Config, date int) error { + log := cfg.Log() + db := cfg.DB() + + balances := pg.NewBalances(db) + events := pg.NewEvents(db) + + users, err := balances.FilterByCreatedBefore(date).FilterVerified().Select() + filtEvents := events.FilterByType(models.TypeEarlyTest) + + if err != nil { + log.WithError(err).Error("failed to filter by updated before") + return err + } + + if users == nil { + log.Infof("no users found") + return nil + } + + for _, user := range users { + userNull := user.Nullifier + eve, err := filtEvents.FilterByNullifier(userNull).Get() + + if err != nil { + log.WithError(err).Error("failed to filter by nullifier") + return err + } + if eve != nil { + break + } + + err = events.Insert(data.Event{ + Nullifier: user.Nullifier, + Type: models.TypeEarlyTest, + Status: data.EventFulfilled, + }) + if err != nil { + return fmt.Errorf("failed to insert `early_test` event: %w", err) + } + } + + return nil +} diff --git a/internal/service/workers/leveljustice/main.go b/internal/service/workers/leveljustice/main.go index e4812fc..ce60041 100644 --- a/internal/service/workers/leveljustice/main.go +++ b/internal/service/workers/leveljustice/main.go @@ -37,5 +37,4 @@ func Run(cfg config.Config, sig chan struct{}) { } sig <- struct{}{} - } From 0596710c11ee6932dfd7ee97c6f8eabaeb4002d4 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 15:47:28 +0300 Subject: [PATCH 03/25] searching for users through hash table --- internal/cli/event.go | 4 +--- internal/cli/main.go | 4 ++-- internal/data/balances.go | 2 +- internal/data/events.go | 1 + internal/data/pg/balances.go | 2 +- internal/data/pg/events.go | 7 ++++++ internal/service/event/main.go | 40 +++++++++++++++++++--------------- 7 files changed, 36 insertions(+), 24 deletions(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index 98d921a..d7c91e7 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -1,8 +1,6 @@ package cli import ( - "log" - "github.com/rarimo/geo-points-svc/internal/config" "github.com/rarimo/geo-points-svc/internal/service/event" ) @@ -10,6 +8,6 @@ import ( func eventStart(cfg config.Config, data int) { err := event.Run(cfg, data) if err != nil { - log.Fatalf("Error starting event: %v", err) + cfg.Log().Error("Error starting event: %v", err) } } diff --git a/internal/cli/main.go b/internal/cli/main.go index 4b09860..4b5766c 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -31,8 +31,8 @@ func Run(args []string) bool { migrateDownCmd = migrateCmd.Command("down", "migrate db down") event = app.Command("event", "claim event command") - eventCmd = event.Command("start", "claim event command") - data = eventCmd.Arg("data", "data after ...").Required().Int() + eventCmd = event.Command("create-early-test", "claim event command") + data = eventCmd.Arg("-before", "data after ...").Required().Int() ) cmd, err := app.Parse(args[1:]) diff --git a/internal/data/balances.go b/internal/data/balances.go index 6556109..fc47673 100644 --- a/internal/data/balances.go +++ b/internal/data/balances.go @@ -54,7 +54,7 @@ type BalancesQ interface { FilterByInternalAID(aid string) BalancesQ FilterByExternalAID(aid string) BalancesQ FilterBySharedHash(hash string) BalancesQ - FilterByCreatedBefore(time int) BalancesQ + FilterByCreatedBefore(date int) BalancesQ FilterVerified() BalancesQ } diff --git a/internal/data/events.go b/internal/data/events.go index 669de0a..1b8b9ce 100644 --- a/internal/data/events.go +++ b/internal/data/events.go @@ -65,4 +65,5 @@ type EventsQ interface { FilterByUpdatedAtBefore(int64) EventsQ FilterByExternalID(string) EventsQ FilterInactiveNotClaimed(types ...string) EventsQ + FilterByNullifiers(nullifiers []string) EventsQ } diff --git a/internal/data/pg/balances.go b/internal/data/pg/balances.go index c6f4d1a..318ab56 100644 --- a/internal/data/pg/balances.go +++ b/internal/data/pg/balances.go @@ -235,5 +235,5 @@ func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ { } func (q *balances) FilterVerified() data.BalancesQ { - return q.applyCondition(squirrel.Gt{"verified": true}) + return q.applyCondition(squirrel.Gt{"is_verified": true}) } diff --git a/internal/data/pg/events.go b/internal/data/pg/events.go index 4e6ada9..7218b00 100644 --- a/internal/data/pg/events.go +++ b/internal/data/pg/events.go @@ -197,6 +197,13 @@ func (q *events) FilterByNullifier(nullifier string) data.EventsQ { return q.applyCondition(squirrel.Eq{"nullifier": nullifier}) } +func (q *events) FilterByNullifiers(nullifiers []string) data.EventsQ { + if len(nullifiers) == 0 { + return q + } + return q.applyCondition(squirrel.Eq{"nullifier": nullifiers}) +} + func (q *events) FilterByStatus(statuses ...data.EventStatus) data.EventsQ { if len(statuses) == 0 { return q diff --git a/internal/service/event/main.go b/internal/service/event/main.go index e36f922..bedeb2b 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -17,7 +17,6 @@ func Run(cfg config.Config, date int) error { events := pg.NewEvents(db) users, err := balances.FilterByCreatedBefore(date).FilterVerified().Select() - filtEvents := events.FilterByType(models.TypeEarlyTest) if err != nil { log.WithError(err).Error("failed to filter by updated before") @@ -29,25 +28,32 @@ func Run(cfg config.Config, date int) error { return nil } + var userNullifiers []string for _, user := range users { - userNull := user.Nullifier - eve, err := filtEvents.FilterByNullifier(userNull).Get() + userNullifiers = append(userNullifiers, user.Nullifier) + } - if err != nil { - log.WithError(err).Error("failed to filter by nullifier") - return err - } - if eve != nil { - break - } + existingEvents, err := events.FilterByType(models.TypeEarlyTest).FilterByNullifiers(userNullifiers).Select() + if err != nil { + log.WithError(err).Error("failed to filter events") + return err + } + + existingEventsMap := make(map[string]data.Event) + for _, event := range existingEvents { + existingEventsMap[event.Nullifier] = event + } - err = events.Insert(data.Event{ - Nullifier: user.Nullifier, - Type: models.TypeEarlyTest, - Status: data.EventFulfilled, - }) - if err != nil { - return fmt.Errorf("failed to insert `early_test` event: %w", err) + for _, user := range users { + if _, exists := existingEventsMap[user.Nullifier]; !exists { + err = events.Insert(data.Event{ + Nullifier: user.Nullifier, + Type: models.TypeEarlyTest, + Status: data.EventFulfilled, + }) + if err != nil { + return fmt.Errorf("failed to insert `early_test` event: %w", err) + } } } From c98de532217cc8b4b85bb5b4da3160c4cde69344 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 16:10:04 +0300 Subject: [PATCH 04/25] rework FilterVerified --- internal/cli/event.go | 2 +- internal/cli/main.go | 2 +- internal/data/events.go | 1 - internal/data/pg/balances.go | 5 ++++- internal/data/pg/events.go | 7 ------- internal/service/event/main.go | 14 +++++++------- 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index d7c91e7..0f93d41 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -8,6 +8,6 @@ import ( func eventStart(cfg config.Config, data int) { err := event.Run(cfg, data) if err != nil { - cfg.Log().Error("Error starting event: %v", err) + cfg.Log().Errorf("Error starting event: %s", err) } } diff --git a/internal/cli/main.go b/internal/cli/main.go index 4b5766c..c0abe01 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -32,7 +32,7 @@ func Run(args []string) bool { event = app.Command("event", "claim event command") eventCmd = event.Command("create-early-test", "claim event command") - data = eventCmd.Arg("-before", "data after ...").Required().Int() + data = eventCmd.Arg("before", "data after ...").Required().Int() ) cmd, err := app.Parse(args[1:]) diff --git a/internal/data/events.go b/internal/data/events.go index 1b8b9ce..669de0a 100644 --- a/internal/data/events.go +++ b/internal/data/events.go @@ -65,5 +65,4 @@ type EventsQ interface { FilterByUpdatedAtBefore(int64) EventsQ FilterByExternalID(string) EventsQ FilterInactiveNotClaimed(types ...string) EventsQ - FilterByNullifiers(nullifiers []string) EventsQ } diff --git a/internal/data/pg/balances.go b/internal/data/pg/balances.go index 318ab56..bc0dd4a 100644 --- a/internal/data/pg/balances.go +++ b/internal/data/pg/balances.go @@ -235,5 +235,8 @@ func (q *balances) applyCondition(cond squirrel.Sqlizer) data.BalancesQ { } func (q *balances) FilterVerified() data.BalancesQ { - return q.applyCondition(squirrel.Gt{"is_verified": true}) + return q.applyCondition(squirrel.Or{ + squirrel.NotEq{"internal_aid": nil}, + squirrel.NotEq{"external_aid": nil}, + }) } diff --git a/internal/data/pg/events.go b/internal/data/pg/events.go index 7218b00..4e6ada9 100644 --- a/internal/data/pg/events.go +++ b/internal/data/pg/events.go @@ -197,13 +197,6 @@ func (q *events) FilterByNullifier(nullifier string) data.EventsQ { return q.applyCondition(squirrel.Eq{"nullifier": nullifier}) } -func (q *events) FilterByNullifiers(nullifiers []string) data.EventsQ { - if len(nullifiers) == 0 { - return q - } - return q.applyCondition(squirrel.Eq{"nullifier": nullifiers}) -} - func (q *events) FilterByStatus(statuses ...data.EventStatus) data.EventsQ { if len(statuses) == 0 { return q diff --git a/internal/service/event/main.go b/internal/service/event/main.go index bedeb2b..2f1aec8 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -13,27 +13,27 @@ func Run(cfg config.Config, date int) error { log := cfg.Log() db := cfg.DB() - balances := pg.NewBalances(db) + balancesQ := pg.NewBalances(db) events := pg.NewEvents(db) - users, err := balances.FilterByCreatedBefore(date).FilterVerified().Select() + balances, err := balancesQ.FilterByCreatedBefore(date).FilterVerified().Select() if err != nil { log.WithError(err).Error("failed to filter by updated before") return err } - if users == nil { - log.Infof("no users found") + if len(balances) == 0 { + log.Infof("no balances found") return nil } var userNullifiers []string - for _, user := range users { + for _, user := range balances { userNullifiers = append(userNullifiers, user.Nullifier) } - existingEvents, err := events.FilterByType(models.TypeEarlyTest).FilterByNullifiers(userNullifiers).Select() + existingEvents, err := events.FilterByType(models.TypeEarlyTest).Select() if err != nil { log.WithError(err).Error("failed to filter events") return err @@ -44,7 +44,7 @@ func Run(cfg config.Config, date int) error { existingEventsMap[event.Nullifier] = event } - for _, user := range users { + for _, user := range balances { if _, exists := existingEventsMap[user.Nullifier]; !exists { err = events.Insert(data.Event{ Nullifier: user.Nullifier, From 1bcec6c8e4f2eddc5c14227e5ea43743aa995cb9 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 16:21:40 +0300 Subject: [PATCH 05/25] delete userNullifiers --- internal/service/event/main.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 2f1aec8..c13318c 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -28,11 +28,6 @@ func Run(cfg config.Config, date int) error { return nil } - var userNullifiers []string - for _, user := range balances { - userNullifiers = append(userNullifiers, user.Nullifier) - } - existingEvents, err := events.FilterByType(models.TypeEarlyTest).Select() if err != nil { log.WithError(err).Error("failed to filter events") From 55408181790d7a2594265c4c3979ab4817c17fd2 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 17:56:42 +0300 Subject: [PATCH 06/25] added autoClaimEventsForBalance to balance when using the script to reward for testing --- internal/service/event/main.go | 98 +++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 13 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index c13318c..3e5af50 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -5,16 +5,22 @@ import ( "github.com/rarimo/geo-points-svc/internal/config" "github.com/rarimo/geo-points-svc/internal/data" + "github.com/rarimo/geo-points-svc/internal/data/evtypes" "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" "github.com/rarimo/geo-points-svc/internal/data/pg" + "github.com/rarimo/geo-points-svc/internal/service/handlers" + "gitlab.com/distributed_lab/logan/v3" ) func Run(cfg config.Config, date int) error { log := cfg.Log() db := cfg.DB() + lvls := cfg.Levels() + evTypes := cfg.EventTypes() balancesQ := pg.NewBalances(db) - events := pg.NewEvents(db) + eventsQ := pg.NewEvents(db) + referralsQ := pg.NewReferrals(db) balances, err := balancesQ.FilterByCreatedBefore(date).FilterVerified().Select() @@ -28,9 +34,9 @@ func Run(cfg config.Config, date int) error { return nil } - existingEvents, err := events.FilterByType(models.TypeEarlyTest).Select() + existingEvents, err := eventsQ.FilterByType(models.TypeEarlyTest).Select() if err != nil { - log.WithError(err).Error("failed to filter events") + log.WithError(err).Error("failed to filter eventsQ") return err } @@ -39,17 +45,83 @@ func Run(cfg config.Config, date int) error { existingEventsMap[event.Nullifier] = event } - for _, user := range balances { - if _, exists := existingEventsMap[user.Nullifier]; !exists { - err = events.Insert(data.Event{ - Nullifier: user.Nullifier, - Type: models.TypeEarlyTest, - Status: data.EventFulfilled, - }) - if err != nil { - return fmt.Errorf("failed to insert `early_test` event: %w", err) - } + for _, balance := range balances { + if _, exists := existingEventsMap[balance.Nullifier]; exists { + continue } + err = eventsQ.Insert(data.Event{ + Nullifier: balance.Nullifier, + Type: models.TypeEarlyTest, + Status: data.EventFulfilled, + }) + if err != nil { + return fmt.Errorf("failed to insert `early_test` event: %w", err) + } + err = autoClaimEventsForBalance(*log, eventsQ, balancesQ, &balance, lvls, referralsQ, *evTypes) + if err != nil { + return fmt.Errorf("failed to auto-claim eventsQ: %w", err) + } + } + + return nil +} + +func autoClaimEventsForBalance( + log logan.Entry, eventsQ data.EventsQ, balanceQ data.BalancesQ, balance *data.Balance, + lvls *config.Levels, referralsQ data.ReferralsQ, evTypes evtypes.Types) error { + + if balance == nil { + log.Debug("Balance absent. Events not claimed.") + return nil + } + + if balance.IsDisabled() || !balance.IsVerified() { + log.Debug("User not eligible for event claiming. Events not claimed.") + return nil + } + + var totalPoints int64 + eventsToClaim, err := eventsQ. + FilterByNullifier(balance.Nullifier). + FilterByStatus(data.EventFulfilled). + Select() + if err != nil { + return fmt.Errorf("failed to select events for user=%s: %w", balance.Nullifier, err) + } + + eventsMap := map[string][]string{} + for _, e := range eventsToClaim { + if _, ok := eventsMap[e.Type]; !ok { + eventsMap[e.Type] = []string{} + } + eventsMap[e.Type] = append(eventsMap[e.Type], e.ID) + } + + for evName, evIDs := range eventsMap { + evType := evTypes.Get(evName, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true)) + if evType == nil { + continue + } + + _, err = eventsQ.FilterByID(evIDs...).Update(data.EventClaimed, nil, &evType.Reward) + if err != nil { + return fmt.Errorf("failedt to update %s events for user=%s: %w", evName, balance.Nullifier, err) + } + + totalPoints += evType.Reward * int64(len(evIDs)) + } + + level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, balance, totalPoints) + if err != nil { + return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) + } + + err = balanceQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{ + data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), + data.ColLevel: level, + }) + if err != nil { + return fmt.Errorf("update balance amount and level: %w", err) } return nil From ead796eaac20e4e9a11814c97da0acfb0c036272 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 18:13:22 +0300 Subject: [PATCH 07/25] rework func event.autoClaimEventsForBalance --- internal/service/event/main.go | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 3e5af50..fcfd964 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -9,7 +9,6 @@ import ( "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" "github.com/rarimo/geo-points-svc/internal/data/pg" "github.com/rarimo/geo-points-svc/internal/service/handlers" - "gitlab.com/distributed_lab/logan/v3" ) func Run(cfg config.Config, date int) error { @@ -57,7 +56,7 @@ func Run(cfg config.Config, date int) error { if err != nil { return fmt.Errorf("failed to insert `early_test` event: %w", err) } - err = autoClaimEventsForBalance(*log, eventsQ, balancesQ, &balance, lvls, referralsQ, *evTypes) + err = autoClaimEventsForBalance(eventsQ, balancesQ, &balance, lvls, referralsQ, *evTypes) if err != nil { return fmt.Errorf("failed to auto-claim eventsQ: %w", err) } @@ -67,33 +66,17 @@ func Run(cfg config.Config, date int) error { } func autoClaimEventsForBalance( - log logan.Entry, eventsQ data.EventsQ, balanceQ data.BalancesQ, balance *data.Balance, + eventsQ data.EventsQ, balanceQ data.BalancesQ, balance *data.Balance, lvls *config.Levels, referralsQ data.ReferralsQ, evTypes evtypes.Types) error { - if balance == nil { - log.Debug("Balance absent. Events not claimed.") - return nil - } - - if balance.IsDisabled() || !balance.IsVerified() { - log.Debug("User not eligible for event claiming. Events not claimed.") - return nil - } - var totalPoints int64 - eventsToClaim, err := eventsQ. - FilterByNullifier(balance.Nullifier). - FilterByStatus(data.EventFulfilled). - Select() + eventsToClaim, err := eventsQ.FilterByStatus(data.EventFulfilled).Select() if err != nil { return fmt.Errorf("failed to select events for user=%s: %w", balance.Nullifier, err) } eventsMap := map[string][]string{} for _, e := range eventsToClaim { - if _, ok := eventsMap[e.Type]; !ok { - eventsMap[e.Type] = []string{} - } eventsMap[e.Type] = append(eventsMap[e.Type], e.ID) } @@ -121,7 +104,7 @@ func autoClaimEventsForBalance( data.ColLevel: level, }) if err != nil { - return fmt.Errorf("update balance amount and level: %w", err) + return fmt.Errorf("error update balance amount and level: %w", err) } return nil From 966e88a5349fc0d9d3f67f3704feff99e1192109 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 6 Aug 2024 19:11:16 +0300 Subject: [PATCH 08/25] rework event.Run --- internal/data/events.go | 2 +- internal/data/pg/events.go | 2 +- internal/service/event/main.go | 83 ++++++++++++++++------------------ 3 files changed, 41 insertions(+), 46 deletions(-) diff --git a/internal/data/events.go b/internal/data/events.go index 669de0a..ccfa9b9 100644 --- a/internal/data/events.go +++ b/internal/data/events.go @@ -58,7 +58,7 @@ type EventsQ interface { SelectAbsentTypes(allTypes ...string) ([]ReopenableEvent, error) FilterByID(...string) EventsQ - FilterByNullifier(string) EventsQ + FilterByNullifier(...string) EventsQ FilterByStatus(...EventStatus) EventsQ FilterByType(...string) EventsQ FilterByNotType(types ...string) EventsQ diff --git a/internal/data/pg/events.go b/internal/data/pg/events.go index 4e6ada9..4750faa 100644 --- a/internal/data/pg/events.go +++ b/internal/data/pg/events.go @@ -193,7 +193,7 @@ func (q *events) FilterByID(ids ...string) data.EventsQ { return q.applyCondition(squirrel.Eq{"id": ids}) } -func (q *events) FilterByNullifier(nullifier string) data.EventsQ { +func (q *events) FilterByNullifier(nullifier ...string) data.EventsQ { return q.applyCondition(squirrel.Eq{"nullifier": nullifier}) } diff --git a/internal/service/event/main.go b/internal/service/event/main.go index fcfd964..8877916 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -21,33 +21,50 @@ func Run(cfg config.Config, date int) error { eventsQ := pg.NewEvents(db) referralsQ := pg.NewReferrals(db) + evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive) + if evType == nil { + log.Infof("Event type %s is inactive", models.TypeEarlyTest) + return nil + } + balances, err := balancesQ.FilterByCreatedBefore(date).FilterVerified().Select() if err != nil { log.WithError(err).Error("failed to filter by updated before") return err } - if len(balances) == 0 { log.Infof("no balances found") return nil } - existingEvents, err := eventsQ.FilterByType(models.TypeEarlyTest).Select() + var nullifiers []string + + for _, balance := range balances { + nullifiers = append(nullifiers, balance.Nullifier) + } + + filteredEvents, err := eventsQ. + FilterByType(models.TypeEarlyTest). + FilterByStatus(data.EventFulfilled). + FilterByNullifier(nullifiers...). + Select() + if err != nil { log.WithError(err).Error("failed to filter eventsQ") return err } - existingEventsMap := make(map[string]data.Event) - for _, event := range existingEvents { - existingEventsMap[event.Nullifier] = event + EventsMap := make(map[string]data.Event) + for _, event := range filteredEvents { + EventsMap[event.Nullifier] = event } for _, balance := range balances { - if _, exists := existingEventsMap[balance.Nullifier]; exists { + if _, exists := EventsMap[balance.Nullifier]; exists { continue } + err = eventsQ.Insert(data.Event{ Nullifier: balance.Nullifier, Type: models.TypeEarlyTest, @@ -56,55 +73,33 @@ func Run(cfg config.Config, date int) error { if err != nil { return fmt.Errorf("failed to insert `early_test` event: %w", err) } - err = autoClaimEventsForBalance(eventsQ, balancesQ, &balance, lvls, referralsQ, *evTypes) - if err != nil { - return fmt.Errorf("failed to auto-claim eventsQ: %w", err) - } - } - - return nil -} - -func autoClaimEventsForBalance( - eventsQ data.EventsQ, balanceQ data.BalancesQ, balance *data.Balance, - lvls *config.Levels, referralsQ data.ReferralsQ, evTypes evtypes.Types) error { - - var totalPoints int64 - eventsToClaim, err := eventsQ.FilterByStatus(data.EventFulfilled).Select() - if err != nil { - return fmt.Errorf("failed to select events for user=%s: %w", balance.Nullifier, err) - } - eventsMap := map[string][]string{} - for _, e := range eventsToClaim { - eventsMap[e.Type] = append(eventsMap[e.Type], e.ID) - } + var totalPoints int64 - for evName, evIDs := range eventsMap { - evType := evTypes.Get(evName, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true)) + evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true)) if evType == nil { continue } - _, err = eventsQ.FilterByID(evIDs...).Update(data.EventClaimed, nil, &evType.Reward) + _, err = eventsQ.Update(data.EventClaimed, nil, &evType.Reward) if err != nil { - return fmt.Errorf("failedt to update %s events for user=%s: %w", evName, balance.Nullifier, err) + return fmt.Errorf("failedt to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) } - totalPoints += evType.Reward * int64(len(evIDs)) - } + totalPoints += evType.Reward - level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, balance, totalPoints) - if err != nil { - return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) - } + level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, &balance, totalPoints) + if err != nil { + return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) + } - err = balanceQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{ - data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), - data.ColLevel: level, - }) - if err != nil { - return fmt.Errorf("error update balance amount and level: %w", err) + err = balancesQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{ + data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), + data.ColLevel: level, + }) + if err != nil { + return fmt.Errorf("error update balance amount and level: %w", err) + } } return nil From 8ec7d949e6a22cdffec71cbef38199917e7384d7 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 11:18:36 +0300 Subject: [PATCH 09/25] fix loop in events.Run --- internal/service/event/main.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 8877916..168332b 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -21,7 +21,7 @@ func Run(cfg config.Config, date int) error { eventsQ := pg.NewEvents(db) referralsQ := pg.NewReferrals(db) - evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive) + evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true)) if evType == nil { log.Infof("Event type %s is inactive", models.TypeEarlyTest) return nil @@ -38,7 +38,7 @@ func Run(cfg config.Config, date int) error { return nil } - var nullifiers []string + nullifiers := make([]string, 0) for _, balance := range balances { nullifiers = append(nullifiers, balance.Nullifier) @@ -76,12 +76,7 @@ func Run(cfg config.Config, date int) error { var totalPoints int64 - evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true)) - if evType == nil { - continue - } - - _, err = eventsQ.Update(data.EventClaimed, nil, &evType.Reward) + _, err = eventsQ.FilterByNullifier(nullifiers...).Update(data.EventClaimed, nil, &evType.Reward) if err != nil { return fmt.Errorf("failedt to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) } @@ -97,6 +92,7 @@ func Run(cfg config.Config, date int) error { data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), data.ColLevel: level, }) + if err != nil { return fmt.Errorf("error update balance amount and level: %w", err) } From e36204a959f4b059c29a066b5b69330ad0f37d6c Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 11:29:14 +0300 Subject: [PATCH 10/25] fix filteredEvents in event.Run --- internal/service/event/main.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 168332b..1fbf371 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -38,20 +38,19 @@ func Run(cfg config.Config, date int) error { return nil } - nullifiers := make([]string, 0) + nullifiers := make([]string, 0, len(balances)) for _, balance := range balances { nullifiers = append(nullifiers, balance.Nullifier) } filteredEvents, err := eventsQ. - FilterByType(models.TypeEarlyTest). FilterByStatus(data.EventFulfilled). FilterByNullifier(nullifiers...). Select() if err != nil { - log.WithError(err).Error("failed to filter eventsQ") + log.WithError(err).Error("Failed to select %s events") return err } From b303a7a70f907e57ef5ccff20728a21e053ace46 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 11:33:26 +0300 Subject: [PATCH 11/25] fix filters in event.Run --- internal/service/event/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 1fbf371..96b3063 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -21,7 +21,7 @@ func Run(cfg config.Config, date int) error { eventsQ := pg.NewEvents(db) referralsQ := pg.NewReferrals(db) - evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive, evtypes.FilterByAutoClaim(true)) + evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive) if evType == nil { log.Infof("Event type %s is inactive", models.TypeEarlyTest) return nil @@ -50,7 +50,7 @@ func Run(cfg config.Config, date int) error { Select() if err != nil { - log.WithError(err).Error("Failed to select %s events") + log.WithError(err).Errorf("Failed to select %s events", err) return err } From 67d67c51c3e301ae5637a7b11e174a9627d7d43c Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 12:11:06 +0300 Subject: [PATCH 12/25] make transaction in event.Run --- internal/service/event/main.go | 76 ++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 96b3063..747276a 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -54,47 +54,51 @@ func Run(cfg config.Config, date int) error { return err } - EventsMap := make(map[string]data.Event) + eventsMap := make(map[string]string, len(filteredEvents)) for _, event := range filteredEvents { - EventsMap[event.Nullifier] = event + eventsMap[event.Nullifier] = "" } for _, balance := range balances { - if _, exists := EventsMap[balance.Nullifier]; exists { - continue - } - - err = eventsQ.Insert(data.Event{ - Nullifier: balance.Nullifier, - Type: models.TypeEarlyTest, - Status: data.EventFulfilled, + err = eventsQ.Transaction(func() error { + if _, exists := eventsMap[balance.Nullifier]; exists { + return nil + } + + err = eventsQ.Insert(data.Event{ + Nullifier: balance.Nullifier, + Type: models.TypeEarlyTest, + Status: data.EventFulfilled, + }) + if err != nil { + return fmt.Errorf("failed to insert `early_test` event: %w", err) + } + + var totalPoints int64 + + _, err = eventsQ.FilterByNullifier(balance.Nullifier).Update(data.EventClaimed, nil, &evType.Reward) + if err != nil { + return fmt.Errorf("failedt to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) + } + + totalPoints += evType.Reward + + level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, &balance, totalPoints) + if err != nil { + return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) + } + + err = balancesQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{ + data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), + data.ColLevel: level, + }) + + if err != nil { + return fmt.Errorf("error update balance amount and level: %w", err) + } + + return nil }) - if err != nil { - return fmt.Errorf("failed to insert `early_test` event: %w", err) - } - - var totalPoints int64 - - _, err = eventsQ.FilterByNullifier(nullifiers...).Update(data.EventClaimed, nil, &evType.Reward) - if err != nil { - return fmt.Errorf("failedt to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) - } - - totalPoints += evType.Reward - - level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, &balance, totalPoints) - if err != nil { - return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) - } - - err = balancesQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{ - data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), - data.ColLevel: level, - }) - - if err != nil { - return fmt.Errorf("error update balance amount and level: %w", err) - } } return nil From a8f5c3aac5e6fc50c83928ba2ca5c067251fda04 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 12:45:45 +0300 Subject: [PATCH 13/25] event.Run add filter by auto claim --- internal/service/event/main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 747276a..de58325 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -44,7 +44,13 @@ func Run(cfg config.Config, date int) error { nullifiers = append(nullifiers, balance.Nullifier) } + claimTypes := evTypes.Names(evtypes.FilterByAutoClaim(true)) + if len(claimTypes) == 0 { + return nil + } + filteredEvents, err := eventsQ. + FilterByType(claimTypes...). FilterByStatus(data.EventFulfilled). FilterByNullifier(nullifiers...). Select() From 5ec8bc72d97dbce630652ecabd8bfe63bebc66e0 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 13:15:03 +0300 Subject: [PATCH 14/25] event.Run remodeled filter by auto claim --- internal/service/event/main.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index de58325..d4dd446 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -44,13 +44,7 @@ func Run(cfg config.Config, date int) error { nullifiers = append(nullifiers, balance.Nullifier) } - claimTypes := evTypes.Names(evtypes.FilterByAutoClaim(true)) - if len(claimTypes) == 0 { - return nil - } - filteredEvents, err := eventsQ. - FilterByType(claimTypes...). FilterByStatus(data.EventFulfilled). FilterByNullifier(nullifiers...). Select() @@ -80,6 +74,10 @@ func Run(cfg config.Config, date int) error { return fmt.Errorf("failed to insert `early_test` event: %w", err) } + if evtypes.FilterByAutoClaim(true)(*evType) { + return nil + } + var totalPoints int64 _, err = eventsQ.FilterByNullifier(balance.Nullifier).Update(data.EventClaimed, nil, &evType.Reward) From 2f4b866705680fda0e86cd1a8858f5b04cb5a847 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 17:59:34 +0300 Subject: [PATCH 15/25] event.Run fix loop --- internal/cli/main.go | 4 ++-- internal/data/evtypes/main.go | 4 ++++ internal/service/event/main.go | 6 ++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/cli/main.go b/internal/cli/main.go index c0abe01..a03e46f 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -32,7 +32,7 @@ func Run(args []string) bool { event = app.Command("event", "claim event command") eventCmd = event.Command("create-early-test", "claim event command") - data = eventCmd.Arg("before", "data after ...").Required().Int() + date = eventCmd.Arg("before", "date after ...").Required().Int() ) cmd, err := app.Parse(args[1:]) @@ -53,7 +53,7 @@ func Run(args []string) bool { case migrateDownCmd.FullCommand(): err = MigrateDown(cfg) case eventCmd.FullCommand(): - eventStart(cfg, *data) + eventStart(cfg, *date) default: log.Errorf("unknown command %s", cmd) return false diff --git a/internal/data/evtypes/main.go b/internal/data/evtypes/main.go index bc9b4ea..c756175 100644 --- a/internal/data/evtypes/main.go +++ b/internal/data/evtypes/main.go @@ -1,6 +1,8 @@ package evtypes import ( + "fmt" + "github.com/rarimo/geo-points-svc/internal/data" "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" ) @@ -91,6 +93,8 @@ func (t *Types) Push(types ...models.EventType) { func (t *Types) ensureInitialized() { if t.m == nil || t.list == nil || !t.dbSynced { + txt := fmt.Sprintf("t.m - %+v, t.lisy - %+v, t.dbSynced %+v", t.m, t.list, t.dbSynced) + fmt.Println(txt) panic("event types are not correctly initialized") } } diff --git a/internal/service/event/main.go b/internal/service/event/main.go index d4dd446..bf969f5 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -22,6 +22,7 @@ func Run(cfg config.Config, date int) error { referralsQ := pg.NewReferrals(db) evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive) + log.Infof("point") if evType == nil { log.Infof("Event type %s is inactive", models.TypeEarlyTest) return nil @@ -45,6 +46,7 @@ func Run(cfg config.Config, date int) error { } filteredEvents, err := eventsQ. + FilterByType(models.TypeEarlyTest). FilterByStatus(data.EventFulfilled). FilterByNullifier(nullifiers...). Select() @@ -60,7 +62,7 @@ func Run(cfg config.Config, date int) error { } for _, balance := range balances { - err = eventsQ.Transaction(func() error { + err = eventsQ.New().Transaction(func() error { if _, exists := eventsMap[balance.Nullifier]; exists { return nil } @@ -82,7 +84,7 @@ func Run(cfg config.Config, date int) error { _, err = eventsQ.FilterByNullifier(balance.Nullifier).Update(data.EventClaimed, nil, &evType.Reward) if err != nil { - return fmt.Errorf("failedt to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) + return fmt.Errorf("failed to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) } totalPoints += evType.Reward From f359ebb82358ab839bf1ebaad7e8a096fd76de96 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 8 Aug 2024 19:00:06 +0300 Subject: [PATCH 16/25] event.Run add New method for balanceQ and eventsQ --- internal/service/event/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index bf969f5..23e2521 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -94,7 +94,7 @@ func Run(cfg config.Config, date int) error { return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) } - err = balancesQ.FilterByNullifier(balance.Nullifier).Update(map[string]any{ + err = balancesQ.New().FilterByNullifier(balance.Nullifier).Update(map[string]any{ data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), data.ColLevel: level, }) From e1fcb8395ed52d7605d2ee907e96c2a8da314c27 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 11:46:53 +0300 Subject: [PATCH 17/25] remade start event --- internal/cli/event.go | 18 ++++++++++++++++++ internal/data/evtypes/init.go | 21 +++++++++++++++++++++ internal/data/evtypes/main.go | 4 ---- internal/service/event/main.go | 1 + internal/service/router.go | 4 ++-- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index 0f93d41..2a45fa9 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -1,11 +1,29 @@ package cli import ( + "fmt" + "sync" + "github.com/rarimo/geo-points-svc/internal/config" + "github.com/rarimo/geo-points-svc/internal/data/evtypes" "github.com/rarimo/geo-points-svc/internal/service/event" ) func eventStart(cfg config.Config, data int) { + var evTypesSig = make(chan struct{}) + wg := &sync.WaitGroup{} + run := func(f func()) { + wg.Add(1) + go func() { + f() + wg.Done() + }() + } + + run(func() { evtypes.InitFoOneTimeEvent(cfg, evTypesSig) }) + <-evTypesSig + + fmt.Println("ini end") err := event.Run(cfg, data) if err != nil { cfg.Log().Errorf("Error starting event: %s", err) diff --git a/internal/data/evtypes/init.go b/internal/data/evtypes/init.go index 1dec196..f6f4380 100644 --- a/internal/data/evtypes/init.go +++ b/internal/data/evtypes/init.go @@ -40,3 +40,24 @@ func Init(_ context.Context, cfg extConfig, sig chan struct{}) { log.Debugf("Adding/overwriting event types from DB: %+v", dbTypes) types.Push(dbTypes...) } + +func InitFoOneTimeEvent(cfg extConfig, sig chan struct{}) { + var ( + log = cfg.Log().WithField("who", "evtypes") + q = pg.NewEventTypes(cfg.DB().Clone()) + types = cfg.EventTypes() + ) + + dbTypes, err := q.New().Select() + if err != nil { + panic(fmt.Errorf("select all event types: %w", err)) + } + + defer func() { + types.dbSynced = true + sig <- struct{}{} + }() + + log.Debugf("Adding/overwriting event types from DB: %+v", dbTypes) + types.Push(dbTypes...) +} diff --git a/internal/data/evtypes/main.go b/internal/data/evtypes/main.go index c756175..bc9b4ea 100644 --- a/internal/data/evtypes/main.go +++ b/internal/data/evtypes/main.go @@ -1,8 +1,6 @@ package evtypes import ( - "fmt" - "github.com/rarimo/geo-points-svc/internal/data" "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" ) @@ -93,8 +91,6 @@ func (t *Types) Push(types ...models.EventType) { func (t *Types) ensureInitialized() { if t.m == nil || t.list == nil || !t.dbSynced { - txt := fmt.Sprintf("t.m - %+v, t.lisy - %+v, t.dbSynced %+v", t.m, t.list, t.dbSynced) - fmt.Println(txt) panic("event types are not correctly initialized") } } diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 23e2521..b2c51ef 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -22,6 +22,7 @@ func Run(cfg config.Config, date int) error { referralsQ := pg.NewReferrals(db) evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive) + log.Infof("point") if evType == nil { log.Infof("Event type %s is inactive", models.TypeEarlyTest) diff --git a/internal/service/router.go b/internal/service/router.go index 077fd95..f714c24 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -32,7 +32,7 @@ func Run(ctx context.Context, cfg config.Config) { r.Route("/integrations/geo-points-svc/v1", func(r chi.Router) { r.Route("/public", func(r chi.Router) { r.Route("/balances", func(r chi.Router) { - r.Use(authMW) + //r.Use(authMW) r.Post("/", handlers.CreateBalance) r.Route("/{nullifier}", func(r chi.Router) { r.Get("/", handlers.GetBalance) @@ -46,7 +46,7 @@ func Run(ctx context.Context, cfg config.Config) { }) r.Route("/events", func(r chi.Router) { - r.Use(authMW) + //r.Use(authMW) r.Get("/", handlers.ListEvents) r.Post("/poll", handlers.FulfillPollEvent) r.Route("/{id}", func(r chi.Router) { From cab96439f6b1de1a91c6cd8f2ae4b61253d75254 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 14:25:19 +0300 Subject: [PATCH 18/25] fix early_test --- internal/cli/event.go | 2 +- internal/data/evtypes/filters.go | 10 ++++++++++ internal/data/evtypes/init.go | 2 +- internal/service/event/main.go | 17 +++++++---------- internal/service/router.go | 4 ++-- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index 2a45fa9..f3b8778 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -20,7 +20,7 @@ func eventStart(cfg config.Config, data int) { }() } - run(func() { evtypes.InitFoOneTimeEvent(cfg, evTypesSig) }) + run(func() { evtypes.InitForOneTimeEvent(cfg, evTypesSig) }) <-evTypesSig fmt.Println("ini end") diff --git a/internal/data/evtypes/filters.go b/internal/data/evtypes/filters.go index b984cdb..6c84a33 100644 --- a/internal/data/evtypes/filters.go +++ b/internal/data/evtypes/filters.go @@ -1,6 +1,7 @@ package evtypes import ( + "fmt" "time" "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" @@ -25,6 +26,15 @@ func FilterNotStarted(ev models.EventType) bool { } func FilterInactive(ev models.EventType) bool { + if ev.Disabled { + fmt.Println("ev.Disabled") + } + if FilterExpired(ev) { + fmt.Println("FilterExpired") + } + if FilterNotStarted(ev) { + fmt.Println("FilterNotStarted") + } return ev.Disabled || FilterExpired(ev) || FilterNotStarted(ev) } diff --git a/internal/data/evtypes/init.go b/internal/data/evtypes/init.go index f6f4380..a2f0819 100644 --- a/internal/data/evtypes/init.go +++ b/internal/data/evtypes/init.go @@ -41,7 +41,7 @@ func Init(_ context.Context, cfg extConfig, sig chan struct{}) { types.Push(dbTypes...) } -func InitFoOneTimeEvent(cfg extConfig, sig chan struct{}) { +func InitForOneTimeEvent(cfg extConfig, sig chan struct{}) { var ( log = cfg.Log().WithField("who", "evtypes") q = pg.NewEventTypes(cfg.DB().Clone()) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index b2c51ef..0e07fda 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -23,7 +23,6 @@ func Run(cfg config.Config, date int) error { evType := evTypes.Get(models.TypeEarlyTest, evtypes.FilterInactive) - log.Infof("point") if evType == nil { log.Infof("Event type %s is inactive", models.TypeEarlyTest) return nil @@ -57,15 +56,13 @@ func Run(cfg config.Config, date int) error { return err } - eventsMap := make(map[string]string, len(filteredEvents)) - for _, event := range filteredEvents { - eventsMap[event.Nullifier] = "" - } - for _, balance := range balances { err = eventsQ.New().Transaction(func() error { - if _, exists := eventsMap[balance.Nullifier]; exists { - return nil + for _, event := range filteredEvents { + if event.Nullifier == balance.Nullifier { + log.Infof("Event %s has nullifier %s", balance.Nullifier, balance.Nullifier) + return nil + } } err = eventsQ.Insert(data.Event{ @@ -73,6 +70,7 @@ func Run(cfg config.Config, date int) error { Type: models.TypeEarlyTest, Status: data.EventFulfilled, }) + if err != nil { return fmt.Errorf("failed to insert `early_test` event: %w", err) } @@ -83,7 +81,7 @@ func Run(cfg config.Config, date int) error { var totalPoints int64 - _, err = eventsQ.FilterByNullifier(balance.Nullifier).Update(data.EventClaimed, nil, &evType.Reward) + _, err = eventsQ.FilterByNullifier(balance.Nullifier).Update(data.EventFulfilled, nil, &evType.Reward) if err != nil { return fmt.Errorf("failed to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) } @@ -107,6 +105,5 @@ func Run(cfg config.Config, date int) error { return nil }) } - return nil } diff --git a/internal/service/router.go b/internal/service/router.go index f714c24..077fd95 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -32,7 +32,7 @@ func Run(ctx context.Context, cfg config.Config) { r.Route("/integrations/geo-points-svc/v1", func(r chi.Router) { r.Route("/public", func(r chi.Router) { r.Route("/balances", func(r chi.Router) { - //r.Use(authMW) + r.Use(authMW) r.Post("/", handlers.CreateBalance) r.Route("/{nullifier}", func(r chi.Router) { r.Get("/", handlers.GetBalance) @@ -46,7 +46,7 @@ func Run(ctx context.Context, cfg config.Config) { }) r.Route("/events", func(r chi.Router) { - //r.Use(authMW) + r.Use(authMW) r.Get("/", handlers.ListEvents) r.Post("/poll", handlers.FulfillPollEvent) r.Route("/{id}", func(r chi.Router) { From 24e50d855ace463964eb6746ffdd7d166ca9935e Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 14:26:55 +0300 Subject: [PATCH 19/25] edit config for early_test --- config.yaml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index 29c1606..e0531cd 100644 --- a/config.yaml +++ b/config.yaml @@ -3,7 +3,7 @@ log: disable_sentry: true db: - url: postgres://points:points@localhost:5432/points?sslmode=disable + url: postgres://points:points@localhost:5440/points?sslmode=disable listener: addr: localhost:8000 @@ -91,6 +91,14 @@ event_types: short_description: Short description auto_claim: true qr_code_value: "qr_code_base64_string" + - name: early_test + title: one-time event + reward: 5 + frequency: unlimited + description: one time event + short_description: Short description + auto_claim: true + no_auto_open: true levels: downgradeable: false From d70e810c9ff1ddb737e4bbfba779ffcd3faa08b2 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 14:29:53 +0300 Subject: [PATCH 20/25] delete trash from evtypes.filters --- internal/data/evtypes/filters.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/internal/data/evtypes/filters.go b/internal/data/evtypes/filters.go index 6c84a33..b984cdb 100644 --- a/internal/data/evtypes/filters.go +++ b/internal/data/evtypes/filters.go @@ -1,7 +1,6 @@ package evtypes import ( - "fmt" "time" "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" @@ -26,15 +25,6 @@ func FilterNotStarted(ev models.EventType) bool { } func FilterInactive(ev models.EventType) bool { - if ev.Disabled { - fmt.Println("ev.Disabled") - } - if FilterExpired(ev) { - fmt.Println("FilterExpired") - } - if FilterNotStarted(ev) { - fmt.Println("FilterNotStarted") - } return ev.Disabled || FilterExpired(ev) || FilterNotStarted(ev) } From 1ef5dcef127aca25e452d654667029770b6af4aa Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 15:18:19 +0300 Subject: [PATCH 21/25] return port 5432 to config --- config.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index e0531cd..6c5e5a1 100644 --- a/config.yaml +++ b/config.yaml @@ -3,7 +3,7 @@ log: disable_sentry: true db: - url: postgres://points:points@localhost:5440/points?sslmode=disable + url: postgres://points:points@localhost:5432/points?sslmode=disable listener: addr: localhost:8000 @@ -116,12 +116,10 @@ auth: verifier: allowed_age: 18 allowed_identity_timestamp: 1715698750 - poseidonsmt_root_verifier: rpc: evm_rpc_url contract: poseidon_smt_contract_address request_timeout: 10s - poll_verifier: rpc: evm_rpc_url proposal_state_address: 0x123...123 From 62f0afe500e2e5f3291a869be3d479b73b5dcb95 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 15:20:00 +0300 Subject: [PATCH 22/25] retern map in service/event/main --- internal/cli/event.go | 17 ++--------------- internal/service/event/main.go | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index f3b8778..b67f15d 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -1,29 +1,16 @@ package cli import ( - "fmt" - "sync" - "github.com/rarimo/geo-points-svc/internal/config" "github.com/rarimo/geo-points-svc/internal/data/evtypes" "github.com/rarimo/geo-points-svc/internal/service/event" ) func eventStart(cfg config.Config, data int) { - var evTypesSig = make(chan struct{}) - wg := &sync.WaitGroup{} - run := func(f func()) { - wg.Add(1) - go func() { - f() - wg.Done() - }() - } - - run(func() { evtypes.InitForOneTimeEvent(cfg, evTypesSig) }) + evTypesSig := make(chan struct{}) + go evtypes.InitForOneTimeEvent(cfg, evTypesSig) <-evTypesSig - fmt.Println("ini end") err := event.Run(cfg, data) if err != nil { cfg.Log().Errorf("Error starting event: %s", err) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index 0e07fda..c0cca27 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -56,13 +56,16 @@ func Run(cfg config.Config, date int) error { return err } + eventsMap := make(map[string]string, len(filteredEvents)) + for _, event := range filteredEvents { + eventsMap[event.Nullifier] = event.ID + } + for _, balance := range balances { err = eventsQ.New().Transaction(func() error { - for _, event := range filteredEvents { - if event.Nullifier == balance.Nullifier { - log.Infof("Event %s has nullifier %s", balance.Nullifier, balance.Nullifier) - return nil - } + if _, exists := eventsMap[balance.Nullifier]; exists { + log.Infof("Event %s has nullifier %s is already done", eventsMap[balance.Nullifier], balance.Nullifier) + return nil } err = eventsQ.Insert(data.Event{ @@ -79,22 +82,18 @@ func Run(cfg config.Config, date int) error { return nil } - var totalPoints int64 - _, err = eventsQ.FilterByNullifier(balance.Nullifier).Update(data.EventFulfilled, nil, &evType.Reward) if err != nil { return fmt.Errorf("failed to update %s events for user=%s: %w", models.TypeEarlyTest, balance.Nullifier, err) } - totalPoints += evType.Reward - - level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, &balance, totalPoints) + level, err := handlers.DoLevelRefUpgrade(lvls, referralsQ, &balance, evType.Reward) if err != nil { return fmt.Errorf("failed to do lvlup and referrals updates: %w", err) } err = balancesQ.New().FilterByNullifier(balance.Nullifier).Update(map[string]any{ - data.ColAmount: pg.AddToValue(data.ColAmount, totalPoints), + data.ColAmount: pg.AddToValue(data.ColAmount, evType.Reward), data.ColLevel: level, }) From 87ae84bc20a5161f9d071b7cb62d764cf14be2cc Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 16:07:26 +0300 Subject: [PATCH 23/25] remake map event.Run --- internal/service/event/main.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index c0cca27..d5ac3b8 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -27,8 +27,8 @@ func Run(cfg config.Config, date int) error { log.Infof("Event type %s is inactive", models.TypeEarlyTest) return nil } - - balances, err := balancesQ.FilterByCreatedBefore(date).FilterVerified().Select() + //.FilterVerified() + balances, err := balancesQ.FilterByCreatedBefore(date).Select() if err != nil { log.WithError(err).Error("failed to filter by updated before") @@ -56,15 +56,16 @@ func Run(cfg config.Config, date int) error { return err } - eventsMap := make(map[string]string, len(filteredEvents)) + eventsMap := make(map[string]struct{}, len(filteredEvents)) + for _, event := range filteredEvents { - eventsMap[event.Nullifier] = event.ID + eventsMap[event.Nullifier] = struct{}{} } for _, balance := range balances { err = eventsQ.New().Transaction(func() error { if _, exists := eventsMap[balance.Nullifier]; exists { - log.Infof("Event %s has nullifier %s is already done", eventsMap[balance.Nullifier], balance.Nullifier) + log.Infof("Event %s is already done for user with nullifier %s ", models.TypeEarlyTest, balance.Nullifier) return nil } From a5900625f5fa48c2a8397c71a8261025ca85f72b Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 16:11:00 +0300 Subject: [PATCH 24/25] In eventStart, redid the work func evtypes.InitForOneTimeEvent(cfg) --- internal/cli/event.go | 4 +--- internal/data/evtypes/init.go | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/internal/cli/event.go b/internal/cli/event.go index b67f15d..f0fb1dc 100644 --- a/internal/cli/event.go +++ b/internal/cli/event.go @@ -7,9 +7,7 @@ import ( ) func eventStart(cfg config.Config, data int) { - evTypesSig := make(chan struct{}) - go evtypes.InitForOneTimeEvent(cfg, evTypesSig) - <-evTypesSig + evtypes.InitForOneTimeEvent(cfg) err := event.Run(cfg, data) if err != nil { diff --git a/internal/data/evtypes/init.go b/internal/data/evtypes/init.go index a2f0819..53d6e46 100644 --- a/internal/data/evtypes/init.go +++ b/internal/data/evtypes/init.go @@ -41,7 +41,7 @@ func Init(_ context.Context, cfg extConfig, sig chan struct{}) { types.Push(dbTypes...) } -func InitForOneTimeEvent(cfg extConfig, sig chan struct{}) { +func InitForOneTimeEvent(cfg extConfig) { var ( log = cfg.Log().WithField("who", "evtypes") q = pg.NewEventTypes(cfg.DB().Clone()) @@ -55,7 +55,6 @@ func InitForOneTimeEvent(cfg extConfig, sig chan struct{}) { defer func() { types.dbSynced = true - sig <- struct{}{} }() log.Debugf("Adding/overwriting event types from DB: %+v", dbTypes) From 6772e6a8cf0a8401547d07685592e976401711dd Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 9 Aug 2024 17:15:14 +0300 Subject: [PATCH 25/25] remove filter for status in filteredEvents event.main --- internal/service/event/main.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/service/event/main.go b/internal/service/event/main.go index d5ac3b8..7c99739 100644 --- a/internal/service/event/main.go +++ b/internal/service/event/main.go @@ -27,8 +27,8 @@ func Run(cfg config.Config, date int) error { log.Infof("Event type %s is inactive", models.TypeEarlyTest) return nil } - //.FilterVerified() - balances, err := balancesQ.FilterByCreatedBefore(date).Select() + + balances, err := balancesQ.FilterByCreatedBefore(date).FilterVerified().Select() if err != nil { log.WithError(err).Error("failed to filter by updated before") @@ -47,7 +47,6 @@ func Run(cfg config.Config, date int) error { filteredEvents, err := eventsQ. FilterByType(models.TypeEarlyTest). - FilterByStatus(data.EventFulfilled). FilterByNullifier(nullifiers...). Select()