File: modification_tracker.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 (59 lines) | stat: -rw-r--r-- 1,548 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
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
# frozen_string_literal: true

module LooseForeignKeys
  class ModificationTracker
    delegate :monotonic_time, to: :'Gitlab::Metrics::System'

    def initialize
      @delete_count_by_table = Hash.new { |h, k| h[k] = 0 }
      @update_count_by_table = Hash.new { |h, k| h[k] = 0 }
      @start_time = monotonic_time
      @deletes_counter = Gitlab::Metrics.counter(
        :loose_foreign_key_deletions,
        'The number of loose foreign key deletions'
      )
      @updates_counter = Gitlab::Metrics.counter(
        :loose_foreign_key_updates,
        'The number of loose foreign key updates'
      )
    end

    def max_runtime
      30.seconds
    end

    def max_deletes
      100_000
    end

    def max_updates
      50_000
    end

    def add_deletions(table, count)
      @delete_count_by_table[table] += count
      @deletes_counter.increment({ table: table }, count)
    end

    def add_updates(table, count)
      @update_count_by_table[table] += count
      @updates_counter.increment({ table: table }, count)
    end

    def over_limit?
      @delete_count_by_table.values.sum >= max_deletes ||
        @update_count_by_table.values.sum >= max_updates ||
        monotonic_time - @start_time >= max_runtime
    end

    def stats
      {
        over_limit: over_limit?,
        delete_count_by_table: @delete_count_by_table,
        update_count_by_table: @update_count_by_table,
        delete_count: @delete_count_by_table.values.sum,
        update_count: @update_count_by_table.values.sum
      }
    end
  end
end