mirror of
https://gitea.invidious.io/iv-org/instances-api.git
synced 2024-08-15 00:53:15 +00:00
Add support for compound sorting
This commit is contained in:
parent
abf3c4e53f
commit
7ab7beabf4
1 changed files with 14 additions and 20 deletions
|
@ -97,7 +97,7 @@ end
|
||||||
|
|
||||||
get "/" do |env|
|
get "/" do |env|
|
||||||
sort_by = env.params.query["sort_by"]?
|
sort_by = env.params.query["sort_by"]?
|
||||||
sort_by ||= "users"
|
sort_by ||= "type,users"
|
||||||
|
|
||||||
instances = sort_instances(INSTANCES, sort_by)
|
instances = sort_instances(INSTANCES, sort_by)
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ get "/instances.json" do |env|
|
||||||
env.response.content_type = "application/json; charset=utf-8"
|
env.response.content_type = "application/json; charset=utf-8"
|
||||||
|
|
||||||
sort_by = env.params.query["sort_by"]?
|
sort_by = env.params.query["sort_by"]?
|
||||||
sort_by ||= "users"
|
sort_by ||= "type,users"
|
||||||
|
|
||||||
instances = sort_instances(INSTANCES, sort_by)
|
instances = sort_instances(INSTANCES, sort_by)
|
||||||
|
|
||||||
|
@ -129,27 +129,21 @@ static_headers do |response, filepath, filestat|
|
||||||
response.headers.add("Cache-Control", "max-age=86400")
|
response.headers.add("Cache-Control", "max-age=86400")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
SORT_PROCS = {
|
||||||
|
"health" => ->(name : String, instance : Instance) { -(instance[:monitor]?.try &.["weeklyRatio"]["ratio"].as_s.to_f || 0.0) },
|
||||||
|
"location" => ->(name : String, instance : Instance) { instance[:region]? || "ZZ" },
|
||||||
|
"name" => ->(name : String, instance : Instance) { name },
|
||||||
|
"signup" => ->(name : String, instance : Instance) { instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? 0 : 1 } || 2 },
|
||||||
|
"type" => ->(name : String, instance : Instance) { instance[:type] },
|
||||||
|
"users" => ->(name : String, instance : Instance) { -(instance[:stats]?.try &.["usage"]?.try &.["users"]["total"].as_i || 0.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] },
|
||||||
|
}
|
||||||
|
|
||||||
def sort_instances(instances, sort_by)
|
def sort_instances(instances, sort_by)
|
||||||
sort_proc = ->(instance : Tuple(String, Instance)) { instance[0] }
|
|
||||||
instances = instances.to_a
|
instances = instances.to_a
|
||||||
|
sorts = sort_by.downcase.split("-", 2)[0].split(",").map { |s| SORT_PROCS[s] }
|
||||||
|
|
||||||
case sort_by
|
instances.sort_by! { |name, instance| sorts.map &.call(name, instance).to_s }
|
||||||
when .starts_with? "name"
|
|
||||||
instances.sort_by! { |name, instance| name }
|
|
||||||
when .starts_with? "version"
|
|
||||||
instances = instances.sort_by { |name, instance| "#{instance[:stats]?.try &.["software"]?.try &.["version"].as_s.split("-")[0] || "0.0.0"}#{name}" }.reverse
|
|
||||||
when .starts_with? "type"
|
|
||||||
instances.sort_by! { |name, instance| instance[:type] }
|
|
||||||
when .starts_with? "signup"
|
|
||||||
instances.sort_by! { |name, instance| instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? 0 : 1 } || 2 }
|
|
||||||
when .starts_with? "location"
|
|
||||||
instances.sort_by! { |name, instance| instance[:region]? || "ZZ" }
|
|
||||||
when .starts_with? "health"
|
|
||||||
instances = instances.sort_by { |name, instance| instance[:monitor]?.try &.["weeklyRatio"]["ratio"].as_s.to_f || 0.0 }.reverse
|
|
||||||
when .starts_with? "users"
|
|
||||||
instances = instances.sort_by { |name, instance| instance[:stats]?.try &.["usage"]?.try &.["users"]["total"].as_i || 0 }.reverse
|
|
||||||
end
|
|
||||||
|
|
||||||
instances.reverse! if sort_by.ends_with?("-reverse")
|
instances.reverse! if sort_by.ends_with?("-reverse")
|
||||||
instances
|
instances
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue