Class: OpenC3::StatisticsProcessor

Inherits:
Processor show all
Defined in:
lib/openc3/processors/statistics_processor.rb

Instance Attribute Summary collapse

Attributes inherited from Processor

#name, #results, #value_type

Instance Method Summary collapse

Methods inherited from Processor

#to_s

Constructor Details

#initialize(item_name, samples_to_average, value_type = :CONVERTED) ⇒ StatisticsProcessor

Returns a new instance of StatisticsProcessor.

Parameters:

  • item_name (String)

    The name of the item to gather statistics on

  • samples_to_average (Integer)

    The number of samples to store for calculations

  • value_type (defaults to: :CONVERTED)

    #See Processor::initialize



30
31
32
33
34
35
# File 'lib/openc3/processors/statistics_processor.rb', line 30

def initialize(item_name, samples_to_average, value_type = :CONVERTED)
  super(value_type)
  @item_name = item_name.to_s.upcase
  @samples_to_average = Integer(samples_to_average)
  reset()
end

Instance Attribute Details

#samplesArray

Returns The set of samples stored by the processor.

Returns:

  • (Array)

    The set of samples stored by the processor



25
26
27
# File 'lib/openc3/processors/statistics_processor.rb', line 25

def samples
  @samples
end

Instance Method Details

#as_json(*a) ⇒ Object



78
79
80
# File 'lib/openc3/processors/statistics_processor.rb', line 78

def as_json(*a)
  { 'name' => @name, 'class' => self.class.name, 'params' => [@item_name, @samples_to_average, @value_type.to_s] }
end

#call(packet, buffer) ⇒ Object

Run statistics on the item

See Processor#call



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/openc3/processors/statistics_processor.rb', line 40

def call(packet, buffer)
  value = packet.read(@item_name, @value_type, buffer)
  # Don't process NaN or Infinite values
  return if value.to_f.nan? || value.to_f.infinite?

  @samples << value
  @samples = @samples[-@samples_to_average..-1] if @samples.length > @samples_to_average
  mean, stddev = Math.stddev_sample(@samples)
  @results[:MAX] = @samples.max
  @results[:MIN] = @samples.min
  @results[:MEAN] = mean
  @results[:STDDEV] = stddev
end

#cloneProcessor Also known as: dup

Make a light weight clone of this processor. This only creates a new hash of results

Returns:

  • (Processor)

    A copy of the processor with a new hash of results



66
67
68
69
70
# File 'lib/openc3/processors/statistics_processor.rb', line 66

def clone
  processor = super()
  processor.samples = processor.samples.clone
  processor
end

#resetObject

Reset any state



55
56
57
58
59
60
61
# File 'lib/openc3/processors/statistics_processor.rb', line 55

def reset
  @samples = []
  @results[:MAX] = nil
  @results[:MIN] = nil
  @results[:MEAN] = nil
  @results[:STDDEV] = nil
end

#to_configObject

Convert to configuration file string



74
75
76
# File 'lib/openc3/processors/statistics_processor.rb', line 74

def to_config
  "  PROCESSOR #{@name} #{self.class.name.to_s.class_name_to_filename} #{@item_name} #{@samples_to_average} #{@value_type}\n"
end