Commit 1fc032b2 authored by Alper Akgun's avatar Alper Akgun Committed by Adam Hegyi

Fix avg_cycle_analytics giving an uncaught error

In usage data avg_cycle_analytics giving an uncaught error and kills
the whole usage ping. This MR catches errors as well as revises the
implementation not to time out due to non-indexable
parent b245ee83
---
title: Fix avg_cycle_analytics uncaught error and optimize query
merge_request: 26381
author:
type: fixed
......@@ -3,15 +3,32 @@
module Gitlab
module CycleAnalytics
class UsageData
include Gitlab::Utils::StrongMemoize
PROJECTS_LIMIT = 10
attr_reader :projects, :options
attr_reader :options
def initialize
@projects = Project.sorted_by_activity.limit(PROJECTS_LIMIT)
@options = { from: 7.days.ago }
end
def projects
strong_memoize(:projects) do
projects = Project.where.not(last_activity_at: nil).order(last_activity_at: :desc).limit(10) +
Project.where.not(last_repository_updated_at: nil).order(last_repository_updated_at: :desc).limit(10)
projects = projects.uniq.sort_by do |project|
[project.last_activity_at, project.last_repository_updated_at].min
end
if projects.size < 10
projects.concat(Project.where(last_activity_at: nil, last_repository_updated_at: nil).limit(10))
end
projects.uniq.first(10)
end
end
def to_json(*)
total = 0
......
......@@ -122,6 +122,8 @@ module Gitlab
def cycle_analytics_usage_data
Gitlab::CycleAnalytics::UsageData.new.to_json
rescue ActiveRecord::StatementInvalid
{ avg_cycle_analytics: {} }
end
def features_usage_data
......
......@@ -324,6 +324,24 @@ describe Gitlab::UsageData do
end
end
describe '#cycle_analytics_usage_data' do
subject { described_class.cycle_analytics_usage_data }
it 'works when queries time out in new' do
allow(Gitlab::CycleAnalytics::UsageData)
.to receive(:new).and_raise(ActiveRecord::StatementInvalid.new(''))
expect { subject }.not_to raise_error
end
it 'works when queries time out in to_json' do
allow_any_instance_of(Gitlab::CycleAnalytics::UsageData)
.to receive(:to_json).and_raise(ActiveRecord::StatementInvalid.new(''))
expect { subject }.not_to raise_error
end
end
describe '#ingress_modsecurity_usage' do
subject { described_class.ingress_modsecurity_usage }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment