Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error.rb « error_tracking « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d6a4694defc0e3abcd2d97be7cfd03edec728d0 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true

class ErrorTracking::Error < ApplicationRecord
  include Sortable

  belongs_to :project

  has_many :events, class_name: 'ErrorTracking::ErrorEvent'

  has_one :first_event,
    -> { order(id: :asc) },
    class_name: 'ErrorTracking::ErrorEvent'

  has_one :last_event,
    -> { order(id: :desc) },
    class_name: 'ErrorTracking::ErrorEvent'

  scope :for_status, -> (status) { where(status: status) }

  validates :project, presence: true
  validates :name, presence: true
  validates :description, presence: true
  validates :actor, presence: true
  validates :status, presence: true

  enum status: {
    unresolved: 0,
    resolved: 1,
    ignored: 2
  }

  def self.report_error(name:, description:, actor:, platform:, timestamp:)
    safe_find_or_create_by(
      name: name,
      actor: actor,
      platform: platform
    ).tap do |error|
      error.update!(
        # Description can contain object id, so it can't be
        # used as a group criteria for similar errors.
        description: description,
        last_seen_at: timestamp
      )
    end
  end

  def self.sort_by_attribute(method)
    case method.to_s
    when 'last_seen'
      order(last_seen_at: :desc)
    when 'first_seen'
      order(first_seen_at: :desc)
    when 'frequency'
      order(events_count: :desc)
    else
      order_id_desc
    end
  end

  def title
    if description.present?
      "#{name} #{description}"
    else
      name
    end
  end

  def title_truncated
    title.truncate(64)
  end

  # For compatibility with sentry integration
  def to_sentry_error
    Gitlab::ErrorTracking::Error.new(
      id: id,
      title: title_truncated,
      message: description,
      culprit: actor,
      first_seen: first_seen_at,
      last_seen: last_seen_at,
      status: status,
      count: events_count
    )
  end

  # For compatibility with sentry integration
  def to_sentry_detailed_error
    Gitlab::ErrorTracking::DetailedError.new(
      id: id,
      title: title_truncated,
      message: description,
      culprit: actor,
      first_seen: first_seen_at.to_s,
      last_seen: last_seen_at.to_s,
      count: events_count,
      user_count: 0, # we don't support user count yet.
      project_id: project.id,
      status: status,
      tags: { level: nil, logger: nil },
      external_url: external_url,
      external_base_url: external_base_url,
      integrated: true,
      first_release_version: first_event&.release,
      last_release_version: last_event&.release
    )
  end

  private

  # For compatibility with sentry integration
  def external_url
    Gitlab::Routing.url_helpers.details_namespace_project_error_tracking_index_url(
      namespace_id: project.namespace,
      project_id: project,
      issue_id: id)
  end

  # For compatibility with sentry integration
  def external_base_url
    Gitlab::Routing.url_helpers.project_url(project)
  end
end