If you make a val public, you may probably also want to see how often that val is used - perhaps even including some statistics which show how often that val was called in a given time span. This is exactly what the "InvocationTracker" was written for.
Simply include a call to the "InvocationTracker" in your val, and from then on any invocations will be tracked in an sqlite table allowing you to easily calculate how often the val was used in total or get the time course of calls.
Using an InvocationTracker
(within the val that should be monitored) is quite simple:
import { InvocationTracker } from 'https://esm.town/v/rozek/InvocationTracker'
const TrackingTable = 'xxx' // enter name of sqlite table that can be used
const Granularity = 15*60*1000 // how precisely should be logged?
;(async () => {
const Tracker = await InvocationTracker.new(TrackingTable,Granularity)
await Tracker.increment() // logs this invocation
... now continue as usual
})()
Later, you may then use the "InvocationTracker" to provide you with some statistics:
import { InvocationTracker } from 'https://esm.town/v/rozek/InvocationTracker'
const TrackingTable = 'xxx' // enter name of sqlite table that can be used
;(async () => {
const Tracker = await InvocationTracker.new(TrackingTable,Granularity)
/**** define the time span that interests you ****/
const from = new Date('xxx').getTime() // enter start date of your time span
const to = Date.now() // here we are interested in all invocations until now
/**** get the total number of invocations in the given time span ****/
const totalInvocations = await Tracker.totalInvocationsInSpan(from,to)
/**** get more information about when your val was called over time ****/
const Invocations = await Tracker.InvocationsInSpan(from,to)
// format: [{ Time,Invocations },...] with the given granularity
// only times with actual invocations are listed, idle times are skipped
})()
The "InvocationTracker" is designed to be accessed simultaneously from multiple concurrently running vals without interfering with each other.
static async new (Name:string, Granularity:number = 15*60*1000):Promise<InvocationTracker>
creates a new "InvocationTracker" instance.Name
must match/^[a-z_][0-9a-z_]+$/i
,Granularity
is the time resolution in millisecondsasync Granularity ():Promise<number>
returns the current time resolution in millisecondsasync setGranularity (newGranularity:number):Promise<void>
sets a new time resolution (in milliseconds).newGranularity
must be an integer >= 1async reset ():Promise<void>
clears all recorded invocationsasync increment ():Promise<void>
increments the invocation count for the current time periodasync InvocationsInSpan (from:number, to:number):Promise<{ Time:number, Invocations:number }[]>
returns a list of invocation counts within the specified time span.from
andto
are Unix timestamps in millisecondsasync totalInvocationsInSpan (from:number, to:number):Promise<number>
returns the total number of invocations within the specified time span.from
andto
are Unix timestamps in milliseconds
Some tests can be found in val InvocationTracker_Test