Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Add max queue age stats report item #105

Merged
merged 3 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ssh_scan_api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class API < Sinatra::Base
"ERRORED" => settings.db.error_count,
"COMPLETED" => settings.db.complete_count,
},
"QUEUED_MAX_AGE" => settings.db.queued_max_age,
"GRADE_REPORT" => settings.db.grade_report
}.to_json
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ssh_scan_api/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def error_count
@database.error_count
end

def queued_max_age
@database.queued_max_age
end

def complete_count
@database.complete_count
end
Expand Down
15 changes: 15 additions & 0 deletions lib/ssh_scan_api/database/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def queue_scan(uuid, socket)
"port" => socket["port"].to_i,
"status" => "QUEUED",
"scan" => nil,
"queue_time" => Time.now,
"worker_id" => nil,
)
end
Expand All @@ -54,6 +55,20 @@ def total_count
@collection.count
end

# The age of the oldest record in QUEUED state, in seconds
def queued_max_age
max_age = 0

@collection.find(status: 'QUEUED').each do |item|
age = Time.now - item["queue_time"]
if age > max_age
max_age = age
end
end

return max_age
end

def run_scan(uuid)
@collection.find(uuid: uuid).update_one(
'$set'=> { 'status' => 'RUNNING' }
Expand Down
8 changes: 8 additions & 0 deletions spec/ssh_scan_api/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def app
}.to_json)
end

# it "should generate a stats report" do
# get "/api/v1/stats"
# expect(last_response.status).to eql(200)
# expect(last_response.body["SCAN_STATES"]).not_to be nil
# expect(last_response.body["QUEUED_MAX_AGE"]).not_to be nil
# expect(last_response.body["GRADE_REPORT"]).not_to be nil
# end

it "should return string uuid" do
ip = "192.168.1.1"
port = 22
Expand Down
29 changes: 29 additions & 0 deletions spec/ssh_scan_api/database/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,33 @@
doc = docs.first
expect(doc["uuid"]).to eql(uuid2)
end

it "should be able to find the max queue age" do
uuid1 = SecureRandom.uuid
uuid2 = SecureRandom.uuid

socket1 = {"target" => "127.0.0.1", "port" => 1337}
socket2 = {"target" => "127.0.0.2", "port" => 1337}

@mongodb.queue_scan(uuid1, socket1)
sleep 5
@mongodb.queue_scan(uuid2, socket2)

age = @mongodb.queued_max_age

expect(age).to be > 5.0
expect(age).to be < 6.0
end

it "should return zero when there are no queued scans" do
uuid1 = SecureRandom.uuid
socket1 = {"target" => "127.0.0.1", "port" => 1337}

@mongodb.queue_scan(uuid1, socket1)
@mongodb.run_scan(uuid1)

age = @mongodb.queued_max_age
expect(age).to eql(0)
end

end
1 change: 1 addition & 0 deletions spec/ssh_scan_api/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
expect(@abstract_database.respond_to?(:next_scan_in_queue)).to be true
expect(@abstract_database.respond_to?(:find_recent_scans)).to be true
expect(@abstract_database.respond_to?(:find_scans)).to be true
expect(@abstract_database.respond_to?(:queued_max_age)).to be true
end

it "should defer #run_count calls to the specific DB implementation" do
Expand Down