-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
142 lines (134 loc) · 3.16 KB
/
get.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
package database
import (
"database/sql"
"time"
)
// Get searches for and returns a the corresponding entry from the
// database if the entry exists.
func (db *DB) Get(id string) (result Node, err error) {
// Attempt to grab lock.
db.lock.Lock()
defer db.lock.Unlock()
// Ping database to check that it still exists.
err = db.conn.Ping()
if err != nil {
return result, err
}
return db.get(id)
}
func (db *DB) get(id string) (result Node, err error) {
row, err := db.conn.Query("SELECT * FROM nodes WHERE id = ?", id)
if err != nil {
return result, err
}
defer row.Close()
if !row.Next() {
return result, sql.ErrNoRows
}
err = row.Scan(
&result.ID,
&result.Username,
&result.Email,
&result.TotalSpace,
&result.UsedSpace,
&result.LastSeen,
&result.FirstSeen,
)
return result, err
}
// GetAllOld returns all of the old node entries in the database in a channel.
func (db *DB) GetAllOlderThan(input time.Duration) (result []Node, err error) {
// Attempt to grab lock.
db.lock.Lock()
defer db.lock.Unlock()
// Ping database to check that it still exists.
err = db.conn.Ping()
if err != nil {
return result, err
}
past := time.Now().Add(-1 * input)
// Query for nodes that have not check in within the last 24 hours.
rows, err := db.conn.Query("SELECT id, email FROM nodes WHERE last_seen < ?", past)
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
var node Node
err = rows.Scan(
&node.ID,
&node.Email)
if err != nil {
return result, err
}
result = append(result, node)
}
return result, nil
}
// GetPoolSize return the sum the nodes total space and used space.
func (db *DB) GetPoolSize() (total float64, used float64, err error) {
// Attempt to grab lock.
db.lock.Lock()
defer db.lock.Unlock()
// Ping database to check that it still exists.
err = db.conn.Ping()
if err != nil {
return total, used, err
}
// Get total pool size from sum of nodes reported values.
row, err := db.conn.Query("SELECT SUM(total_space) FROM nodes")
if err != nil {
return total, used, err
}
defer row.Close()
if !row.Next() {
return total, used, sql.ErrNoRows
}
err = row.Scan(&total)
if err != nil {
return total, used, err
}
err = row.Close()
if err != nil {
return total, used, err
}
// Get used pool size from sum of nodes reported values.
row, err = db.conn.Query("SELECT SUM(used_space) FROM nodes")
if err != nil {
return total, used, err
}
defer row.Close()
if !row.Next() {
return total, used, sql.ErrNoRows
}
err = row.Scan(&used)
if err != nil {
return total, used, err
}
return total, used, nil
}
// GetNodesOnline calculates the number of nodes reporting to the database.
func (db *DB) GetNodesOnline() (nodes int, err error) {
// Attempt to grab lock.
db.lock.Lock()
defer db.lock.Unlock()
// Ping database to check that it still exists.
err = db.conn.Ping()
if err != nil {
return -1, err
}
// Get total number of nodes reporting.
row, err := db.conn.Query("SELECT COUNT(id) FROM nodes")
if err != nil {
return -1, err
}
defer row.Close()
if !row.Next() {
return nodes, sql.ErrNoRows
}
err = row.Scan(&nodes)
if err != nil {
return -1, err
}
return nodes, nil
}