Skip to content

Commit 97aa639

Browse files
committed
handled types map, float, int, and their slices
1 parent 3d5a687 commit 97aa639

File tree

8 files changed

+1071
-105
lines changed

8 files changed

+1071
-105
lines changed

graphcache/graphcache.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,8 @@ func (gc *GraphCache) CacheResponse(queryPrefix string, field string, object map
352352
if objMap, ok := obj.(map[string]interface{}); ok {
353353
_, k := gc.CacheResponse(queryPrefix, key, objMap, object)
354354
responseObjects = append(responseObjects, k)
355-
}
356-
if objMap, ok := obj.(string); ok {
357-
responseObjects = append(responseObjects, objMap)
355+
} else {
356+
appendToInterfaceArray(obj, &responseObjects)
358357
}
359358
}
360359
if !utils.ArrayContains(responseObjects, "") {
@@ -368,6 +367,13 @@ func (gc *GraphCache) CacheResponse(queryPrefix string, field string, object map
368367
return object, cacheKey
369368
}
370369

370+
func appendToInterfaceArray[T any](obj interface{}, responseObjects *[]T) {
371+
switch v := obj.(type) {
372+
case T:
373+
*responseObjects = append(*responseObjects, v)
374+
}
375+
}
376+
371377
func (gc *GraphCache) GetQueryResponseKey(queryPrefix string, queryDoc *ast.OperationDefinition, response map[string]interface{}, variables map[string]interface{}) map[string]interface{} {
372378
queryType := queryDoc.Operation
373379
parentKey := queryDoc.Name

main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func RunUsersOperations(t *testing.T, client *test_client.GraphQLClient) (time.D
6161
fmt.Println("error creating user ", err)
6262
return totalTimeTaken, err
6363
}
64+
assert.NotNil(t, user)
6465
totalTimeTaken += tt
6566

6667
client.FlushByType("User", "")

test_api/todo/db/connection.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,82 @@ func (d Connection) GetTodosByUserID(todos *[]Todo, userID string) error {
104104
return d.DB.Where("user_id=?", userID).Find(todos).Error
105105
}
106106

107+
func (d Connection) GetCompletionRateForUser(user *User) (float64, error) {
108+
var todos []Todo
109+
err := d.GetTodosByUserID(&todos, user.ID.String())
110+
if err != nil {
111+
return 0, err
112+
}
113+
var completed, total int
114+
for _, todo := range todos {
115+
total++
116+
if todo.Done {
117+
completed++
118+
}
119+
}
120+
if total == 0 {
121+
return 0, nil
122+
}
123+
return float64(completed) / float64(total), nil
124+
}
125+
126+
func (d Connection) GetCompletionRateForLast7Days(user *User) ([]float64, error) {
127+
var todos []Todo
128+
err := d.GetTodosByUserID(&todos, user.ID.String())
129+
if err != nil {
130+
return nil, err
131+
}
132+
completionRates := make([]float64, 7)
133+
for i := 0; i < 7; i++ {
134+
var completed, total int
135+
for _, todo := range todos {
136+
if todo.CreatedAt.After(time.Now().AddDate(0, 0, -i)) {
137+
total++
138+
if todo.Done {
139+
completed++
140+
}
141+
}
142+
}
143+
if total == 0 {
144+
completionRates[i] = 0
145+
continue
146+
}
147+
completionRates[i] = float64(completed) / float64(total)
148+
}
149+
return completionRates, nil
150+
}
151+
152+
func (d Connection) GetTodosCountByUser(user *User) (int, error) {
153+
var todos []Todo
154+
err := d.GetTodosByUserID(&todos, user.ID.String())
155+
if err != nil {
156+
return 0, err
157+
}
158+
return len(todos), nil
159+
}
160+
161+
func (d Connection) GetActivityHistoryForLast7Days(user *User) ([]int, error) {
162+
// if the user created a todo then return 1, else return 0
163+
var todos []Todo
164+
err := d.GetTodosByUserID(&todos, user.ID.String())
165+
if err != nil {
166+
return nil, err
167+
}
168+
169+
activityHistory := make([]int, 7)
170+
for i := 0; i < 7; i++ {
171+
var activity int
172+
for _, todo := range todos {
173+
if todo.CreatedAt.After(time.Now().AddDate(0, 0, -i)) {
174+
activity++
175+
}
176+
}
177+
activityHistory[i] = activity
178+
}
179+
180+
return activityHistory, nil
181+
}
182+
107183
func (d Connection) CreateTodo(todo *Todo) error {
108184
todo.CreatedAt = time.Now()
109185
todo.UpdatedAt = time.Now()

test_api/todo/db/models.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@ import (
77
)
88

99
type Todo struct {
10-
ID uuid.UUID `json:"id" gorm:"id"`
11-
Text string `json:"text" gorm:"text"`
12-
Done bool `json:"done" gorm:"done"`
13-
UserID uuid.UUID `json:"userId" gorm:"userId"`
14-
User *User `json:"user" gorm:"-"`
15-
CreatedAt time.Time `json:"createdAt" gorm:"createdAt"`
16-
UpdatedAt time.Time `json:"updatedAt" gorm:"updatedAt"`
10+
ID uuid.UUID `json:"id" gorm:"id"`
11+
Text string `json:"text" gorm:"text"`
12+
Done bool `json:"done" gorm:"done"`
13+
UserID uuid.UUID `json:"userId" gorm:"userId"`
14+
User *User `json:"user" gorm:"-"`
15+
CreatedAt time.Time `json:"createdAt" gorm:"createdAt"`
16+
UpdatedAt time.Time `json:"updatedAt" gorm:"updatedAt"`
17+
Meta map[string]interface{} `json:"meta" gorm:"-"`
18+
ActivityHistory []map[string]interface{} `json:"activityHistory" gorm:"-"`
1719
}
1820

1921
type User struct {
20-
ID uuid.UUID `json:"id" gorm:"id"`
21-
Name string `json:"name" gorm:"name"`
22-
Email string `json:"email" gorm:"email"`
23-
Username string `json:"username" gorm:"username"`
24-
Tags []string `json:"tags" gorm:"-"`
25-
CreatedAt time.Time `json:"createdAt" gorm:"createdAt"`
26-
UpdatedAt time.Time `json:"updatedAt" gorm:"updatedAt"`
27-
Todos []Todo `json:"todos" gorm:"-"`
28-
Meta map[string]interface{} `json:"meta" gorm:"-"`
22+
ID uuid.UUID `json:"id" gorm:"id"`
23+
Name string `json:"name" gorm:"name"`
24+
Email string `json:"email" gorm:"email"`
25+
Username string `json:"username" gorm:"username"`
26+
Tags []string `json:"tags" gorm:"-"`
27+
CreatedAt time.Time `json:"createdAt" gorm:"createdAt"`
28+
UpdatedAt time.Time `json:"updatedAt" gorm:"updatedAt"`
29+
Todos []Todo `json:"todos" gorm:"-"`
30+
Meta map[string]interface{} `json:"meta" gorm:"-"`
31+
TodosCount int `json:"todosCount" gorm:"-"`
32+
CompletionRate float64 `json:"completionRate" gorm:"-"`
33+
CompletionRateLast7Days []float64 `json:"completionRateLast7Days" gorm:"-"`
34+
ActivityStreak7Days []int `json:"activityStreak7Days" gorm:"-"`
2935
}

0 commit comments

Comments
 (0)