import { Table } from '@google-cloud/bigquery' ; import { InsertableTable } from './infrastructure/table' ; import { DailyDataLimitMetricsReport , DailyFeatureMetricsReport } from './model' ; export interface FeatureRow { serverId : string ; serverVersion : string ; timestamp : string ; dataLimit : DailyDataLimitMetricsReport ; } export class BigQueryFeaturesTable implements InsertableTable < FeatureRow > { constructor ( private bigqueryTable ) { } async insert ( rows ) { await this . bigqueryTable . insert ( rows ) ; } } export async function postFeatureMetrics ( table , report ) { const featureRow = { serverId : report . serverId , serverVersion : report . serverVersion , timestamp : new Date ( report . timestampUtcMs ) . toISOString ( ) , dataLimit : report . dataLimit } ; return table . insert ( [ featureRow ] ) ; } export function isValidFeatureMetricsReport ( obj ) { if ( ! obj ) { return false ; } const requiredFeatureMetricsReportFields = [ 'serverId' , 'serverVersion' , 'timestampUtcMs' , 'dataLimit' ] ; for ( const fieldName of requiredFeatureMetricsReportFields ) { if ( ! obj [ fieldName ] ) { return false ; } } if ( typeof obj . serverId !== 'string' || typeof obj . serverVersion !== 'string' || typeof obj . timestampUtcMs !== 'number' ) { return false ; } if ( typeof obj . dataLimit . enabled !== 'boolean' ) { return false ; } const perKeyLimitCount = obj . dataLimit . perKeyLimitCount ; if ( perKeyLimitCount === undefined ) { return true ; } if ( typeof perKeyLimitCount === 'number' ) { return obj . dataLimit . perKeyLimitCount >= 0 ; } return false ; }