Skip to content

Commit

Permalink
make a copy of userinfo and eventtime structures (are created on the …
Browse files Browse the repository at this point in the history
…stack in C)
  • Loading branch information
burivuh committed Sep 9, 2019
1 parent 4edde70 commit aab2352
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 50 deletions.
120 changes: 75 additions & 45 deletions snippets/pyaloha/ccode.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,43 @@ class CEVENTTIME(ctypes.Structure):
('server_upload', ctypes.c_uint64)
]

__slots__ = (
'client_creation', 'server_upload',
'dtime', 'is_accurate'
)

delta_past = datetime.timedelta(days=6 * 30)
delta_future = datetime.timedelta(days=1)

def _setup_time(self):
def get_approx_time(self):
if not self.dtime:
self._setup_time()
return self.dtime, self.is_accurate

def make_object(self):
client_dtime = SerializableDatetime.utcfromtimestamp(
self.client_creation / 1000. # timestamp is in millisecs
)
server_dtime = SerializableDatetime.utcfromtimestamp(
self.server_upload / 1000. # timestamp is in millisecs
)

self.dtime = server_dtime
self.is_accurate = False
dtime = server_dtime
if client_dtime >= server_dtime - CEVENTTIME.delta_past and\
client_dtime <= server_dtime + CEVENTTIME.delta_future:
self.dtime = client_dtime
self.is_accurate = True
dtime = client_dtime

def get_approx_time(self):
if not self.dtime:
self._setup_time()
return self.dtime, self.is_accurate
pytime = PythonEventTime()
pytime.client_creation = client_dtime
pytime.server_upload = server_dtime
pytime.dtime = dtime
return pytime


class PythonEventTime(object):
__slots__ = (
'client_creation', 'server_upload',
'dtime', 'is_accurate'
)

@property
def is_accurate(self):
return self.dtime == self.client_creation

def __dumpdict__(self):
return {
Expand All @@ -72,20 +82,6 @@ class IDInfo(ctypes.Structure):

__slots__ = ('os', 'uid')

def __dumpdict__(self):
return {
'os': self.os
}

def is_on_android(self):
return self.os == 1

def is_on_ios(self):
return self.os == 2

def is_on_unknown_os(self):
return self.os == 0

def validate(self):
if self.os not in IDInfo._os_valid_range:
raise ValueError('Incorrect os value: %s' % self.os)
Expand Down Expand Up @@ -113,13 +109,6 @@ def get_location(self):
return (self.lat, self.lon)
return None

def __dumpdict__(self):
dct = super(GeoIDInfo, self).__dumpdict__()
if self.has_geo():
dct['lat'] = round(self.lat, 6)
dct['lon'] = round(self.lon, 6)
return dct


class CUSERINFO(GeoIDInfo):
_fields_ = [
Expand All @@ -128,19 +117,60 @@ class CUSERINFO(GeoIDInfo):

__slots__ = ('raw_uid',)

def setup(self):
def make_object(self):
self.validate()
setattr(self, 'uid', int(self.raw_uid, 16))

def stripped(self):
if self.has_geo():
return GeoIDInfo(
uid=self.uid, os=self.os,
lat=self.lat, lon=self.lon
)
return IDInfo(
uid=self.uid, os=self.os
)
pyinfo = PythonGeoUserInfo()
pyinfo.uid = int(self.raw_uid, 16)
pyinfo.os = self.os
pyinfo.lat = round(self.lat, 6)
pyinfo.lon = round(self.lon, 6)
return pyinfo

pyinfo = PythonUserInfo()
pyinfo.uid = int(self.raw_uid, 16)
pyinfo.os = self.os
return pyinfo


class PythonUserInfo(object):
__slots__ = ('uid', 'os')

@property
def has_geo(self):
return False

def is_on_android(self):
return self.os == 1

def is_on_ios(self):
return self.os == 2

def is_on_unknown_os(self):
return self.os == 0

def __dumpdict__(self):
return {
'os': self.os,
'uid': self.uid
}


class PythonGeoUserInfo(PythonUserInfo):
__slots__ = ('lat', 'lon')

@property
def has_geo(self):
return True

def __dumpdict__(self):
d = super(PythonGeoUserInfo, self).__dumpdict__()
d.update({
'lat': self.lat,
'lon': self.lon
})
return d


CCALLBACK = ctypes.CFUNCTYPE(
Expand Down
9 changes: 4 additions & 5 deletions snippets/pyaloha/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ def __init__(self,
if data_list_len:
self.data_list = data_list
self.data_list_len = data_list_len
event_time._setup_time()
self.event_time = event_time
self.user_info = user_info
self.user_info.setup()

self.event_time = event_time.make_object()
self.user_info = user_info.make_object()
self.key = key

def process_me(self, processor):
Expand All @@ -21,7 +20,7 @@ def __dumpdict__(self):
return {
'key': self.key,
'type': self.__class__.__name__,
'user_info': self.user_info.stripped(),
'user_info': self.user_info,
'event_time': self.event_time
}

Expand Down

0 comments on commit aab2352

Please sign in to comment.