33"""
44from __future__ import absolute_import
55
6+ import datetime
67import logging
78from threading import Lock
89
@@ -58,6 +59,52 @@ def __init__(self, session_id, services):
5859 self .stack = StackInABox ()
5960 self .stack .base_url = self .session_id
6061 self .init_services ()
62+ self .created_at = datetime .datetime .utcnow ()
63+ self ._last_accessed_time = self .created_at
64+ self ._access_count = 0
65+ self ._http_status_dict = {}
66+
67+ def _update_trackers (self ):
68+ """
69+ Update the session trackers
70+ """
71+ self ._access_count = self ._access_count + 1
72+ self ._last_accessed_time = datetime .datetime .utcnow ()
73+
74+ def _track_result (self , result ):
75+ """
76+ Track the results from StackInABox
77+ """
78+ status , _ , _ = result
79+ if status not in self ._http_status_dict :
80+ self ._http_status_dict [status ] = 0
81+
82+ self ._http_status_dict [status ] = (
83+ self ._http_status_dict [status ] + 1
84+ )
85+
86+ return result
87+
88+ @property
89+ def last_accessed_at (self ):
90+ """
91+ Return the time the session was last accessed
92+ """
93+ return self ._last_accessed_time
94+
95+ @property
96+ def access_count (self ):
97+ """
98+ Return the number of times the session has been called
99+ """
100+ return self ._access_count
101+
102+ @property
103+ def status_tracker (self ):
104+ """
105+ Return the current copy of HTTP Status Code Trackers
106+ """
107+ return self ._http_status_dict
61108
62109 def init_services (self ):
63110 """
@@ -72,6 +119,7 @@ def init_services(self):
72119 )
73120 )
74121 self .stack .register (svc )
122+ self ._http_status_dict = {}
75123
76124 @property
77125 def base_url (self ):
@@ -104,6 +152,7 @@ def reset(self):
104152 Reset the StackInABox instance to the initial state by
105153 resetting the instance then re-registering all the services.
106154 """
155+ self ._update_trackers ()
107156 logger .debug (
108157 'Session {0}: Waiting for lock' .format (
109158 self .session_id
@@ -123,6 +172,7 @@ def call(self, *args, **kwargs):
123172 """
124173 Wrapper to same in the StackInABox instance
125174 """
175+ self ._update_trackers ()
126176 logger .debug (
127177 'Session {0}: Waiting for lock' .format (
128178 self .session_id
@@ -135,12 +185,15 @@ def call(self, *args, **kwargs):
135185 )
136186 )
137187
138- return self .stack .call (* args , ** kwargs )
188+ return self ._track_result (
189+ self .stack .call (* args , ** kwargs )
190+ )
139191
140192 def try_handle_route (self , * args , ** kwargs ):
141193 """
142194 Wrapper to same in the StackInABox instance
143195 """
196+ self ._update_trackers ()
144197 logger .debug (
145198 'Session {0}: Waiting for lock' .format (
146199 self .session_id
@@ -153,12 +206,15 @@ def try_handle_route(self, *args, **kwargs):
153206 )
154207 )
155208
156- return self .stack .try_handle_route (* args , ** kwargs )
209+ return self ._track_result (
210+ self .stack .try_handle_route (* args , ** kwargs )
211+ )
157212
158213 def request (self , * args , ** kwargs ):
159214 """
160215 Wrapper to same in the StackInABox instance
161216 """
217+ self ._update_trackers ()
162218 logger .debug (
163219 'Session {0}: Waiting for lock' .format (
164220 self .session_id
@@ -171,12 +227,15 @@ def request(self, *args, **kwargs):
171227 )
172228 )
173229
174- return self .stack .request (* args , ** kwargs )
230+ return self ._track_result (
231+ self .stack .request (* args , ** kwargs )
232+ )
175233
176234 def sub_request (self , * args , ** kwargs ):
177235 """
178236 Pass-thru to the StackInABox instance's sub_request
179237 """
238+ self ._update_trackers ()
180239 logger .debug (
181240 'Session {0}: Waiting for lock' .format (
182241 self .session_id
@@ -189,4 +248,6 @@ def sub_request(self, *args, **kwargs):
189248 )
190249 )
191250
192- return self .stack .sub_request (* args , ** kwargs )
251+ return self ._track_result (
252+ self .stack .sub_request (* args , ** kwargs )
253+ )
0 commit comments