import { wrapError } from '../client/error_wrapper' ; import { RouteInitialization } from '../types' ; import { anomaliesTableDataSchema , categoryDefinitionSchema , categoryExamplesSchema , getDatafeedResultsChartDataSchema , maxAnomalyScoreSchema , partitionFieldValuesSchema , anomalySearchSchema , } from './schemas/results_service_schema' ; import { resultsServiceProvider } from '../models/results_service' ; import { jobIdSchema } from './schemas/anomaly_detectors_schema' ; import { getCategorizerStatsSchema , getCategorizerStoppedPartitionsSchema , } from './schemas/results_service_schema' ; import type { MlClient } from '../lib/ml_client' ; function getAnomaliesTableData ( mlClient , payload ) { const rs = resultsServiceProvider ( mlClient ) ; const { jobIds , criteriaFields , influencers , aggregationInterval , threshold , earliestMs , latestMs , dateFormatTz , maxRecords , maxExamples , influencersFilterQuery , functionDescription , } = payload ; return rs . getAnomaliesTableData ( jobIds , criteriaFields , influencers , aggregationInterval , threshold , earliestMs , latestMs , dateFormatTz , maxRecords , maxExamples , influencersFilterQuery , functionDescription ) ; } function getCategoryDefinition ( mlClient , payload ) { const rs = resultsServiceProvider ( mlClient ) ; return rs . getCategoryDefinition ( payload . jobId , payload . categoryId ) ; } function getCategoryExamples ( mlClient , payload ) { const rs = resultsServiceProvider ( mlClient ) ; const { jobId , categoryIds , maxExamples } = payload ; return rs . getCategoryExamples ( jobId , categoryIds , maxExamples ) ; } function getMaxAnomalyScore ( mlClient , payload ) { const rs = resultsServiceProvider ( mlClient ) ; const { jobIds , earliestMs , latestMs } = payload ; return rs . getMaxAnomalyScore ( jobIds , earliestMs , latestMs ) ; } function getPartitionFieldsValues ( mlClient , payload ) { const rs = resultsServiceProvider ( mlClient ) ; const { jobId , searchTerm , criteriaFields , earliestMs , latestMs , fieldsConfig } = payload ; return rs . getPartitionFieldsValues ( jobId , searchTerm , criteriaFields , earliestMs , latestMs , fieldsConfig ) ; } function getCategorizerStats ( mlClient , params , query ) { const { jobId } = params ; const { partitionByValue } = query ; const rs = resultsServiceProvider ( mlClient ) ; return rs . getCategorizerStats ( jobId , partitionByValue ) ; } function getCategoryStoppedPartitions ( mlClient , payload ) { const { jobIds , fieldToBucket } = payload ; const rs = resultsServiceProvider ( mlClient ) ; return rs . getCategoryStoppedPartitions ( jobIds , fieldToBucket ) ; } /**
 * Routes for results service
 */ export function resultsServiceRoutes ( { router , routeGuard } ) { apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/anomalies_table_data' , validate : { body : anomaliesTableDataSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getAnomaliesTableData ( mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/category_definition' , validate : { body : categoryDefinitionSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getCategoryDefinition ( mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/max_anomaly_score' , validate : { body : maxAnomalyScoreSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getMaxAnomalyScore ( mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/category_examples' , validate : { body : categoryExamplesSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getCategoryExamples ( mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/partition_fields_values' , validate : { body : partitionFieldValuesSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getPartitionFieldsValues ( mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/anomaly_search' , validate : { body : anomalySearchSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const { jobIds , query } = request . body ; const { body } = await mlClient . anomalySearch ( query , jobIds ) ; return response . ok ( { body , } ) ; } catch ( error ) { return response . customError ( wrapError ( error ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema apiSchema router . get ( { path : '/api/ml/results/{jobId}/categorizer_stats' , validate : { params : jobIdSchema , query : getCategorizerStatsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getCategorizerStats ( mlClient , request . params , request . query ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/category_stopped_partitions' , validate : { body : getCategorizerStoppedPartitionsSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { mlClient , request , response } ) => { try { const resp = await getCategoryStoppedPartitions ( mlClient , request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; apiGroup api apiName apiDescription apiSchema router . post ( { path : '/api/ml/results/datafeed_results_chart' , validate : { body : getDatafeedResultsChartDataSchema , } , options : { tags : [ 'access:ml:canGetJobs' ] , } , } , routeGuard . fullLicenseAPIGuard ( async ( { client , mlClient , request , response } ) => { try { const { getDatafeedResultsChartData } = resultsServiceProvider ( mlClient , client ) ; const resp = await getDatafeedResultsChartData ( request . body ) ; return response . ok ( { body : resp , } ) ; } catch ( e ) { return response . customError ( wrapError ( e ) ) ; } } ) ) ; }