diff options
author | Mark Chao <mchao@gitlab.com> | 2019-08-08 16:18:57 +0300 |
---|---|---|
committer | James Lopez <james@gitlab.com> | 2019-08-08 16:18:57 +0300 |
commit | 4a6f959ab8f2928225a055d9dc62647c78df5bbe (patch) | |
tree | f28f44032118d02c5b1ed23f2b8bee08c9d62402 /lib | |
parent | 8505049e1f2510642715d9bfd56fc022cd42caf0 (diff) |
Record usage on snippet usage
Generalize wiki page counter for other page types to extend to.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/usage_data.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/usage_data_counters/base_counter.rb | 39 | ||||
-rw-r--r-- | lib/gitlab/usage_data_counters/note_counter.rb | 39 | ||||
-rw-r--r-- | lib/gitlab/usage_data_counters/snippet_counter.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/usage_data_counters/wiki_page_counter.rb | 30 |
5 files changed, 91 insertions, 27 deletions
diff --git a/lib/gitlab/usage_data.rb b/lib/gitlab/usage_data.rb index d5657c474c8..7e3a695e52a 100644 --- a/lib/gitlab/usage_data.rb +++ b/lib/gitlab/usage_data.rb @@ -140,6 +140,8 @@ module Gitlab [ Gitlab::UsageDataCounters::WikiPageCounter, Gitlab::UsageDataCounters::WebIdeCounter, + Gitlab::UsageDataCounters::NoteCounter, + Gitlab::UsageDataCounters::SnippetCounter, Gitlab::UsageDataCounters::SearchCounter ] end diff --git a/lib/gitlab/usage_data_counters/base_counter.rb b/lib/gitlab/usage_data_counters/base_counter.rb new file mode 100644 index 00000000000..2b52571c3cc --- /dev/null +++ b/lib/gitlab/usage_data_counters/base_counter.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Gitlab::UsageDataCounters + class BaseCounter + extend RedisCounter + + UnknownEvent = Class.new(StandardError) + + class << self + def redis_key(event) + Gitlab::Sentry.track_exception(UnknownEvent, extra: { event: event }) unless known_events.include?(event.to_s) + + "USAGE_#{prefix}_#{event}".upcase + end + + def count(event) + increment(redis_key event) + end + + def read(event) + total_count(redis_key event) + end + + def totals + known_events.map { |e| ["#{prefix}_#{e}".to_sym, read(e)] }.to_h + end + + private + + def known_events + self::KNOWN_EVENTS + end + + def prefix + self::PREFIX + end + end + end +end diff --git a/lib/gitlab/usage_data_counters/note_counter.rb b/lib/gitlab/usage_data_counters/note_counter.rb new file mode 100644 index 00000000000..e93a0bcfa27 --- /dev/null +++ b/lib/gitlab/usage_data_counters/note_counter.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Gitlab::UsageDataCounters + class NoteCounter < BaseCounter + KNOWN_EVENTS = %w[create].freeze + PREFIX = 'note' + COUNTABLE_TYPES = %w[Snippet].freeze + + class << self + def redis_key(event, noteable_type) + "#{super(event)}_#{noteable_type}".upcase + end + + def count(event, noteable_type) + return unless countable?(noteable_type) + + increment(redis_key(event, noteable_type)) + end + + def read(event, noteable_type) + return 0 unless countable?(noteable_type) + + total_count(redis_key(event, noteable_type)) + end + + def totals + { + snippet_comment: read(:create, 'Snippet') + } + end + + private + + def countable?(noteable_type) + COUNTABLE_TYPES.include?(noteable_type.to_s) + end + end + end +end diff --git a/lib/gitlab/usage_data_counters/snippet_counter.rb b/lib/gitlab/usage_data_counters/snippet_counter.rb new file mode 100644 index 00000000000..e4d234ce4d9 --- /dev/null +++ b/lib/gitlab/usage_data_counters/snippet_counter.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Gitlab::UsageDataCounters + class SnippetCounter < BaseCounter + KNOWN_EVENTS = %w[create update].freeze + PREFIX = 'snippet' + end +end diff --git a/lib/gitlab/usage_data_counters/wiki_page_counter.rb b/lib/gitlab/usage_data_counters/wiki_page_counter.rb index c8b59a3160c..9cfe0be5bab 100644 --- a/lib/gitlab/usage_data_counters/wiki_page_counter.rb +++ b/lib/gitlab/usage_data_counters/wiki_page_counter.rb @@ -1,32 +1,8 @@ # frozen_string_literal: true module Gitlab::UsageDataCounters - class WikiPageCounter - extend RedisCounter - - KNOWN_EVENTS = %w[create update delete].map(&:freeze).freeze - - UnknownEvent = Class.new(StandardError) - - class << self - # Each event gets a unique Redis key - def redis_key(event) - raise UnknownEvent, event unless KNOWN_EVENTS.include?(event.to_s) - - "USAGE_WIKI_PAGES_#{event}".upcase - end - - def count(event) - increment(redis_key event) - end - - def read(event) - total_count(redis_key event) - end - - def totals - KNOWN_EVENTS.map { |e| ["wiki_pages_#{e}".to_sym, read(e)] }.to_h - end - end + class WikiPageCounter < BaseCounter + KNOWN_EVENTS = %w[create update delete].freeze + PREFIX = 'wiki_pages' end end |