bug fixes and extensions for cybertools.tracking
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1525 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
959dd2a56f
commit
b2f2c5c64c
3 changed files with 46 additions and 5 deletions
|
@ -38,6 +38,27 @@ What happens if we store more than on record for one set of keys?
|
||||||
>>> [t.data for t in t2]
|
>>> [t.data for t in t2]
|
||||||
[{'somekey': 'somevalue'}, {'somekey': 'newvalue'}]
|
[{'somekey': 'somevalue'}, {'somekey': 'newvalue'}]
|
||||||
|
|
||||||
|
It is also possible to retrieve the last entry for a set of keys directliy:
|
||||||
|
|
||||||
|
>>> tracks.getLastUserTrack('a001', 0, 'u1')
|
||||||
|
<Track ['a001', 1, 'u1', ...]: {'somekey': 'newvalue'}>
|
||||||
|
|
||||||
|
Instead of creating a new track object for each call one can also replace
|
||||||
|
an existing one (if present). The replace entry is always the last one
|
||||||
|
for a given set of keys:
|
||||||
|
|
||||||
|
>>> tracks.saveUserTrack('a001', 0, 'u1', {'somekey': 'newvalue2'}, replace=True)
|
||||||
|
'0000003'
|
||||||
|
>>> t3 = tracks.getUserTracks('a001', 0, 'u1')
|
||||||
|
>>> [t.data for t in t3]
|
||||||
|
[{'somekey': 'somevalue'}, {'somekey': 'newvalue2'}]
|
||||||
|
|
||||||
|
>>> tracks.saveUserTrack('a001', 0, 'u2', {'somekey': 'user2'}, replace=True)
|
||||||
|
'0000004'
|
||||||
|
>>> t4 = tracks.getUserTracks('a001', 0, 'u2')
|
||||||
|
>>> [t.data for t in t4]
|
||||||
|
[{'somekey': 'user2'}]
|
||||||
|
|
||||||
The tracks of a tracking store may be reindexed:
|
The tracks of a tracking store may be reindexed:
|
||||||
|
|
||||||
>>> tracks.reindexTracks()
|
>>> tracks.reindexTracks()
|
||||||
|
|
|
@ -63,9 +63,17 @@ class TrackingStorage(BTreeContainer):
|
||||||
if taskId in self.currentRuns:
|
if taskId in self.currentRuns:
|
||||||
del self.currentRuns[taskId]
|
del self.currentRuns[taskId]
|
||||||
|
|
||||||
def saveUserTrack(self, taskId, runId, userName, data):
|
def saveUserTrack(self, taskId, runId, userName, data, replace=False):
|
||||||
if not runId:
|
if not runId:
|
||||||
runId = self.currentRuns.get(taskId) or self.startRun(taskId)
|
runId = self.currentRuns.get(taskId) or self.startRun(taskId)
|
||||||
|
trackNum = 0
|
||||||
|
if replace:
|
||||||
|
track = self.getLastUserTrack(taskId, runId, userName)
|
||||||
|
if track:
|
||||||
|
trackId = str(track.__name__)
|
||||||
|
trackNum = int(trackId)
|
||||||
|
del self[trackId]
|
||||||
|
if not trackNum:
|
||||||
self.trackNum += 1
|
self.trackNum += 1
|
||||||
trackNum = self.trackNum
|
trackNum = self.trackNum
|
||||||
trackId = self.idFromNum(trackNum)
|
trackId = self.idFromNum(trackNum)
|
||||||
|
@ -95,6 +103,12 @@ class TrackingStorage(BTreeContainer):
|
||||||
runId = self.currentRuns.get(taskId)
|
runId = self.currentRuns.get(taskId)
|
||||||
return self.query(taskId=taskId, runId=runId, userName=userName)
|
return self.query(taskId=taskId, runId=runId, userName=userName)
|
||||||
|
|
||||||
|
def getLastUserTrack(self, taskId, runId, userName):
|
||||||
|
tracks = self.getUserTracks(taskId, runId, userName)
|
||||||
|
if tracks:
|
||||||
|
return sorted(tracks, key=lambda x: x.timeStamp)[-1]
|
||||||
|
else: return None
|
||||||
|
|
||||||
def query(self, **kw):
|
def query(self, **kw):
|
||||||
result = None
|
result = None
|
||||||
for idx in kw:
|
for idx in kw:
|
||||||
|
@ -110,7 +124,7 @@ class TrackingStorage(BTreeContainer):
|
||||||
return result and [self[self.idFromNum(r)] for r in result] or set()
|
return result and [self[self.idFromNum(r)] for r in result] or set()
|
||||||
|
|
||||||
def intersect(self, r1, r2):
|
def intersect(self, r1, r2):
|
||||||
return r1 and intersection(r1, r2) or r2
|
return r1 is None and r2 or intersection(r1, r2)
|
||||||
|
|
||||||
def getUserNames(self, taskId):
|
def getUserNames(self, taskId):
|
||||||
return sorted(self.taskUsers.get(taskId, []))
|
return sorted(self.taskUsers.get(taskId, []))
|
||||||
|
|
|
@ -63,6 +63,12 @@ class ITrackingStorage(Interface):
|
||||||
task id given. If a 0 run id is given use the current one.
|
task id given. If a 0 run id is given use the current one.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def getLastUserTrack(taskId, runId, userName):
|
||||||
|
""" Return the last user track (that with the highest timestamp value)
|
||||||
|
corresponding to the user name and task id given.
|
||||||
|
If a 0 run id is given use the current one.
|
||||||
|
"""
|
||||||
|
|
||||||
def getUserNames(taskId):
|
def getUserNames(taskId):
|
||||||
""" Return all user names (user ids) that have tracks for the
|
""" Return all user names (user ids) that have tracks for the
|
||||||
task given.
|
task given.
|
||||||
|
|
Loading…
Add table
Reference in a new issue