diff options
Diffstat (limited to 'opendc/models')
30 files changed, 96 insertions, 114 deletions
diff --git a/opendc/models/allowed_object.py b/opendc/models/allowed_object.py index acd58e94..7ff742fe 100644 --- a/opendc/models/allowed_object.py +++ b/opendc/models/allowed_object.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class AllowedObject(Model): +class AllowedObject(Model): JSON_TO_PYTHON_DICT = { 'AllowedObject': { 'roomType': 'room_type', diff --git a/opendc/models/authorization.py b/opendc/models/authorization.py index 90c7d399..82d37b76 100644 --- a/opendc/models/authorization.py +++ b/opendc/models/authorization.py @@ -1,10 +1,8 @@ -import json - from opendc.models.model import Model from opendc.models.user import User -class Authorization(Model): +class Authorization(Model): JSON_TO_PYTHON_DICT = { 'Authorization': { 'userId': 'user_id', @@ -26,12 +24,12 @@ class Authorization(Model): self.simulation_id ) ) - + if authorization is None: return False return authorization.has_at_least(authorization_level) - + def has_at_least(self, required_level): """Return True if this Authorization has at least the required level.""" @@ -41,7 +39,7 @@ class Authorization(Model): authorization_levels = ['VIEW', 'EDIT', 'OWN'] try: - index_actual = authorization_levels.index(self.authorization_level) + index_actual = authorization_levels.index(self.authorization_level) index_required = authorization_levels.index(required_level) except: return False @@ -50,4 +48,3 @@ class Authorization(Model): return True else: return False - diff --git a/opendc/models/cpu.py b/opendc/models/cpu.py index a2a0e79f..5b9b44fb 100644 --- a/opendc/models/cpu.py +++ b/opendc/models/cpu.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class CPU(Model): +class CPU(Model): JSON_TO_PYTHON_DICT = { 'CPU': { 'id': 'id', diff --git a/opendc/models/datacenter.py b/opendc/models/datacenter.py index 32ce1d49..aeb9b3ad 100644 --- a/opendc/models/datacenter.py +++ b/opendc/models/datacenter.py @@ -1,11 +1,8 @@ -import json - from opendc.models.model import Model from opendc.models.section import Section -from opendc.util import database, exceptions -class Datacenter(Model): +class Datacenter(Model): JSON_TO_PYTHON_DICT = { 'datacenter': { 'id': 'id', diff --git a/opendc/models/experiment.py b/opendc/models/experiment.py index c7381084..e875f8d1 100644 --- a/opendc/models/experiment.py +++ b/opendc/models/experiment.py @@ -1,9 +1,9 @@ from opendc.models.model import Model from opendc.models.simulation import Simulation -from opendc.util import database, exceptions +from opendc.util import exceptions -class Experiment(Model): +class Experiment(Model): JSON_TO_PYTHON_DICT = { 'Experiment': { 'id': 'id', diff --git a/opendc/models/failure_model.py b/opendc/models/failure_model.py index 8dd16d6c..ff6459e9 100644 --- a/opendc/models/failure_model.py +++ b/opendc/models/failure_model.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class FailureModel(Model): +class FailureModel(Model): JSON_TO_PYTHON_DICT = { 'FailureModel': { 'id': 'id', diff --git a/opendc/models/gpu.py b/opendc/models/gpu.py index d9978ec7..37de235c 100644 --- a/opendc/models/gpu.py +++ b/opendc/models/gpu.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class GPU(Model): +class GPU(Model): JSON_TO_PYTHON_DICT = { 'GPU': { 'id': 'id', diff --git a/opendc/models/job.py b/opendc/models/job.py index fb133a72..e013b991 100644 --- a/opendc/models/job.py +++ b/opendc/models/job.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class Job(Model): +class Job(Model): JSON_TO_PYTHON_DICT = { 'Job': { 'id': 'id', diff --git a/opendc/models/machine.py b/opendc/models/machine.py index 90945ef1..0d9fbd54 100644 --- a/opendc/models/machine.py +++ b/opendc/models/machine.py @@ -4,8 +4,8 @@ from opendc.models.model import Model from opendc.models.rack import Rack from opendc.util import database, exceptions -class Machine(Model): +class Machine(Model): JSON_TO_PYTHON_DICT = { 'machine': { 'id': 'id', @@ -34,7 +34,7 @@ class Machine(Model): def _update_devices(self, before_insert): """Update this Machine's devices in the database.""" - + for device_table in self.device_table_to_attribute.keys(): # First, create the statements to execute @@ -48,15 +48,14 @@ class Machine(Model): database.execute(statement, (before_insert.id,)) # Then, add current ones - - for device_id in getattr(before_insert, before_insert.device_table_to_attribute[device_table]): + for device_id in getattr(before_insert, before_insert.device_table_to_attribute[device_table]): statement = 'INSERT INTO machine_{} (machine_id, {}) VALUES (%s, %s)'.format( device_table, before_insert.device_table_to_attribute[device_table][:-1] ) - - database.execute(statement, (before_insert.id, device_id)) + + database.execute(statement, (before_insert.id, device_id)) @classmethod def from_tile_id_and_rack_position(cls, tile_id, position): @@ -65,13 +64,13 @@ class Machine(Model): try: rack = Rack.from_tile_id(tile_id) except: - return cls(id = -1) - + return cls(id=-1) + try: statement = 'SELECT id FROM machines WHERE rack_id = %s AND position = %s' machine_id = database.fetchone(statement, (rack.id, position))[0] except: - return cls(id = -1) + return cls(id=-1) return cls.from_primary_key((machine_id,)) @@ -93,7 +92,7 @@ class Machine(Model): """Insert this Machine by also updating its devices.""" before_insert = copy.deepcopy(self) - + super(Machine, self).insert() before_insert.id = self.id @@ -101,21 +100,21 @@ class Machine(Model): def read(self): """Read this Machine by also getting its CPU, GPU, Memory and Storage IDs.""" - + super(Machine, self).read() for device_table in self.device_table_to_attribute.keys(): - + statement = 'SELECT * FROM machine_{} WHERE machine_id = %s'.format(device_table) results = database.fetchall(statement, (self.id,)) - + device_ids = [] for row in results: device_ids.append(row[2]) setattr(self, self.device_table_to_attribute[device_table], device_ids) - + setattr(self, 'tags', []) def update(self): diff --git a/opendc/models/machine_state.py b/opendc/models/machine_state.py index 693b57d2..7f19ba01 100644 --- a/opendc/models/machine_state.py +++ b/opendc/models/machine_state.py @@ -1,8 +1,8 @@ from opendc.models.model import Model from opendc.util import database -class MachineState(Model): +class MachineState(Model): JSON_TO_PYTHON_DICT = { 'MachineState': { 'taskId': 'task_id', @@ -15,21 +15,22 @@ class MachineState(Model): } TABLE_NAME = 'machine_states' - COLUMNS = ['id', 'task_id', 'machine_id', 'experiment_id', 'tick', 'temperature_c', 'in_use_memory_mb', 'load_fraction'] + COLUMNS = ['id', 'task_id', 'machine_id', 'experiment_id', 'tick', 'temperature_c', 'in_use_memory_mb', + 'load_fraction'] - COLUMNS_PRIMARY_KEY= ['id'] + COLUMNS_PRIMARY_KEY = ['id'] @classmethod def _from_database_row(cls, row): """Instantiate a MachineState from a database row (including tick from the TaskState).""" return cls( - task_id = row[1], - machine_id = row[2], - temperature_c = row[5], - in_use_memory_mb = row[6], - load_fraction = row[7], - tick = row[4] + task_id=row[1], + machine_id=row[2], + temperature_c=row[5], + in_use_memory_mb=row[6], + load_fraction=row[7], + tick=row[4] ) @classmethod @@ -37,11 +38,11 @@ class MachineState(Model): """Query MachineStates by their Experiment id.""" machine_states = [] - + statement = 'SELECT * FROM machine_states WHERE experiment_id = %s' results = database.fetchall(statement, (experiment_id,)) - - for row in results: + + for row in results: machine_states.append(cls._from_database_row(row)) return machine_states @@ -51,11 +52,11 @@ class MachineState(Model): """Query MachineStates by their Experiment id and tick.""" machine_states = [] - + statement = 'SELECT * FROM machine_states WHERE experiment_id = %s AND machine_states.tick = %s' results = database.fetchall(statement, (experiment_id, tick)) - - for row in results: + + for row in results: machine_states.append(cls._from_database_row(row)) return machine_states diff --git a/opendc/models/memory.py b/opendc/models/memory.py index 56497bd4..961c0479 100644 --- a/opendc/models/memory.py +++ b/opendc/models/memory.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class Memory(Model): +class Memory(Model): JSON_TO_PYTHON_DICT = { 'Memory': { 'id': 'id', diff --git a/opendc/models/model.py b/opendc/models/model.py index 18ea61f4..30da9c67 100644 --- a/opendc/models/model.py +++ b/opendc/models/model.py @@ -1,7 +1,7 @@ from opendc.util import database, exceptions -class Model(object): +class Model(object): # MUST OVERRIDE IN DERIVED CLASS JSON_TO_PYTHON_DICT = { @@ -9,14 +9,14 @@ class Model(object): 'jsonParameterName': 'python_parameter_name' } } - + PATH = '' PATH_PARAMETERS = {} TABLE_NAME = '' COLUMNS = [] COLUMNS_PRIMARY_KEY = [] - + # INITIALIZATION def __init__(self, **kwargs): @@ -50,7 +50,7 @@ class Model(object): for json_name in parameter_map: python_name = parameter_map[json_name] - + if json_name in json_object: parameters[python_name] = json_object.get(json_name) @@ -71,12 +71,11 @@ class Model(object): if hasattr(self, python_name): parameters[json_name] = getattr(self, python_name) - + else: parameters[json_name] = None return parameters - # API CALL GENERATION @@ -114,7 +113,7 @@ class Model(object): @classmethod def _generate_primary_key_string(cls): """Generate the SQLite primary key string for this Model.""" - + return ' AND '.join(['{} = %s'.format(x) for x in cls.COLUMNS_PRIMARY_KEY]) @classmethod @@ -199,7 +198,7 @@ class Model(object): parameters = {} for i, column in enumerate(cls.COLUMNS_PRIMARY_KEY): parameters[column] = primary_key_tuple[i] - + return cls(**parameters) @classmethod @@ -209,7 +208,7 @@ class Model(object): if column_name is not None and value is not None: statement = 'SELECT * FROM {} WHERE {} = %s'.format(cls.TABLE_NAME, column_name) database_models = database.fetchall(statement, (value,)) - + else: statement = 'SELECT * FROM {}'.format(cls.TABLE_NAME) database_models = database.fetchall(statement) @@ -288,7 +287,7 @@ class Model(object): ) values = self._generate_insert_columns_tuple() - + try: last_row_id = database.execute(statement, values) except Exception as e: diff --git a/opendc/models/object.py b/opendc/models/object.py index 4103107a..f9990d81 100644 --- a/opendc/models/object.py +++ b/opendc/models/object.py @@ -1,8 +1,7 @@ from opendc.models.model import Model -from opendc.util import database, exceptions -class Object(Model): +class Object(Model): JSON_TO_PYTHON_DICT = { 'Object': { 'id': 'id', @@ -16,5 +15,5 @@ class Object(Model): def google_id_has_at_least(self, google_id, authorization_level): """Return True if the user has at least the given auth level over this Tile.""" - + return True diff --git a/opendc/models/path.py b/opendc/models/path.py index 6ce29b9b..09651a66 100644 --- a/opendc/models/path.py +++ b/opendc/models/path.py @@ -1,10 +1,10 @@ from opendc.models.authorization import Authorization from opendc.models.model import Model from opendc.models.user import User -from opendc.util import database, exceptions +from opendc.util import exceptions -class Path(Model): +class Path(Model): JSON_TO_PYTHON_DICT = { 'Path': { 'id': 'id', diff --git a/opendc/models/queued_experiment.py b/opendc/models/queued_experiment.py index fbaed9cb..1cb52c49 100644 --- a/opendc/models/queued_experiment.py +++ b/opendc/models/queued_experiment.py @@ -1,9 +1,7 @@ from opendc.models.model import Model -from opendc.models.experiment import Experiment -from opendc.util import database, exceptions -class QueuedExperiment(Model): +class QueuedExperiment(Model): JSON_TO_PYTHON_DICT = { 'QueuedExperiment': { 'experimentId': 'experiment_id' diff --git a/opendc/models/rack.py b/opendc/models/rack.py index da965849..74104fcb 100644 --- a/opendc/models/rack.py +++ b/opendc/models/rack.py @@ -1,10 +1,9 @@ from opendc.models.model import Model -from opendc.models.tile import Tile from opendc.models.object import Object -from opendc.util import database, exceptions +from opendc.models.tile import Tile -class Rack(Model): +class Rack(Model): JSON_TO_PYTHON_DICT = { 'rack': { 'id': 'id', @@ -27,7 +26,7 @@ class Rack(Model): tile = Tile.from_primary_key((tile_id,)) if not tile.exists(): - return Rack(id = -1) + return Rack(id=-1) return cls.from_primary_key((tile.object_id,)) @@ -48,7 +47,7 @@ class Rack(Model): def insert(self): """Insert a Rack by first inserting an object.""" - obj = Object(type = 'RACK') + obj = Object(type='RACK') obj.insert() self.id = obj.id diff --git a/opendc/models/rack_state.py b/opendc/models/rack_state.py index e43dc940..c0f0ff6c 100644 --- a/opendc/models/rack_state.py +++ b/opendc/models/rack_state.py @@ -1,8 +1,8 @@ from opendc.models.model import Model from opendc.util import database -class RackState(Model): +class RackState(Model): JSON_TO_PYTHON_DICT = { 'RackState': { 'rackId': 'rack_id', @@ -16,9 +16,9 @@ class RackState(Model): """Instantiate a RackState from a database row.""" return cls( - rack_id = row[0], - load_fraction = row[1], - tick = row[2] + rack_id=row[0], + load_fraction=row[1], + tick=row[2] ) @classmethod @@ -63,7 +63,6 @@ class RackState(Model): rack_states.append(cls._from_database_row(row)) return rack_states - def google_id_has_at_least(self, google_id, authorization_level): """Return True if the User has at least the given auth level over this RackState.""" diff --git a/opendc/models/room.py b/opendc/models/room.py index 76f9f7b3..66346bb2 100644 --- a/opendc/models/room.py +++ b/opendc/models/room.py @@ -1,9 +1,9 @@ -from opendc.models.model import Model from opendc.models.datacenter import Datacenter -from opendc.util import database, exceptions +from opendc.models.model import Model +from opendc.util import exceptions -class Room(Model): +class Room(Model): JSON_TO_PYTHON_DICT = { 'room': { 'id': 'id', diff --git a/opendc/models/room_state.py b/opendc/models/room_state.py index 169aaa55..2729407f 100644 --- a/opendc/models/room_state.py +++ b/opendc/models/room_state.py @@ -1,8 +1,8 @@ from opendc.models.model import Model from opendc.util import database -class RoomState(Model): +class RoomState(Model): JSON_TO_PYTHON_DICT = { 'RoomState': { 'roomId': 'room_id', @@ -16,9 +16,9 @@ class RoomState(Model): """Instantiate a RoomState from a database row.""" return cls( - room_id = row[0], - load_fraction = row[1], - tick = row[2] + room_id=row[0], + load_fraction=row[1], + tick=row[2] ) @classmethod @@ -71,7 +71,6 @@ class RoomState(Model): room_states.append(cls._from_database_row(row)) return room_states - def google_id_has_at_least(self, google_id, authorization_level): """Return True if the User has at least the given auth level over this RackState.""" diff --git a/opendc/models/room_type.py b/opendc/models/room_type.py index 73b06cc1..1d107e95 100644 --- a/opendc/models/room_type.py +++ b/opendc/models/room_type.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class RoomType(Model): +class RoomType(Model): JSON_TO_PYTHON_DICT = { 'RoomType': { 'name': 'name' diff --git a/opendc/models/scheduler.py b/opendc/models/scheduler.py index 9d78ec6f..b70830ab 100644 --- a/opendc/models/scheduler.py +++ b/opendc/models/scheduler.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class Scheduler(Model): +class Scheduler(Model): JSON_TO_PYTHON_DICT = { 'Scheduler': { 'name': 'name' diff --git a/opendc/models/section.py b/opendc/models/section.py index 5434cdfb..4e953eae 100644 --- a/opendc/models/section.py +++ b/opendc/models/section.py @@ -1,9 +1,9 @@ from opendc.models.model import Model from opendc.models.path import Path -from opendc.util import database, exceptions +from opendc.util import exceptions -class Section(Model): +class Section(Model): JSON_TO_PYTHON_DICT = { 'Section': { 'id': 'id', @@ -23,7 +23,7 @@ class Section(Model): # Get the Path try: - path = Path.from_primary_key((self.path_id,)) + path = Path.from_primary_key((self.path_id,)) except exceptions.RowNotFoundError: return False diff --git a/opendc/models/simulation.py b/opendc/models/simulation.py index b698867c..8c3726c8 100644 --- a/opendc/models/simulation.py +++ b/opendc/models/simulation.py @@ -1,12 +1,10 @@ -import json - from opendc.models.authorization import Authorization from opendc.models.model import Model from opendc.models.user import User -from opendc.util import database, exceptions +from opendc.util import exceptions -class Simulation(Model): +class Simulation(Model): JSON_TO_PYTHON_DICT = { 'Simulation': { 'id': 'id', @@ -22,7 +20,7 @@ class Simulation(Model): def google_id_has_at_least(self, google_id, authorization_level): """Return True if the user has at least the given auth level over this Simulation.""" - + # Get the User id try: diff --git a/opendc/models/storage.py b/opendc/models/storage.py index e82fa0b3..02c568fb 100644 --- a/opendc/models/storage.py +++ b/opendc/models/storage.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class Storage(Model): +class Storage(Model): JSON_TO_PYTHON_DICT = { 'Storage': { 'id': 'id', diff --git a/opendc/models/task.py b/opendc/models/task.py index da0f5785..4e6485fb 100644 --- a/opendc/models/task.py +++ b/opendc/models/task.py @@ -1,7 +1,7 @@ from opendc.models.model import Model + class Task(Model): - JSON_TO_PYTHON_DICT = { 'Task': { 'id': 'id', diff --git a/opendc/models/task_duration.py b/opendc/models/task_duration.py index 0d3432e3..1dc3ca01 100644 --- a/opendc/models/task_duration.py +++ b/opendc/models/task_duration.py @@ -1,8 +1,8 @@ from opendc.models.model import Model from opendc.util import database -class TaskDuration(Model): +class TaskDuration(Model): JSON_TO_PYTHON_DICT = { 'TaskDuration': { 'taskId': 'task_id', @@ -15,8 +15,8 @@ class TaskDuration(Model): """Instantiate a RoomState from a database row.""" return cls( - task_id = row[0], - duration = row[1] + task_id=row[0], + duration=row[1] ) @classmethod diff --git a/opendc/models/task_state.py b/opendc/models/task_state.py index 7d216aa0..8dca2d03 100644 --- a/opendc/models/task_state.py +++ b/opendc/models/task_state.py @@ -1,8 +1,8 @@ from opendc.models.model import Model from opendc.util import database -class TaskState(Model): +class TaskState(Model): JSON_TO_PYTHON_DICT = { 'TaskState': { 'id': 'id', @@ -31,18 +31,18 @@ class TaskState(Model): for row in results: task_states.append( cls( - id = row[0], - task_id = row[1], - experiment_id = row[2], - tick = row[3], - flops_left = row[4] + id=row[0], + task_id=row[1], + experiment_id=row[2], + tick=row[3], + flops_left=row[4] ) ) return task_states def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the Use rhas at least the given auth level over this TaskState.""" + """Return True if the User has at least the given auth level over this TaskState.""" if authorization_level in ['EDIT', 'OWN']: return False diff --git a/opendc/models/tile.py b/opendc/models/tile.py index 748c76c5..344b6135 100644 --- a/opendc/models/tile.py +++ b/opendc/models/tile.py @@ -1,10 +1,10 @@ from opendc.models.model import Model -from opendc.models.room import Room from opendc.models.object import Object -from opendc.util import database, exceptions +from opendc.models.room import Room +from opendc.util import exceptions -class Tile(Model): +class Tile(Model): JSON_TO_PYTHON_DICT = { 'tile': { 'id': 'id', @@ -42,6 +42,5 @@ class Tile(Model): super(Tile, self).read() if self.object_id is not None: - obj = Object.from_primary_key((self.object_id,)) self.object_type = obj.type diff --git a/opendc/models/trace.py b/opendc/models/trace.py index ce8d4923..99245ac3 100644 --- a/opendc/models/trace.py +++ b/opendc/models/trace.py @@ -1,7 +1,7 @@ from opendc.models.model import Model -class Trace(Model): +class Trace(Model): JSON_TO_PYTHON_DICT = { 'Trace': { 'id': 'id', diff --git a/opendc/models/user.py b/opendc/models/user.py index 885170d0..fde45b0c 100644 --- a/opendc/models/user.py +++ b/opendc/models/user.py @@ -1,9 +1,7 @@ -import json - from opendc.models.model import Model -class User(Model): +class User(Model): JSON_TO_PYTHON_DICT = { 'User': { 'id': 'id', |
