This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.coffee
169 lines (139 loc) · 4.64 KB
/
model.coffee
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
config = require './config'
pg = require 'pg'
class DataStorage
constructor: (databaseUri, callback) ->
@db = new pg.Client(databaseUri)
@db.connect callback
upsertDailyData: (data, date) ->
for ip, datum of data
result = @db.query "SELECT upsert_daily($1, $2, $3, $4)",
[ip, date, datum.upload, datum.download]
upsertHourlyData: (data, date) ->
for ip, datum of data
result = @db.query "SELECT upsert_hourly($1, $2, $3, $4)",
[ip, date, datum.upload, datum.download]
getDataFromDate: (date, callback) ->
@db.query "SELECT * FROM daily ORDER BY ip ASC WHERE date = $1", [date], callback
getDataFromIP: (ip, since, callback) ->
due = new Date(since.getTime())
due.setMonth(due.getMonth()+1)
@db.query "SELECT * FROM daily WHERE ip = $1 AND date >= $2 AND date < $3 ORDER BY date ASC", [ip, since, due], callback
getData: (ip, date, callback) ->
@db.query "SELECT * FROM daily WHERE ip = $1 AND date = $2", [ip, date], callback
getHourlyData: (ip, since, to, callback) ->
@db.query "SELECT * FROM hourly WHERE ip = $1 AND time >= $2 AND time < $3 ORDER BY time DESC", [ip, since, to], callback
getLatestDailyData: (callback) ->
date = new Date
date.setHours(0,0,0,0)
@db.query "SELECT * FROM daily WHERE date = $1", [date], callback
getLatestHourlyData: (callback) ->
date = new Date
date.setMinutes(0,0,0)
@db.query "SELECT * FROM hourly WHERE time = $1", [date], callback
class Data
constructor: (data) ->
if data
@upload = data.upload
@download = data.download
else
@upload = 0
@download = 0
getTotal: -> @upload + @download
addUpload: (bytes) -> @upload += bytes
addDownload: (bytes) -> @download += bytes
getUploadString: -> (@upload/1048576).toFixed(2)
getDownloadString: -> (@download/1048576).toFixed(2)
getTotalString: -> (@getTotal()/1048576).toFixed(2)
class Collection
constructor: (date) ->
@data = {}
@rotated = false
@date = date
getIp: (ip, createNew=false) ->
if not (ip of @data)
@data[ip] = new Data
return @data[ip]
setIp: (ip, upload, download) ->
ip = @getIp ip, true
ip.upload = upload
ip.download = download
return ip
rotate: (date) ->
@oldData = @data
@data = {}
@oldDate = @date
@date = date
@rotated = true
deleteOld: ->
@rotated = false
delete @oldData
delete @oldDate
class DailyCollection extends Collection
constructor: (@dataStorage)->
date = new Date
date.setHours(0,0,0,0)
super date
rotate: ->
date = new Date
date.setHours(0,0,0,0)
super date
save: ->
if @rotated
data = @oldData
date = @oldDate
else
data = @data
date = @date
@dataStorage.upsertDailyData data, date
@deleteOld()
restore: (callback) ->
collection = this
@dataStorage.getLatestDailyData (error, result) ->
collection.setIp data.ip, data.upload, data.download for data in result.rows
callback() if callback
getHistory: (ip, month, callback)->
console.log month
@dataStorage.getDataFromIP ip, month, (error, result) ->
callback error, result
class HourlyCollection extends Collection
constructor: (@dataStorage)->
date = new Date
date.setMinutes(0,0,0)
super date
rotate: ->
date = new Date
date.setMinutes(0,0,0)
super date
save: ->
if @rotated
data = @oldData
date = @oldDate
else
data = @data
date = @date
@dataStorage.upsertHourlyData data, date
@deleteOld()
restore: (callback) ->
collection = this
@dataStorage.getLatestHourlyData (error, result) ->
collection.setIp data.ip, data.upload, data.download for data in result.rows
callback() if callback
getHistoryPlot: (ip, callback)->
hourlyIpData = @getIp ip
recordingTime = @date.getTime()
since = new Date(recordingTime-86400000)
@dataStorage.getHourlyData ip, since, @date, (error, data) ->
historyPlot=[{label: 'Download', data: []}, {label: 'Upload', data: []}]
for row in data.rows
time = row.time.getTime()
historyPlot[0].data.push [time, row.download/1048576]
historyPlot[1].data.push [time, row.upload/1048576]
# reverse the array to get the corrected order
historyPlot[0].data.reverse()
historyPlot[1].data.reverse()
historyPlot[0].data.push [recordingTime, hourlyIpData.download/1048576]
historyPlot[1].data.push [recordingTime, hourlyIpData.upload/1048576]
callback (historyPlot)
dataStorage = new DataStorage config.databaseUri
exports.hourly = new HourlyCollection dataStorage
exports.daily = new DailyCollection dataStorage