Logging latest activity for a given user?

My customer wants reporting on users’ latest activity. The Users service has a column for last_login by default, but relying on this isn’t a great representation of reality because the most engaged users are also the most likely to stay logged in for a while.

What’s the best way to store a timestamp for a users’ most recent activity?**

** I’m pretty sure “best way” means the fastest way to update a user-level timestamp on each content_panel.clear(); content_panel.add_component(new_form()) event (i.e., a new “page” load). Ideally, I’d like this to happen automagically via the MainForm (parent of the content_panel) without needing to address it in the __init__ of each individual form.

What about creating a new table for this information…
Whenever something happens that you want to store you could do a silent call to ther sever module.

You could also combine this with sessions/cookies

Something like:

def load_form(self, Form):
  self.content_panel.clear() 
  self.content_panel.add_component(Form())
  anvil.server.call_s('update_session', Form.__name__)

serverside

from datetime import datetime

@anvil.server.callable
def update_session(form_name):
  # pseudo code
  row_id = anvil.server.session['row_id'] 
  anvil.server.session['form_loads'].append((form_name, datetime.now()))
  row = app_tables.session_table.get_by_id(row_id)
  row['session_data'] = anvil.server.session.session # the dict associated with the session

And then maybe when the session starts - i.e. main form is loaded for the first time:

def __init__(self, **properties):
  ...
  anvil.server.call_s('new_sessoin')
@anvil.server.callable
def new_session():
  user = anvil.users.get_user()
  start_time = datetime.now()
  row = app_tables.session_table.add_row()
  anvil.server.session['row_id'] = row.get_id()
  anvil.server.session['user'] = user
  anvil.server.session['start_time'] = start_time
  anvil.server.session['form_loads'] = []
  row['user'] = user
  row['start_time'] = start_time
  row['session_data'] = anvil.server.session.session # the dict associated with the session

You probably don’t need session data but it might be useful as it’s persistent between server calls

hey @stucork !


unfortunately i cant get your example to work - i tried with different column types. in session_table.session_data

is it bcoz the session item changed?
in Anvil Docs | Sessions and Cookies i also couldnt find anything that helps here…
any help appreciated! :slight_smile: