Exemple #1
0
    def generate_sparse_table(self, as_of_dates):
        """Convert the object's input table
        into a sparse states table for the given as_of_dates

        Args:
            as_of_dates (list of datetime.dates) Dates to include in the sparse
                state table
        """
        logging.debug("Generating sparse table using as_of_dates: %s",
                      as_of_dates)
        self._create_and_populate_sparse_table(as_of_dates)
        self.db_engine.execute(
            "create index on {} (entity_id, as_of_date)".format(
                self.sparse_table_name))
        logging.info(
            "Indices created on entity_id and as_of_date for sparse state table"
        )
        if not table_has_data(self.sparse_table_name, self.db_engine):
            raise ValueError(self._empty_table_message(as_of_dates))

        logging.info("Sparse states table generated at %s",
                     self.sparse_table_name)
        logging.info("Generating stats on %s", self.sparse_table_name)
        logging.info(
            "Row count of %s: %s",
            self.sparse_table_name,
            table_row_count(self.sparse_table_name, self.db_engine),
        )
Exemple #2
0
def table_should_have_data(table_name, db_engine):
    """Ensures that the table has at least one row

    Args:
        table_name (string) A table name (with schema)
        db_engine (sqlalchemy.engine)

    Raises: ValueError if the table does not have at least one row
    """
    table_should_exist(table_name, db_engine)
    if not table_has_data(table_name, db_engine):
        raise ValueError("{} table does not have any data".format(table_name))
Exemple #3
0
    def generate_sparse_table(self, as_of_dates):
        """Convert the object's input table (either dense states or events)
        into a sparse states table for the given as_of_dates

        Args:
            as_of_dates (list of datetime.dates) Dates to include in the sparse
                state table
        """
        logging.debug('Generating sparse table using as_of_dates: %s',
                      as_of_dates)
        self._generate_sparse_table(self.sparse_table_query_func(as_of_dates))
        if not table_has_data(self.sparse_table_name, self.db_engine):
            raise ValueError(
                "No entities in table '{input_table}' define time ranges "
                "that encompass any of experiment's \"as-of-dates\":\n\n"
                "\t{as_of_dates}\n\n"
                "Please check temporal and/or state configurations.".format(
                    input_table=(self.dense_state_table or self.events_table),
                    as_of_dates=', '.join(
                        str(as_of_date)
                        for as_of_date in (as_of_dates if len(as_of_dates) <= 5
                                           else as_of_dates[:5] + ['…'])),
                ))
    def generate_entity_date_table(self, as_of_dates):
        """Convert the object's input table
        into a states table for the given as_of_dates

        Args:
            as_of_dates (list of datetime.dates) Dates to include in the
                state table
        """
        logger.spam(
            f"Generating entity_date table {self.entity_date_table_name} using as_of_dates: {as_of_dates}"
        )
        self._create_and_populate_entity_date_table(as_of_dates)
        logger.spam(
            f"Table {self.entity_date_table_name} created and populated")

        if not table_has_data(self.entity_date_table_name, self.db_engine):
            raise ValueError(self._empty_table_message(as_of_dates))

        logger.debug(
            f"Entity-date table generated at {self.entity_date_table_name}")
        logger.spam(f"Generating stats on {self.entity_date_table_name}")
        logger.spam(
            f"Row count of {self.entity_date_table_name}: {table_row_count(self.entity_date_table_name, self.db_engine)}"
        )
 def test_table_has_data(self):
     self.engine.execute('create table incidents (col1 varchar)')
     self.engine.execute('create table compliments (col1 varchar)')
     self.engine.execute('insert into compliments values (\'good job\')')
     assert dbreflect.table_has_data('compliments', self.engine)
     assert not dbreflect.table_has_data('incidents', self.engine)
 def test_table_has_data(self):
     self.engine.execute("create table incidents (col1 varchar)")
     self.engine.execute("create table compliments (col1 varchar)")
     self.engine.execute("insert into compliments values ('good job')")
     assert dbreflect.table_has_data("compliments", self.engine)
     assert not dbreflect.table_has_data("incidents", self.engine)