Configure an Entity Type to Persist User Events
You can store and reference user-initiated events for observability or metric viewing purposes with the following Types:
- User.Event: Base Type for user-initiated events
- User.Event.Login: Sub Type for login events
- User.Event.Activity: Sub Type for any user-initiated activity
Mix the User.Event.Activity Type with an entity Type to store the following metrics in the database:
- Login events
- Last application event for each application everyday
To get usage metrics for a particular user, complete the following steps:
- Create a user-defined Type that mixes User.Event.Activity
- Create an instance of the Type and persist it in the database
- Fetch the metric for a given user
Steps to get metrics on user-initiated events
The following example demonstrates how to collect user-based usage metrics on a hypothetical model named Narwhal.
1. Create a user-defined Type that mixes User.Event.Activity
Create an entity Type for what you want to collect metrics on, and mix the User.Event.Activity Type.
For example, create a ModelUsageMetric Type that includes a field for the model name:
entity type ModelUsageMetric mixes User.Event.Activity {
modelName: !string
}This Type tracks user-initiated events and includes the modelName field to store the name of the model you want to get metrics on.
2. Create an instance of the Type and persist it in the database
In C3 AI Console, run the following code to instantiate the ModelUsageMetric Type and upsert it. Replace <user ID> with the user ID of the user you want collect an event for.
ModelUsageMetric.make({
modelName: 'Narwhal',
userId: '<user ID>',
date: DateTime.now()
}).upsert()This code passes the model name Narwhal, the user ID, the current date and time, and upserts an instance of the ModelUsageMetric Type to the database.
3. Fetch the metric for a given user
In C3 AI Console, run the following code to fetch an event for the model Narwhal and a given user ID:
ModelUsageMetric.fetch({
filter: Filter.eq('modelName', 'Narwhal').and().eq('userId', '<user ID>')
});This code returns login events and the last application event for each application in the environment for the specified user and their usage of the model Narwhal.