def _save_raw_data_chunk(chunk, file_pk, prog_key, increment, *args, **kwargs): """Save the raw data to the database.""" import_file = ImportFile.objects.get(pk=file_pk) # Save our "column headers" and sample rows for F/E. source_type = get_source_type(import_file) for c in chunk: raw_bs = BuildingSnapshot() raw_bs.import_file = import_file raw_bs.extra_data = c raw_bs.source_type = source_type # We require a save to get our PK # We save here to set our initial source PKs. raw_bs.save() super_org = import_file.import_record.super_organization raw_bs.super_organization = super_org set_initial_sources(raw_bs) raw_bs.save() # Indicate progress increment_cache(prog_key, increment)
def create_models(data, import_file): """ Create a BuildingSnapshot, a CanonicalBuilding, and a Meter. Then, create TimeSeries models for each meter reading in data. :param data: dictionary of building data from a Green Button XML file in the form returned by xml_importer.building_data :param import_file: ImportFile referencing the original xml file; needed for linking to BuildingSnapshot and for determining super_organization :returns: the created CanonicalBuilding """ # cache data on import_file; this is a proof of concept and we # only have two example files available so we hardcode the only # heading present. import_file.cached_first_row = ROW_DELIMITER.join(["address"]) import_file.cached_second_to_fifth_row = ROW_DELIMITER.join( [data['address']] ) import_file.save() raw_bs = BuildingSnapshot() raw_bs.import_file = import_file # We require a save to get our PK # We save here to set our initial source PKs. raw_bs.save() super_org = import_file.import_record.super_organization raw_bs.super_organization = super_org set_initial_sources(raw_bs) raw_bs.address_line_1 = data['address'] raw_bs.source_type = GREEN_BUTTON_BS raw_bs.save() # create canonical building cb = CanonicalBuilding.objects.create(canonical_snapshot=raw_bs) raw_bs.canonical_building = cb raw_bs.save() # log building creation AuditLog.objects.create( organization=import_file.import_record.super_organization, user=import_file.import_record.owner, content_object=cb, action="create_building", action_note="Created building", ) # create meter for this dataset (each dataset is a single energy type) e_type = energy_type(data['service_category']) e_type_string = next( pair[1] for pair in seed.models.ENERGY_TYPES if pair[0] == e_type ) m_name = "gb_{0}[{1}]".format(str(raw_bs.id), e_type_string) m_energy_units = energy_units(data['meter']['uom']) meter = Meter.objects.create( name=m_name, energy_type=e_type, energy_units=m_energy_units ) meter.building_snapshot.add(raw_bs) meter.save() # now time series data for the meter for reading in data['interval']['readings']: start_time = int(reading['start_time']) duration = int(reading['duration']) begin_time = datetime.fromtimestamp(start_time) end_time = datetime.fromtimestamp(start_time + duration) value = reading['value'] cost = reading['cost'] new_ts = TimeSeries.objects.create( begin_time=begin_time, end_time=end_time, reading=value, cost=cost ) new_ts.meter = meter new_ts.save() return cb
def create_models(data, import_file): """ Create a BuildingSnapshot, a CanonicalBuilding, and a Meter. Then, create TimeSeries models for each meter reading in data. :param data: dictionary of building data from a Green Button XML file in the form returned by xml_importer.building_data :param import_file: ImportFile referencing the original xml file; needed for linking to BuildingSnapshot and for determining super_organization :returns: the created CanonicalBuilding """ # cache data on import_file; this is a proof of concept and we # only have two example files available so we hardcode the only # heading present. import_file.cached_first_row = ROW_DELIMITER.join(["address"]) import_file.cached_second_to_fifth_row = ROW_DELIMITER.join( [data['address']]) import_file.save() raw_bs = BuildingSnapshot() raw_bs.import_file = import_file # We require a save to get our PK # We save here to set our initial source PKs. raw_bs.save() super_org = import_file.import_record.super_organization raw_bs.super_organization = super_org raw_bs.address_line_1 = data['address'] raw_bs.source_type = GREEN_BUTTON_BS raw_bs.save() # create canonical building cb = CanonicalBuilding.objects.create(canonical_snapshot=raw_bs) raw_bs.canonical_building = cb raw_bs.save() # log building creation AuditLog.objects.create( organization=import_file.import_record.super_organization, user=import_file.import_record.owner, content_object=cb, action="create_building", action_note="Created building", ) # create meter for this dataset (each dataset is a single energy type) e_type = energy_type(data['service_category']) e_type_string = next(pair[1] for pair in seed.models.ENERGY_TYPES if pair[0] == e_type) m_name = "gb_{0}[{1}]".format(str(raw_bs.id), e_type_string) m_energy_units = energy_units(data['meter']['uom']) meter = Meter.objects.create(name=m_name, energy_type=e_type, energy_units=m_energy_units) meter.building_snapshot.add(raw_bs) meter.save() # now time series data for the meter for reading in data['interval']['readings']: start_time = int(reading['start_time']) duration = int(reading['duration']) begin_time = datetime.fromtimestamp(start_time, tz=timezone.get_current_timezone()) end_time = datetime.fromtimestamp(start_time + duration, tz=timezone.get_current_timezone()) value = reading['value'] cost = reading['cost'] new_ts = TimeSeries.objects.create(begin_time=begin_time, end_time=end_time, reading=value, cost=cost) new_ts.meter = meter new_ts.save() return cb
def setUp(self): self.bs = BuildingSnapshot()
class TestBuildingSnapshot(TestCase): def setUp(self): self.bs = BuildingSnapshot() def tearDown(self): self.bs = None def test_tax_lot_id(self): """ """ self.bs.tax_lot_id = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.tax_lot_id), 128) def test_tax_lot_id_int(self): """ """ self.bs.tax_lot_id = 123123123 self.bs.save() self.assertEqual(self.bs.tax_lot_id, 123123123) # Check that the data is converted correctly bs2 = BuildingSnapshot.objects.get(pk=self.bs.pk) self.assertEqual(bs2.tax_lot_id, u'123123123') def test_pm_property_id(self): """ """ self.bs.pm_property_id = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.pm_property_id), 128) def test_custom_id_1(self): """ """ self.bs.custom_id_1 = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.custom_id_1), 128) def test_lot_number(self): """ """ self.bs.lot_number = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.lot_number), 128) def test_block_number(self): """ """ self.bs.block_number = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.block_number), 128) def test_district(self): """ """ self.bs.district = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.district), 128) def test_owner(self): """ The owner field of the BuildingSnapshot model should truncate values to a max of 128 characters """ self.bs.owner = "*" * 130 self.bs.save() self.assertEqual(len(self.bs.owner), 128) def test_owner_email(self): """ """ self.bs.owner_email = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.owner_email), 128) def test_owner_telephone(self): """ """ self.bs.owner_telephone = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.owner_telephone), 128) def test_owner_address(self): """ """ self.bs.owner_address = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.owner_address), 128) def test_owner_city_state(self): """ """ self.bs.owner_city_state = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.owner_city_state), 128) def test_owner_postal_code(self): """ """ self.bs.owner_postal_code = '-' * 130 self.bs.save() self.assertEqual(len(self.bs.owner_postal_code), 128) def test_property_name(self): """ """ self.bs.property_name = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.property_name), 255) def test_address_line_1(self): """ """ self.bs.address_line_1 = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.address_line_1), 255) def test_address_line_2(self): """ """ self.bs.address_line_2 = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.address_line_2), 255) def test_city(self): """ """ self.bs.city = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.city), 255) def test_postal_code(self): """ """ self.bs.postal_code = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.postal_code), 255) def test_state_province(self): """ """ self.bs.state_province = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.state_province), 255) def test_building_certification(self): """ """ self.bs.building_certification = '-' * 260 self.bs.save() self.assertEqual(len(self.bs.building_certification), 255)