File: project_daily_statistic.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (32 lines) | stat: -rw-r--r-- 940 bytes parent folder | download
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
# frozen_string_literal: true

class ProjectDailyStatistic < ApplicationRecord
  include CounterAttribute

  belongs_to :project

  counter_attribute :fetch_count

  scope :of_project, ->(project) { where(project: project) }
  scope :of_last_30_days, -> { where('date >= ?', 29.days.ago.utc.to_date) }
  scope :sorted_by_date_desc, -> { order(project_id: :desc, date: :desc) }
  scope :sum_fetch_count, -> { sum(:fetch_count) }

  def self.find_or_create_project_daily_statistic(project_id, date)
    daily_statistic = find_by(project_id: project_id, date: date)
    return daily_statistic if daily_statistic

    result = upsert(
      { project_id: project_id, date: date, fetch_count: 0 },
      unique_by: [:project_id, :date],
      on_duplicate: :skip
    )

    statistic_id = result&.rows&.first&.first
    if statistic_id
      find_by_id(statistic_id)
    else
      find_by!(project_id: project_id, date: date)
    end
  end
end