def get_serializable_dict(self): """ Build a dictionary suitable for serialization from this object. This data structure will match that which the client sends to us :return: dict representation of this object """ user_dict = { "userId": str(self.user_id), "eventTime": int(unix_time_from_uuid1(self.event_time)), "tripId": str(self.trip_id), "eventId": str(self.event_id), "eventType": self.event_type, "location": { "lat": self.lat, "lon": self.lng, "accuracy": self.accuracy }, "speed": self.speed } if self.sdk != {}: user_dict.update({"sdk": self.sdk}) if self.meta != None: user_dict.update(self.meta) return user_dict
def default(self,o): if isinstance(o,cassandra.util.Date): return str(o) elif isinstance(o, uuid.UUID): return str(unix_time_from_uuid1(o)) else: return o
def __init__(self, description, timeuuid, source, source_elapsed, thread_name): self.description = description self.datetime = datetime.utcfromtimestamp(unix_time_from_uuid1(timeuuid)) self.source = source if source_elapsed is not None: self.source_elapsed = timedelta(microseconds=source_elapsed) else: self.source_elapsed = None self.thread_name = thread_name
def test_times_from_uuid1(self): node = uuid.getnode() now = time.time() u = uuid.uuid1(node, 0) t = util.unix_time_from_uuid1(u) self.assertAlmostEqual(now, t, 2) dt = util.datetime_from_uuid1(u) t = calendar.timegm(dt.timetuple()) + dt.microsecond / 1e6 self.assertAlmostEqual(now, t, 2)
def test_times_from_uuid1(self): node = uuid.getnode() now = time.time() u = uuid.uuid1(node, 0) t = util.unix_time_from_uuid1(u) self.assertAlmostEqual(now, t, 3) dt = util.datetime_from_uuid1(u) t = calendar.timegm(dt.timetuple()) + dt.microsecond / 1e6 self.assertAlmostEqual(now, t, 3)
def test_uuid_from_time(self): t = time.time() seq = 0x2aa5 node = uuid.getnode() u = util.uuid_from_time(t, node, seq) # using AlmostEqual because time precision is different for # some platforms self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4) self.assertEqual(u.node, node) self.assertEqual(u.clock_seq, seq) # random node u1 = util.uuid_from_time(t, clock_seq=seq) u2 = util.uuid_from_time(t, clock_seq=seq) self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4) self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4) self.assertEqual(u.clock_seq, seq) # not impossible, but we shouldn't get the same value twice self.assertNotEqual(u1.node, u2.node) # random seq u1 = util.uuid_from_time(t, node=node) u2 = util.uuid_from_time(t, node=node) self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4) self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4) self.assertEqual(u.node, node) # not impossible, but we shouldn't get the same value twice self.assertNotEqual(u1.clock_seq, u2.clock_seq) # node too large with self.assertRaises(ValueError): u = util.uuid_from_time(t, node=2**48) # clock_seq too large with self.assertRaises(ValueError): u = util.uuid_from_time(t, clock_seq=0x4000) # construct from datetime dt = util.datetime_from_timestamp(t) u = util.uuid_from_time(dt, node, seq) self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4) self.assertEqual(u.node, node) self.assertEqual(u.clock_seq, seq)
def test_uuid_from_time(self): t = time.time() seq = 0x2aa5 node = uuid.getnode() u = util.uuid_from_time(t, node, seq) # using AlmostEqual because time precision is different for # some platforms self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4) self.assertEqual(u.node, node) self.assertEqual(u.clock_seq, seq) # random node u1 = util.uuid_from_time(t, clock_seq=seq) u2 = util.uuid_from_time(t, clock_seq=seq) self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4) self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4) self.assertEqual(u.clock_seq, seq) # not impossible, but we shouldn't get the same value twice self.assertNotEqual(u1.node, u2.node) # random seq u1 = util.uuid_from_time(t, node=node) u2 = util.uuid_from_time(t, node=node) self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4) self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4) self.assertEqual(u.node, node) # not impossible, but we shouldn't get the same value twice self.assertNotEqual(u1.clock_seq, u2.clock_seq) # node too large with self.assertRaises(ValueError): u = util.uuid_from_time(t, node=2 ** 48) # clock_seq too large with self.assertRaises(ValueError): u = util.uuid_from_time(t, clock_seq=0x4000) # construct from datetime dt = util.datetime_from_timestamp(t) u = util.uuid_from_time(dt, node, seq) self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4) self.assertEqual(u.node, node) self.assertEqual(u.clock_seq, seq)
def unix_time_from_uuid1(u): msg = "'cassandra.cqltypes.unix_time_from_uuid1' has moved to 'cassandra.util'. This entry point will be removed in the next major version." warnings.warn(msg, DeprecationWarning) log.warning(msg) return util.unix_time_from_uuid1(u)
def my_timestamp(self): return util.unix_time_from_uuid1(self.val)
def add_message(request): print request.user b = json.loads(request.body) raw_date_submit = datetime.utcnow() date_submit = raw_date_submit.date() uuid_time = uuid_from_time(raw_date_submit) bind_values = ['*****@*****.**',date_submit,uuid_time,b['content']] db_connection.bind_and_execute_stmt('INSERT',bind_values) return HttpResponse(json.dumps([{'email_id' : '*****@*****.**' , 'date_happened' : date_submit.isoformat() , 'event_time' : unix_time_from_uuid1(uuid_time) , 'content' : b['content']}]))
from cassandra.cluster import Cluster from cassandra.util import unix_time_from_uuid1 from cassandra import ConsistencyLevel import time, datetime cluster = Cluster(['localhost']) session = cluster.connect('irr') for t in range(10): print("inserting data... %d" % (t, )) session.execute( "INSERT INTO rt_series (id, ts, val) VALUES (%(id)s,now(),%(val)s) USING TTL 3600;", { 'id': "1234ABCD", 'val': t }) time.sleep(0.1) rows = session.execute('SELECT id, ts, val FROM rt_series LIMIT 10') for row in rows: print(row[0], row[1], unix_time_from_uuid1(row[1]), row[2])