Esempio n. 1
0
def comparison_query(fields, filters, table=DEFAULT_TABLE):

    # parse filters
    startDate = filters.get('startDate')
    endDate = filters.get('endDate')
    requestTypes = filters.get('requestTypes')
    ncList = filters.get('ncList', [])
    cdList = filters.get('cdList', [])

    # validate input
    if not isinstance(fields, list) or len(fields) == 0:
        raise Exception('fields must be provided')

    if (
        startDate is None or
        endDate is None or
        not isinstance(requestTypes, list) or
        not isinstance(ncList, list) or
        not isinstance(cdList, list)
    ):
        raise Exception('invalid filters')

    # try picklebase
    if pb.available():
        return pb.query(table, fields, filters)

    # hit database
    fields = (', ').join(fields)
    requestTypes = (', ').join([f"'{rt}'" for rt in requestTypes])

    if len(ncList) > 0:
        ncList = (', ').join([str(nc) for nc in ncList])
        where = f'nc IN ({ncList})'
    else:
        cdList = (', ').join([str(cd) for cd in cdList])
        where = f'cd IN ({cdList})'

    return pd.read_sql(f"""
        SELECT {fields}
        FROM {table}
        WHERE
            createddate >= '{startDate}' AND
            createddate <= '{endDate}' AND
            requesttype IN ({requestTypes}) AND
            {where}
    """, db.engine)
Esempio n. 2
0
                 tablefmt='psql',
                 headers='keys'))


if __name__ == '__main__':
    from utils.log import log_heading
    import time
    from settings import Server

    time.sleep(1)

    log_heading('checks')
    check_db_version()
    check_db_update()

    # log_heading('database contents')
    # show_db_contents()

    if not Server.UPDATE_ON_START:
        import pb
        if not pb.enabled:
            pb.clear_data()
        elif not pb.available():
            pb.populate()

    from utils.settings import log_settings
    log_heading('settings')
    log_settings()

    log_heading('starting server')