def initialise_all_db(settings):
    """
    Initialise the database connection, create all tables if they don't exist then initialise default data if it hasn't
    already been.

    :param settings:
    :return:
    """

    # Initialise the database connection.
    engine = engine_from_config(settings, "sqlalchemy.", pool_recycle=3600)
    DBSession.configure(bind=engine)

    # Test if the database has already been initialised with default data (is this the first time its run?)
    initialised = engine.dialect.has_table(engine.connect(), "project")

    # Create all database tables if they don't exist (on first run)
    Base.metadata.create_all(engine)

    # If this is the first run, initialise all default database data.
    if not initialised:
        with transaction.manager:
            session = DBSession()
            initialise_default_schemas(session)
            initialise_project_templates(session)
            initialise_method_templates(session)
            initialise_security(session)
            transaction.commit()
def check_logs(config_uri):
    settings = get_appsettings(config_uri)

    # Initialise the database connection.
    engine = engine_from_config(settings, 'sqlalchemy.', pool_recycle=3600)
    DBSession.configure(bind=engine)

    session = DBSession()

    ingester_api = services.get_ingester_platform_api(settings)

    start_time = datetime.datetime.now()

    active_datasets = session.query(Dataset).filter_by(disabled=False).filter(Dataset.dam_id is not None).all()
    for dataset in active_datasets:
        logs = ingester_api.getIngesterLogs(dataset.dam_id)

        error_logs = []
        warning_logs = []
        for log in logs:
            test = start_time - log['timestanp']
            # If this log is from the last 24 hours.
            if  start_time - log['timestanp'] < 86400:
                if log['level'] == 'error':
                    error_logs.append(log)
                elif log['level'] == 'warning':
                    warning_logs.append(log)

        if len(error_logs) > 0:
            send_email_notifications(session, dataset, type=NotificationConfig.log_errors.key, errors=error_logs)

        if len(warning_logs) > 0:
            send_email_notifications(session, dataset, type=NotificationConfig.log_errors.key, warnings=warning_logs)