def test_consumption_with_multiple_rebases_none_upfront(data_mgr):
    node_uuid = "99.99.99.99.1"
    start_time = base.MIN_TIME

    # generate 5 cumulative entries of 5Wh each => 1025Wh, 20Wh observed
    insert_cumulative_entries(data_mgr, node_uuid, start_time=start_time, entry_value=5, interval_duration=60, start_meter_value=1000, num_entries=5)

    # rebase to 1100
    start_time += 360
    data_mgr.db_mgr.write_meter_entry(node_uuid, when_start_raw=start_time,
                                      when_start_raw_nonce=base.get_nonce(), when_start=start_time,
                                      entry_type='MREBS', entry_value=0,
                                      duration=0, meter_value=1100, rec_status='NORM')

    # generate 5 cumulative entries of 5Wh each => 1125Wh
    insert_cumulative_entries(data_mgr, node_uuid, start_time=start_time, entry_value=5, interval_duration=60, start_meter_value=1100, num_entries=5)

    # rebase to 1200
    start_time += 360
    data_mgr.db_mgr.write_meter_entry(node_uuid, when_start_raw=start_time,
                                      when_start_raw_nonce=base.get_nonce(), when_start=start_time,
                                      entry_type='MREBS', entry_value=0,
                                      duration=0, meter_value=1200, rec_status='NORM')

    # generate 10 cumulative entries of 5Wh each => 1250Wh
    insert_cumulative_entries(data_mgr, node_uuid, start_time=start_time, entry_value=5, interval_duration=60, start_meter_value=1200, num_entries=10)

    # consumption should be 1250 - 1100 + 20 == 170Wh; starting at 1005 as there is no baseline read
    assert data_mgr.get_meter_consumption(node_uuid) == 170
    data_mgr.db_mgr.delete_all_meter_entries(node_uuid)
Example #2
0
    def upsert_synth_meter_updates(self,
                                   node_uuid,
                                   overwrite_time_from,
                                   overwrite_time_to,
                                   meter_entries,
                                   rebase_first=True,
                                   lift_later=False):
        self.delete_meter_entries_in_range(
            node_uuid,
            overwrite_time_from,
            overwrite_time_to,
            entry_type=db.EntryType.METER_UPDATE)
        self.delete_meter_entries_in_range(
            node_uuid,
            overwrite_time_from,
            overwrite_time_to,
            entry_type=db.EntryType.METER_UPDATE_SYNTH)
        if rebase_first:
            timestamp_nonce = base.get_nonce()
            self.db_mgr.write_meter_entry(
                node_uuid, int(meter_entries[0]['when_start']),
                timestamp_nonce, int(meter_entries[0]['when_start']),
                db.EntryType.METER_REBASE_SYNTH.value, 0, 0,
                int(meter_entries[0]['meter_value']),
                db.RecStatus.NORMAL.value)

        for entry in meter_entries:
            timestamp_nonce = base.get_nonce()
            self.db_mgr.write_meter_entry(
                node_uuid, int(entry['when_start']), timestamp_nonce,
                int(entry['when_start']),
                db.EntryType.METER_UPDATE_SYNTH.value,
                int(entry['entry_value']), int(entry['entry_interval_length']),
                int(entry['meter_value']), db.RecStatus.NORMAL.value)

        if lift_later:
            new_meter_value = meter_entries[-1]['meter_value']
            later_entries = self.get_meter_entries(
                node_uuid=node_uuid,
                time_from=(meter_entries[-1]['when_start'] + 1),
                rec_status=db.RecStatus.NORMAL.value,
                limit_count=None)
            for entry in later_entries:
                new_meter_value += entry['entry_value']
                self.db_mgr.update_meter_entry(
                    node_uuid,
                    when_start_raw=entry['when_start_raw'],
                    when_start_raw_nonce=entry['when_start_raw_nonce'],
                    new_meter_value=new_meter_value,
                    new_entry_value=None,
                    new_rec_status=None,
                    new_duration=None,
                    new_entry_type=None,
                    new_when_start=None)
def insert_cumulative_entries(data_mgr, node_uuid, start_time, entry_value, interval_duration, start_meter_value, num_entries):
    entry_time = start_time
    meter_value = start_meter_value + entry_value

    for i in range(num_entries):
        data_mgr.db_mgr.write_meter_entry(node_uuid, when_start_raw=entry_time,
                                 when_start_raw_nonce=base.get_nonce(), when_start=entry_time,
                                 entry_type='MUPS', entry_value=entry_value,
                                 duration=interval_duration, meter_value=meter_value, rec_status='NORM')
        entry_time += interval_duration
        meter_value += entry_value
Example #4
0
 def proc_meter_rebase(self, node_uuid, entry_timestamp, meter_value):
     #TODO: handle more intelligently, implement definitive master - consider that meter node cannot be reached in realtime
     #meter wins except for reboot, rollover? metervalue as utterly notional except to track accuracy vs smart meter? What really matters is use in time period...
     timestamp_nonce = base.get_nonce()
     self.db_mgr.write_meter_entry(node_uuid, int(entry_timestamp),
                                   timestamp_nonce, int(entry_timestamp),
                                   db.EntryType.METER_REBASE.value, 0, 0,
                                   int(meter_value),
                                   db.RecStatus.NORMAL.value)
     if self.do_ev_file:
         self.ev_logger.info("{},{},{},{},{},{},{}".format(
             'MTRREBASE', int(entry_timestamp), timestamp_nonce,
             int(entry_timestamp), db.EntryType.METER_REBASE.value,
             int(meter_value), db.RecStatus.NORMAL.value))
def test_consumption_with_single_rebase_upfront(data_mgr):
    node_uuid = "99.99.99.99.1"
    start_time = base.MIN_TIME

    # write baseline
    data_mgr.db_mgr.write_meter_entry(node_uuid, when_start_raw=start_time,
                                      when_start_raw_nonce=base.get_nonce(), when_start=start_time,
                                      entry_type='MREBS', entry_value=0,
                                      duration=0, meter_value=1000, rec_status='NORM')

    # generate 20 cumulative entries of 5Wh each => 1100Wh
    insert_cumulative_entries(data_mgr, node_uuid, start_time=start_time, entry_value=5, interval_duration=60, start_meter_value=1000, num_entries=20)

    # consumption should be 1100 - 1000 == 100Wh
    assert data_mgr.get_meter_consumption(node_uuid) == 100
    data_mgr.db_mgr.delete_all_meter_entries(node_uuid)
Example #6
0
    def proc_meter_update(self, node_uuid, meter_entries):
        for entry in meter_entries:
            timestamp_nonce = base.get_nonce()

            self.db_mgr.write_meter_entry(node_uuid, int(entry['when_start']),
                                          timestamp_nonce,
                                          int(entry['when_start']),
                                          db.EntryType.METER_UPDATE.value,
                                          int(entry['entry_value']),
                                          int(entry['entry_interval_length']),
                                          int(entry['meter_value']),
                                          db.RecStatus.NORMAL.value)
            if self.do_ev_file:
                self.ev_logger.info("{},{},{},{},{},{},{},{},{},{}".format(
                    'MTRUPDATE', node_uuid,
                    int(entry['when_start']), timestamp_nonce,
                    int(entry['when_start']), db.EntryType.METER_UPDATE.value,
                    int(entry['entry_value']),
                    int(entry['entry_interval_length']),
                    int(entry['meter_value']), db.RecStatus.NORMAL.value))