<commit_before>require 'time'
# @api public
class MachineDisk < Base::MachineDisk
  attr_accessor :stats,
                :usage,
                :key,
                :unit_number,
                :controller_key

  KB = 1024
  MB = 1024**2
  GB = 1024**3
  TB = 1024**4
  def stats=(stats)
    logger.debug("Adding disk stats")
    @stats = stats
  end

  def readings(inode, _interval = 300, _since = 5.minutes.ago.utc, _until = Time.now.utc)
    #logger.info('machine_disk.readings')

    #Create list of timestamps
    timestamps = { }
    if _since < Time.now.utc
      start  = _since.round(5.minutes).utc
      finish = _until.floor(5.minutes).utc
      if finish <= start
        finish = start+300
      end
      intervals  = ((finish - start) / _interval).round
      timestamps = { }
      i          = 1
      while i <= intervals do
        timestamps[start+(i*300)] = false
        i                         += 1
      end
    end

    #Create machine disk readings from stats variable
    result = []
    timestamps.keys.each do |timestamp|
      if !@stats.nil? 
        if @stats.key?(timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z")
          #logger.info("found "+timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z")
          metrics = @stats[timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z"]
          if @controller_key.eql?(1000) 
            # SCSI controller
            # virtualDisk.write.average.scsi0:0
            read_metric = "virtualDisk.read.average.scsi#{@key-2000}:#{@unit_number}"
            write_metric = "virtualDisk.write.average.scsi#{@key-2000}:#{@unit_number}"
          elsif @controller_key.eql?(2000)  
            # IDE controller
            read_metric = "virtualDisk.read.average.ide#{@key-3000}:#{@unit_number}"
            write_metric = "virtualDisk.write.average.ide#{@key-3000}:#{@unit_number}"
          end
          logger.debug(write_metric)
          logger.debug(metrics.keys)
          result << MachineDiskReading.new({ :usage     => @usage / GB,
                                             :read      => metrics.nil? ? 0 : metrics[read_metric] == -1 ? 0 : metrics[read_metric],
                                             :write     => metrics.nil? ? 0 : metrics[write_metric] == -1 ? 0 : metrics[write_metric],
                                             :date_time => timestamp.iso8601.to_s })
        else
          logger.info("missing "+timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z "+@stats.to_s)
          result << MachineDiskReading.new(
            {
              :usage     => @usage / GB,
              :read      => 0,
              :write     => 0,
              :date_time => timestamp.iso8601.to_s<commit_msg>Fixed reporting ide disk metrics<commit_after>require 'time'
# @api public
class MachineDisk < Base::MachineDisk
  attr_accessor :stats,
                :usage,
                :key,
                :unit_number,
                :controller_key

  KB = 1024
  MB = 1024**2
  GB = 1024**3
  TB = 1024**4
  def stats=(stats)
    logger.debug("Adding disk stats")
    @stats = stats
  end

  def readings(inode, _interval = 300, _since = 5.minutes.ago.utc, _until = Time.now.utc)
    #logger.info('machine_disk.readings')

    #Create list of timestamps
    timestamps = { }
    if _since < Time.now.utc
      start  = _since.round(5.minutes).utc
      finish = _until.floor(5.minutes).utc
      if finish <= start
        finish = start+300
      end
      intervals  = ((finish - start) / _interval).round
      timestamps = { }
      i          = 1
      while i <= intervals do
        timestamps[start+(i*300)] = false
        i                         += 1
      end
    end

    #Create machine disk readings from stats variable
    result = []
    timestamps.keys.each do |timestamp|
      if !@stats.nil? 
        if @stats.key?(timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z")
          #logger.info("found "+timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z")
          metrics = @stats[timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z"]
          if @controller_key.eql?(1000) 
            # SCSI controller
            # virtualDisk.write.average.scsi0:0
            read_metric = "virtualDisk.read.average.scsi#{@key-2000}:#{@unit_number}"
            write_metric = "virtualDisk.write.average.scsi#{@key-2000}:#{@unit_number}"
          elsif @controller_key.eql?(200)  
            # IDE controller
            read_metric = "virtualDisk.read.average.ide#{@key-3000}:#{@unit_number}"
            write_metric = "virtualDisk.write.average.ide#{@key-3000}:#{@unit_number}"
          end
          logger.debug(write_metric)
          logger.debug(metrics.keys)
          result << MachineDiskReading.new({ :usage     => @usage / GB,
                                             :read      => metrics.nil? ? 0 : metrics[read_metric] == -1 ? 0 : metrics[read_metric],
                                             :write     => metrics.nil? ? 0 : metrics[write_metric] == -1 ? 0 : metrics[write_metric],
                                             :date_time => timestamp.iso8601.to_s })
        else
          logger.info("missing "+timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S')+".000Z "+@stats.to_s)
          result << MachineDiskReading.new(
            {
              :usage     => @usage / GB,
              :read      => 0,
              :write     => 0,
              :date_time => timestamp.iso8601.to_s
{'fraction_non_alphanumeric': 0.14201618837380428, 'fraction_numerical': 0.03182487122884474, 'mean_word_length': 1.8040226921093347, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6502143', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1670, 'n_words': 299}