Skip to content

Commit 5ea455c

Browse files
committed
[sc-80908] new DSSUserActivity class
1 parent 4190a72 commit 5ea455c

File tree

1 file changed

+77
-9
lines changed

1 file changed

+77
-9
lines changed

dataikuapi/dss/admin.py

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
13
from .future import DSSFuture
24
import json, warnings
35

@@ -164,23 +166,21 @@ def delete(self):
164166
def get_settings(self):
165167
"""
166168
Gets the settings of the user
169+
167170
:rtype: :class:`DSSUserSettings`
168171
"""
169172
raw = self.client._perform_json("GET", "/admin/users/%s" % self.login)
170173
return DSSUserSettings(self.client, self.login, raw)
171174

172-
def get_last_activity(self):
175+
def get_activity(self):
173176
"""
174-
Gets the last activity of a user:
175-
176-
* last successful login attempt
177-
* last failed login attempt
178-
* last loading of DSS (new tab or tab refresh)
177+
Gets the activity of the user
179178
180-
:return: the user's activity, as a dict
181-
:rtype: dict
179+
:return: the user's activity
180+
:rtype: :class:`DSSUserActivity`
182181
"""
183-
return self.client._perform_json("GET", "/admin/users/%s/activity" % self.login)
182+
raw = self.client._perform_json("GET", "/admin/users/%s/activity" % self.login)
183+
return DSSUserActivity(self.client, self.login, raw)
184184

185185
########################################################
186186
# Legacy
@@ -237,6 +237,7 @@ def get_client_as(self):
237237
else:
238238
raise ValueError("Don't know how to proxy this client")
239239

240+
240241
class DSSOwnUser(object):
241242
"""
242243
A handle to interact with your own user
@@ -377,6 +378,73 @@ def save(self):
377378
self.client._perform_empty("PUT", "/current-user", body = self.settings)
378379

379380

381+
class DSSUserActivity(object):
382+
"""
383+
Settings for a DSS user.
384+
Do not call this directly, use :meth:`DSSUser.get_activity`
385+
"""
386+
387+
def __init__(self, client, login, activity):
388+
self.client = client
389+
self.login = login
390+
self.activity = activity
391+
392+
def get_raw(self):
393+
"""
394+
Get the raw activity of the user as a dict.
395+
396+
:return: the raw activity
397+
:rtype: dict
398+
"""
399+
return self.activity
400+
401+
def get_last_successful_login_attempt(self, as_date=False):
402+
"""
403+
Get the last successful login attempt of the user as a timestamp or as a :class:`datetime.datetime`
404+
405+
Returns `0` or `1970-01-01 01:00:00` if there is no logged attempt.
406+
407+
:return: the last successful login attempt
408+
:rtype: int or :class:`datetime.datetime`
409+
"""
410+
timestamp = self.activity["lastSuccessfulLogin"]
411+
if as_date:
412+
return datetime.datetime.fromtimestamp(timestamp / 1000)
413+
else:
414+
return timestamp
415+
416+
def get_last_failed_login_attempt(self, as_date=False):
417+
"""
418+
Get the last failed login attempt of the user as a timestamp or as a :class:`datetime.datetime`
419+
420+
Returns `0` or `1970-01-01 01:00:00` if there is no logged attempt.
421+
422+
:return: the last failed login attempt
423+
:rtype: int or :class:`datetime.datetime`
424+
"""
425+
timestamp = self.activity["lastFailedLogin"]
426+
if as_date:
427+
return datetime.datetime.fromtimestamp(timestamp / 1000)
428+
else:
429+
return timestamp
430+
431+
def get_last_session_loading(self, as_date=False):
432+
"""
433+
Get the last session loading of the user as a timestamp or as a :class:`datetime.datetime`, i.e. the last time
434+
he opened a new DSS tab or refreshed his session.
435+
436+
Returns `0` or `1970-01-01 01:00:00` if there is no logged attempt.
437+
438+
:return: the last session loading
439+
:rtype: int or :class:`datetime.datetime`
440+
"""
441+
timestamp = self.activity["lastLoaded"]
442+
if as_date:
443+
return datetime.datetime.fromtimestamp(timestamp / 1000)
444+
else:
445+
return timestamp
446+
447+
380448
class DSSGroup(object):
381449
"""
382450
A group on the DSS instance.

0 commit comments

Comments
 (0)