Class: Archsight::Annotations::ComputedEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/archsight/annotations/computed.rb

Overview

ComputedEvaluator provides the DSL context for computing annotation values. It exposes aggregation functions and relation traversal methods.

Instance Method Summary collapse

Constructor Details

#initialize(instance, database, manager) ⇒ ComputedEvaluator

Returns a new instance of ComputedEvaluator.



27
28
29
30
31
32
# File 'lib/archsight/annotations/computed.rb', line 27

def initialize(instance, database, manager)
  @instance = instance
  @database = database
  @manager = manager
  @resolver = Archsight::Annotations::ComputedRelationResolver.new(instance, database)
end

Instance Method Details

#annotation(key) ⇒ Object

Access a regular annotation value from the current instance



35
36
37
# File 'lib/archsight/annotations/computed.rb', line 35

def annotation(key)
  @instance.annotations[key]
end

#avg(instances, key) ⇒ Float?

Average numeric annotation values

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Float, nil)

    Average or nil if no values



94
95
96
97
# File 'lib/archsight/annotations/computed.rb', line 94

def avg(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.avg(values)
end

#collect(instances, key) ⇒ Array

Collect unique annotation values

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Array)

    Unique sorted values



121
122
123
124
# File 'lib/archsight/annotations/computed.rb', line 121

def collect(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.collect(values)
end

#computed(key) ⇒ Object

Access a computed annotation value (triggers computation if needed)



40
41
42
# File 'lib/archsight/annotations/computed.rb', line 40

def computed(key)
  @manager.compute_for_key(@instance, key)
end

#count(instances, key = nil) ⇒ Integer

Count instances or non-nil annotation values

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String, nil) (defaults to: nil)

    Optional annotation key; if nil, counts instances

Returns:

  • (Integer)

    Count



81
82
83
84
85
86
87
88
# File 'lib/archsight/annotations/computed.rb', line 81

def count(instances, key = nil)
  if key
    values = extract_values(instances, key)
    Archsight::Annotations::ComputedAggregators.count(values)
  else
    instances.length
  end
end

#first(instances, key) ⇒ Object?

Get first non-nil annotation value

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Object, nil)

    First non-nil value



130
131
132
133
# File 'lib/archsight/annotations/computed.rb', line 130

def first(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.first(values)
end

#get(instance, key) ⇒ Object?

Get an annotation value from an instance, triggering computation if needed

Parameters:

  • instance (Object)

    Resource instance

  • key (String)

    Annotation key to extract

Returns:

  • (Object, nil)

    Annotation value



148
149
150
151
# File 'lib/archsight/annotations/computed.rb', line 148

def get(instance, key)
  @manager.compute_for_key(instance, key) if instance.class.computed_annotations.any? { |d| d.matches?(key) }
  instance.annotations[key]
end

#incoming(kind = nil) ⇒ Object

Get direct incoming relations (<- Kind)



57
58
59
# File 'lib/archsight/annotations/computed.rb', line 57

def incoming(kind = nil)
  @resolver.incoming(kind)
end

#incoming_transitive(kind = nil, max_depth: 10) ⇒ Object

Get transitive incoming relations (<~ Kind)



62
63
64
# File 'lib/archsight/annotations/computed.rb', line 62

def incoming_transitive(kind = nil, max_depth: 10)
  @resolver.incoming_transitive(kind, max_depth: max_depth)
end

#max(instances, key) ⇒ Float?

Maximum numeric annotation value

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Float, nil)

    Maximum value or nil if no values



112
113
114
115
# File 'lib/archsight/annotations/computed.rb', line 112

def max(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.max(values)
end

#min(instances, key) ⇒ Float?

Minimum numeric annotation value

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Float, nil)

    Minimum value or nil if no values



103
104
105
106
# File 'lib/archsight/annotations/computed.rb', line 103

def min(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.min(values)
end

#most_common(instances, key) ⇒ Object?

Get most common annotation value (mode)

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Object, nil)

    Most frequent value



139
140
141
142
# File 'lib/archsight/annotations/computed.rb', line 139

def most_common(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.most_common(values)
end

#outgoing(kind = nil) ⇒ Object

Get direct outgoing relations (-> Kind)



47
48
49
# File 'lib/archsight/annotations/computed.rb', line 47

def outgoing(kind = nil)
  @resolver.outgoing(kind)
end

#outgoing_transitive(kind = nil, max_depth: 10) ⇒ Object

Get transitive outgoing relations (~> Kind)



52
53
54
# File 'lib/archsight/annotations/computed.rb', line 52

def outgoing_transitive(kind = nil, max_depth: 10)
  @resolver.outgoing_transitive(kind, max_depth: max_depth)
end

#sum(instances, key) ⇒ Float?

Sum numeric annotation values from instances

Parameters:

  • instances (Array)

    Array of resource instances

  • key (String)

    Annotation key to extract values from

Returns:

  • (Float, nil)

    Sum of values or nil if no values



72
73
74
75
# File 'lib/archsight/annotations/computed.rb', line 72

def sum(instances, key)
  values = extract_values(instances, key)
  Archsight::Annotations::ComputedAggregators.sum(values)
end