Updown implementation (#47)

* Updown debut implementation - currently broken

* add support for updown.io

* errorStats parameter does not exist anymore removing

* add comment for read only key

* use directly the exception message when priting the error message

---------

Co-authored-by: Emilien Devos <4016501+unixfox@users.noreply.github.com>
This commit is contained in:
TheFrenchGhosty 2024-04-21 22:43:35 +02:00 committed by GitHub
parent bd6bfce1c3
commit 420ec3ea5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 28 deletions

View File

@ -31,25 +31,18 @@ INSTANCES = {} of String => Instance
spawn do spawn do
loop do loop do
monitors = [] of JSON::Any monitors = [] of JSON::Any
page = 1 begin
loop do client = HTTP::Client.new(URI.parse("https://updown.io/p/wqufo"))
begin client.connect_timeout = 10.seconds
client = HTTP::Client.new(URI.parse("https://stats.uptimerobot.com/89VnzSKAn")) client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds # Read only key for accessing the uptime checks
client.read_timeout = 10.seconds response = JSON.parse(client.get("/api/checks?api-key=ro-52iHyp6LBqQq7rGp4N7p").body)
response = JSON.parse(client.get("/api/getMonitorList/89VnzSKAn?page=#{page}").body) monitors += response.as_a
rescue ex
monitors += response["psp"]["monitors"].as_a puts "Error pulling monitors: #{ex.message}"
page += 1 break
break if response["psp"]["perPage"].as_i * (page - 1) + 1 > response["psp"]["totalMonitors"].as_i
rescue ex
error_message = response.try &.as?(String).try &.["errorStats"]?
error_message ||= ex.message
puts "Error pulling monitors: #{error_message}"
break
end
end end
begin begin
body = HTTP::Client.get(URI.parse("https://raw.githubusercontent.com/iv-org/documentation/master/docs/instances.md")).body body = HTTP::Client.get(URI.parse("https://raw.githubusercontent.com/iv-org/documentation/master/docs/instances.md")).body
rescue ex rescue ex
@ -98,7 +91,7 @@ spawn do
end end
end end
monitor = monitors.try &.select { |monitor| monitor["name"].try &.as_s == host }[0]? monitor = monitors.try &.select { |monitor| monitor["alias"].try &.as_s == host }[0]?
instances[host] = {flag: flag, region: region, stats: stats, cors: cors, api: api, type: type, uri: uri.to_s, monitor: monitor || instances[host]?.try &.[:monitor]?} instances[host] = {flag: flag, region: region, stats: stats, cors: cors, api: api, type: type, uri: uri.to_s, monitor: monitor || instances[host]?.try &.[:monitor]?}
end end
@ -151,15 +144,15 @@ static_headers do |response, filepath, filestat|
end end
SORT_PROCS = { SORT_PROCS = {
"health" => ->(name : String, instance : Instance) { -(instance[:monitor]?.try &.["30dRatio"]["ratio"].as_s.to_f || 0.0) }, "health" => ->(alias : String, instance : Instance) { -(instance[:monitor]?.try &.["uptime"].as_s.to_f || 0.0) },
"location" => ->(name : String, instance : Instance) { instance[:region]? || "ZZ" }, "location" => ->(alias : String, instance : Instance) { instance[:region]? || "ZZ" },
"name" => ->(name : String, instance : Instance) { name }, "name" => ->(name : String, instance : Instance) { name },
"signup" => ->(name : String, instance : Instance) { instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? 0 : 1 } || 2 }, "signup" => ->(alias : String, instance : Instance) { instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? 0 : 1 } || 2 },
"type" => ->(name : String, instance : Instance) { instance[:type] }, "type" => ->(alias : String, instance : Instance) { instance[:type] },
"cors" => ->(name : String, instance : Instance) { instance[:cors] == nil ? 2 : instance[:cors] ? 0 : 1 }, "cors" => ->(alias : String, instance : Instance) { instance[:cors] == nil ? 2 : instance[:cors] ? 0 : 1 },
"api" => ->(name : String, instance : Instance) { instance[:api] == nil ? 2 : instance[:api] ? 0 : 1 }, "api" => ->(alias : String, instance : Instance) { instance[:api] == nil ? 2 : instance[:api] ? 0 : 1 },
"users" => ->(name : String, instance : Instance) { -(instance[:stats]?.try &.["usage"]?.try &.["users"]["total"].as_i || 0) }, "users" => ->(alias : String, instance : Instance) { -(instance[:stats]?.try &.["usage"]?.try &.["users"]["total"].as_i || 0) },
"version" => ->(name : String, instance : Instance) { instance[:stats]?.try &.["software"]?.try &.["version"].as_s.try &.split("-", 2)[0].split(".").map { |a| -a.to_i } || [0, 0, 0] }, "version" => ->(alias : String, instance : Instance) { instance[:stats]?.try &.["software"]?.try &.["version"].as_s.try &.split("-", 2)[0].split(".").map { |a| -a.to_i } || [0, 0, 0] },
} }
def sort_instances(instances, sort_by) def sort_instances(instances, sort_by)

View File

@ -121,7 +121,7 @@
<td><%= instance[:stats]?.try &.["usage"]?.try &.["users"]["total"] || "-" %></td> <td><%= instance[:stats]?.try &.["usage"]?.try &.["users"]["total"] || "-" %></td>
<td><%= instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? "✔" : "❌" } || "-" %></td> <td><%= instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? "✔" : "❌" } || "-" %></td>
<td><%= instance[:flag]? ? "#{instance[:flag]} #{instance[:region]}" : "-" %></td> <td><%= instance[:flag]? ? "#{instance[:flag]} #{instance[:region]}" : "-" %></td>
<td><%= instance[:monitor]?.try &.["30dRatio"]["ratio"] || "-" %></td> <td><a href='https://updown.io/<%= instance[:monitor]?.try &.["token"] || "p/resvf" %>'><%= instance[:monitor]?.try &.["uptime"] || "-" %></a></td>
<td><%= instance[:cors] == nil ? "-" : instance[:cors] ? "✔" : "❌" %></td> <td><%= instance[:cors] == nil ? "-" : instance[:cors] ? "✔" : "❌" %></td>
<td><%= instance[:api] == nil ? "-" : instance[:api] ? "✔" : "❌" %></td> <td><%= instance[:api] == nil ? "-" : instance[:api] ? "✔" : "❌" %></td>
</tr> </tr>