def get(self, timestamp): """Get the TSDBRow located at timestamp. .. note:: The timestamp of the returned row may be different from the timestamp requested. This is due to the fact that the application storing the row used an actual timestamp but the query refers to the slot in which the timestamp argument falls. .. note:: If an invalid row is fetched, the timestamp is set to the requested timestamp since an invalid row has a timestamp of 0. .. note:: If the chunk does not exist it is created with all zeros and thus an invalid row is returned. For the current use case creating the chunk is acceptable as the datasets are not expected to be sparse. """ slot = calculate_slot(timestamp, self.metadata['STEP']) try: min_slot = calculate_slot(self.min_timestamp(), self.metadata['STEP']) if slot < min_slot: raise TSDBVarRangeError( "%d is less than the minimum slot %d" % (timestamp, min_slot)) max_slot = calculate_slot(self.max_timestamp(), self.metadata['STEP']) + self.metadata['STEP'] - 1 if slot > max_slot: raise TSDBVarRangeError( "%d is greater than the maximum slot %d" % (timestamp, max_slot)) except TSDBVarEmpty: raise TSDBVarRangeError(timestamp) try: chunk = self._chunk(timestamp) val = chunk.read_row(timestamp) except TSDBVarChunkDoesNotExistError: val = self.type.get_invalid_row() if not val.flags & ROW_VALID: # if row isn't valid the timestamp is 0 # set the timestamp to the slot timestamp instead val.timestamp = timestamp return val
def select_generator(var, begin, end, flags): current = calculate_slot(begin, self.metadata['STEP']) max_ts = self.max_timestamp() now = int(time.time()) if max_ts > now: max_ts = now while current <= end: try: row = var.get(current) except TSDBVarRangeError: # looking for data beyond the end of recorded data so stop. if current > max_ts: raise StopIteration if row.timestamp > end: break if not flags or row.flags & flags == flags: yield row current += var.metadata['STEP'] raise StopIteration
def test_calculate_slot(): for (raw, expected, step) in ( (0, 0, 30), (1, 0, 30), (29, 0, 30), (30, 30, 30), ): assert calculate_slot(raw, step) == expected
def test_calculate_slot(): for (raw, expected, step) in ( (0, 0, 30), (1, 0, 30), (29, 0, 30), (30, 30, 30),): assert calculate_slot(raw, step) == expected