Class: SourceMonitor::Analytics::SourcesIndexMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/analytics/sources_index_metrics.rb

Constant Summary collapse

FETCH_INTERVAL_KEYS =
%w[
  fetch_interval_minutes_gteq
  fetch_interval_minutes_lt
  fetch_interval_minutes_lteq
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_scope:, result_scope:, search_params:, lookback: SourceActivityRates::DEFAULT_LOOKBACK, now: Time.current) ⇒ SourcesIndexMetrics

Returns a new instance of SourcesIndexMetrics.



14
15
16
17
18
19
20
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 14

def initialize(base_scope:, result_scope:, search_params:, lookback: SourceActivityRates::DEFAULT_LOOKBACK, now: Time.current)
  @base_scope = base_scope
  @result_scope = result_scope
  @search_params = (search_params || {}).dup
  @lookback = lookback
  @now = now
end

Instance Attribute Details

#search_paramsObject (readonly)

Returns the value of attribute search_params.



12
13
14
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 12

def search_params
  @search_params
end

Instance Method Details

#fetch_interval_distributionObject



22
23
24
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 22

def fetch_interval_distribution
  @fetch_interval_distribution ||= SourceFetchIntervalDistribution.new(scope: distribution_scope).buckets
end

#fetch_interval_filterObject



61
62
63
64
65
66
67
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 61

def fetch_interval_filter
  min = integer_param(search_params["fetch_interval_minutes_gteq"])
  max = integer_param(search_params["fetch_interval_minutes_lt"]) || integer_param(search_params["fetch_interval_minutes_lteq"])
  return if min.nil? && max.nil?

  { min:, max: }
end

#item_activity_ratesObject



42
43
44
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 42

def item_activity_rates
  @item_activity_rates ||= SourceActivityRates.new(scope: result_scope, lookback:, now:).per_source_rates
end

#selected_fetch_interval_bucketObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 26

def selected_fetch_interval_bucket
  filter = fetch_interval_filter
  return if filter.blank?

  fetch_interval_distribution.find do |bucket|
    min_match = filter[:min].present? ? filter[:min].to_i == bucket.min.to_i : bucket.min.nil?
    max_match = if bucket.max.nil?
      filter[:max].nil?
    else
      filter[:max].present? && filter[:max].to_i == bucket.max.to_i
    end

    min_match && max_match
  end
end

#word_count_averages(source_ids) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/source_monitor/analytics/sources_index_metrics.rb', line 46

def word_count_averages(source_ids)
  if source_ids.any?
    base = SourceMonitor::ItemContent.joins(:item).where(sourcemon_items: { source_id: source_ids })
    feed = base.where.not(feed_word_count: nil)
               .group("sourcemon_items.source_id")
               .average(:feed_word_count)
    scraped = base.where.not(scraped_word_count: nil)
                 .group("sourcemon_items.source_id")
                 .average(:scraped_word_count)
    { feed:, scraped: }
  else
    { feed: {}, scraped: {} }
  end
end