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-04-29 21:17:54 +05:30
|
|
|
integration_field = self.class.jira_dvcs_integration_field(cloud: cloud)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
return if persisted? && updated_today?(integration_field)
|
|
|
|
|
|
|
|
persist_jira_dvcs_usage(integration_field)
|
|
|
|
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')
|