-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_model.go
More file actions
162 lines (136 loc) · 4.07 KB
/
stats_model.go
File metadata and controls
162 lines (136 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"database/sql"
"fmt"
"log"
"os"
"time"
)
type CreatedByType string
const (
CreateTypeClient CreatedByType = "client"
CreateTypeAdmin CreatedByType = "admin"
)
type ActivityLog struct {
ID int `json:"id"`
EventType string `json:"event_type"`
UserID string `json:"user_id"`
CreatedBy CreatedByType `json:"created_by"`
CreateDate time.Time `json:"create_date"`
}
type ActivityLogProtocol struct {
DB *sql.DB
}
const activityLogCreateTable string = `
CREATE TABLE IF NOT EXISTS activity_logs (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
event_type TEXT,
user_id TEXT,
create_by TEXT,
created_date DATETIME
);`
func NewActivityLogProtocol() (*ActivityLogProtocol, error) {
sqlPath := "./data/events.db"
isLocal := os.Getenv("IS_LOCAL")
if isLocal == "" {
sqlPath = "/data/events.db"
}
db, err := sql.Open("sqlite3", sqlPath)
if err != nil {
return nil, err
}
if _, err := db.Exec(activityLogCreateTable); err != nil {
return nil, err
}
return &ActivityLogProtocol{
DB: db,
}, nil
}
func (ap *ActivityLogProtocol) InsertActivity(activity ActivityLog) error {
insertStudentSQL := `INSERT INTO activity_logs(event_type, user_id, create_by, created_date) VALUES (?, ?, ?, ?)`
statement, err := ap.DB.Prepare(insertStudentSQL) // Prepare statement.
// This is good to avoid SQL injections
if err != nil {
fmt.Printf("error while preparing db activity : %+v\n", activity)
return err
}
_, err = statement.Exec(activity.EventType, activity.UserID, activity.CreatedBy, activity.CreateDate)
if err != nil {
fmt.Printf("error while inserting db activity : %+v\n", activity)
return err
}
return nil
}
func (ap *ActivityLogProtocol) GetCountForEvents(eventType string, createdBy CreatedByType) (int, error) {
row, err := ap.DB.Query("SELECT COUNT(*) AS record_count FROM activity_logs WHERE event_type = ? AND create_by = ? AND created_date >= datetime('now', '-7 days')", eventType, createdBy)
if err != nil {
log.Fatal(err)
}
defer row.Close()
var count int
for row.Next() { // Iterate and fetch the records from result cursor
row.Scan(&count)
}
return count, nil
}
func (ap *ActivityLogProtocol) EventCountOverTime(eventType string) (count []int, err error) {
row, err := ap.DB.Query("select count(created_date) from activity_logs where event_type = ? and created_date >= datetime('now', '-7 days') group by strftime('%d', created_date) order by strftime('%d', created_date)", eventType)
if err != nil {
return
}
defer row.Close()
for row.Next() {
var dayCount int
if err = row.Scan(&dayCount); err != nil {
return
}
count = append(count, dayCount)
}
return
}
func (ap *ActivityLogProtocol) EventCountByUser() (data []ActivityStatsDataPoint, err error) {
row, err := ap.DB.Query("select count(*), user_id, event_type from activity_logs where created_date >= datetime('now', '-7 days') group by user_id ")
if err != nil {
return
}
defer row.Close()
for row.Next() {
var event ActivityStatsDataPoint
if err = row.Scan(&event.Count, &event.Key, &event.Label); err != nil {
return
}
client, err := GetClient(event.Key)
event.Key = "Demo Client"
if err == nil {
firstName, ok := client["givenName"]
if ok && firstName != "" {
familyName, ok := client["familyName"]
if ok && familyName != "" {
event.Key = fmt.Sprintf("%s %s", firstName, familyName)
}
}
}
data = append(data, event)
}
return
}
type ActivityStatsResponse struct {
Data []ActivityStatsInfo `json:"data"`
}
type ActivityStatsType string
const (
ActivityStatsTypePie ActivityStatsType = "pie"
ActivityStatsTypeLine ActivityStatsType = "line"
ActivityStatsTypeBarSingle ActivityStatsType = "bar-single"
ActivityStatsTypeBarMulti ActivityStatsType = "bar-multiple"
)
type ActivityStatsInfo struct {
Type ActivityStatsType `json:"type"`
Title string `json:"title"`
Data []ActivityStatsDataPoint `json:"data"`
}
type ActivityStatsDataPoint struct {
Key string `json:"key"`
Label string `json:"label"`
Count int `json:"count"`
}