2021-10-08 00:39:06 +00:00
|
|
|
require 'sinatra/activerecord'
|
|
|
|
|
2021-10-09 00:48:07 +00:00
|
|
|
Time.zone = "America/Campo_Grande"
|
2023-09-12 02:25:26 +00:00
|
|
|
ActiveRecord.default_timezone = :local
|
2021-10-09 00:48:07 +00:00
|
|
|
|
2020-06-03 03:02:01 +00:00
|
|
|
class Rate < ActiveRecord::Base
|
2021-10-09 14:43:29 +00:00
|
|
|
def self.transfer(type_transfer)
|
|
|
|
self.find_rate.collect {
|
|
|
|
|p| [p.created_at.strftime('%d/%m %H:%M'), p.public_send(type_transfer)]
|
|
|
|
}
|
2020-06-03 03:02:01 +00:00
|
|
|
end
|
|
|
|
|
2021-10-09 14:43:29 +00:00
|
|
|
def self.extremes(type, value)
|
2021-11-03 02:44:27 +00:00
|
|
|
hash = self.group("date(created_at)").public_send(type, value)
|
|
|
|
hash.transform_keys!{ |k| k.to_date.strftime('%d-%m') }
|
|
|
|
hash.transform_values!{ |v| v.ceil(2) }
|
2021-10-09 03:18:03 +00:00
|
|
|
end
|
|
|
|
|
2021-10-09 14:43:29 +00:00
|
|
|
def self.find_rate
|
|
|
|
where(created_at: Date.today.all_day)
|
2021-10-09 03:18:03 +00:00
|
|
|
end
|
|
|
|
|
2023-09-09 23:08:04 +00:00
|
|
|
def self.last_time
|
|
|
|
self.transfer('download').last.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.last_value(type)
|
|
|
|
self.transfer(type).last.last
|
|
|
|
end
|
|
|
|
|
2021-10-08 00:39:06 +00:00
|
|
|
def self.save
|
2023-09-12 02:46:41 +00:00
|
|
|
results = self.run_speedtest
|
2023-09-12 02:26:21 +00:00
|
|
|
if results
|
|
|
|
Rate.create({
|
|
|
|
download: results[:download],
|
|
|
|
upload: results[:upload],
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-12 02:46:41 +00:00
|
|
|
def self.run_speedtest
|
2023-09-12 02:47:47 +00:00
|
|
|
output = `speedtest --accept-license --accept-gdpr`
|
2023-09-12 02:26:21 +00:00
|
|
|
if $?.success?
|
|
|
|
download_speed = output.match(/Download:\s+(\d+\.\d+)\sMbps/)[1]
|
|
|
|
upload_speed = output.match(/Upload:\s+(\d+\.\d+)\sMbps/)[1]
|
|
|
|
{
|
|
|
|
download: download_speed.to_f,
|
|
|
|
upload: upload_speed.to_f
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2021-10-08 00:39:06 +00:00
|
|
|
end
|
2020-06-03 03:02:01 +00:00
|
|
|
end
|