Пример #1
0
    def load_SQL_database(self):
        table_columns = [
            "ResultID", "FeatureCode", "Variable", "Unit", "Type",
            "Organization", "Date Created"
        ]
        self.table.set_columns(table_columns)
        db = self.get_selected_database()

        if not len(db):
            return

        if db["args"]["engine"] == "sqlite":
            session_factory = dbconnection2.createConnection(
                engine=db["args"]["engine"], address=db["args"]["address"])
            session = db2.connect(session_factory)
            series = session.getAllSeries()
        elif db["args"]["engine"] == "postgresql":  # db is postresql
            session_factory = dbUtilities.build_session_from_connection_string(
                db["connection_string"])
            session = db2.connect(session_factory)
            series = session.getAllSeries()
        else:
            # Fails if db is not sqlite or postresql
            raise Exception("Failed to load database")

        if not series:
            self.table.empty_list_message.Show()
            return

        self.table.empty_list_message.Hide()
        data = self.series_to_table_data(series)

        self.table.set_table_content(data)
Пример #2
0
    def load_SQL_database(self):
        table_columns = ["ResultID", "FeatureCode", "Variable", "Unit", "Type", "Organization", "Date Created"]
        self.table.set_columns(table_columns)
        db = self.get_selected_database()

        if not len(db):
            return

        if db["args"]["engine"] == "sqlite":
            session_factory = dbconnection2.createConnection(engine=db["args"]["engine"], address=db["args"]["address"])
            session = db2.connect(session_factory)
            series = session.getAllSeries()
        elif db["args"]["engine"] == "postgresql":  # db is postresql
            session_factory = dbUtilities.build_session_from_connection_string(db["connection_string"])
            session = db2.connect(session_factory)
            series = session.getAllSeries()
        else:
            # Fails if db is not sqlite or postresql
            raise Exception("Failed to load database")

        if not series:
            self.table.empty_list_message.Show()
            return

        self.table.empty_list_message.Hide()
        data = self.series_to_table_data(series)

        self.table.set_table_content(data)
Пример #3
0
 def get_database_session(self):
     """
     Connect to the respective database and return the session
     Exception is raised if it is not a sqlite or postresql
     :return:
     """
     db = self.get_selected_database()
     if db["args"]['engine'] == "sqlite":
         session_factory = dbconnection.createConnection(engine=db["args"]["engine"], address=db["args"]["address"],
                                                  db=db["args"]["db"], user=db["args"]["user"],
                                                  password=db["args"]["pwd"])
         session = db2.connect(session_factory)
         return session
     elif db["args"]["engine"] == "postgresql":
         session_factory = dbUtilities.build_session_from_connection_string(db['connection_string'])
         session = db2.connect(session_factory)
         return session
     else:
         raise Exception("Failed to load simulations database")
Пример #4
0
 def get_database_session(self):
     """
     Connect to the respective database and return the session
     Exception is raised if it is not a sqlite or postresql
     :return:
     """
     db = self.get_selected_database()
     if db["args"]['engine'] == "sqlite":
         session_factory = dbconnection.createConnection(
             engine=db["args"]["engine"],
             address=db["args"]["address"],
             db=db["args"]["db"],
             user=db["args"]["user"],
             password=db["args"]["pwd"])
         session = db2.connect(session_factory)
         return session
     elif db["args"]["engine"] == "postgresql":
         session_factory = dbUtilities.build_session_from_connection_string(
             db['connection_string'])
         session = db2.connect(session_factory)
         return session
     else:
         raise Exception("Failed to load simulations database")
Пример #5
0
def save(obj, datasave, modelids):
    """
    Saves simulation calculations to an ODM2 database
    Args:
        obj: Engine object
        datasave: datasave object that contains database and user info
        modelids: list of model ids to save data for

    Returns:

    """

    if datasave.session is not None:
        session = datasave.session
    else:
        msg = "Could not connect to database for results saving: %s" % datasave.database_args["address"]
        elog.error(msg)
        sPrint(msg, MessageType.ERROR)
        return 0

    db = dbv2.connect(sessionFactory=session)

    sPrint("Saving Simulation Results...")
    st = time.time()

    action_date = datetime.datetime.now()
    action_utc_offset = int((action_date - datetime.datetime.utcnow()).total_seconds() / 3600)

    # insert data!
    for modelid in modelids:

        # get the current model instance
        model_obj = obj.get_model_by_id(modelid)
        model_inst = obj.get_model_object(modelid).instance()
        model_name = model_inst.name()

        # get the output exchange items to save for this model
        oeis = datasave.datasets[model_name]
        items = []
        for oei in oeis:
            items.append(model_inst.outputs()[oei])

        sPrint("..found %d items to save for model %s" % (len(items), model_name), MessageType.INFO)

        if len(items) > 0:

            # create a simulation in the database
            id = db.create_simulation(
                coupledSimulationName=datasave.simulationName,
                user_obj=datasave.user,
                action_date=action_date,
                action_utc_offset=action_utc_offset,
                ei=items,
                simulation_start=model_inst.simulation_start(),
                simulation_end=model_inst.simulation_end(),
                timestep_value=model_inst.time_step(),
                timestep_unit="seconds",
                description=model_inst.description(),
                name=model_inst.name(),
            )
            if id is None:
                sPrint("Failed to save results for: %s " % (model_name), MessageType.ERROR)

    sPrint("Saving Complete, elapsed time = %3.5f seconds" % (time.time() - st), MessageType.INFO)
Пример #6
0
def save(obj, datasave, modelids):
    """
    Saves simulation calculations to an ODM2 database
    Args:
        obj: Engine object
        datasave: datasave object that contains database and user info
        modelids: list of model ids to save data for

    Returns:

    """

    if datasave.session is not None:
        session = datasave.session
    else:
        msg = 'Could not connect to database for results saving: %s' % datasave.database_args['address']
        elog.error(msg)
        sPrint(msg, MessageType.ERROR)
        return 0

    db = dbv2.connect(sessionFactory=session)

    sPrint('Saving Simulation Results...')
    st = time.time()

    action_date = datetime.datetime.now()
    action_utc_offset = int((action_date - datetime.datetime.utcnow()).total_seconds()/3600)

    # insert data!
    for modelid in modelids:

        # get the current model instance
        model_obj = obj.get_model_by_id(modelid)
        model_inst = obj.get_model_object(modelid).instance()
        model_name = model_inst.name()

        # get the output exchange items to save for this model
        oeis =  datasave.datasets[model_name]
        items = []
        for oei in oeis:
            items.append(model_inst.outputs()[oei])

        sPrint('..found %d items to save for model %s' % (len(items), model_name), MessageType.INFO)

        if len(items) > 0:

            # create a simulation in the database
            id = db.create_simulation(coupledSimulationName=datasave.simulationName,
                                      user_obj=datasave.user,
                                      action_date=action_date,
                                      action_utc_offset=action_utc_offset,
                                      ei=items,
                                      simulation_start=model_inst.simulation_start(),
                                      simulation_end=model_inst.simulation_end(),
                                      timestep_value=model_inst.time_step(),
                                      timestep_unit='seconds',
                                      description=model_inst.description(),
                                      name=model_inst.name()
                                      )
            if id is None:
                sPrint('Failed to save results for: %s ' %
                       (model_name), MessageType.ERROR)

    sPrint('Saving Complete, elapsed time = %3.5f seconds' %
           (time.time() - st), MessageType.INFO)