-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stats.go
75 lines (61 loc) · 1.89 KB
/
stats.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
package main
import (
"github.com/getsentry/sentry-go"
"net/http"
"os"
"strconv"
)
func (app *Application) GetStats(w http.ResponseWriter, r *http.Request) {
tx, err := app.db.Begin()
if err != nil {
sentry.CaptureException(err)
http.Error(w, "cannot start transaction", http.StatusInternalServerError)
return
}
defer tx.Rollback()
videoAmount := 0
totalFileSize := 0
totalLength := 0
uniqueChannels := 0
err = tx.QueryRow("select count(id) as video_amount, "+
"sum(file_size) as total_file_size, "+
"sum(video_length) as total_length, "+
"count(distinct(channel_id)) as unique_channels "+
"from videos").Scan(&videoAmount, &totalFileSize, &totalLength, &uniqueChannels)
if err != nil {
sentry.CaptureException(err)
http.Error(w, "failed to query for videos", http.StatusInternalServerError)
return
}
usdPerGbPerMonth, err := strconv.ParseFloat(os.Getenv("S3_USD_PER_GB_PER_MONTH"), 64)
if err != nil {
sentry.CaptureException(err)
http.Error(w, "failed to parse S3_USD_PER_GB_PER_MONTH", http.StatusInternalServerError)
return
}
// 1 gb = 1'000'000'000 bytes
bytesToGb := float64(totalFileSize) / 1_000_000_000.0
s3Bill := bytesToGb * usdPerGbPerMonth
if err := tx.Commit(); err != nil {
sentry.CaptureException(err)
http.Error(w, "cannot commit transaction", http.StatusInternalServerError)
return
}
stats := Stats{
VideoAmount: videoAmount,
TotalFileSize: totalFileSize,
TotalLength: totalLength,
UniqueChannels: uniqueChannels,
S3BillPerMonth: s3Bill,
UsdPerGbPerMonth: usdPerGbPerMonth,
}
SerializeJson(w, stats)
}
type Stats struct {
VideoAmount int `json:"videoAmount"`
TotalFileSize int `json:"totalFileSize"`
TotalLength int `json:"totalLength"`
UniqueChannels int `json:"uniqueChannels"`
S3BillPerMonth float64 `json:"s3BillPerMonth"`
UsdPerGbPerMonth float64 `json:"usdPerGbPerMonth"`
}