Skip to content

Commit

Permalink
remade timezone, responses in endpoints about daily questions and mod…
Browse files Browse the repository at this point in the history
…ified the logic for checking for answers
  • Loading branch information
kish1n committed Aug 20, 2024
1 parent a579031 commit 7dde043
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 292 deletions.
35 changes: 0 additions & 35 deletions internal/config/daily_question_time_hash.go

This file was deleted.

104 changes: 104 additions & 0 deletions internal/config/daily_questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package config

import (
"fmt"
"sync"
"time"

"github.com/rarimo/geo-points-svc/internal/data"
"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/kv"
)

var mu sync.Mutex
var endOfDayMutex sync.Mutex

type DailyQuestions struct {
Timezone int
QuestionsQueue map[string]int64
}

func (c *config) DailyQuestions() *DailyQuestions {
return c.DailyQuestion.Do(func() interface{} {
var cfg struct {
Timezone int `fig:"timezone"`
}

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, "daily_questions")).
Please()
if err != nil {
panic(fmt.Errorf("failed to figure out daily questions config: %w", err))
}

res := cfg.Timezone

return &DailyQuestions{
Timezone: res,
QuestionsQueue: make(map[string]int64),
}

}).(*DailyQuestions)
}

func (c DailyQuestions) InsertInQuestionsQueue(key string, value int64) {
if c.QuestionsQueue == nil {
c.QuestionsQueue = make(map[string]int64)
}
(c).QuestionsQueue[key] = value
}

func (c DailyQuestions) GetFromQuestionsQueue(key string) *int64 {
if c.QuestionsQueue == nil {
return nil
}
value, exists := c.QuestionsQueue[key]
if !exists {
return nil
}
return &value
}

func (c DailyQuestions) SetDailyQuestionTimeWithExpiration(eve *data.Event, nullifier string, deadline int64) {
c.InsertInQuestionsQueue(nullifier, deadline)

now := time.Now().UTC()

go func() {
time.Sleep(time.Duration(deadline) * time.Second)

mu.Lock()
defer mu.Unlock()

getTime := c.GetFromQuestionsQueue(nullifier)
if now.Unix() < *getTime+deadline {
if eve != nil {
delete(c.QuestionsQueue, nullifier)
}
}
}()

go func() {
endOfDayMutex.Lock()
defer endOfDayMutex.Unlock()

c.RemoveAllQuestionsAtEndDay()
}()
}

func (c DailyQuestions) RemoveAllQuestionsAtEndDay() {
now := time.Now().UTC()
endOfDay := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
timeUntilEndOfDay := endOfDay.Sub(now)

go func() {
time.Sleep(timeUntilEndOfDay)

mu.Lock()
defer mu.Unlock()

for nullifier := range c.QuestionsQueue {
delete(c.QuestionsQueue, nullifier)
}
}()
}
9 changes: 0 additions & 9 deletions internal/config/location.go

This file was deleted.

41 changes: 15 additions & 26 deletions internal/config/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"time"

"github.com/rarimo/geo-auth-svc/pkg/auth"
"github.com/rarimo/geo-auth-svc/pkg/hmacsig"
"github.com/rarimo/geo-points-svc/internal/data/evtypes"
Expand All @@ -23,10 +21,9 @@ type Config interface {
hmacsig.SigCalculatorProvider
PollVerifierer

DailyQuestionsTimeHash() DailyQuestionsTimeHash
Levels() *Levels
Verifiers() Verifiers
Location() Location
DailyQuestions() *DailyQuestions
}

type config struct {
Expand All @@ -39,33 +36,25 @@ type config struct {
hmacsig.SigCalculatorProvider
PollVerifierer

dailyQuestionsTimeHash DailyQuestionsTimeHash
timeLocation Location

passport root.VerifierProvider

levels comfig.Once
verifier comfig.Once
getter kv.Getter
DailyQuestion comfig.Once
levels comfig.Once
verifier comfig.Once
getter kv.Getter
}

func New(getter kv.Getter) Config {
location, err := time.LoadLocation("Asia/Tbilisi")
if err != nil {
panic("Error load location in config")
}
return &config{
getter: getter,
Databaser: pgdb.NewDatabaser(getter),
Listenerer: comfig.NewListenerer(getter),
Logger: comfig.NewLogger(getter, comfig.LoggerOpts{}),
Auther: auth.NewAuther(getter), //nolint:misspell
PollVerifierer: NewPollVerifier(getter),
Broadcasterer: broadcaster.New(getter),
dailyQuestionsTimeHash: make(DailyQuestionsTimeHash),
timeLocation: location,
passport: root.NewVerifierProvider(getter, root.PoseidonSMT),
EventTypeser: evtypes.NewConfig(getter),
SigCalculatorProvider: hmacsig.NewCalculatorProvider(getter),
getter: getter,
Databaser: pgdb.NewDatabaser(getter),
Listenerer: comfig.NewListenerer(getter),
Logger: comfig.NewLogger(getter, comfig.LoggerOpts{}),
Auther: auth.NewAuther(getter), //nolint:misspell
PollVerifierer: NewPollVerifier(getter),
Broadcasterer: broadcaster.New(getter),
passport: root.NewVerifierProvider(getter, root.PoseidonSMT),
EventTypeser: evtypes.NewConfig(getter),
SigCalculatorProvider: hmacsig.NewCalculatorProvider(getter),
}
}
3 changes: 1 addition & 2 deletions internal/data/daily_questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ type DailyQuestionsQ interface {
Select() ([]DailyQuestion, error)
Get() (*DailyQuestion, error)

FilterTodayQuestions(location string) DailyQuestionsQ
FilterByStartAtToday(location string) DailyQuestionsQ
FilterTodayQuestions(offset int) DailyQuestionsQ
FilterByCreatedAt(date time.Time) DailyQuestionsQ
FilterByID(ID int) DailyQuestionsQ
}
2 changes: 1 addition & 1 deletion internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type EventsQ interface {
FilterByNotType(types ...string) EventsQ
FilterByUpdatedAtBefore(int64) EventsQ

FilterTodayEvents(location string) EventsQ
FilterTodayEvents(offset int) EventsQ

FilterByExternalID(string) EventsQ
FilterInactiveNotClaimed(types ...string) EventsQ
Expand Down
31 changes: 7 additions & 24 deletions internal/data/pg/daily_questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,20 @@ func (q *dailyQuestionsQ) Get() (*data.DailyQuestion, error) {
return &res, nil
}

func (q *dailyQuestionsQ) FilterByStartAtToday(location string) data.DailyQuestionsQ {
loc, _ := time.LoadLocation(location)

now := time.Now().In(loc)
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc).UTC()
todayEnd := todayStart.Add(24 * time.Hour).Add(-time.Nanosecond).UTC()

return q.applyCondition(squirrel.And{
squirrel.GtOrEq{"starts_at": todayStart},
squirrel.LtOrEq{"starts_at": todayEnd},
})
}

func (q *dailyQuestionsQ) FilterByCreatedAt(date time.Time) data.DailyQuestionsQ {
return q.applyCondition(squirrel.Gt{"created_at": date})
}

func (q *dailyQuestionsQ) FilterTodayQuestions(location string) data.DailyQuestionsQ {
loc, _ := time.LoadLocation(location)
//Don't check the error because we have already done this in the config
now := time.Now().In(loc)

todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
todayEnd := todayStart.Add(24 * time.Hour).Add(-time.Nanosecond)
func (q *dailyQuestionsQ) FilterTodayQuestions(offset int) data.DailyQuestionsQ {
location := time.FixedZone(fmt.Sprintf("GMT%+d", offset), offset*3600)

utcStart := todayStart.UTC()
utcEnd := todayEnd.UTC()
now := time.Now().In(location)
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location).UTC()
todayEnd := todayStart.Add(24 * time.Hour).Add(-time.Nanosecond).UTC()

return q.applyCondition(squirrel.And{
squirrel.GtOrEq{"starts_at": utcStart},
squirrel.LtOrEq{"starts_at": utcEnd},
squirrel.GtOrEq{"starts_at": todayStart},
squirrel.LtOrEq{"starts_at": todayEnd},
})
}

Expand Down
9 changes: 4 additions & 5 deletions internal/data/pg/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,11 @@ func (q *events) FilterByUpdatedAtBefore(unix int64) data.EventsQ {
return q.applyCondition(squirrel.Lt{"updated_at": unix})
}

func (q *events) FilterTodayEvents(location string) data.EventsQ {
loc, _ := time.LoadLocation(location)
//Don't check the error because we have already done this in the config
now := time.Now().In(loc)
func (q *events) FilterTodayEvents(offset int) data.EventsQ {
location := time.FixedZone(fmt.Sprintf("GMT%+d", offset), offset*3600)
now := time.Now().In(location)

todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location)
todayEnd := todayStart.Add(24 * time.Hour).Add(-time.Nanosecond)

utcStart := todayStart.UTC().Unix()
Expand Down
21 changes: 5 additions & 16 deletions internal/service/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const (
sigCalculatorCtxKey
voteVerifierCtxKey
dailyQuestionsCtxKey
dailyQuestionsHashCtxKey
locationCtxKey
dailyQuestionsCfgCtxKey
)

func CtxLog(entry *logan.Entry) func(context.Context) context.Context {
Expand Down Expand Up @@ -121,24 +120,14 @@ func Verifiers(r *http.Request) config.Verifiers {
return r.Context().Value(verifiersCtxKey).(config.Verifiers)
}

func CtxLocation(v config.Location) func(context.Context) context.Context {
func CtxDailyQuestion(v *config.DailyQuestions) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, locationCtxKey, v)
return context.WithValue(ctx, dailyQuestionsCfgCtxKey, v)
}
}

func Location(r *http.Request) config.Location {
return r.Context().Value(locationCtxKey).(config.Location)
}

func CtxDailyQuestionTimeHash(v config.DailyQuestionsTimeHash) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, dailyQuestionsHashCtxKey, v)
}
}

func DailyQuestionTimeHash(r *http.Request) config.DailyQuestionsTimeHash {
return r.Context().Value(dailyQuestionsHashCtxKey).(config.DailyQuestionsTimeHash)
func DailyQuestions(r *http.Request) *config.DailyQuestions {
return r.Context().Value(dailyQuestionsCfgCtxKey).(*config.DailyQuestions)
}

func CtxPollVerifier(v *config.PollVerifier) func(context.Context) context.Context {
Expand Down
Loading

0 comments on commit 7dde043

Please sign in to comment.