debian-mirror-gitlab/app/models/project_feature_usage.rb

51 lines
1.4 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
class ProjectFeatureUsage < ApplicationRecord
self.primary_key = :project_id
2021-03-08 18:12:59 +05:30
JIRA_DVCS_CLOUD_FIELD = 'jira_dvcs_cloud_last_sync_at'
JIRA_DVCS_SERVER_FIELD = 'jira_dvcs_server_last_sync_at'
2020-11-24 15:15:51 +05:30
belongs_to :project
validates :project, presence: true
scope :with_jira_dvcs_integration_enabled, -> (cloud: true) do
where.not(jira_dvcs_integration_field(cloud: cloud) => nil)
end
class << self
def jira_dvcs_integration_field(cloud: true)
cloud ? JIRA_DVCS_CLOUD_FIELD : JIRA_DVCS_SERVER_FIELD
end
end
def log_jira_dvcs_integration_usage(cloud: true)
2021-09-04 01:27:46 +05:30
::Gitlab::Database::LoadBalancing::Session.without_sticky_writes do
integration_field = self.class.jira_dvcs_integration_field(cloud: cloud)
2021-04-29 21:17:54 +05:30
2021-09-04 01:27:46 +05:30
# The feature usage is used only once later to query the feature usage in a
# long date range. Therefore, we just need to update the timestamp once per
# day
break if persisted? && updated_today?(integration_field)
2021-04-29 21:17:54 +05:30
2021-09-04 01:27:46 +05:30
persist_jira_dvcs_usage(integration_field)
end
2021-04-29 21:17:54 +05:30
end
private
def updated_today?(integration_field)
self[integration_field].present? && self[integration_field].today?
end
def persist_jira_dvcs_usage(integration_field)
assign_attributes(integration_field => Time.current)
save
2020-11-24 15:15:51 +05:30
rescue ActiveRecord::RecordNotUnique
reset
retry
end
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
ProjectFeatureUsage.prepend_mod_with('ProjectFeatureUsage')