-
Notifications
You must be signed in to change notification settings - Fork 22
/
monitors.go
140 lines (112 loc) · 3.1 KB
/
monitors.go
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
package usage
import (
"context"
"fmt"
"github.com/tom-draper/api-analytics/server/database"
)
func HourlyMonitorsCount() (int, error) {
return MonitorsCount("1 hour")
}
func DailyMonitorsCount() (int, error) {
return MonitorsCount("24 hours")
}
func WeeklyMonitorsCount() (int, error) {
return MonitorsCount("7 days")
}
func MonthlyMonitorsCount() (int, error) {
return MonitorsCount("30 days")
}
func TotalMonitorsCount() (int, error) {
return MonitorsCount("")
}
func MonitorsCount(interval string) (int, error) {
conn := database.NewConnection()
defer conn.Close(context.Background())
var count int
query := "SELECT COUNT(*) FROM monitor"
if interval != "" {
query += fmt.Sprintf(" WHERE created_at >= NOW() - interval '%s'", interval)
}
query += ";"
err := conn.QueryRow(context.Background(), query).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func HourlyMonitors() ([]database.MonitorRow, error) {
return Monitors("1 hour")
}
func DailyMonitors() ([]database.MonitorRow, error) {
return Monitors("24 hours")
}
func WeeklyMonitors() ([]database.MonitorRow, error) {
return Monitors("7 days")
}
func MonthlyMonitors() ([]database.MonitorRow, error) {
return Monitors("30 days")
}
func TotalMonitors() ([]database.MonitorRow, error) {
return Monitors("")
}
func Monitors(interval string) ([]database.MonitorRow, error) {
conn := database.NewConnection()
defer conn.Close(context.Background())
query := "SELECT api_key, url, secure, ping, created_at FROM monitor"
if interval != "" {
query += fmt.Sprintf(" WHERE created_at >= NOW() - interval '%s'", interval)
}
query += " ORDER BY created_at;"
rows, err := conn.Query(context.Background(), query)
if err != nil {
return nil, err
}
defer rows.Close()
var monitors []database.MonitorRow
for rows.Next() {
var monitor database.MonitorRow
err := rows.Scan(&monitor.APIKey, &monitor.URL, &monitor.Secure, &monitor.Ping, &monitor.CreatedAt)
if err == nil {
monitors = append(monitors, monitor)
}
}
return monitors, nil
}
func HourlyUserMonitors() ([]UserCount, error) {
return UserMonitors("1 hour")
}
func DailyUserMonitors() ([]UserCount, error) {
return UserMonitors("24 hours")
}
func WeeklyUserMonitors() ([]UserCount, error) {
return UserMonitors("7 days")
}
func MonthlyUserMonitors() ([]UserCount, error) {
return UserMonitors("30 days")
}
func TotalUserMonitors() ([]UserCount, error) {
return UserMonitors("")
}
func UserMonitors(interval string) ([]UserCount, error) {
conn := database.NewConnection()
defer conn.Close(context.Background())
query := "SELECT api_key, COUNT(*) AS count FROM monitor"
if interval != "" {
query += fmt.Sprintf(" WHERE created_at >= NOW() - interval '%s'", interval)
}
query += " GROUP BY api_key ORDER BY count DESC;"
rows, err := conn.Query(context.Background(), query)
if err != nil {
return nil, err
}
defer rows.Close()
var monitors []UserCount
for rows.Next() {
userMonitors := new(UserCount)
err := rows.Scan(&userMonitors.APIKey, &userMonitors.Count)
if err == nil {
monitors = append(monitors, *userMonitors)
}
}
return monitors, nil
}