import { SavedObjectsClientContract } from 'kibana/server' ; import { usageMetricSavedObjectType } from '../../../common/types' ; import { LiveQuerySessionUsage } from '../../usage/types' ; export interface RouteUsageMetric { queries : number ; errors : number ; } export type RouteString = 'live_query' ; export const routeStrings = [ 'live_query' ] ; export async function getOrCreateMetricObject < T > ( soClient , route ) { try { return await soClient . get < T > ( usageMetricSavedObjectType , route ) ; } catch ( e ) { return await soClient . create ( usageMetricSavedObjectType , { errors : 0 , count : 0 , } , { id : route , } ) ; } } export async function getCount ( soClient , route ) { return await getOrCreateMetricObject < LiveQuerySessionUsage > ( soClient , route ) ; } export interface CounterValue { count : number ; errors : number ; } export async function incrementCount ( soClient , route , key = 'count' , increment = 1 ) { const metric = await getOrCreateMetricObject < CounterValue > ( soClient , route ) ; metric . attributes [ key ] += increment ; await soClient . update ( usageMetricSavedObjectType , route , metric . attributes ) ; } export async function getRouteMetric ( soClient , route ) { return ( await getCount ( soClient , route ) ) . attributes ; }