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

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 48e8a84c8c)

Co-authored-by: Spencer <spencer@elastic.co>
This commit is contained in:
Kibana Machine 2022-02-14 17:54:31 -05:00 committed by GitHub
parent 664b0e98f0
commit b4abacdbd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 11 deletions

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -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" */

View file

@ -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 =

View file

@ -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';

View file

@ -9051,7 +9051,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 {