Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.1] [ci-stats] add Client class for accessing test group stats (#125164) #125587

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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