Skip to content

Commit

Permalink
Traffic and last online as a numeric data type (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored Oct 15, 2024
1 parent fe3c116 commit 1f2ec79
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 105 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ group :test do
gem 'rack-test', '~> 2.1'
gem 'rspec', '~> 3.13'
gem 'simplecov', require: false
gem 'timecop', '~> 0.9.10'
gem 'webmock', '~> 3.24'
end

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ GEM
diff-lcs
patience_diff
tilt (2.3.0)
timecop (0.9.10)
unicode-display_width (2.6.0)
uri (0.13.1)
webmock (3.24.0)
Expand Down Expand Up @@ -185,6 +186,7 @@ DEPENDENCIES
sinatra-contrib (~> 4.0)
stackprof (~> 0.2.26)
super_diff (~> 0.13.0)
timecop (~> 0.9.10)
webmock (~> 3.24)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.9.9
v2.0.0
2 changes: 1 addition & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def authorize_resource
end

def instance_versions
File.read('VERSION').gsub('v', '')
File.read('VERSION').gsub('v', '').gsub("\n", '')
end

def request_body
Expand Down
4 changes: 2 additions & 2 deletions lib/webhooks/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def aggregate_events
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

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

if new_data_sum > last_data_sum
last_event == Events::CONNECTED ? nil : Events::CONNECTED
Expand Down
23 changes: 20 additions & 3 deletions lib/wire_guard/stat_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
module WireGuard
# This class returns current data on WG server statistics
class StatParser
TIME_UNITS = {
'day' => 86_400,
'hour' => 3600,
'minute' => 60,
'second' => 1
}.freeze

def initialize
@raw_data = StatGenerator.show
@result = {}
Expand Down Expand Up @@ -40,14 +47,24 @@ def parse_wg_line(peer_data)
end

def build_latest_data(data)
data[2..]&.join(' ')
parse_time_ago(data[2..]&.join(' ')).to_s
end

def build_traffic_data(data)
{
received: data[-6..-5]&.join(' '),
sent: data[-3..-2]&.join(' ')
received: data[-6..-5]&.join(' ')&.to_unit&.base_scalar.to_i,
sent: data[-3..-2]&.join(' ')&.to_unit&.base_scalar.to_i
}
end

def parse_time_ago(time_string)
total_seconds = 0

time_string.scan(/(\d+)\s+(day?|hour?|minute?|second?)/) do |value, unit|
total_seconds += (value.to_i * TIME_UNITS[unit])
end

Time.now - total_seconds
end
end
end
26 changes: 14 additions & 12 deletions spec/app/clients_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
before do
allow(WireGuard::StatGenerator).to receive_messages(show: wg_show_stub)
create_conf_file('spec/fixtures/wg0_stat.json', wg_stat_path)
Timecop.freeze(Date.new(2024, 10, 10))
end

after do
FileUtils.rm_rf(wg_stat_path)
Timecop.return
end

let(:key) { '4' }
Expand Down Expand Up @@ -42,10 +44,10 @@
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
last_online: '45 seconds ago',
last_online: '2024-10-15 19:34:41 +0000',
traffic: {
received: '56.28 MiB',
sent: '1.35 GiB'
received: 59_013_857,
sent: 1_449_551_462
},
data: {}
}
Expand Down Expand Up @@ -105,10 +107,10 @@
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
last_online: '45 seconds ago',
last_online: '2024-10-15 19:34:41 +0000',
traffic: {
received: '56.28 MiB',
sent: '1.35 GiB'
received: 59_013_857,
sent: 1_449_551_462
},
data: {}
},
Expand All @@ -124,10 +126,10 @@
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
last_online: '50 seconds ago',
last_online: '2024-10-15 18:34:41 +0000',
traffic: {
received: '199.29 MiB',
sent: '722.39 MiB'
received: 208_970_711,
sent: 757_480_816
},
data: {}
},
Expand All @@ -143,10 +145,10 @@
dns: '1.1.1.1',
persistent_keepalive: 0,
endpoint: '2.2.2.2:51820',
last_online: '1 minute, 13 seconds ago',
last_online: '2024-10-09 23:58:47 +0000',
traffic: {
received: '62.44 MiB',
sent: '3.21 GiB'
received: 65_473_085,
sent: 3_446_711_255
},
data: {}
}
Expand Down
12 changes: 6 additions & 6 deletions spec/fixtures/empty_wg0_stat.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=": {
"last_online": "45 seconds ago",
"last_online": "2024-10-15 19:34:41 +0000",
"traffic": {
"received": "56.28 MiB",
"sent": "1.35 GiB"
"received": 59013857,
"sent": 1449551462
}
},
"hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=": {},
"bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=": {
"last_online": "43 seconds ago",
"last_online": "2024-10-15 18:34:41 +0000",
"traffic": {
"received": "62.44 MiB",
"sent": "3.21 GiB"
"received": 65473085,
"sent": 3446711255
}
}
}
18 changes: 9 additions & 9 deletions spec/fixtures/wg0_stat.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=": {
"last_online": "45 seconds ago",
"last_online": "2024-10-15 19:34:41 +0000",
"traffic": {
"received": "56.28 MiB",
"sent": "1.35 GiB"
"received": 59013857,
"sent": 1449551462
}
},
"hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=": {
"last_online": "50 seconds ago",
"last_online": "2024-10-15 18:34:41 +0000",
"traffic": {
"received": "199.29 MiB",
"sent": "722.39 MiB"
"received": 208970711,
"sent": 757480816
}
},
"bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=": {
"last_online": "43 seconds ago",
"last_online": "2024-10-15 17:34:41 +0000",
"traffic": {
"received": "62.44 MiB",
"sent": "3.21 GiB"
"received": 65473085,
"sent": 3446711255
}
}
}
Loading

0 comments on commit 1f2ec79

Please sign in to comment.