diff options
| author | Omar Roth <omarroth@hotmail.com> | 2019-03-01 19:25:16 -0600 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2019-03-01 19:25:16 -0600 |
| commit | 4be82c5ca618a27b8729c4c1406c549651b6d37a (patch) | |
| tree | f6329939194fc0a506bca4c710b56e2f0afecd68 /src | |
| parent | 0eaf8f38a1fbb6253df8f4e4e57c616d8ffec425 (diff) | |
| download | invidious-4be82c5ca618a27b8729c4c1406c549651b6d37a.tar.gz invidious-4be82c5ca618a27b8729c4c1406c549651b6d37a.tar.bz2 invidious-4be82c5ca618a27b8729c4c1406c549651b6d37a.zip | |
Add /api/v1/stats
Diffstat (limited to 'src')
| -rw-r--r-- | src/invidious.cr | 54 | ||||
| -rw-r--r-- | src/invidious/helpers/helpers.cr | 1 | ||||
| -rw-r--r-- | src/invidious/views/preferences.ecr | 5 |
3 files changed, 55 insertions, 5 deletions
diff --git a/src/invidious.cr b/src/invidious.cr index 5d7b2e72..44dd4183 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -122,11 +122,32 @@ config.video_threads.times do |i| end end -# stats = Statistics.new -# if config.statistics -# spawn do -# end -# end +statistics = { + "error" => "Statistics are not availabile.", +} +if config.statistics_enabled + spawn do + loop do + statistics = { + "version" => "2.0", + "software" => { + "name" => "invidious", + "version" => "#{CURRENT_VERSION}-#{CURRENT_COMMIT}", + }, + "openRegistrations" => config.registration_enabled, + "usage" => { + "total" => PG_DB.query_one("SELECT count(*) FROM users", as: Int64), + "activeHalfyear" => PG_DB.query_one("SELECT count(*) FROM users WHERE CURRENT_TIMESTAMP - updated < '6 months'", as: Int64), + "activeMonth" => PG_DB.query_one("SELECT count(*) FROM users WHERE CURRENT_TIMESTAMP - updated < '1 month'", as: Int64), + }, + "updatedAt" => Time.now.to_unix, + } + + sleep 1.minute + Fiber.yield + end + end +end top_videos = [] of Video if config.top_enabled @@ -1291,6 +1312,10 @@ post "/preferences" do |env| registration_enabled ||= "off" config.registration_enabled = registration_enabled == "on" + statistics_enabled = env.params.body["statistics_enabled"]?.try &.as(String) + statistics_enabled ||= "off" + config.statistics_enabled = statistics_enabled == "on" + File.write("config/config.yml", config.to_yaml) end else @@ -2381,6 +2406,25 @@ end # API Endpoints +get "/api/v1/stats" do |env| + env.response.content_type = "application/json" + + if statistics["error"]? + halt env, status_code: 500, response: statistics.to_json + end + + if !config.statistics_enabled + error_message = {"error" => "Statistics are not enabled."}.to_json + halt env, status_code: 400, response: error_message + end + + if env.params.query["pretty"]? && env.params.query["pretty"] == "1" + statistics.to_pretty_json + else + statistics.to_json + end +end + get "/api/v1/captions/:id" do |env| locale = LOCALES[env.get("locale").as(String)]? diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 26b6244c..b63fd8bb 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -22,6 +22,7 @@ user: String, captcha_enabled: {type: Bool, default: true}, login_enabled: {type: Bool, default: true}, registration_enabled: {type: Bool, default: true}, + statistics_enabled: {type: Bool, default: false}, admins: {type: Array(String), default: [] of String}, }) end diff --git a/src/invidious/views/preferences.ecr b/src/invidious/views/preferences.ecr index ea6a84c8..20762bc5 100644 --- a/src/invidious/views/preferences.ecr +++ b/src/invidious/views/preferences.ecr @@ -189,6 +189,11 @@ function update_value(element) { <label for="registration_enabled"><%= translate(locale, "Registration enabled? ") %></label> <input name="registration_enabled" id="registration_enabled" type="checkbox" <% if config.registration_enabled %>checked<% end %>> </div> + + <div class="pure-control-group"> + <label for="statistics_enabled"><%= translate(locale, "Report statistics? ") %></label> + <input name="statistics_enabled" id="statistics_enabled" type="checkbox" <% if config.statistics_enabled %>checked<% end %>> + </div> <% end %> <% if env.get? "user" %> |
