Bug 1978 - trouver un moyen de rationaliser l'achat du r930 cper 2017

- refactoring : improved so that now PowerDiagram also uses the Inventory class instead of duplicating code. This will:
  1. make the code easier to maintain when there is a change in the inventory database tables
  2. make the code easier to adapt when we switch from a sql file to a mysql server
This commit is contained in:
Guillaume Raffy 2018-02-07 08:43:05 +00:00
parent 7c6656dc69
commit 3ba55f8758
2 changed files with 20 additions and 70 deletions

View File

@ -13,7 +13,8 @@ import os
import re import re
import sys import sys
import pygraphviz import pygraphviz
from mysql2sqlite import mysql_to_sqlite from inventory import Inventory
from SimpaDbUtil import SqlFile, SqlDatabaseReader
def add_capacity_constraints(capacity1, capacity2): def add_capacity_constraints(capacity1, capacity2):
""" """
@ -164,80 +165,29 @@ class PowerConfig(object):
def __init__(self, simpa_db_sql_file_path): def __init__(self, simpa_db_sql_file_path):
self.machines = {} self.machines = {}
self.connections = [] self.connections = []
self._parse_from_simpa_db_sql_file(simpa_db_sql_file_path)
def _parse_from_simpa_db_sql_file(self, simpa_db_sql_file_path): sql_source = SqlFile(simpa_db_sql_file_path)
sql_reader = SqlDatabaseReader(sql_source)
inventory = Inventory(sql_reader)
self._parse_from_inventory(inventory)
def get_table_attr( cur, table, key_name, key_value, attr_name ): def _parse_from_inventory(self, inventory):
attr_value = None """
cur.execute("SELECT "+attr_name+" FROM "+table+" WHERE "+key_name+"='"+key_value+"'") :param Inventory inventory:
rows = cur.fetchall() """
if len(rows) > 0:
attr_value = rows[0][0]
return attr_value
def machine_name_toMachine_spec_id( cur, machine_name ): rows = inventory.query("SELECT * FROM machine_to_power")
machine_spec_id = get_table_attr( cur, 'machines', 'name', machine_name, 'machine_spec_id' )
return machine_spec_id
def machine_spec_id_to_power_consumption( cur, machine_spec_id ):
power_consumption = get_table_attr( cur, 'machine_spec_to_power_consumption', 'machine_spec_id', machine_spec_id, 'power_consumption' )
return power_consumption
def get_plug_type_attr(cur, 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 = get_table_attr( cur, 'powerplug_type_desc', 'plug_type_id', plug_type, attr_name )
return attr_value
def read_plug_capacity(cur, plug):
plug_capacity = None
machine_spec_id = machine_name_toMachine_spec_id(cur, 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'),
cur.execute("SELECT plug_type FROM powerplug_desc WHERE machine_spec_id='%s' AND powerplug_id='%s'" % (machine_spec_id, plug.name))
rows = cur.fetchall()
if len(rows) > 0:
plug_type = rows[0][0]
# print('plug_type : %s' % plug_type)
plug_capacity = get_plug_type_attr(cur, 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
sqliteDbPath=':memory:' # sqlite-specific special name for a file stored in memory. We could use something like '/tmp/simpadb.sqlite' here but this would make parsing really slow (1 minute instead of 1s), unless either :
# - proper fix : group of INSERT statements are surrounded by BEGIN and COMMIT (see http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows)
# - the file is stored on a solid state disk
try:
os.remove(sqliteDbPath)
except:
pass
con = sqlite3.connect(sqliteDbPath)
f = open(simpa_db_sql_file_path, 'r')
sql = f.read() # watch out for built-in `str`
#print(sql)
cur = con.cursor()
#print(mysql_to_sqlite(sql))
cur.executescript(mysql_to_sqlite(sql))
cur.execute("SELECT * FROM machine_to_power")
rows = cur.fetchall()
for row in rows: for row in rows:
(to_plug_as_str, from_plug_as_str, powercordid)=row (to_plug_as_str, from_plug_as_str, powercordid)=row
if to_plug_as_str != '': if to_plug_as_str != '':
conn = self._add_connection(from_plug_as_str, to_plug_as_str) conn = self._add_connection(from_plug_as_str, to_plug_as_str)
for plug in (conn.from_plug, conn.to_plug): for plug in (conn.from_plug, conn.to_plug):
plug_capacity = read_plug_capacity(cur, plug) plug_capacity = inventory.read_plug_capacity(plug)
if plug_capacity is not None: if plug_capacity is not None:
plug.set_current_capacity_constraint(plug_capacity) plug.set_current_capacity_constraint(plug_capacity)
cur.execute("SELECT * FROM power_output_specs") rows = inventory.query("SELECT * FROM power_output_specs")
rows = cur.fetchall()
for row in rows: for row in rows:
# print row # print row
@ -255,9 +205,9 @@ class PowerConfig(object):
# print(machine_name) # print(machine_name)
machine_spec_id = machine_name_toMachine_spec_id(cur, machine_name) machine_spec_id = inventory.machine_name_to_machine_spec_id(machine_name)
if machine_spec_id is not None: if machine_spec_id is not None:
power_consumption = machine_spec_id_to_power_consumption(cur, machine_spec_id) power_consumption = inventory.machine_spec_id_to_power_consumption(machine_spec_id)
if power_consumption is not None: if power_consumption is not None:
machine.power_consumption = power_consumption machine.power_consumption = power_consumption

View File

@ -18,7 +18,7 @@ class Inventory( object ):
def query(self, sql_query): def query(self, sql_query):
return self._sql_reader.query(sql_query) return self._sql_reader.query(sql_query)
def machine_name_toMachine_spec_id(self, 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' ) machine_spec_id = self._sql_reader.get_table_attr( 'machines', 'name', machine_name, 'machine_spec_id' )
return machine_spec_id return machine_spec_id
@ -36,7 +36,7 @@ class Inventory( object ):
def read_plug_capacity(self, plug): def read_plug_capacity(self, plug):
plug_capacity = None plug_capacity = None
machine_spec_id = self.machine_name_toMachine_spec_id(plug.machine.name) machine_spec_id = self.machine_name_to_machine_spec_id(plug.machine.name)
if machine_spec_id is not None: if machine_spec_id is not None:
# INSERT INTO `powerplug_desc` (`machine_spec_id`, `powerplug_id`, `plug_type`) VALUES # INSERT INTO `powerplug_desc` (`machine_spec_id`, `powerplug_id`, `plug_type`) VALUES
# ('atos_mpdu_2901382', 'i', 'iec60309_blue_pne6h_32a_m'), # ('atos_mpdu_2901382', 'i', 'iec60309_blue_pne6h_32a_m'),
@ -45,7 +45,7 @@ class Inventory( object ):
plug_type = rows[0][0] plug_type = rows[0][0]
# print('plug_type : %s' % plug_type) # print('plug_type : %s' % plug_type)
plug_capacity = get_plug_type_attr(plug_type, 'max_amps') plug_capacity = self.get_plug_type_attr(plug_type, 'max_amps')
#if plug_capacity: #if plug_capacity:
# print('plug_capacity : %f A' % 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") # print("read_plug_capacity : plug capacity for plug.machine.name="+plug.machine.name+" plug="+str(plug)+" : "+ str(plug_capacity)+ "A")