Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add playback success rate to /api/v1/stats #4085

Merged
merged 4 commits into from
Nov 4, 2023
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
17 changes: 17 additions & 0 deletions src/invidious/helpers/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,20 @@ def proxy_file(response, env)
IO.copy response.body_io, env.response
end
end

# Fetch the playback requests tracker from the statistics endpoint.
#
# Creates a new tracker when unavailable.
def get_playback_statistic
if (tracker = Invidious::Jobs::StatisticsRefreshJob::STATISTICS["playback"]) && tracker.as(Hash).empty?
tracker = {
"totalRequests" => 0_i64,
"successfulRequests" => 0_i64,
"ratio" => 0_f64,
}

Invidious::Jobs::StatisticsRefreshJob::STATISTICS["playback"] = tracker
end

return tracker.as(Hash(String, Int64 | Float64))
end
12 changes: 11 additions & 1 deletion src/invidious/jobs/statistics_refresh_job.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Invidious::Jobs::StatisticsRefreshJob < Invidious::Jobs::BaseJob
"updatedAt" => Time.utc.to_unix,
"lastChannelRefreshedAt" => 0_i64,
},

#
# "totalRequests" => 0_i64,
# "successfulRequests" => 0_i64
# "ratio" => 0_i64
#
"playback" => {} of String => Int64 | Float64,
}

private getter db : DB::Database
Expand All @@ -30,7 +37,7 @@ class Invidious::Jobs::StatisticsRefreshJob < Invidious::Jobs::BaseJob

loop do
refresh_stats
sleep 1.minute
sleep 10.minute
Fiber.yield
end
end
Expand All @@ -56,5 +63,8 @@ class Invidious::Jobs::StatisticsRefreshJob < Invidious::Jobs::BaseJob
"updatedAt" => Time.utc.to_unix,
"lastChannelRefreshedAt" => Invidious::Database::Statistics.channel_last_update.try &.to_unix || 0_i64,
}

# Reset playback requests tracker
STATISTICS["playback"] = {} of String => Int64 | Float64
end
end
16 changes: 16 additions & 0 deletions src/invidious/routes/api/v1/misc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ module Invidious::Routes::API::V1::Misc
if !CONFIG.statistics_enabled
return {"software" => SOFTWARE}.to_json
else
# Calculate playback success rate
if (tracker = Invidious::Jobs::StatisticsRefreshJob::STATISTICS["playback"]?)
tracker = tracker.as(Hash(String, Int64 | Float64))

if !tracker.empty?
total_requests = tracker["totalRequests"]
success_count = tracker["successfulRequests"]

if total_requests.zero?
tracker["ratio"] = 1_i64
else
tracker["ratio"] = (success_count / (total_requests)).round(2)
end
end
end

return Invidious::Jobs::StatisticsRefreshJob::STATISTICS.to_json
end
end
Expand Down
5 changes: 5 additions & 0 deletions src/invidious/routes/video_playback.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ module Invidious::Routes::VideoPlayback
# Remove the Range header added previously.
headers.delete("Range") if range_header.nil?

playback_statistics = get_playback_statistic()
playback_statistics["totalRequests"] += 1

if response.status_code >= 400
env.response.content_type = "text/plain"
haltf env, response.status_code
else
playback_statistics["successfulRequests"] += 1
end

if url.includes? "&file=seg.ts"
Expand Down
5 changes: 5 additions & 0 deletions src/invidious/videos/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def extract_video_info(video_id : String, proxy_region : String? = nil)
# YouTube may return a different video player response than expected.
# See: https://github.com/TeamNewPipe/NewPipe/issues/8713
# Line to be reverted if one day we solve the video not available issue.

# Although technically not a call to /videoplayback the fact that YouTube is returning the
# wrong video means that we should count it as a failure.
get_playback_statistic()["totalRequests"] += 1

return {
"version" => JSON::Any.new(Video::SCHEMA_VERSION.to_i64),
"reason" => JSON::Any.new("Can't load the video on this Invidious instance. YouTube is currently trying to block Invidious instances. <a href=\"https://github.com/iv-org/invidious/issues/3822\">Click here for more info about the issue.</a>"),
Expand Down