-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
149 lines (129 loc) · 3.82 KB
/
handler.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
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
func indexHandler(e *echo.Echo) echo.HandlerFunc {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]interface{}{
"api": "latenightcommits",
"resources": e.Routes(),
})
}
}
func commitLogHandler(db *gorm.DB) echo.HandlerFunc {
type CommitLogResponse struct {
Log []*Commit `json:"log"`
HasNextPage bool `json:"has_next_page"`
}
return func(c echo.Context) error {
page, _ := strconv.Atoi(c.QueryParam("page"))
if page < 1 {
page = 1
}
limit, _ := strconv.Atoi(c.QueryParam("per_page"))
if limit < 1 {
limit = 20
}
if limit > 1000 {
limit = 1000
}
var commits []*Commit
offset := (page - 1) * limit
dbSession := db.Session(&gorm.Session{PrepareStmt: true})
if err := dbSession.Raw(`
SELECT c.*
FROM commits c
INNER JOIN (
SELECT id FROM commits ORDER BY created_at DESC LIMIT ? OFFSET ?
) cid ON c.id = cid.id
ORDER BY created_at DESC
`, limit, offset).Scan(&commits).Error; err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to retrieve commits")
}
return c.JSON(http.StatusOK, CommitLogResponse{
Log: commits,
HasNextPage: len(commits) == limit,
})
}
}
func statsHandler(db *gorm.DB) echo.HandlerFunc {
type DailyStatsResults struct {
Date time.Time
Count int64
}
type CachedStats struct {
DailyStats []*DailyStatsResults
TotalCommits int64
}
var statsCache *CachedStats
// Clear cache every 5 minutes
go func() {
for range time.Tick(5 * time.Minute) {
statsCache = nil
}
}()
return func(c echo.Context) error {
// If cache is empty, perform data fetching
if statsCache == nil {
statsCache = &CachedStats{}
dbSession := db.Session(&gorm.Session{PrepareStmt: true})
err := dbSession.
Raw(`
SELECT COUNT(id) as 'count', DATE(created_at) as 'date'
FROM commits
WHERE created_at >= (CURRENT_DATE - INTERVAL 7 DAY)
GROUP BY date
ORDER BY date DESC
LIMIT 7;
`).Scan(&statsCache.DailyStats).Error
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to retrieve stats")
}
err = dbSession.Raw(`SELECT COUNT(id) FROM commits;`).Scan(&statsCache.TotalCommits).Error
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to retrieve stats total")
}
}
// Setup global chart options
globalOpts := charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemeWesteros,
})
// Prep dates stats chart
datesChart := charts.NewBar()
datesChart.SetGlobalOptions(globalOpts,
charts.WithTitleOpts(opts.Title{
Title: "Commit Stats by date",
Subtitle: fmt.Sprintf(
"🤓 total commits added each day over the last 7 days - out of %s in total",
formatNumber(statsCache.TotalCommits),
),
}),
charts.WithTooltipOpts(opts.Tooltip{Show: true}),
)
// Prepare data to fit into chart
dates := make([]string, 0)
data := make([]opts.BarData, 0)
// Loop through stats in reverse order since we want to display the date ascending from right to left
for i := len(statsCache.DailyStats) - 1; i >= 0; i-- {
row := statsCache.DailyStats[i]
dates = append(dates, row.Date.Format("2006/01/02"))
data = append(data, opts.BarData{Value: row.Count})
}
// Put dates data into chart
datesChart.SetXAxis(dates).AddSeries("Commits on date", data)
// Setup page to hold all charts
page := components.NewPage()
page.PageTitle = "Stats | LateNightCommits"
page.AddCharts(datesChart)
return page.Render(c.Response())
}
}