-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infrastructure UI] Create MetricsExplorerViewsService and MetricsExp…
…lorerViewsClient (#155878) ## 📓 Summary Depends on #154900 Closes #155112 This PR implements the `InventoryViewsService` and `InventoryViewsClient`, injecting an instance of the client in the KibanaContextForPlugin and exposing so a set of utilities to retrieve/update inventory views: - `findMetricsExplorerViews` - `getMetricsExplorerView` - `createMetricsExplorerView` - `updateMetricsExplorerView` - `deleteMetricsExplorerView` ## 👣 Next steps - Implement #154725 to consume the service --------- Co-authored-by: Marco Antonio Ghiani <[email protected]> Co-authored-by: Carlos Crespo <[email protected]>
- Loading branch information
1 parent
bf64874
commit 5422d08
Showing
11 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/infra/common/metrics_explorer_views/errors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/* eslint-disable max-classes-per-file */ | ||
|
||
export class FetchMetricsExplorerViewError extends Error { | ||
constructor(message: string, public cause?: Error) { | ||
super(message); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
this.name = 'FetchMetricsExplorerViewError'; | ||
} | ||
} | ||
|
||
export class UpsertMetricsExplorerViewError extends Error { | ||
constructor(message: string, public cause?: Error) { | ||
super(message); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
this.name = 'UpsertMetricsExplorerViewError'; | ||
} | ||
} | ||
|
||
export class DeleteMetricsExplorerViewError extends Error { | ||
constructor(message: string, public cause?: Error) { | ||
super(message); | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
this.name = 'DeleteMetricsExplorerViewError'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
*/ | ||
|
||
export * from './defaults'; | ||
export * from './errors'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/infra/public/services/metrics_explorer_views/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './metrics_explorer_views_client'; | ||
export * from './metrics_explorer_views_service'; | ||
export * from './types'; |
17 changes: 17 additions & 0 deletions
17
...lugins/infra/public/services/metrics_explorer_views/metrics_explorer_views_client.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { IMetricsExplorerViewsClient } from './types'; | ||
|
||
export const createMetricsExplorerViewsClientMock = | ||
(): jest.Mocked<IMetricsExplorerViewsClient> => ({ | ||
findMetricsExplorerViews: jest.fn(), | ||
getMetricsExplorerView: jest.fn(), | ||
createMetricsExplorerView: jest.fn(), | ||
updateMetricsExplorerView: jest.fn(), | ||
deleteMetricsExplorerView: jest.fn(), | ||
}); |
130 changes: 130 additions & 0 deletions
130
x-pack/plugins/infra/public/services/metrics_explorer_views/metrics_explorer_views_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { HttpStart } from '@kbn/core/public'; | ||
import { | ||
CreateMetricsExplorerViewAttributesRequestPayload, | ||
createMetricsExplorerViewRequestPayloadRT, | ||
findMetricsExplorerViewResponsePayloadRT, | ||
getMetricsExplorerViewUrl, | ||
metricsExplorerViewResponsePayloadRT, | ||
UpdateMetricsExplorerViewAttributesRequestPayload, | ||
} from '../../../common/http_api/latest'; | ||
import { | ||
DeleteMetricsExplorerViewError, | ||
FetchMetricsExplorerViewError, | ||
MetricsExplorerView, | ||
UpsertMetricsExplorerViewError, | ||
} from '../../../common/metrics_explorer_views'; | ||
import { decodeOrThrow } from '../../../common/runtime_types'; | ||
import { IMetricsExplorerViewsClient } from './types'; | ||
|
||
export class MetricsExplorerViewsClient implements IMetricsExplorerViewsClient { | ||
constructor(private readonly http: HttpStart) {} | ||
|
||
async findMetricsExplorerViews(): Promise<MetricsExplorerView[]> { | ||
const response = await this.http.get(getMetricsExplorerViewUrl()).catch((error) => { | ||
throw new FetchMetricsExplorerViewError(`Failed to fetch metrics explorer views: ${error}`); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
findMetricsExplorerViewResponsePayloadRT, | ||
(message: string) => | ||
new FetchMetricsExplorerViewError(`Failed to decode metrics explorer views: ${message}"`) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
async getMetricsExplorerView(metricsExplorerViewId: string): Promise<MetricsExplorerView> { | ||
const response = await this.http | ||
.get(getMetricsExplorerViewUrl(metricsExplorerViewId)) | ||
.catch((error) => { | ||
throw new FetchMetricsExplorerViewError( | ||
`Failed to fetch metrics explorer view "${metricsExplorerViewId}": ${error}` | ||
); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
metricsExplorerViewResponsePayloadRT, | ||
(message: string) => | ||
new FetchMetricsExplorerViewError( | ||
`Failed to decode metrics explorer view "${metricsExplorerViewId}": ${message}"` | ||
) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
async createMetricsExplorerView( | ||
metricsExplorerViewAttributes: CreateMetricsExplorerViewAttributesRequestPayload | ||
): Promise<MetricsExplorerView> { | ||
const response = await this.http | ||
.post(getMetricsExplorerViewUrl(), { | ||
body: JSON.stringify( | ||
createMetricsExplorerViewRequestPayloadRT.encode({ | ||
attributes: metricsExplorerViewAttributes, | ||
}) | ||
), | ||
}) | ||
.catch((error) => { | ||
throw new UpsertMetricsExplorerViewError( | ||
`Failed to create new metrics explorer view: ${error}` | ||
); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
metricsExplorerViewResponsePayloadRT, | ||
(message: string) => | ||
new UpsertMetricsExplorerViewError( | ||
`Failed to decode newly written metrics explorer view: ${message}"` | ||
) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
async updateMetricsExplorerView( | ||
metricsExplorerViewId: string, | ||
metricsExplorerViewAttributes: UpdateMetricsExplorerViewAttributesRequestPayload | ||
): Promise<MetricsExplorerView> { | ||
const response = await this.http | ||
.put(getMetricsExplorerViewUrl(metricsExplorerViewId), { | ||
body: JSON.stringify( | ||
createMetricsExplorerViewRequestPayloadRT.encode({ | ||
attributes: metricsExplorerViewAttributes, | ||
}) | ||
), | ||
}) | ||
.catch((error) => { | ||
throw new UpsertMetricsExplorerViewError( | ||
`Failed to update metrics explorer view "${metricsExplorerViewId}": ${error}` | ||
); | ||
}); | ||
|
||
const { data } = decodeOrThrow( | ||
metricsExplorerViewResponsePayloadRT, | ||
(message: string) => | ||
new UpsertMetricsExplorerViewError( | ||
`Failed to decode updated metrics explorer view "${metricsExplorerViewId}": ${message}"` | ||
) | ||
)(response); | ||
|
||
return data; | ||
} | ||
|
||
deleteMetricsExplorerView(metricsExplorerViewId: string): Promise<null> { | ||
return this.http | ||
.delete(getMetricsExplorerViewUrl(metricsExplorerViewId)) | ||
.then(() => null) | ||
.catch((error) => { | ||
throw new DeleteMetricsExplorerViewError( | ||
`Failed to delete metrics explorer view "${metricsExplorerViewId}": ${error}` | ||
); | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ugins/infra/public/services/metrics_explorer_views/metrics_explorer_views_service.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { createMetricsExplorerViewsClientMock } from './metrics_explorer_views_client.mock'; | ||
import { MetricsExplorerViewsServiceStart } from './types'; | ||
|
||
export const createMetricsExplorerViewsServiceStartMock = () => ({ | ||
client: createMetricsExplorerViewsClientMock(), | ||
}); | ||
|
||
export const _ensureTypeCompatibility = (): MetricsExplorerViewsServiceStart => | ||
createMetricsExplorerViewsServiceStartMock(); |
25 changes: 25 additions & 0 deletions
25
...ck/plugins/infra/public/services/metrics_explorer_views/metrics_explorer_views_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { MetricsExplorerViewsClient } from './metrics_explorer_views_client'; | ||
import { | ||
MetricsExplorerViewsServiceStartDeps, | ||
MetricsExplorerViewsServiceSetup, | ||
MetricsExplorerViewsServiceStart, | ||
} from './types'; | ||
|
||
export class MetricsExplorerViewsService { | ||
public setup(): MetricsExplorerViewsServiceSetup {} | ||
|
||
public start({ http }: MetricsExplorerViewsServiceStartDeps): MetricsExplorerViewsServiceStart { | ||
const client = new MetricsExplorerViewsClient(http); | ||
|
||
return { | ||
client, | ||
}; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
x-pack/plugins/infra/public/services/metrics_explorer_views/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { HttpStart } from '@kbn/core/public'; | ||
import { | ||
MetricsExplorerView, | ||
MetricsExplorerViewAttributes, | ||
} from '../../../common/metrics_explorer_views'; | ||
|
||
export type MetricsExplorerViewsServiceSetup = void; | ||
|
||
export interface MetricsExplorerViewsServiceStart { | ||
client: IMetricsExplorerViewsClient; | ||
} | ||
|
||
export interface MetricsExplorerViewsServiceStartDeps { | ||
http: HttpStart; | ||
} | ||
|
||
export interface IMetricsExplorerViewsClient { | ||
findMetricsExplorerViews(): Promise<MetricsExplorerView[]>; | ||
getMetricsExplorerView(metricsExplorerViewId: string): Promise<MetricsExplorerView>; | ||
createMetricsExplorerView( | ||
metricsExplorerViewAttributes: Partial<MetricsExplorerViewAttributes> | ||
): Promise<MetricsExplorerView>; | ||
updateMetricsExplorerView( | ||
metricsExplorerViewId: string, | ||
metricsExplorerViewAttributes: Partial<MetricsExplorerViewAttributes> | ||
): Promise<MetricsExplorerView>; | ||
deleteMetricsExplorerView(metricsExplorerViewId: string): Promise<null>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters