def set_project_domain(self, project_code, domain_key): """Modify the Domain of the Project whose code is given in parameter. It raises HPCStatsRuntimeError if either the Project code or the Domain key are not found in DB. """ domain = Domain(domain_key, None) if not domain.existing(self.db): raise HPCStatsRuntimeError( \ "unable to find domain %s in database" \ % (domain_key)) project = Project(None, project_code, None) if not project.find(self.db): raise HPCStatsRuntimeError( \ "unable to find project %s in database" \ % (project_code)) # Load the Project in DB to get its description project.load(self.db) project.domain = domain logger.info("updating project %s with new domain %s", project_code, domain_key) project.update(self.db)
def create_domain(self, domain_key, domain_name): """Creates the domain whose key and name are given in parameters. It raises HPCStatsRuntimeError if another Domain with the same key already exists in DB. """ domain = Domain(domain_key, domain_name) if domain.existing(self.db): raise HPCStatsRuntimeError("domain %s already exists in database" % (domain_key)) logger.info("creating domain %s in database", domain_key) domain.save(self.db)
def load(self, db): """Load the Project based on the content of the DB and set objects attributes accordingly. The project_id attribute must have been set previously, typically by calling find() method. It raises raises HPCStatsRuntimeError if the Project is not found in DB. """ if self.project_id is None: raise HPCStatsRuntimeError( "could not load project %s since not found in database" \ % (str(self))) req = """ SELECT project_code, project_description, domain_id FROM Project WHERE project_id = %s """ params = ( self.project_id, ) db.execute(req, params) # We know here there is only one result thanks to existing() method result = db.cur.fetchone() self.code = result[0] self.description = result[1] # build the Domain object with the key found in DB domain_key = result[2] self.domain = Domain(domain_key, None)
def test_update(self): """ProjectImporterCSV.update() works with simple data """ domain1 = Domain('dom1', 'domain name 1') project1 = Project(domain1, 'code1', 'project description 1') MockPg2.PG_REQS['save_project'].set_assoc(params=(project1.code, project1.description, domain1.key), result=[[1]]) self.importer.projects = [project1] self.importer.domains = [domain1] self.importer.update()
def load(self): """Connects to all known Slurm databases to extract project codes in jobs wckeys. If a cluster raises a HPCStatsSourceError, it prints an error and continues with the next one. """ self.domains = [] self.projects = [] self.default_domain = Domain(self.default_domain_key, self.default_domain_name) self.domains.append(self.default_domain) for cluster in self.clusters_db.keys(): try: self.load_cluster(cluster) except HPCStatsSourceError, err: logger.error("Error with cluster %s: %s", cluster, err)
def load(self): """Open CSV file and load project out of it. Raises Exceptions if error is found in the file. Returns the list of Projects with their Domains. """ self.check() self.domains = [] self.projects = [] with open(self.csv_file, 'r') as csvfile: file_reader = csv.reader(csvfile, delimiter=';', quotechar='|') for row in file_reader: project_code = row[0] project_name = row[1] # domains domain_str = row[2] domain_m = re.match(r"\[(.*)\](.*)", domain_str) if domain_m: domain_key = domain_m.group(1) domain_name = domain_m.group(2) else: raise HPCStatsSourceError( \ "Project CSV %s domain format is invalid" \ % (project_code)) domain_key = domain_key.strip() domain_name = domain_name.strip() if len(domain_key) == 0: raise HPCStatsSourceError( \ "Project CSV %s domain key is empty" \ % (project_code)) if len(domain_name) == 0: raise HPCStatsSourceError( \ "Project CSV %s domain name is empty" \ % (project_code)) # Create the Domain and search for it among the already # existing ones. If not found, append to the list of Domains. new_domain = Domain(key=domain_key, name=domain_name) domain = self.find_domain(new_domain) if domain is None: domain = new_domain self.domains.append(domain) # Create the Project and search for it among the already # existing ones. If found, raise HPCStatsSourceError project = Project(domain=domain, code=project_code, description=project_name) # check for duplicate project and raise error if found if self.find_project(project): raise HPCStatsSourceError( \ "duplicated project code %s in CSV file" \ % (project_code)) self.projects.append(project) return self.projects