Class: GrowthForecast::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/growthforecast/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Graph

TODO strict validations



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/growthforecast/graph.rb', line 13

def initialize(obj)
  if obj.is_a?(String)
    obj = JSON.parse(obj)
  end

  obj.keys.each do |k|
    obj[k.to_sym] = obj.delete(k)
  end

  if obj[:complex]
    raise ArgumentError, "complex graph is for GrowthForecast::Complex"
  end

  @id = obj[:id] ? obj[:id].to_i : nil
  @service_name = obj[:service_name]
  @section_name = obj[:section_name]
  @graph_name = obj[:graph_name]
  @description = obj[:description]
  @mode = obj[:mode] || 'gauge'
  @sort = (obj[:sort] || 19).to_i
  @color = obj[:color]
  @gmode = obj[:gmode] || 'gauge'
  @type = obj[:type] || 'AREA'
  @ulimit = obj[:ulimit] || 1000000000
  @llimit = obj[:llimit] || -1000000000
  @stype = obj[:stype] || 'AREA'
  @sulimit = obj[:sulimit] || 100000
  @sllimit = obj[:sllimit] || -100000
  @adjust = obj[:adjust] || '*'
  @adjustval = (obj[:adjustval] || 1).to_i
  @unit = obj[:unit] || ''
  @number = (obj[:number] || 0).to_i
  @data = obj[:data] || []
  @created_at = obj[:created_at] ? Time.strptime(obj[:created_at], GrowthForecast::TIME_FORMAT) : nil
  @updated_at = obj[:updated_at] ? Time.strptime(obj[:updated_at], GrowthForecast::TIME_FORMAT) : nil
end

Instance Attribute Details

#adjustObject

Returns the value of attribute adjust.



8
9
10
# File 'lib/growthforecast/graph.rb', line 8

def adjust
  @adjust
end

#adjustvalObject

Returns the value of attribute adjustval.



8
9
10
# File 'lib/growthforecast/graph.rb', line 8

def adjustval
  @adjustval
end

#colorObject

Returns the value of attribute color.



6
7
8
# File 'lib/growthforecast/graph.rb', line 6

def color
  @color
end

#created_atObject

Returns the value of attribute created_at.



9
10
11
# File 'lib/growthforecast/graph.rb', line 9

def created_at
  @created_at
end

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/growthforecast/graph.rb', line 9

def data
  @data
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/growthforecast/graph.rb', line 6

def description
  @description
end

#gmodeObject

Returns the value of attribute gmode.



6
7
8
# File 'lib/growthforecast/graph.rb', line 6

def gmode
  @gmode
end

#graph_nameObject

Returns the value of attribute graph_name.



5
6
7
# File 'lib/growthforecast/graph.rb', line 5

def graph_name
  @graph_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/growthforecast/graph.rb', line 5

def id
  @id
end

#llimitObject

Returns the value of attribute llimit.



7
8
9
# File 'lib/growthforecast/graph.rb', line 7

def llimit
  @llimit
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/growthforecast/graph.rb', line 6

def mode
  @mode
end

#numberObject

Returns the value of attribute number.



9
10
11
# File 'lib/growthforecast/graph.rb', line 9

def number
  @number
end

#section_nameObject

Returns the value of attribute section_name.



5
6
7
# File 'lib/growthforecast/graph.rb', line 5

def section_name
  @section_name
end

#service_nameObject

Returns the value of attribute service_name.



5
6
7
# File 'lib/growthforecast/graph.rb', line 5

def service_name
  @service_name
end

#sllimitObject

Returns the value of attribute sllimit.



7
8
9
# File 'lib/growthforecast/graph.rb', line 7

def sllimit
  @sllimit
end

#sortObject

Returns the value of attribute sort.



6
7
8
# File 'lib/growthforecast/graph.rb', line 6

def sort
  @sort
end

#stypeObject

Returns the value of attribute stype.



7
8
9
# File 'lib/growthforecast/graph.rb', line 7

def stype
  @stype
end

#sulimitObject

Returns the value of attribute sulimit.



7
8
9
# File 'lib/growthforecast/graph.rb', line 7

def sulimit
  @sulimit
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/growthforecast/graph.rb', line 7

def type
  @type
end

#ulimitObject

Returns the value of attribute ulimit.



7
8
9
# File 'lib/growthforecast/graph.rb', line 7

def ulimit
  @ulimit
end

#unitObject

Returns the value of attribute unit.



8
9
10
# File 'lib/growthforecast/graph.rb', line 8

def unit
  @unit
end

#updated_atObject

Returns the value of attribute updated_at.



9
10
11
# File 'lib/growthforecast/graph.rb', line 9

def updated_at
  @updated_at
end

Instance Method Details

#complex?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/growthforecast/graph.rb', line 50

def complex?
  false
end

#to_jsonObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/growthforecast/graph.rb', line 54

def to_json
  if @color.nil?
    raise RuntimeError, "cannot stringify as json without 'color' parameter"
  end

  {
    'complex' => false,
    'id' => @id,
    'service_name' => @service_name, 'section_name' => @section_name, 'graph_name' => @graph_name,
    'description' => @description, 'mode' => @mode, 'sort' => @sort, 'color' => @color, 'gmode' => @gmode,
    'type' => @type, 'ulimit' => @ulimit, 'llimit' => @llimit, 'stype' => @stype, 'sulimit' => @sulimit, 'sllimit' => @sllimit,
    'adjust' => @adjust, 'adjustval' => @adjustval, 'unit' => @unit,
    'number' => @number, 'data' => @data,
    'created_at' => @created_at.strftime(GrowthForecast::TIME_FORMAT), 'updated_at' => @updated_at.strftime(GrowthForecast::TIME_FORMAT)
  }.to_json
end