cocluto/inventory.py

107 lines
4.9 KiB
Python

# encoding: utf-8
import sys
import os
import re
import datetime
import numpy as np
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 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_serial_number):
ordering_id = self._sql_reader.get_table_attr( 'machines', 'serial_number', machine_serial_number, '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_serial_number):
print(computer_serial_number)
num_cpus = int(self._sql_reader.get_table_attr( 'computer_to_cpu', 'serial_number', computer_serial_number, 'num_cpu' ))
cpu_model = self._sql_reader.get_table_attr( 'computer_to_cpu', 'serial_number', computer_serial_number, '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_ownership(self, item_serial_number):
ownership=[]
rows = self._sql_reader.query("SELECT * FROM ownership")
for row in rows:
(serial_number, owner, owner_ratio, comment)=row
if serial_number == item_serial_number:
ownership.append({'owner':owner, 'owner_ratio':owner_ratio})
return ownership