def test_flush_geometry(self): inserter = BulkInserter(GmfData) connection = writer.connections['reslt_writer'] inserter.add_entry(location='POINT(1 1)', output_id=1) fields = inserter.fields inserter.flush() if fields[0] == 'output_id': values = '%s, GeomFromText(%s, 4326)' else: values = 'GeomFromText(%s, 4326), %s' self.assertEquals('INSERT INTO "hzrdr"."gmf_data" (%s) VALUES (%s)' % (", ".join(fields), values), connection.sql)
def test_flush_geometry(self): inserter = BulkInserter(GMFData) session = DummySession() inserter.add_entry(location='POINT(1 1)', output_id=1) fields = inserter.fields inserter.flush(session) if fields[0] == 'output_id': values = '%s, GeomFromText(%s, 4326)' else: values = 'GeomFromText(%s, 4326), %s' self.assertEquals('INSERT INTO hzrdr.gmf_data (%s) VALUES (%s)' % (", ".join(fields), values), session.sql)
def test_flush(self): inserter = BulkInserter(OqUser) session = DummySession() inserter.add_entry(user_name='user1', full_name='An user') fields = inserter.fields inserter.flush(session) self.assertEquals('INSERT INTO admin.oq_user (%s) VALUES (%%s, %%s)' % (", ".join(fields)), session.sql) inserter.add_entry(user_name='user1', full_name='An user') inserter.add_entry(user_name='user2', full_name='Another user') fields = inserter.fields inserter.flush(session) self.assertEquals('INSERT INTO admin.oq_user (%s) VALUES' \ ' (%%s, %%s), (%%s, %%s)' % (", ".join(fields)), session.sql)
def test_flush(self): inserter = BulkInserter(OqUser) connection = writer.connections['admin'] inserter.add_entry(user_name='user1', full_name='An user') fields = inserter.fields inserter.flush() self.assertEquals('INSERT INTO "admin"."oq_user" (%s) VALUES' \ ' (%%s, %%s)' % (", ".join(fields)), connection.sql) inserter.add_entry(user_name='user1', full_name='An user') inserter.add_entry(user_name='user2', full_name='Another user') fields = inserter.fields inserter.flush() self.assertEquals('INSERT INTO "admin"."oq_user" (%s) VALUES' \ ' (%%s, %%s), (%%s, %%s)' % (", ".join(fields)), connection.sql)
class AggregateResultWriter(object): """ Manager to serialize to db Aggregate results (Mean curves, Quantile Curves, Maps, etc.). It implements the context manager pattern to take care of the transaction management :attribute _job The current job :attribute _imt The intensity measure type for this aggregate result """ def __init__(self, job, imt): self._job = job self._imt = imt self._inserter = BulkInserter(self.__class__.model) self._aggregate_result = None self._transaction_handler = None def _create_output(self): """ Create an Output object related to the aggregate result """ output = models.Output.objects.create_output( job=self._job, output_type=self.__class__.output_type, display_name=self.display_name()) return output def display_name(self): """ The display name of the output being created (used for the Output object) """ raise NotImplementedError def create_aggregate_result(self): """ Create an Aggregate result (both the Output object and the corresponding curve/map/etc. object """ output = self._create_output() self._aggregate_result = self._create_aggregate_result_item(output) return self._aggregate_result, output def _create_aggregate_result_item(self, output): """ Create an aggregate result item (only the HazardCurve / HazardMap). Abstract method """ raise NotImplementedError def add_data(self, location, _): """ Add a aggregate result data (to be serialized when flush is called). Abstract method """ raise NotImplementedError def __enter__(self): self._transaction_handler = transaction.commit_on_success( using='reslt_writer') self._transaction_handler.__enter__() return self def __exit__(self, exc_type, exc_val, exc_tb): if not exc_type: self._flush_data() else: raise exc_val self._transaction_handler.__exit__(exc_type, exc_val, exc_tb) def _flush_data(self): """ Flush the data to the db """ self._inserter.flush()