Skip to content

Commit 1f2ec79

Browse files
authored
Traffic and last online as a numeric data type (#87)
1 parent fe3c116 commit 1f2ec79

File tree

17 files changed

+134
-105
lines changed

17 files changed

+134
-105
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ group :test do
3333
gem 'rack-test', '~> 2.1'
3434
gem 'rspec', '~> 3.13'
3535
gem 'simplecov', require: false
36+
gem 'timecop', '~> 0.9.10'
3637
gem 'webmock', '~> 3.24'
3738
end
3839

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ GEM
146146
diff-lcs
147147
patience_diff
148148
tilt (2.3.0)
149+
timecop (0.9.10)
149150
unicode-display_width (2.6.0)
150151
uri (0.13.1)
151152
webmock (3.24.0)
@@ -185,6 +186,7 @@ DEPENDENCIES
185186
sinatra-contrib (~> 4.0)
186187
stackprof (~> 0.2.26)
187188
super_diff (~> 0.13.0)
189+
timecop (~> 0.9.10)
188190
webmock (~> 3.24)
189191

190192
BUNDLED WITH

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.9.9
1+
v2.0.0

app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def authorize_resource
8888
end
8989

9090
def instance_versions
91-
File.read('VERSION').gsub('v', '')
91+
File.read('VERSION').gsub('v', '').gsub("\n", '')
9292
end
9393

9494
def request_body

lib/webhooks/aggregator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def aggregate_events
3939
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
4040

4141
def calculate_event(last_data, new_data, last_event)
42-
new_data_sum = new_data[:traffic][:received].to_unit + new_data[:traffic][:sent].to_unit
43-
last_data_sum = last_data['traffic']['received'].to_unit + last_data['traffic']['sent'].to_unit
42+
new_data_sum = new_data[:traffic][:received] + new_data[:traffic][:sent]
43+
last_data_sum = last_data['traffic']['received'] + last_data['traffic']['sent']
4444

4545
if new_data_sum > last_data_sum
4646
last_event == Events::CONNECTED ? nil : Events::CONNECTED

lib/wire_guard/stat_parser.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
module WireGuard
44
# This class returns current data on WG server statistics
55
class StatParser
6+
TIME_UNITS = {
7+
'day' => 86_400,
8+
'hour' => 3600,
9+
'minute' => 60,
10+
'second' => 1
11+
}.freeze
12+
613
def initialize
714
@raw_data = StatGenerator.show
815
@result = {}
@@ -40,14 +47,24 @@ def parse_wg_line(peer_data)
4047
end
4148

4249
def build_latest_data(data)
43-
data[2..]&.join(' ')
50+
parse_time_ago(data[2..]&.join(' ')).to_s
4451
end
4552

4653
def build_traffic_data(data)
4754
{
48-
received: data[-6..-5]&.join(' '),
49-
sent: data[-3..-2]&.join(' ')
55+
received: data[-6..-5]&.join(' ')&.to_unit&.base_scalar.to_i,
56+
sent: data[-3..-2]&.join(' ')&.to_unit&.base_scalar.to_i
5057
}
5158
end
59+
60+
def parse_time_ago(time_string)
61+
total_seconds = 0
62+
63+
time_string.scan(/(\d+)\s+(day?|hour?|minute?|second?)/) do |value, unit|
64+
total_seconds += (value.to_i * TIME_UNITS[unit])
65+
end
66+
67+
Time.now - total_seconds
68+
end
5269
end
5370
end

spec/app/clients_serializer_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
before do
55
allow(WireGuard::StatGenerator).to receive_messages(show: wg_show_stub)
66
create_conf_file('spec/fixtures/wg0_stat.json', wg_stat_path)
7+
Timecop.freeze(Date.new(2024, 10, 10))
78
end
89

910
after do
1011
FileUtils.rm_rf(wg_stat_path)
12+
Timecop.return
1113
end
1214

1315
let(:key) { '4' }
@@ -42,10 +44,10 @@
4244
dns: '1.1.1.1',
4345
persistent_keepalive: 0,
4446
endpoint: '2.2.2.2:51820',
45-
last_online: '45 seconds ago',
47+
last_online: '2024-10-15 19:34:41 +0000',
4648
traffic: {
47-
received: '56.28 MiB',
48-
sent: '1.35 GiB'
49+
received: 59_013_857,
50+
sent: 1_449_551_462
4951
},
5052
data: {}
5153
}
@@ -105,10 +107,10 @@
105107
dns: '1.1.1.1',
106108
persistent_keepalive: 0,
107109
endpoint: '2.2.2.2:51820',
108-
last_online: '45 seconds ago',
110+
last_online: '2024-10-15 19:34:41 +0000',
109111
traffic: {
110-
received: '56.28 MiB',
111-
sent: '1.35 GiB'
112+
received: 59_013_857,
113+
sent: 1_449_551_462
112114
},
113115
data: {}
114116
},
@@ -124,10 +126,10 @@
124126
dns: '1.1.1.1',
125127
persistent_keepalive: 0,
126128
endpoint: '2.2.2.2:51820',
127-
last_online: '50 seconds ago',
129+
last_online: '2024-10-15 18:34:41 +0000',
128130
traffic: {
129-
received: '199.29 MiB',
130-
sent: '722.39 MiB'
131+
received: 208_970_711,
132+
sent: 757_480_816
131133
},
132134
data: {}
133135
},
@@ -143,10 +145,10 @@
143145
dns: '1.1.1.1',
144146
persistent_keepalive: 0,
145147
endpoint: '2.2.2.2:51820',
146-
last_online: '1 minute, 13 seconds ago',
148+
last_online: '2024-10-09 23:58:47 +0000',
147149
traffic: {
148-
received: '62.44 MiB',
149-
sent: '3.21 GiB'
150+
received: 65_473_085,
151+
sent: 3_446_711_255
150152
},
151153
data: {}
152154
}

spec/fixtures/empty_wg0_stat.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=": {
3-
"last_online": "45 seconds ago",
3+
"last_online": "2024-10-15 19:34:41 +0000",
44
"traffic": {
5-
"received": "56.28 MiB",
6-
"sent": "1.35 GiB"
5+
"received": 59013857,
6+
"sent": 1449551462
77
}
88
},
99
"hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=": {},
1010
"bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=": {
11-
"last_online": "43 seconds ago",
11+
"last_online": "2024-10-15 18:34:41 +0000",
1212
"traffic": {
13-
"received": "62.44 MiB",
14-
"sent": "3.21 GiB"
13+
"received": 65473085,
14+
"sent": 3446711255
1515
}
1616
}
1717
}

spec/fixtures/wg0_stat.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=": {
3-
"last_online": "45 seconds ago",
3+
"last_online": "2024-10-15 19:34:41 +0000",
44
"traffic": {
5-
"received": "56.28 MiB",
6-
"sent": "1.35 GiB"
5+
"received": 59013857,
6+
"sent": 1449551462
77
}
88
},
99
"hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=": {
10-
"last_online": "50 seconds ago",
10+
"last_online": "2024-10-15 18:34:41 +0000",
1111
"traffic": {
12-
"received": "199.29 MiB",
13-
"sent": "722.39 MiB"
12+
"received": 208970711,
13+
"sent": 757480816
1414
}
1515
},
1616
"bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=": {
17-
"last_online": "43 seconds ago",
17+
"last_online": "2024-10-15 17:34:41 +0000",
1818
"traffic": {
19-
"received": "62.44 MiB",
20-
"sent": "3.21 GiB"
19+
"received": 65473085,
20+
"sent": 3446711255
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)