summaryrefslogtreecommitdiff
path: root/src/invidious/database/statistics.cr
blob: 1df549e25b0b93215f3746fef71348d56efacf76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require "./base.cr"

module Invidious::Database::Statistics
  extend self

  # -------------------
  #  User stats
  # -------------------

  def count_users_total : Int64
    request = <<-SQL
      SELECT count(*) FROM users
    SQL

    PG_DB.query_one(request, as: Int64)
  end

  def count_users_active_1m : Int64
    request = <<-SQL
      SELECT count(*) FROM users
      WHERE CURRENT_TIMESTAMP - updated < '6 months'
    SQL

    PG_DB.query_one(request, as: Int64)
  end

  def count_users_active_6m : Int64
    request = <<-SQL
      SELECT count(*) FROM users
      WHERE CURRENT_TIMESTAMP - updated < '1 month'
    SQL

    PG_DB.query_one(request, as: Int64)
  end

  # -------------------
  #  Channel stats
  # -------------------

  def channel_last_update : Time?
    request = <<-SQL
      SELECT updated FROM channels
      ORDER BY updated DESC
      LIMIT 1
    SQL

    PG_DB.query_one?(request, as: Time)
  end
end