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

[Infrastructure UI] Implement Metrics explorer views CRUD endpoints #155621

Merged

Conversation

tonyghiani
Copy link
Contributor

📓 Summary

Part of #152617
Closes #155111

This PR implements the CRUD endpoints for the metrics explorer views.
Following the approach used for the InventoryView service, it exposes a client that abstracts all the logic concerned to the metrics-explorer-view saved objects.

It also follows the guideline provided for Versioning interfaces and Versioning HTTP APIs, preparing for the serverless.

🤓 Tips for the reviewer

You can open the Kibana dev tools and play with the following snippet to test the create APIs, or you can perform the same requests with your preferred client:

// Get all
GET kbn:/api/infra/metrics_explorer_views

// Create one
POST kbn:/api/infra/metrics_explorer_views
{
  "attributes": {
    "name": "My view"
  }
}

// Get one
GET kbn:/api/infra/metrics_explorer_views/<switch-with-id>

// Update one
PUT kbn:/api/infra/metrics_explorer_views/<switch-with-id>
{
  "attributes": {
    "name": "My view 2"
  }
}

// Delete one
DELETE kbn:/api/infra/metrics_explorer_views/<switch-with-id>

@tonyghiani tonyghiani added Feature:Metrics UI Metrics UI feature Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services labels Apr 24, 2023
@apmmachine
Copy link
Contributor

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@tonyghiani tonyghiani marked this pull request as ready for review April 25, 2023 07:15
@tonyghiani tonyghiani requested a review from a team as a code owner April 25, 2023 07:15
@elasticmachine
Copy link
Contributor

Pinging @elastic/infra-monitoring-ui (Team:Infra Monitoring UI)

@tonyghiani tonyghiani added the release_note:skip Skip the PR/issue when compiling release notes label Apr 25, 2023
@crespocarlos crespocarlos self-requested a review April 25, 2023 13:04
@tonyghiani
Copy link
Contributor Author

@crespocarlos as this is an implementation mirroring the one done for the inventory-view saved object, you can find contextual comments useful for the review in the related PR.

Copy link
Contributor

@crespocarlos crespocarlos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! The CRUD works as expected. Loved how simple and easy to understand the endpoints are.

I just left a few minor comments and I missed 2 possible additional test cases that could be nice to have.

@@ -0,0 +1,29 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is similar to create_metrics_explorer_view.ts. I'm wondering if we could have types like create_or_update_explorer_view.ts types instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree they are the same, but since the request payloads don't strictly represent the same structure in future, I preferred to keep them split for isolated responsibilities.

},
async (_requestContext, request, response) => {
const { body } = request;
const { metricsExplorerViews } = (await getStartServices())[2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can completely ignore this. For me, it's easier to understand this:

const [, , { metricsExplorerViews }] = await getStartServices();

});
});

describe('.update', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a test here to validate PUT /api/infra/metrics_explorer_views/0 and DELETE /api/infra/metrics_explorer_views/0 to check the message when metrics explorer view with id is "0". Do you think it's worth it to have a test case for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests don't validate that piece of logic because the gate for the accepted values is at the endpoint level, when the parameters are validated, it shouldn't even reach the client if the value is not acceptable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah. indeed.

public async find(query: MetricsExplorerViewRequestQuery): Promise<MetricsExplorerView[]> {
this.logger.debug('Trying to load metrics explorer views ...');

const sourceId = query.sourceId ?? 'default';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default is repeated in the get and update functions. We could create a constant for this, wdyt?


if (defaultViewPosition !== -1) {
const element = views.splice(defaultViewPosition, 1)[0];
views.unshift(element);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: We could create a new array instead of mutating the views array passed as a parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense for immutability purposes, but as we already modified the original instance with .splice to remove the item, there shouldn't be any issue with this mutation.
I measure the performance impact and it's irrelevant in this case.

this.infraSources.getSourceConfiguration(this.savedObjectsClient, sourceId),
this.savedObjectsClient.find({
type: metricsExplorerViewSavedObjectName,
perPage: 1000, // Fetch 1 page by default with a max of 1000 results
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion:perPage: 1000 is also repeated in assertNameConflict. We could create a constant for the 1000.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will extract the all views retrieval into a method

}
```

## Update one: `PUT /api/infra/metrics_explorer_views/{metricsExplorerViewId}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to document the 400 returned when PUT /api/infra/metrics_explorer_views/0 is called?

}
```

## Delete one: `DELETE /api/infra/metrics_explorer_views/{metricsExplorerViewId}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to document the 400 returned when DELETE /api/infra/metrics_explorer_views/0 is called?

@tonyghiani
Copy link
Contributor Author

@crespocarlos thanks for the review, I addressed the comments where we could improve the implementation and updated respectively also the inventory views counterpart to keep them in sync.

Copy link
Contributor

@crespocarlos crespocarlos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for making the changes.

@tonyghiani
Copy link
Contributor Author

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
infra 1365 1371 +6

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
infra 43 44 +1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
infra 2.0MB 2.0MB +14.0B

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
infra 11 12 +1

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
infra 96.7KB 97.8KB +1.1KB
Unknown metric groups

API count

id before after diff
infra 46 47 +1

ESLint disabled line counts

id before after diff
enterpriseSearch 17 19 +2
securitySolution 399 402 +3
total +5

Total ESLint disabled count

id before after diff
enterpriseSearch 18 20 +2
securitySolution 479 482 +3
total +5

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@tonyghiani tonyghiani merged commit 61bb52c into elastic:main Apr 26, 2023
@tonyghiani tonyghiani deleted the 155111-create-metrics-explorer-views branch April 26, 2023 12:24
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Metrics UI Metrics UI feature release_note:skip Skip the PR/issue when compiling release notes Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Infrastructure UI] Create metric explorer views service and CRUD endpoints
6 participants