Class: TrendingProject

Inherits:
ApplicationRecord show all
Defined in:
app/models/trending_project.rb

Constant Summary collapse

MONTHS_TO_INCLUDE =

The number of months to include in the trending calculation.

1
PROJECTS_LIMIT =

The maximum number of projects to include in the trending set.

100

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.refresh!Object

Populates the trending projects table with the current list of trending projects.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/trending_project.rb', line 14

def self.refresh!
  # The calculation **must** run in a transaction. If the removal of data and
  # insertion of new data were to run separately a user might end up with an
  # empty list of trending projects for a short period of time.
  transaction do
    delete_all

    timestamp = connection.quote(MONTHS_TO_INCLUDE.months.ago)

    connection.execute <<-EOF.strip_heredoc
      INSERT INTO #{table_name} (project_id)
      SELECT project_id
      FROM notes
      INNER JOIN projects ON projects.id = notes.project_id
      WHERE notes.created_at >= #{timestamp}
      AND notes.system IS FALSE
      AND projects.visibility_level = #{Gitlab::VisibilityLevel::PUBLIC}
      GROUP BY project_id
      ORDER BY count(*) DESC
      LIMIT #{PROJECTS_LIMIT};
    EOF
  end
end