From 07b8bc970ad1cd71eb42b83dbf8047974006b87f Mon Sep 17 00:00:00 2001 From: Kieran O'Neill Date: Tue, 23 Jul 2024 12:58:21 +0300 Subject: [PATCH] feat: add total number of daily quests completed (#13) * build: remove swagger on make deploy command * feat: add new query to fetch the total number of daily quests completed and convert compeleted to a boolean --- Makefile | 2 +- .../internal/queries/fetchdailyevents.go | 6 +-- .../internal/queries/fetchtotaldailyevents.go | 29 ++++++++++++++ .../v1/quests/internal/types/dailyevent.go | 4 +- .../v1/quests/internal/types/dailyquest.go | 6 ++- .../quests/internal/types/totaldailyevent.go | 6 +++ packages/v1/quests/main.go | 38 ++++++++++++++++--- 7 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 packages/v1/quests/internal/queries/fetchtotaldailyevents.go create mode 100644 packages/v1/quests/internal/types/totaldailyevent.go diff --git a/Makefile b/Makefile index f785b87..346cd7a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ scripts_dir := ./scripts all: setup swagger deploy deploy: - doppler run -- doctl serverless deploy "${PWD}" + doppler run -- doctl serverless deploy "${PWD}" --exclude=swagger list: doctl serverless functions list diff --git a/packages/v1/quests/internal/queries/fetchdailyevents.go b/packages/v1/quests/internal/queries/fetchdailyevents.go index 009f8da..6404bee 100644 --- a/packages/v1/quests/internal/queries/fetchdailyevents.go +++ b/packages/v1/quests/internal/queries/fetchdailyevents.go @@ -10,7 +10,7 @@ func FetchDailyEvents(account string, logger *utils.Logger) ([]_types.DailyEvent var response _types.PostHogQueryResponse var result []_types.DailyEvent - query := fmt.Sprintf(`{"query":{"kind":"HogQLQuery","query":"select events.event as \"event-name\", count() as \"completed\" from events where person.pdi.distinct_id = '%s' and events.properties.genesisHash = 'IXnoWtviVVJW5LGivNFc0Dq14V3kqaXuK2u5OQrdVZo=' and events.timestamp >= today() group by events.event, person.pdi.distinct_id order by count() desc"}}`, account) + query := fmt.Sprintf(`{"query":{"kind":"HogQLQuery","query":"select events.event as \"event-name\", count() > 0 as \"completed\" from events where person.pdi.distinct_id = '%s' and events.properties.genesisHash = 'IXnoWtviVVJW5LGivNFc0Dq14V3kqaXuK2u5OQrdVZo=' and events.timestamp >= today() group by \"event-name\" order by \"event-name\""}}`, account) err := PostHogQuery(query, &response) if err != nil { logger.Error(err) @@ -20,8 +20,8 @@ func FetchDailyEvents(account string, logger *utils.Logger) ([]_types.DailyEvent for _, value := range response.Results { result = append(result, _types.DailyEvent{ - Amount: int(value[1].(float64)), - Name: value[0].(string), + Completed: int(value[1].(float64)) > 0, + Name: value[0].(string), }) } diff --git a/packages/v1/quests/internal/queries/fetchtotaldailyevents.go b/packages/v1/quests/internal/queries/fetchtotaldailyevents.go new file mode 100644 index 0000000..ef7803f --- /dev/null +++ b/packages/v1/quests/internal/queries/fetchtotaldailyevents.go @@ -0,0 +1,29 @@ +package queries + +import ( + "fmt" + "lib/utils" + _types "quests/internal/types" +) + +func FetchTotalDailyEvents(account string, logger *utils.Logger) ([]_types.TotalDailyEvent, error) { + var response _types.PostHogQueryResponse + var result []_types.TotalDailyEvent + + query := fmt.Sprintf(`{"query":{"kind":"HogQLQuery","query":"select events.event as \"event-name\", count(distinct dateTrunc('day', events.timestamp)) AS \"total\" from events where person.pdi.distinct_id = '%s' and events.properties.genesisHash = 'IXnoWtviVVJW5LGivNFc0Dq14V3kqaXuK2u5OQrdVZo=' group by \"event-name\" order by \"event-name\""}}`, account) + err := PostHogQuery(query, &response) + if err != nil { + logger.Error(err) + + return nil, err + } + + for _, value := range response.Results { + result = append(result, _types.TotalDailyEvent{ + Name: value[0].(string), + Total: int(value[1].(float64)), + }) + } + + return result, nil +} diff --git a/packages/v1/quests/internal/types/dailyevent.go b/packages/v1/quests/internal/types/dailyevent.go index e059810..a506b2f 100644 --- a/packages/v1/quests/internal/types/dailyevent.go +++ b/packages/v1/quests/internal/types/dailyevent.go @@ -1,6 +1,6 @@ package types type DailyEvent struct { - Amount int - Name string + Completed bool + Name string } diff --git a/packages/v1/quests/internal/types/dailyquest.go b/packages/v1/quests/internal/types/dailyquest.go index 1a29107..93ad163 100644 --- a/packages/v1/quests/internal/types/dailyquest.go +++ b/packages/v1/quests/internal/types/dailyquest.go @@ -1,10 +1,12 @@ package types // DailyQuest -// @Description The ID of the quest and the amount of times it has been completed. +// @Description The ID of the quest, whether the quest has been completed today and the total number of daily quests. type DailyQuest struct { // The amount of times the quest has been completed - Completed int `json:"completed" example:"22"` + Completed bool `json:"completed" example:"true"` // The ID of the quest Id string `json:"id" example:"send-native-currency-action"` + // The total number of daily quests completed + Total int `json:"total" example:"22"` } diff --git a/packages/v1/quests/internal/types/totaldailyevent.go b/packages/v1/quests/internal/types/totaldailyevent.go new file mode 100644 index 0000000..7f7489f --- /dev/null +++ b/packages/v1/quests/internal/types/totaldailyevent.go @@ -0,0 +1,6 @@ +package types + +type TotalDailyEvent struct { + Name string + Total int +} diff --git a/packages/v1/quests/main.go b/packages/v1/quests/main.go index 1036d84..30cff20 100644 --- a/packages/v1/quests/main.go +++ b/packages/v1/quests/main.go @@ -81,6 +81,21 @@ func Main(request _types.Request) *_types.Response { logger.Debug(fmt.Sprintf("received the event references %s", eventReferences)) + logger.Debug(fmt.Sprintf("getting total daily events from posthog for account \"%s\"", request.Account)) + + totalDailyEvents, err := _queries.FetchTotalDailyEvents(request.Account, logger) + if err != nil { + return &_types.Response{ + Body: _types.ResponseBody{ + Error: errors.NewPostHogError("failed to fetch total daily events from posthog", err), + }, + Headers: headers, + StatusCode: http.StatusInternalServerError, + } + } + + logger.Debug(fmt.Sprintf("received total daily events from posthog for account \"%s\"", request.Account)) + logger.Debug(fmt.Sprintf("getting daily events from posthog for account \"%s\"", request.Account)) dailyEvents, err := _queries.FetchDailyEvents(request.Account, logger) @@ -96,21 +111,32 @@ func Main(request _types.Request) *_types.Response { logger.Debug(fmt.Sprintf("received daily events from posthog for account \"%s\"", request.Account)) - // map the daily quests from teh events, defaulting to zero for quests that are not in the daily events from posthog + // map the daily quests from the events, defaulting to zero for quests that are not in the daily events from posthog for _, eventReference := range eventReferences { - completed := 0 - index := slices.IndexFunc(dailyEvents, func(event _types.DailyEvent) bool { + completed := false + total := 0 + index := slices.IndexFunc(totalDailyEvents, func(event _types.TotalDailyEvent) bool { return event.Name == eventReference }) - // if the event reference is in the daily events, get the amount + // if the event reference is in the total daily events, get the total if index > -1 { - completed = dailyEvents[index].Amount + total = totalDailyEvents[index].Total + } + + index = slices.IndexFunc(dailyEvents, func(event _types.DailyEvent) bool { + return event.Name == eventReference + }) + + // if the event reference is in the daily events, get the completed + if index > -1 { + completed = dailyEvents[index].Completed } dailyQuests = append(dailyQuests, _types.DailyQuest{ - Id: eventReference, Completed: completed, + Id: eventReference, + Total: total, }) }