def new_session(self, run_comment): """Log a new session to the database and return the ID. This function logs a new session to the database and returns the session ID. For MySQL, this writes to the Session table in the central database. For SQLite, this writes an entry to the session tracking database. Then a session ID specific database is created and the information is replicated in that Session table. Since SQLite auto increment values always start at one, an offset is applied to make the session IDs commensurate with MySQL. Parameters ---------- run_comment: str The startup comment for the simulation run. Returns ------- int The session ID for this simulation run. """ hostname = get_hostname() user = get_user() version = get_version() date = datetime.utcnow() if self.db_dialect == "mysql": date = date.strftime("%Y-%m-%d %H:%M:%S") insert = self.session.insert() conn = self.engine.connect() result = conn.execute(insert, sessionUser=user, sessionHost=hostname, sessionDate=date, version=version, runComment=run_comment) self.session_id = result.lastrowid if self.db_dialect == "sqlite": # Get the session ID from the tracking file unless it was just created. conn = self.engine.connect() select = self.session_tracking.select().order_by(desc(self.session_tracking.c.sessionId)).limit(1) result = conn.execute(select) row = result.fetchone() try: self.session_id = int(row[0]) + 1 except TypeError: self.session_id = self.session_start insert = self.session_tracking.insert() result = conn.execute(insert, sessionId=self.session_id, sessionUser=user, sessionHost=hostname, sessionDate=date, version=version, runComment=run_comment) # Create the database for the given session ID. sqlite_session_db = "{}_{}.db".format(get_hostname(), self.session_id) self.session_engine = self._make_engine(sqlite_session_db) self._create_tables(self.session_metadata, use_autoincrement=False) self.session_metadata.create_all(self.session_engine) insert = self.session.insert() conn = self.session_engine.connect() result = conn.execute(insert, sessionId=self.session_id, sessionUser=user, sessionHost=hostname, sessionDate=date, version=version, runComment=run_comment) return self.session_id
def new_session(self, run_comment): """Log a new session to the database and return the ID. This function logs a new session to the database and returns the session ID. This writes an entry to the session tracking database. Then a session ID specific database is created and the information is replicated in that Session table. Since SQLite auto increment values always start at one, an offset is applied to make the session IDs commensurate with OpSim3 style ones. Parameters ---------- run_comment: str The startup comment for the simulation run. Returns ------- int The session ID for this simulation run. """ hostname = get_hostname() user = get_user() version = get_version() date = datetime.utcnow() # Get the session ID from the tracking file unless it was just created. conn = self.engine.connect() select = self.session_tracking.select().order_by(desc(self.session_tracking.c.sessionId)).limit(1) result = conn.execute(select) row = result.fetchone() try: self.session_id = int(row[0]) + 1 except TypeError: self.session_id = self.session_start insert = self.session_tracking.insert() result = conn.execute(insert, sessionId=self.session_id, sessionUser=user, sessionHost=hostname, sessionDate=date, version=version, runComment=run_comment) # Create the database for the given session ID. sqlite_session_db = "{}_{}.db".format(get_hostname(), self.session_id) self.session_engine = self._make_engine(sqlite_session_db) self._create_tables(self.session_metadata, use_autoincrement=False) self.session_metadata.create_all(self.session_engine) insert = self.session.insert() conn = self.session_engine.connect() result = conn.execute(insert, sessionId=self.session_id, sessionUser=user, sessionHost=hostname, sessionDate=date, version=version, runComment=run_comment) return self.session_id
def track_session(self): """Record the simulation session into the tracking database. """ payload = { "sessionID": self.session_id, "hostname": get_hostname(), "user": get_user(), "startup_comment": self.startup_comment, "code_test": self.session_type_codes[self.session_type], "status_id": 1.0, "run_version": get_version(), } result = requests.get(self.tracking_url, params=payload, timeout=3.0) if result.ok: self.log.debug("Tracking for session was recorded successfully.") else: self.log.warning("Tracking for session was not recorded successfully!")
def track_session(self, hostname=None, user=None, version=None): """Record the simulation session into the tracking database. Parameters ---------- hostname : str, optional An alternate hostname. user : str, optional An alternate username. version : str, optional An alternate version number. """ if hostname is None: hostname = get_hostname() if user is None: user = get_user() if version is None: version = get_version() payload = { 'sessionID': self.session_id, 'hostname': hostname, 'user': user, 'startup_comment': self.startup_comment, 'code_test': self.session_type_codes[self.session_type], 'status_id': 1.0, 'run_version': version } result = requests.get(self.tracking_url, params=payload, timeout=3.0) if result.ok: self.log.debug("Tracking for session was recorded successfully.") else: self.log.warning( "Tracking for session was not recorded successfully!")