<commit_before>module Bipbip

  class Storage::Copperegg < Storage

    def initialize(name, config)
      super(name, config)
      CopperEgg::Api.apikey = config['api_key']
    end

    def setup_plugin(plugin)
      @metric_groups ||= _load_metric_groups
      @dashboards ||= _load_dashboards

      if ![5, 15, 60, 300, 900, 3600, 21600].include?(plugin.frequency)
        Bipbip.logger.fatal "Copperegg cannot use frequency #{plugin.frequency}"
        exit 1
      end

      metric_group = @metric_groups.detect { |m| m.name == plugin.name }
      if metric_group.nil? || !metric_group.is_a?(CopperEgg::MetricGroup)
        Bipbip.logger.info "Creating copperegg metric group `#{plugin.name}`"
        metric_group = CopperEgg::MetricGroup.new(:name => plugin.name, :label => plugin.name, :frequency => plugin.frequency)
      end
      metric_group.frequency = plugin.frequency
      metric_group.metrics = plugin.metrics_schema.map do |sample|
        {
            :name => sample[:name],
            :type => 'ce_' + sample[:type],
            :unit => sample[:unit],
        }
      end
      metric_group.save

      dashboard = @dashboards.detect { |d| d.name == plugin.name }
      if dashboard.nil?
        Bipbip.logger.info "Creating copperegg dashboard `#{plugin.name}`"
        metrics = metric_group.metrics || []
        CopperEgg::CustomDashboard.create(metric_group, :name => plugin.name, :identifiers => nil, :metrics => metrics)
      end
    end

    def store_sample(plugin, time, data)
      CopperEgg::MetricSample.save(plugin.name, plugin.source_identifier, time.to_i, data)
    end

    def _load_metric_groups
      Bipbip.logger.info 'Loading copperegg metric groups'
      metric_groups = CopperEgg::MetricGroup.find
      if metric_groups.nil?
        Bipbip.logger.fatal 'Cannot load copperegg metric groups'
        exit 1
      end
      metric_groups
    end

    def _load_dashboards<commit_msg>Use plugin's metric_group to identify copperegg dashboard and metric_group<commit_after>module Bipbip

  class Storage::Copperegg < Storage

    def initialize(name, config)
      super(name, config)
      CopperEgg::Api.apikey = config['api_key']
    end

    def setup_plugin(plugin)
      @metric_groups ||= _load_metric_groups
      @dashboards ||= _load_dashboards

      if ![5, 15, 60, 300, 900, 3600, 21600].include?(plugin.frequency)
        Bipbip.logger.fatal "Copperegg cannot use frequency #{plugin.frequency}"
        exit 1
      end

      metric_group = @metric_groups.detect { |m| m.name == plugin.metric_group }
      if metric_group.nil? || !metric_group.is_a?(CopperEgg::MetricGroup)
        Bipbip.logger.info "Creating copperegg metric group `#{plugin.metric_group}`"
        metric_group = CopperEgg::MetricGroup.new(:name => plugin.metric_group, :label => plugin.metric_group, :frequency => plugin.frequency)
      end
      metric_group.frequency = plugin.frequency
      metric_group.metrics = plugin.metrics_schema.map do |sample|
        {
            :name => sample[:name],
            :type => 'ce_' + sample[:type],
            :unit => sample[:unit],
        }
      end
      metric_group.save

      dashboard = @dashboards.detect { |d| d.name == plugin.metric_group }
      if dashboard.nil?
        Bipbip.logger.info "Creating copperegg dashboard `#{plugin.metric_group}`"
        metrics = metric_group.metrics || []
        CopperEgg::CustomDashboard.create(metric_group, :name => plugin.metric_group, :identifiers => nil, :metrics => metrics)
      end
    end

    def store_sample(plugin, time, data)
      CopperEgg::MetricSample.save(plugin.metric_group, plugin.source_identifier, time.to_i, data)
    end

    def _load_metric_groups
      Bipbip.logger.info 'Loading copperegg metric groups'
      metric_groups = CopperEgg::MetricGroup.find
      if metric_groups.nil?
        Bipbip.logger.fatal 'Cannot load copperegg metric groups'
        exit 1
      end
      metric_groups
    end

    def _load_dashboards
{'fraction_non_alphanumeric': 0.10827706926731683, 'fraction_numerical': 0.011002750687671918, 'mean_word_length': 2.9100684261974585, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '2862998', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1217, 'n_words': 269}