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



33
34
35
36
37
38
# File 'lib/openc3/processors/statistics_processor.rb', line 33

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



28
29
30
# File 'lib/openc3/processors/statistics_processor.rb', line 28

def samples
  @samples
end

Instance Method Details

#as_json(*a) ⇒ Object



81
82
83
# File 'lib/openc3/processors/statistics_processor.rb', line 81

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openc3/processors/statistics_processor.rb', line 43

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



69
70
71
72
73
# File 'lib/openc3/processors/statistics_processor.rb', line 69

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

#resetObject

Reset any state



58
59
60
61
62
63
64
# File 'lib/openc3/processors/statistics_processor.rb', line 58

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

#to_configObject

Convert to configuration file string



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

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