cocluto/inventory.py

165 lines
7.6 KiB
Python

# encoding: utf-8
import sys
import os
import re
import datetime
from SimpaDbUtil import SqlDatabaseReader, SqlFile
class Inventory( object ):
def __init__(self, sql_reader):
"""
:param SqlDatabaseReader sql_reader: the inventory database
"""
super(Inventory, self).__init__()
self._sql_reader = sql_reader
def query(self, sql_query):
return self._sql_reader.query(sql_query)
def get_machine_serial_number(self, machine_name):
'''
returns the serial number of the given machine
'''
machine_serial_number = self._sql_reader.get_table_attr( 'machines', 'name', machine_name, 'serial_number' )
return machine_serial_number
def get_machine_name(self, machine_serial_number):
'''
returns the user-friendly name of the given machine
'''
machine_name = self._sql_reader.get_table_attr( 'machines', 'serial_number', machine_serial_number, 'name' )
return machine_name
def machine_name_to_machine_spec_id(self, machine_name ):
machine_spec_id = self._sql_reader.get_table_attr( 'machines', 'name', machine_name, 'machine_spec_id' )
return machine_spec_id
# electricity related methods
def machine_spec_id_to_power_consumption(self, machine_spec_id ):
power_consumption = self._sql_reader.get_table_attr( 'machine_spec_to_power_consumption', 'machine_spec_id', machine_spec_id, 'power_consumption' )
return power_consumption
def get_plug_type_attr(self, plug_type, attr_name):
# INSERT INTO `powerplug_type_desc` (`plug_type_id`, `genre`, `max_amps`) VALUES
# ('iec60309_blue_pne6h_32a_m', 'm', 32.0),
attr_value = self._sql_reader.get_table_attr('powerplug_type_desc', 'plug_type_id', plug_type, attr_name )
return attr_value
def read_plug_capacity(self, plug):
plug_capacity = None
machine_spec_id = self.machine_name_to_machine_spec_id(plug.machine.name)
if machine_spec_id is not None:
# INSERT INTO `powerplug_desc` (`machine_spec_id`, `powerplug_id`, `plug_type`) VALUES
# ('atos_mpdu_2901382', 'i', 'iec60309_blue_pne6h_32a_m'),
rows = self._sql_reader.query("SELECT plug_type FROM powerplug_desc WHERE machine_spec_id='%s' AND powerplug_id='%s'" % (machine_spec_id, plug.name))
if len(rows) > 0:
plug_type = rows[0][0]
# print('plug_type : %s' % plug_type)
plug_capacity = self.get_plug_type_attr(plug_type, 'max_amps')
#if plug_capacity:
# print('plug_capacity : %f A' % plug_capacity)
# print("read_plug_capacity : plug capacity for plug.machine.name="+plug.machine.name+" plug="+str(plug)+" : "+ str(plug_capacity)+ "A")
return plug_capacity
# cluster related methods
def get_machine_purchase_date(self, machine_id):
ordering_id = self._sql_reader.get_table_attr( 'machines', 'name', machine_id, 'command_id' )
# print(ordering_id)
# handle case of multiple orders
ordering_id = ordering_id.split('+')[0]
if len(ordering_id) == 0:
return None
ordering_date_as_str = self._sql_reader.get_table_attr( 'orderings', 'ordering_id', ordering_id, 'ordering_date' )
if ordering_date_as_str is None:
return None
if len(ordering_date_as_str) == 0:
return None
ordering_date = datetime.datetime.strptime(ordering_date_as_str, '%d/%m/%Y')
return ordering_date
def get_cpu_dflops(self, cpu_model):
'''
returns the number of double precision operation per second this cpu can achieve
'''
#INSERT INTO `cpu_specs` (`cpu_model`, `num_cores`, `clock_speed`, `dflops_per_core_per_cycle`, `comment`) VALUES
#('intel_xeon_x5550', 4, 2.67, 4, ''),
num_cores = int(self._sql_reader.get_table_attr( 'cpu_specs', 'cpu_model', cpu_model, 'num_cores' ))
clock_speed = float(self._sql_reader.get_table_attr( 'cpu_specs', 'cpu_model', cpu_model, 'clock_speed' ))
dflops_per_core_per_cycle = int(self._sql_reader.get_table_attr('cpu_specs', 'cpu_model', cpu_model, 'dflops_per_core_per_cycle' ))
# print(num_cores, clock_speed, dflops_per_core_per_cycle)
return clock_speed * dflops_per_core_per_cycle * num_cores
def get_computer_dflops(self, computer_name):
# print(computer_serial_number)
num_cpus = int(self._sql_reader.get_table_attr( 'computer_to_cpu', 'computer_id', computer_name, 'num_cpu' ))
cpu_model = self._sql_reader.get_table_attr( 'computer_to_cpu', 'computer_id', computer_name, 'cpu_model' )
flops = num_cpus * self.get_cpu_dflops( cpu_model )
return flops
def get_computer_options_price(self, computer_name):
options_price = 0.0
if computer_name == 'simpatix58' or computer_name == 'simpatix59':
return 7675.0 / 4 * 2 # each of these computers has 2 nvidia fermi C2050 gpus
return options_price
def get_item_container(self, item_id):
"""
:param str item_id: the identifier of an inventory item (a machine (eg simpa-switch002), a group of machines (ceph), etc.)
:return str: the item that contains the given item, None if this item has no contrainer
"""
container_id = None
rows = self._sql_reader.query("SELECT container_id FROM container WHERE part_id='%s'" % item_id)
if len(rows) > 0:
container_id = rows[0][0]
return container_id
def get_item_price(self, item_id, include_contents=False, include_maintenance=False):
"""
:param str item_id: the identifier of an inventory item (a machine (eg simpa-switch002), a group of machines (ceph), etc.)
:return float: the price of the item exluding taxes
"""
item_price = self._sql_reader.get_table_attr( 'machines', 'name', item_id, 'price_ex_vat' )
if item_price == None:
item_price = 0.0
else:
item_price = float(item_price)
if include_maintenance:
# INSERT INTO `maintenance` (`maintenance_id`, `machine_id`, `price_ex_vat`, `command_id`, `comment`) VALUES
rows = self._sql_reader.query("SELECT price_ex_vat FROM maintenance WHERE machine_id='%s'" % item_id)
for row in rows:
maintenance_price_ex_vat = float(row[0])
item_price += maintenance_price_ex_vat
if include_contents:
# add the price of included parts
rows = self._sql_reader.query("SELECT part_id FROM container WHERE container_id='%s'" % item_id)
for row in rows:
part_id = row[0]
item_price += self.get_item_price(part_id, include_contents, include_maintenance)
#print(u'price of %s : %.2f € HT' % (item_id, item_price))
return item_price
def get_item_ownership(self, item_id):
ownership=[]
rows = self._sql_reader.query("SELECT * FROM ownership WHERE machine_id='%s'" % item_id)
for row in rows:
(machine_id, owner, owner_ratio, comment)=row
ownership.append({'owner':owner, 'owner_ratio':owner_ratio})
return ownership
def get_item_use(self, item_id):
ownership=[]
rows = self._sql_reader.query("SELECT * FROM machine_users")
for row in rows:
(machine_id, user, user_ratio, comment)=row
if machine_id == item_id:
ownership.append({'user':user, 'user_ratio':user_ratio})
return ownership