Skip to content

Commit

Permalink
feat: add total number of daily quests completed (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kieranroneill authored Jul 23, 2024
1 parent c6fa98e commit 07b8bc9
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/v1/quests/internal/queries/fetchdailyevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
})
}

Expand Down
29 changes: 29 additions & 0 deletions packages/v1/quests/internal/queries/fetchtotaldailyevents.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions packages/v1/quests/internal/types/dailyevent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

type DailyEvent struct {
Amount int
Name string
Completed bool
Name string
}
6 changes: 4 additions & 2 deletions packages/v1/quests/internal/types/dailyquest.go
Original file line number Diff line number Diff line change
@@ -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"`
}
6 changes: 6 additions & 0 deletions packages/v1/quests/internal/types/totaldailyevent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

type TotalDailyEvent struct {
Name string
Total int
}
38 changes: 32 additions & 6 deletions packages/v1/quests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
})
}

Expand Down

0 comments on commit 07b8bc9

Please sign in to comment.