<commit_before>module Metric::CiMixin::Capture
  def perf_capture_object(targets = nil)
    if self.kind_of?(ExtManagementSystem)
      self.class::MetricsCapture.new(targets, ext_management_system)
    else
      self.class.module_parent::MetricsCapture.new(targets || self, ext_management_system)
    end
  end

  # delegate currently only used on Ems
  delegate :perf_collect_metrics, :to => :perf_capture_object

  def perf_capture_realtime(*args)
    perf_capture('realtime', *args)
  end

  def perf_capture_hourly(*args)
    perf_capture('hourly', *args)
  end

  def perf_capture_historical(*args)
    perf_capture('historical', *args)
  end

  def perf_capture(interval_name, start_time = nil, end_time = nil)
    unless Metric::Capture::VALID_CAPTURE_INTERVALS.include?(interval_name)
      raise ArgumentError, _("invalid interval_name '%{name}'") % {:name => interval_name}
    end
    raise ArgumentError, _("end_time cannot be specified if start_time is nil") if start_time.nil? && !end_time.nil?

    # if target (== self) is archived, skip it
    if respond_to?(:ems_id) && ems_id.nil?
      _log.warn("C&U collection's target is archived (no EMS associated), skipping. Target: #{log_target}")
      return
    end

    metrics_capture = perf_capture_object
    start_time, end_time = fix_capture_start_end_time(interval_name, start_time, end_time)
    start_range, end_range, counters_data = just_perf_capture(interval_name, start_time, end_time, metrics_capture)

    perf_process(interval_name, start_range, end_range, counters_data) if start_range
  end

  # Determine the start_time for capturing if not provided
  # interval_name == realtime is the only one that passes no start_time/end_time
  def fix_capture_start_end_time(interval_name, start_time, end_time)
    start_time ||=
      case interval_name
      when "historical" # Vm or Host
        Metric::Capture.historical_start_time
      when "hourly" # Storage (value is ignored)
        last_perf_capture_on || 4.hours.ago.utc
      else # "realtime" for Vm or Host
        [last_perf_capture_on, 4.hours.ago.utc.beginning_of_day].compact.max
      end
    [start_time&.utc, end_time&.utc]
  end

  # Determine the expected start time, so we can detect gaps or missing data
  # @param interval_name [String]
  # @param start_time [Date] start date passed to pref_capture
  # @return [String, nil] Expected start date for the gap.
  #     nil if none
  def calculate_gap(interval_name, start_time)
    expected_start_range = start_time
    # If we've changed power state within the last hour, the returned data
    #   may not include all the data we'd expect
    return if try(:state_changed_on) && state_changed_on > 1.hour.ago<commit_msg>run realtime metrics for past 4 hours<commit_after>module Metric::CiMixin::Capture
  def perf_capture_object(targets = nil)
    if self.kind_of?(ExtManagementSystem)
      self.class::MetricsCapture.new(targets, ext_management_system)
    else
      self.class.module_parent::MetricsCapture.new(targets || self, ext_management_system)
    end
  end

  # delegate currently only used on Ems
  delegate :perf_collect_metrics, :to => :perf_capture_object

  def perf_capture_realtime(*args)
    perf_capture('realtime', *args)
  end

  def perf_capture_hourly(*args)
    perf_capture('hourly', *args)
  end

  def perf_capture_historical(*args)
    perf_capture('historical', *args)
  end

  def perf_capture(interval_name, start_time = nil, end_time = nil)
    unless Metric::Capture::VALID_CAPTURE_INTERVALS.include?(interval_name)
      raise ArgumentError, _("invalid interval_name '%{name}'") % {:name => interval_name}
    end
    raise ArgumentError, _("end_time cannot be specified if start_time is nil") if start_time.nil? && !end_time.nil?

    # if target (== self) is archived, skip it
    if respond_to?(:ems_id) && ems_id.nil?
      _log.warn("C&U collection's target is archived (no EMS associated), skipping. Target: #{log_target}")
      return
    end

    metrics_capture = perf_capture_object
    start_time, end_time = fix_capture_start_end_time(interval_name, start_time, end_time)
    start_range, end_range, counters_data = just_perf_capture(interval_name, start_time, end_time, metrics_capture)

    perf_process(interval_name, start_range, end_range, counters_data) if start_range
  end

  # Determine the start_time for capturing if not provided
  # interval_name == realtime is the only one that passes no start_time/end_time
  def fix_capture_start_end_time(interval_name, start_time, end_time)
    start_time ||=
      case interval_name
      when "historical" # Vm or Host
        Metric::Capture.historical_start_time
      when "hourly" # Storage (value is ignored)
        last_perf_capture_on || 4.hours.ago.utc
      else # "realtime" for Vm or Host
        [last_perf_capture_on, 4.hours.ago.utc].compact.max
      end
    [start_time&.utc, end_time&.utc]
  end

  # Determine the expected start time, so we can detect gaps or missing data
  # @param interval_name [String]
  # @param start_time [Date] start date passed to pref_capture
  # @return [String, nil] Expected start date for the gap.
  #     nil if none
  def calculate_gap(interval_name, start_time)
    expected_start_range = start_time
    # If we've changed power state within the last hour, the returned data
    #   may not include all the data we'd expect
    return if try(:state_changed_on) && state_changed_on > 1.hour.ago
{'fraction_non_alphanumeric': 0.08677306916162172, 'fraction_numerical': 0.001284168042560998, 'mean_word_length': 4.192380952380953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5920564', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1698, 'n_words': 467}