Example #1
0
    def decode_timeseries_row(self,
                              tsrow,
                              tscols=None,
                              convert_timestamp=False):
        """
        Decodes a TsRow into a list

        :param tsrow: the protobuf TsRow to decode.
        :type tsrow: riak.pb.riak_ts_pb2.TsRow
        :param tscols: the protobuf TsColumn data to help decode.
        :type tscols: list
        :rtype list
        """
        row = []
        for i, cell in enumerate(tsrow.cells):
            col = None
            if tscols is not None:
                col = tscols[i]
            if cell.HasField('varchar_value'):
                if col and not (col.type == TsColumnType.Value('VARCHAR')
                                or col.type == TsColumnType.Value('BLOB')):
                    raise TypeError('expected VARCHAR or BLOB column')
                else:
                    row.append(cell.varchar_value)
            elif cell.HasField('sint64_value'):
                if col and col.type != TsColumnType.Value('SINT64'):
                    raise TypeError('expected SINT64 column')
                else:
                    row.append(cell.sint64_value)
            elif cell.HasField('double_value'):
                if col and col.type != TsColumnType.Value('DOUBLE'):
                    raise TypeError('expected DOUBLE column')
                else:
                    row.append(cell.double_value)
            elif cell.HasField('timestamp_value'):
                if col and col.type != TsColumnType.Value('TIMESTAMP'):
                    raise TypeError('expected TIMESTAMP column')
                else:
                    dt = cell.timestamp_value
                    if convert_timestamp:
                        dt = datetime_from_unix_time_millis(
                            cell.timestamp_value)
                    row.append(dt)
            elif cell.HasField('boolean_value'):
                if col and col.type != TsColumnType.Value('BOOLEAN'):
                    raise TypeError('expected BOOLEAN column')
                else:
                    row.append(cell.boolean_value)
            else:
                row.append(None)
        return row
Example #2
0
    def decode_timeseries_row(self, tsrow, tscols=None,
                              convert_timestamp=False):
        """
        Decodes a TsRow into a list

        :param tsrow: the protobuf TsRow to decode.
        :type tsrow: riak.pb.riak_ts_pb2.TsRow
        :param tscols: the protobuf TsColumn data to help decode.
        :type tscols: list
        :rtype list
        """
        row = []
        for i, cell in enumerate(tsrow.cells):
            col = None
            if tscols is not None:
                col = tscols[i]
            if cell.HasField('varchar_value'):
                if col and not (col.type == TsColumnType.Value('VARCHAR') or
                                col.type == TsColumnType.Value('BLOB')):
                    raise TypeError('expected VARCHAR or BLOB column')
                else:
                    row.append(cell.varchar_value)
            elif cell.HasField('sint64_value'):
                if col and col.type != TsColumnType.Value('SINT64'):
                    raise TypeError('expected SINT64 column')
                else:
                    row.append(cell.sint64_value)
            elif cell.HasField('double_value'):
                if col and col.type != TsColumnType.Value('DOUBLE'):
                    raise TypeError('expected DOUBLE column')
                else:
                    row.append(cell.double_value)
            elif cell.HasField('timestamp_value'):
                if col and col.type != TsColumnType.Value('TIMESTAMP'):
                    raise TypeError('expected TIMESTAMP column')
                else:
                    dt = cell.timestamp_value
                    if convert_timestamp:
                        dt = datetime_from_unix_time_millis(
                            cell.timestamp_value)
                    row.append(dt)
            elif cell.HasField('boolean_value'):
                if col and col.type != TsColumnType.Value('BOOLEAN'):
                    raise TypeError('expected BOOLEAN column')
                else:
                    row.append(cell.boolean_value)
            else:
                row.append(None)
        return row
Example #3
0
    def test_unix_millis_small_value(self):
        if is_timeseries_supported():
            # this is what would be stored in Riak TS
            v = 1001
            dt = datetime_from_unix_time_millis(v)

            # This is how Python represents the above
            utp = 1.001
            dtp = datetime.datetime.utcfromtimestamp(utp)
            self.assertEqual(dt, dtp)

            utm = unix_time_millis(dt)
            self.assertEqual(v, utm)
        else:
            pass
Example #4
0
    def test_conv_ms_timestamp_to_datetime_and_back(self):
        if is_timeseries_supported():
            # this is what would be stored in Riak TS
            v = 144379690987
            dt = datetime_from_unix_time_millis(v)

            # This is how Python represents the above
            utp = 144379690.987000
            dtp = datetime.datetime.utcfromtimestamp(utp)
            self.assertEqual(dt, dtp)

            utm = unix_time_millis(dt)
            self.assertEqual(v, utm)
        else:
            pass
Example #5
0
    def decode_timeseries_row(self, tsrow, tsct, convert_timestamp=False):
        """
        Decodes a TTB-encoded TsRow into a list

        :param tsrow: the TTB decoded TsRow to decode.
        :type tsrow: TTB dncoded row
        :param tsct: the TTB decoded column types (atoms).
        :type tsct: list
        :param convert_timestamp: Convert timestamps to datetime objects
        :type tsobj: boolean
        :rtype list
        """
        row = []
        for i, cell in enumerate(tsrow):
            if cell is None:
                row.append(None)
            elif isinstance(cell, list) and len(cell) == 0:
                row.append(None)
            else:
                if convert_timestamp and tsct[i] == timestamp_a:
                    row.append(datetime_from_unix_time_millis(cell))
                else:
                    row.append(cell)
        return row
 def test_encode_decode_timestamp(self):
     ts0ms = unix_time_millis(ts0)
     self.assertEqual(ts0ms, ex0ms)
     ts0_d = datetime_from_unix_time_millis(ts0ms)
     self.assertEqual(ts0, ts0_d)
Example #7
0
 def test_unix_millis_validation(self):
     v = 144379690.987
     with self.assertRaises(ValueError):
         datetime_from_unix_time_millis(v)
Example #8
0
 def test_encode_decode_timestamp(self):
     ts0ms = unix_time_millis(ts0)
     self.assertEqual(ts0ms, ex0ms)
     ts0_d = datetime_from_unix_time_millis(ts0ms)
     self.assertEqual(ts0, ts0_d)