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

New generic metrics API endpoint #265

Merged
merged 9 commits into from
Dec 1, 2020
Merged

New generic metrics API endpoint #265

merged 9 commits into from
Dec 1, 2020

Conversation

macfarlandian
Copy link
Collaborator

@macfarlandian macfarlandian commented Nov 30, 2020

Description of the change

Extends the existing Spotlight backend to support the next-gen Spotlight client, which will request metric files arbitrarily by name rather than in pre-defined groups.

To do this I added some tests for the existing API endpoints and then refactored them to decouple the fetch action from the hard-coded metric groups so the base fetching logic can be reused. The new endpoint validates the requested files against its list of known metrics before initiating a fetch.

The new endpoint required a different caching strategy (per file instead of per group). This does technically mean the ND files would be cached twice, but since the total data volume is only on the order of a few MB I don't think that's a big concern in practice.

Also, now that there are tests for the spotlight-api package, I added them to the CI workflow and coverage reports. This shouldn't shake up the overall coverage metrics very much because API test coverage is actually pretty good just from these few tests that I wrote. (For testing I factored out the Express configuration from the code that starts the server, so that I could test the Express API surface with Supertest without spawning a bunch of dangling server processes that Jest objected to. This seemed like it would be more straightforward than coming up with a server teardown process for the test environment.)

You will also see some ad-hoc type hints in JSDoc comments. These are supported by, e.g., the native Typescript integration in VS Code, so I added some where it helped me reason about what I was doing, but they don't actually do anything at compile time. Nice way to bridge the gap from JS though!

Finally, note that the surface of this new endpoint somewhat resembles the designs proposed for a consolidated metrics service, though this only covers a fraction of that scope. The most significant aspects of that proposal adopted here are probably these:

  • the new endpoint is POST-only, keeping implementation detail out of the URL
  • I called the new endpoint "public," implying that its counterpart "private" may also exist someday (which ... might be true). This looks a little weird alongside the other endpoint names but I do think it makes sense from a forward-looking perspective.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Configuration change (adjusts configuration to achieve some end related to functionality, development, performance, or security)

Related issues

Part of #261

Checklists

Development

These boxes should be checked by the submitter prior to merging:

  • Manual testing against realistic data has been performed locally

Code review

These boxes should be checked by reviewers prior to merging:

  • This pull request has a descriptive title and information useful to a reviewer
  • This pull request has been moved out of a Draft state, has no "Work In Progress" label, and has assigned reviewers
  • Potential security implications or infrastructural changes have been considered, if relevant

@coveralls
Copy link

coveralls commented Nov 30, 2020

Pull Request Test Coverage Report for Build 392855445

  • 66 of 68 (97.06%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+2.7%) to 57.574%

Changes Missing Coverage Covered Lines Changed/Added Lines %
spotlight-api/app.js 22 23 95.65%
spotlight-api/core/metricsApi.js 33 34 97.06%
Totals Coverage Status
Change from base Build 369017266: 2.7%
Covered Lines: 998
Relevant Lines: 1695

💛 - Coveralls

Copy link
Member

@jessex jessex left a comment

Choose a reason for hiding this comment

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

Well done, @macfarlandian! No major notes here, just a nitpick request.

I feel neutral about the type hints given that they have no code-time impact and are novel to the rest of the codebase, but let's see if they pick up some momentum.

spotlight-api/core/metricsApi.js Outdated Show resolved Hide resolved
Copy link

@daschi daschi left a comment

Choose a reason for hiding this comment

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

yay for tests! also thought the type hints were helpful

@macfarlandian macfarlandian merged commit 72f63cd into master Dec 1, 2020
@macfarlandian macfarlandian deleted the ian/261-new-api branch December 1, 2020 17:42
macfarlandian added a commit that referenced this pull request Dec 1, 2020
Copy link

@jovergaag jovergaag left a comment

Choose a reason for hiding this comment

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

I'm a little late to the game here but wanted to read through this anyway. Great tests!

Are you hoping to migrate the API to TypeScript at some point or leave it as is?

@@ -32,7 +32,7 @@ const isDemoMode = demoMode.isDemoMode();
function responder(res) {
return function respond(err, data) {
if (err) {
res.send(err);
res.status(500).json({ error: err.message });

Choose a reason for hiding this comment

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

Great catch. I've had an issue open in pulse dashboard that I haven't had time to look into yet, to figure out why fetch errors were not raising an error properly. You found it. 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah we don't have much in the way of error handling in this app but if we did .... this would have been a problem, it just sends a blob of HTML with an error message in it or something like that

@macfarlandian
Copy link
Collaborator Author

@jovergaag maybe eventually but it's not a big priority at the moment. If there were some major advantage to be gained like sharing type definitions across packages, etc., then I would be more keen on it, but so far nothing like that has really presented itself. With the specter of a new consolidated metrics service hanging over this I don't want to invest any more time in it than is absolutely necessary for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants