-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix duplicate statistics when calling multiple times (#38)
- Loading branch information
Showing
11 changed files
with
158 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--format documentation | ||
--require spec_helper | ||
--order rand | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v1.6.6 | ||
v1.7.-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module WireGuard | ||
# This class returns current data on WG server statistics | ||
class StatParser | ||
def initialize | ||
@raw_data = StatGenerator.show | ||
@result = {} | ||
end | ||
|
||
def parse | ||
return {} if raw_data.nil? or raw_data.empty? | ||
|
||
parse_data(raw_data.split("\n")) | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :raw_data, :result | ||
|
||
def parse_data(data) | ||
data.each do |line| | ||
peer_data = line.strip.split | ||
parse_wg_line(peer_data) | ||
end | ||
end | ||
|
||
def parse_wg_line(peer_data) | ||
case peer_data.first | ||
when 'peer:' | ||
result[peer_data.last] = {} | ||
@last_peer = peer_data.last | ||
when 'latest' | ||
result[@last_peer][:last_online] = build_latest_data(peer_data) | ||
when 'transfer:' | ||
result[@last_peer][:traffic] = build_traffic_data(peer_data) | ||
end | ||
end | ||
|
||
def build_latest_data(data) | ||
data[-3..]&.join(' ') | ||
end | ||
|
||
def build_traffic_data(data) | ||
{ | ||
received: data[-6..-5]&.join(' '), | ||
sent: data[-3..-2]&.join(' ') | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe WireGuard::StatParser do | ||
subject(:parse) { described_class.new.parse } | ||
|
||
before do | ||
allow(WireGuard::StatGenerator).to receive_messages(show: wg_show_stub) | ||
end | ||
|
||
context 'when all data is present' do | ||
let(:wg_show_stub) { File.read('spec/fixtures/stat.txt') } | ||
let(:expected_result) do | ||
{ | ||
'LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=' => { | ||
last_online: '25 seconds ago', | ||
traffic: { | ||
received: '56.28 MiB', | ||
sent: '1.35 GiB' | ||
} | ||
}, | ||
'hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=' => { | ||
last_online: '30 seconds ago', | ||
traffic: { | ||
received: '199.29 MiB', | ||
sent: '722.39 MiB' | ||
} | ||
}, | ||
'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=' => { | ||
last_online: '13 seconds ago', | ||
traffic: { | ||
received: '62.44 MiB', | ||
sent: '3.21 GiB' | ||
} | ||
} | ||
} | ||
end | ||
|
||
it 'returns the expected result' do | ||
expect(parse).to eq(expected_result) | ||
end | ||
end | ||
|
||
context 'when data is not available for all clients' do | ||
let(:wg_show_stub) { File.read('spec/fixtures/stat_with_empty.txt') } | ||
let(:expected_result) do | ||
{ | ||
'LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=' => {}, | ||
'hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=' => {}, | ||
'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=' => { | ||
last_online: '13 seconds ago', | ||
traffic: { | ||
received: '62.44 MiB', | ||
sent: '3.21 GiB' | ||
} | ||
} | ||
} | ||
end | ||
|
||
it 'returns the expected result' do | ||
expect(parse).to eq(expected_result) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters