Example #1
0
    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
Example #2
0
    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
Example #3
0
    def update_session(self, eng_comment, hostname=None):
        """Update the simulation session in the tracking database with a comment.

        This function allows the simulation session's entry in the tracking database to be updated with an
        engineering comment. This is hopefully to record that the simulation has completed successfully, but
        may be used to indicate a simulation failure.

        Parameters
        ----------
        eng_comment : str
            A comment about the simulation session hopefully for a successful run.
        hostname : str, optional
            An alternate hostname
        """
        if hostname is None:
            hostname = get_hostname()

        payload = {
            'sessionID': self.session_id,
            'hostname': hostname,
            'eng_comment': eng_comment
        }

        result = requests.get(self.update_url, params=payload, timeout=3.0)
        if result.ok:
            self.log.debug("Update for session was recorded successfully.")
        else:
            self.log.warning(
                "Update for session was not recorded successfully!")
Example #4
0
    def __init__(self, sqlite_save_path=None, session_id_start=None, sqlite_session_save_path=None):
        """Initialize the class.

        Parameters
        ----------
        sqlite_save_path : str
            A path to save all resulting database files for SQLite.
        session_id_start : int
            A new starting session Id for counting new simulations.
        """
        self.log = logging.getLogger("database.SocsDatabase")
        self.db_dialect = "sqlite"
        self.session_id = -1
        self.session_start = session_id_start if session_id_start is not None else 2000
        self.metadata = MetaData()
        self.engine = None
        self.sqlite_save_path = sqlite_save_path
        self.sqlite_session_save_path = sqlite_session_save_path

        # Parameters for SQLite operations
        self.session_engine = None
        self.session_metadata = MetaData()

        self.session_tracking = tables.create_session(self.metadata, autoincrement=False)
        sqlite_session_tracking_db = "{}_sessions.db".format(get_hostname())
        self.engine = self._make_engine(sqlite_session_tracking_db, self.sqlite_session_save_path)

        # Parameter for holding data lists
        self.data_list = collections.defaultdict(list)
Example #5
0
    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!")
Example #6
0
def generate_logfile_path(log_file_path="log", session_id="1000"):
    """Generate the full log file path.

    Parameters
    ----------
    log_file_path : str, optional
        The location to write the log file.
    session_id : str, optional
        The OpSim session ID tag.

    Returns
    -------
    str
        The full path of the log file.
    """
    if not os.path.exists(log_file_path):
        log_file_path = ""
    log_file = os.path.join(log_file_path, "{}_{}.log".format(get_hostname(), session_id))
    return log_file
Example #7
0
    def update_session(self, eng_comment):
        """Update the simulation session in the tracking database with a comment.

        This function allows the simulation session's entry in the tracking database to be updated with an
        engineering comment. This is hopefully to record that the simulation has completed successfully, but
        may be used to indicate a simulation failure.

        Parameters
        ----------
        eng_comment : str
            A comment about the simulation session hopefully for a successful run.
        """
        payload = {"sessionID": self.session_id, "hostname": get_hostname(), "eng_comment": eng_comment}

        result = requests.get(self.update_url, params=payload, timeout=3.0)
        if result.ok:
            self.log.debug("Update for session was recorded successfully.")
        else:
            self.log.warning("Update for session was not recorded successfully!")
Example #8
0
def generate_logfile_path(log_file_path="log", session_id="1000"):
    """Generate the full log file path.

    Parameters
    ----------
    log_file_path : str, optional
        The location to write the log file.
    session_id : str, optional
        The OpSim session ID tag.

    Returns
    -------
    str
        The full path of the log file.
    """
    if not os.path.exists(log_file_path):
        log_file_path = ""
    log_file = os.path.join(log_file_path,
                            "{}_{}.log".format(get_hostname(), session_id))
    return log_file
Example #9
0
    def __init__(self, dialect="mysql", mysql_config_path=None, sqlite_save_path=None, session_id_start=None):
        """Initialize the class.

        Parameters
        ----------
        dialect : str
            The flavor of the database to use. Options: mysql or sqlite.
        mysql_config_path : str
            An alternate path for the .my.cnf configuration file for MySQL.
        sqlite_save_path : str
            A path to save all resulting database files for SQLite.
        session_id_start : int
            A new starting session Id for counting new simulations.
        """
        self.db_name = "SocsDB"
        self.log = logging.getLogger("database.SocsDatabase")
        self.db_dialect = dialect
        self.session_id = -1
        self.session_start = session_id_start if session_id_start is not None else 1000
        self.metadata = MetaData()
        self.engine = None
        self.mysql_config_path = mysql_config_path
        self.sqlite_save_path = sqlite_save_path

        # Parameters for SQLite operations
        self.session_engine = None
        self.session_metadata = MetaData()

        if self.db_dialect == "mysql":
            self._create_tables(session_id_start=self.session_start)
            self.engine = self._make_engine()
        if self.db_dialect == "sqlite":
            self.session_tracking = tables.create_session(self.metadata, autoincrement=False)
            sqlite_session_tracking_db = "{}_sessions.db".format(get_hostname())
            self.engine = self._make_engine(sqlite_session_tracking_db)

        # Parameter for holding data lists
        self.data_list = collections.defaultdict(list)
Example #10
0
    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!")