Class: Resource

Inherits:
Base
  • Object
show all
Includes:
DeterministicHash
Defined in:
app/models/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeterministicHash

#deterministic_hash

Methods inherited from Base

ma_accessor, ma_reader, ma_writer, #serialize_more_attributes

Class Method Details

.region_codes(region_area) ⇒ Object


13
14
15
16
17
# File 'app/models/resource.rb', line 13

def self.region_codes(region_area)
  Resource.region_area(region_area).select(:region, :provider_id).distinct.map do |resource|
    resource.provider.region_code(resource.region)
  end
end

.regions(region_area) ⇒ Object


19
20
21
# File 'app/models/resource.rb', line 19

def self.regions(region_area)
  Resource.region_area(region_area).select(:region).distinct.map(&:region)
end

Instance Method Details

#as_json(options = {}) ⇒ Object


51
52
53
54
55
56
57
58
# File 'app/models/resource.rb', line 51

def as_json(options={})
  hash = {}
  hash[:resource_code] = self.resource_code
  hash[:resource_type] = self.resource_type
  hash[:name] = self.name
  hash[:provider] = self.provider.name
  hash.merge(params_for_resource_type)
end

#compute_as_json(resource) ⇒ Object (private)


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/resource.rb', line 82

def compute_as_json(resource)
  hash = {}
  hash[:cores] = resource.cores.to_f
  hash[:mem_gb] = resource.mem_gb.to_f
  hash[:price_per_hour] = resource.price_per_hour.to_f
  hash[:price_per_month] = resource.price_per_month.to_f
  hash[:region] = resource.region
  hash[:region_area] = resource.region_area
  hash[:bandwidth_mpbs] = resource.bandwidth_mbps if resource.bandwidth_mbps
  hash[:created_at] = resource.created_at
  hash[:updated_at] = resource.updated_at
  hash
end

#derive_region_codeObject


23
24
25
# File 'app/models/resource.rb', line 23

def derive_region_code
  self.provider.region_code(self.region)
end

#derive_resource_codeObject

Assuming that a resource is identified by the composite key: 1) region_code (provider_name + region_name) 2) resource name


30
31
32
33
# File 'app/models/resource.rb', line 30

def derive_resource_code
  resource_string = self.region_code.to_s + self.name.to_s
  deterministic_hash(resource_string)
end

#generate_region_codeObject (private)


74
75
76
# File 'app/models/resource.rb', line 74

def generate_region_code
  self.region_code = derive_region_code
end

#generate_resource_codeObject (private)


78
79
80
# File 'app/models/resource.rb', line 78

def generate_resource_code
  self.resource_code = derive_resource_code
end

#is_compute?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'app/models/resource.rb', line 35

def is_compute?
  resource_type == 'compute'
end

#params_for_resource_typeObject (private)


62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/resource.rb', line 62

def params_for_resource_type
  case self.resource_type
    when 'compute'
      compute_as_json(self)
    when 'storage'
      storage_as_json(self)
    else
      Logger.warn "Resource (id=#{self.id}) with invalid type!"
      {}
  end
end

#price_per_hourObject


47
48
49
# File 'app/models/resource.rb', line 47

def price_per_hour
  BigDecimal.new(more_attributes['price_per_hour'].to_s || 0)
end

#price_per_monthObject


39
40
41
42
43
44
45
# File 'app/models/resource.rb', line 39

def price_per_month
  if more_attributes['price_per_month']
    BigDecimal.new(more_attributes['price_per_month'].to_s)
  else
    price_per_hour * 744
  end
end

#storage_as_json(resource) ⇒ Object (private)


96
97
98
99
100
101
102
103
104
# File 'app/models/resource.rb', line 96

def storage_as_json(resource)
  hash = {}
  hash[:price_per_month_gb] = resource.price_per_month_gb
  hash[:region] = resource.region
  hash[:region_area] = resource.region_area
  hash[:created_at] = resource.created_at
  hash[:updated_at] = resource.updated_at
  hash
end