3 from .cache import Cache
6 logger = logging.getLogger("mw.lib.sessions.functions")
9 def cluster(user_events, cutoff=defaults.CUTOFF):
11 Clusters user sessions from a sequence of user events. Note that,
12 `event` data will simply be returned in the case of a revert.
14 This function serves as a convenience wrapper around calls to
15 :class:`~mw.lib.sessions.Cache`'s :meth:`~mw.lib.sessions.Cache.process`
19 user_events : iter( (user, timestamp, event) )
20 an iterable over tuples of user, timestamp and event data.
23 * timestamp : :class:`mw.Timestamp`
27 the maximum time between events within a user session
30 a iterator over :class:`~mw.lib.sessions.Session`
33 >>> from mw.lib import sessions
36 ... ("Willy on wheels", 100000, {'rev_id': 1}),
37 ... ("Walter", 100001, {'rev_id': 2}),
38 ... ("Willy on wheels", 100001, {'rev_id': 3}),
39 ... ("Walter", 100035, {'rev_id': 4}),
40 ... ("Willy on wheels", 103602, {'rev_id': 5})
43 >>> for user, events in sessions.cluster(user_events):
46 ('Willy on wheels', [{'rev_id': 1}, {'rev_id': 3}])
47 ('Walter', [{'rev_id': 2}, {'rev_id': 4}])
48 ('Willy on wheels', [{'rev_id': 5}])
53 # Construct the session manager
57 for user, timestamp, event in user_events:
59 for session in cache.process(user, timestamp, event):
62 # Yield the left-overs
63 for session in cache.get_active_sessions():
67 # For backwards compatibility