Skip to content

Commit

Permalink
[ci-stats] add Client class for accessing test group stats (#125164)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Spencer and kibanamachine authored Feb 14, 2022
1 parent f4713f5 commit 48e8a84
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 11 deletions.
89 changes: 89 additions & 0 deletions packages/kbn-dev-utils/src/ci_stats_reporter/ci_stats_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Axios from 'axios';
import { ToolingLog } from '../tooling_log';

import { parseConfig, Config } from './ci_stats_config';
import { CiStatsMetadata } from './ci_stats_metadata';

interface LatestTestGroupStatsOptions {
/** The Kibana branch to get stats for, eg "main" */
branch: string;
/** The CI job names to filter builds by, eg "kibana-hourly" */
ciJobNames: string[];
/** Filter test groups by group type */
testGroupType?: string;
}

interface CompleteSuccessBuildSource {
jobName: string;
jobRunner: string;
completedAt: string;
commit: string;
startedAt: string;
branch: string;
result: 'SUCCESS';
jobId: string;
targetBranch: string | null;
fromKibanaCiProduction: boolean;
requiresValidMetrics: boolean | null;
jobUrl: string;
mergeBase: string | null;
}

interface TestGroupSource {
'@timestamp': string;
buildId: string;
name: string;
type: string;
startTime: string;
durationMs: number;
meta: CiStatsMetadata;
}

interface LatestTestGroupStatsResp {
build: CompleteSuccessBuildSource & { id: string };
testGroups: Array<TestGroupSource & { id: string }>;
}

export class CiStatsClient {
/**
* Create a CiStatsReporter by inspecting the ENV for the necessary config
*/
static fromEnv(log: ToolingLog) {
return new CiStatsClient(parseConfig(log));
}

constructor(private readonly config?: Config) {}

isEnabled() {
return !!this.config?.apiToken;
}

async getLatestTestGroupStats(options: LatestTestGroupStatsOptions) {
if (!this.config || !this.config.apiToken) {
throw new Error('No ciStats config available, call `isEnabled()` before using the client');
}

const resp = await Axios.request<LatestTestGroupStatsResp>({
baseURL: 'https://ci-stats.kibana.dev',
url: '/v1/test_group_stats',
params: {
branch: options.branch,
ci_job_name: options.ciJobNames.join(','),
test_group_type: options.testGroupType,
},
headers: {
Authentication: `token ${this.config.apiToken}`,
},
});

return resp.data;
}
}
16 changes: 16 additions & 0 deletions packages/kbn-dev-utils/src/ci_stats_reporter/ci_stats_metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

/** Container for metadata that can be attached to different ci-stats objects */
export interface CiStatsMetadata {
/**
* Arbitrary key-value pairs which can be attached to CiStatsTiming and CiStatsMetric
* objects stored in the ci-stats service
*/
[key: string]: string | string[] | number | boolean | undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@ import httpAdapter from 'axios/lib/adapters/http';
import { ToolingLog } from '../tooling_log';
import { parseConfig, Config } from './ci_stats_config';
import type { CiStatsTestGroupInfo, CiStatsTestRun } from './ci_stats_test_group_types';
import { CiStatsMetadata } from './ci_stats_metadata';

const BASE_URL = 'https://ci-stats.kibana.dev';

/** Container for metadata that can be attached to different ci-stats objects */
export interface CiStatsMetadata {
/**
* Arbitrary key-value pairs which can be attached to CiStatsTiming and CiStatsMetric
* objects stored in the ci-stats service
*/
[key: string]: string | string[] | number | boolean | undefined;
}

/** A ci-stats metric record */
export interface CiStatsMetric {
/** Top-level categorization for the metric, e.g. "page load bundle size" */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { CiStatsMetadata } from './ci_stats_reporter';
import type { CiStatsMetadata } from './ci_stats_metadata';

export type CiStatsTestResult = 'fail' | 'pass' | 'skip';
export type CiStatsTestType =
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-dev-utils/src/ci_stats_reporter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export type { Config } from './ci_stats_config';
export * from './ship_ci_stats_cli';
export { getTimeReporter } from './report_time';
export * from './ci_stats_test_group_types';
export * from './ci_stats_client';
2 changes: 1 addition & 1 deletion packages/kbn-pm/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9049,7 +9049,7 @@ var _ci_stats_config = __webpack_require__(218);
*/
// @ts-expect-error not "public", but necessary to prevent Jest shimming from breaking things
const BASE_URL = 'https://ci-stats.kibana.dev';
/** Container for metadata that can be attached to different ci-stats objects */
/** A ci-stats metric record */

/** Object that helps report data to the ci-stats service */
class CiStatsReporter {
Expand Down

0 comments on commit 48e8a84

Please sign in to comment.