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

Serve the execution results through an endpoint #40

Merged
merged 8 commits into from
Oct 5, 2022

Conversation

dottorblaster
Copy link
Contributor

As above, we're exposing an API to look into historical execution results.

@dottorblaster dottorblaster added the enhancement New feature or request label Oct 3, 2022
@dottorblaster dottorblaster self-assigned this Oct 3, 2022
@dottorblaster dottorblaster force-pushed the endpoint-results branch 3 times, most recently from 04a42f4 to 6832695 Compare October 3, 2022 15:51
@dottorblaster dottorblaster marked this pull request as ready for review October 3, 2022 15:52
Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

So far so good! I have noted some comments.
Could we add controller tests for the new endpoint?

lib/wanda_web/api_spec.ex Show resolved Hide resolved
items_per_page = Map.get(params, :items_per_page, 10)
group_id = Map.get(params, :group_id)

results = Results.get_execution_results(group_id, page, items_per_page)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we directly send the database representation of the result in the data field here?
I would expect some sort of transformation/mapping to send something more domain related.
I would maybe convert the ExecutionResult (which is the database mode) to the Result struct or something new. Something that we really need in the frontend, tailored for it.

Copy link
Member

Choose a reason for hiding this comment

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

we have views for that, i think we can iterate adding the views in a new PR.

https://hexdocs.pm/phoenix/views.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@arbulu89 I'm super glad you noticed that, yeah it's something we have to do but I'd like to do that in a follow-up PR because I was afraid this PR was getting really huge

Copy link
Contributor

Choose a reason for hiding this comment

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

@dottorblaster Fine for me them! We can follow up.

group_id = Map.get(params, :group_id)

results = Results.get_execution_results(group_id, page, items_per_page)
json(conn, %{page: page, items_per_page: items_per_page, data: results})
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really understand why we send back the page and item_per_page. They are coming directly from the request, so the user already know them.

Copy link
Member

Choose a reason for hiding this comment

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

+1 to remove them and maybe iterate to add the total count.
See for instance GH APi: https://api.github.com/search/repositories?q=tetris+language:assembly&sort=stars&order=desc&page=1&per_page=10

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we take inspiration from HTTP 206 (RFC 9110)?

I like how it proposed to send data on the HTTP header about the position of the served content, see here

Copy link
Contributor

Choose a reason for hiding this comment

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

Not suggesting do it on the header, just that I like the way it describes the range and total count

lib/wanda/results.ex Outdated Show resolved Hide resolved
Copy link
Member

@fabriziosestito fabriziosestito left a comment

Choose a reason for hiding this comment

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

Some comments to be addressed and controller tests are missing, we need to test that the return json is in the format we specified in the open API spex.

Refer to this: https://github.com/open-api-spex/open_api_spex#validate-responses

We could use factory to generate a list of results.

@@ -0,0 +1,24 @@
defmodule WandaWeb.ApiSpec do
Copy link
Member

Choose a reason for hiding this comment

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

In trento web we faced a similar situation in relation to apis/openapi/openapispex, see here

Let's consider If there's some valuable learning we can bring here - even on what not to do 😅

description: "A single execution of checks",
type: :object,
properties: %{
execution_id: %Schema{type: :string, description: "Execution ID"},
Copy link
Member

Choose a reason for hiding this comment

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

We could add a format: :uuid to execution_id and group_id, for a friendlier representation on the api doc.

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 tried but I got an error, i googled a lot but I didn't find a solution that wasn't "create your own format with a huge regex"

Copy link
Member

Choose a reason for hiding this comment

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

Not talking about validation, no need for regex 😄

Just really a silly thing like we do here for example https://github.com/trento-project/web/blob/main/lib/trento_web/openapi/schema/host.ex#L37

%Schema{type: :string, description: "Execution ID", format: :uuid},

It would take care of giving that information in the swagger ui and it generate uuid samples.
If it is causing troubles feel free to skip.

properties: %{
execution_id: %Schema{type: :string, description: "Execution ID"},
group_id: %Schema{type: :string, description: "Group ID"},
payload: %Schema{type: :object, description: "Payload of the check execution"}
Copy link
Member

Choose a reason for hiding this comment

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

Probably you already thought of this.

What is the plan about the shape of the payload object?
As an API consumer, even if at the moment is just us 😄, I would find it useful to know that shape.

Copy link
Contributor Author

@dottorblaster dottorblaster Oct 4, 2022

Choose a reason for hiding this comment

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

Does it have a fixed shape? I thought it was something that can really vary, therefore I didn't want to get stuck writing that schema right now, but it's material for a followup for sure

Copy link
Member

Choose a reason for hiding this comment

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

Well, it does have a shape, and that shape is Wanda.Execution.Result without :execution_id and :group_id.

Does it have a fixed shape? I thought it was something that can really vary

Until we change it 😄

The point to me is more
how stable do we think it is in relation to the needs?
What significant changes do we foresee to that representation in the upcoming iterations?

However, I understand it might be a tedious job, moreover if things change so fast that updating the documentation becomes a burden, yet if that happens there might be another kind of issue to tackle (coupling for instance, but let's leave it out)

Bottom line: if not planned to add that missing piece in this or following PRs, then I'd find it wise to track debt.
How does it sound?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I collected a followup task to better define that shape

Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

Hey @dottorblaster ,
It looks good to me now. I have added some other minor comments. But feel free to omit them.

lib/wanda_web/controllers/executions_controller.ex Outdated Show resolved Hide resolved
description: "Check execution listing response",
type: :object,
properties: %{
page: %Schema{type: :string, description: "Current page"},
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this got outdated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn you're right, thanks

Copy link
Member

@fabriziosestito fabriziosestito left a comment

Choose a reason for hiding this comment

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

LGTM, please track the debt and the leftovers

@dottorblaster dottorblaster deleted the endpoint-results branch October 5, 2022 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

5 participants