sections_fts: 44
This data as json
rowid | title | content |
---|---|---|
44 | track_event(datasette, event) | datasette - Datasette class You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) . event - Event Information about the event, represented as an instance of a subclass of the Event base class. This hook will be called any time an event is tracked by code that calls the datasette.track_event(...) internal method. The event object will always have the following properties: name : a string representing the name of the event, for example logout or create-table . actor : a dictionary representing the actor that triggered the event, or None if the event was not triggered by an actor. created : a datatime.datetime object in the timezone.utc timezone representing the time the event object was created. Other properties on the event will be available depending on the type of event. You can also access those as a dictionary using event.properties() . The events fired by Datasette core are documented here . This example plugin logs details of all events to standard error: from datasette import hookimpl import json import sys @hookimpl def track_event(event): name = event.name actor = event.actor properties = event.properties() msg = json.dumps( { "name": name, "actor": actor, "properties": properties, } ) print(msg, file=sys.stderr, flush=True) The function can also return an async function which will be awaited. This is useful for writing to a database. This example logs events to a datasette_events table in a database called events . It uses the startup(datasette) hook to create that table if it does not exist. from datasette import hookimpl import json @hookimpl def startup(datasette): async def inner(): db = datasette.get_database("events") await db.execute_write( """ create table if not exists datasette_events ( id integer primary key, event_type text, created text, actor text, properties text ) """ ) return inner @hookimpl def track_event(datasette, event): async def inner(): db = datasette.get_database("events") properties = event.properties() await db.execute_write( """ insert into datasette_events (event_type, created, actor, properties) values (?, strftime('%Y-%m-%d %H:%M:%S', 'now'), ?, ?) """, ( event.name, json.dumps(event.actor), json.dumps(properties), ), ) return inner Example: datasette-events-db |