1+ package main
2+
3+ // Stand alone (non-daemon) utility to analyse various aspects of user resource usage
4+ // At some point this may be turned into a daemon, but for now it's likely just to be
5+ // run from cron on a periodic basis (ie every few hours)
6+
7+ import (
8+ "fmt"
9+ "log"
10+
11+ "github.com/docker/go-units"
12+ com "github.com/sqlitebrowser/dbhub.io/common"
13+ )
14+
15+ var (
16+ Debug = false
17+ )
18+
19+ func main () {
20+ // Read server configuration
21+ err := com .ReadConfig ()
22+ if err != nil {
23+ log .Fatalf ("Configuration file problem: '%s'" , err )
24+ }
25+
26+ // Connect to PostgreSQL server
27+ err = com .ConnectPostgreSQL ()
28+ if err != nil {
29+ log .Fatal (err )
30+ }
31+
32+ // Connect to MQ server
33+ com .Conf .Live .Nodename = "Usage Analysis"
34+ com .AmqpChan , err = com .ConnectMQ ()
35+ if err != nil {
36+ log .Fatal (err )
37+ }
38+
39+ // Get the list of all users with at least one database
40+ userList , err := com .AnalysisUsersWithDBs ()
41+ if err != nil {
42+ log .Fatalln (err )
43+ }
44+
45+ type dbSizes struct {
46+ Live int64
47+ Standard int64
48+ }
49+ userStorage := make (map [string ]dbSizes )
50+
51+ // Loop through the users, calculating the total disk space used by each
52+ for user , numDBs := range userList {
53+ if Debug {
54+ fmt .Printf ("User: %s, # databases: %d\n " , user , numDBs )
55+ }
56+
57+ // Get the list of standard databases for a user
58+ dbList , err := com .UserDBs (user , com .DB_BOTH )
59+ if err != nil {
60+ log .Fatal (err )
61+ }
62+
63+ // For each standard database, count the list of commits and amount of space used
64+ var spaceUsedStandard int64
65+ for _ , db := range dbList {
66+ commitList , err := com .GetCommitList (user , db .Database )
67+ if err != nil {
68+ log .Println (err )
69+ }
70+
71+ // Calculate space used by standard databases
72+ for _ , commit := range commitList {
73+ tree := commit .Tree .Entries
74+ for _ , j := range tree {
75+ spaceUsedStandard += j .Size
76+ }
77+ }
78+
79+ if Debug {
80+ fmt .Printf ("User: %s, Standard database: %s, # Commits: %d, Space used: %s\n " , user , db .Database , len (commitList ), units .HumanSize (float64 (spaceUsedStandard )))
81+ }
82+ }
83+
84+ // Get the list of live databases for a user
85+ liveList , err := com .LiveUserDBs (user , com .DB_BOTH )
86+ if err != nil {
87+ log .Fatal (err )
88+ }
89+
90+ // For each live database, get the amount of space used
91+ var spaceUsedLive int64
92+ for _ , db := range liveList {
93+ _ , liveNode , err := com .CheckDBLive (user , db .Database )
94+ if err != nil {
95+ log .Fatal (err )
96+ return
97+ }
98+
99+ // Ask our AMQP backend for the database size
100+ z , err := com .LiveSize (liveNode , user , user , db .Database )
101+ if err != nil {
102+ log .Fatal (err )
103+ }
104+ spaceUsedLive += z
105+
106+ if Debug {
107+ fmt .Printf ("User: %s, Live database: %s, Space used: %s\n " , user , db .Database , units .HumanSize (float64 (spaceUsedLive )))
108+ }
109+ }
110+ userStorage [user ] = dbSizes {Standard : spaceUsedStandard , Live : spaceUsedLive }
111+ }
112+
113+ // Store the information in our PostgreSQL backend
114+ for user , z := range userStorage {
115+ err = com .AnalysisRecordUserStorage (user , z .Standard , z .Live )
116+ if err != nil {
117+ log .Fatalln ()
118+ }
119+ }
120+
121+ log .Printf ("%s run complete" , com .Conf .Live .Nodename )
122+ }
0 commit comments