Skip to content

Conversation

@ecrupper
Copy link
Contributor

Adding before and after query parameters which take in unix epoch timestamps and fetches builds within those time constraints (if provided). Closes issue 187.

@ecrupper ecrupper requested a review from a team as a code owner February 15, 2022 06:35
@ecrupper ecrupper self-assigned this Feb 15, 2022
@codecov
Copy link

codecov bot commented Feb 15, 2022

Codecov Report

Merging #586 (98a0527) into master (3fc4394) will decrease coverage by 0.06%.
The diff coverage is 21.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #586      +/-   ##
==========================================
- Coverage   54.38%   54.32%   -0.07%     
==========================================
  Files         181      181              
  Lines       15152    15177      +25     
==========================================
+ Hits         8241     8245       +4     
- Misses       6594     6615      +21     
  Partials      317      317              
Impacted Files Coverage Δ
api/build.go 1.82% <0.00%> (-0.04%) ⬇️
database/postgres/build_list.go 91.17% <100.00%> (+0.13%) ⬆️
database/sqlite/build_list.go 91.24% <100.00%> (+0.12%) ⬆️

@kneal
Copy link
Contributor

kneal commented Feb 15, 2022

Saw comment about offline conversation, however, would it make more sense to just expose the created, started, finished, etc in query params, then the user can just pick the ranges for the specific timestamp they are interested in.

before and after is easy to read but it is somewhat limiting but again I'm not sure what was discussed in that offline conversation.

@ecrupper
Copy link
Contributor Author

ecrupper commented Feb 15, 2022

I'm not sure what the offline conversation was either since it was a year ago, but I chose started because it seemed the most reliable of the timestamps. If we exposed all the timestamp fields, we would still need a way to utilize some sort of before and after capability I would imagine. I'm not sure if this is what we would want, but maybe we could utilize QueryArray in some fashion.

@kneal
Copy link
Contributor

kneal commented Feb 15, 2022

Good point, yeah, might be better to go with this design and expand when needed then. Unless @wass3r remembers the other context, since he mentioned the offline conversation.

@wass3r
Copy link
Collaborator

wass3r commented Feb 15, 2022

For additional context, the pagination was talked about in the offline conversation, so nothing in regards to the time range selection - at least that i can recall.

I'm personally ok with the before and after or from to to approach and have it limited to created. Like Neal said, should be easy to expand upon if needed.

dtanner
dtanner previously approved these changes Feb 17, 2022
// capture before query parameter if present, default to now
//
// nolint: gomnd, lll // ignore magic number and long line length
before, err := strconv.ParseInt(c.DefaultQuery("before", strconv.FormatInt(time.Now().UTC().Unix(), 10)), 10, 64)
Copy link
Contributor

Choose a reason for hiding this comment

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

question - what are the last two arguments?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thats the base (10) and the size in bits (64):

https://pkg.go.dev/strconv#ParseInt

err = c.Postgres.
Table(constants.TableBuild).
Where("repo_id = ?", r.GetID()).
Where("started < ?", before).
Copy link
Contributor

Choose a reason for hiding this comment

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

Something to check: Can you look at this query out in production to see how it behaves from a performance and cost perspective? It's probably ok assuming there's an index on repo_id which would narrow the rows down a lot, but since there's not an index on started, I'm curious how fast those queries and what their cost is.

I'm assuming you know how to grab the generated query.? And then you could throw that in a query tool and run explain analyze $your_query. That'll show how long it takes and its cost. It's probably fine, especially if people don't use this often, but as the database grows over time, this kind of stuff can add up and need to be considered for database tuning and maintenance.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably a good thing to explore 👍

FWIW:

We help minimize the DB query cost on list queries due to the LIMIT condition which comes from pagination:

Limit(perPage).

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that performance should be explored and validated. A LIMIT doesn't do much if it's paired up with an ORDER statement since the DB will still have to do a full sort on that ORDER column before it can even consider LIMITing the return rows. If you didn't have the order, then a limit on its own would probably be fine, but we have them both together.

https://dba.stackexchange.com/questions/110636/poor-performance-on-query-with-limit-when-i-add-an-order-by

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We indeed have repo_id as an index in the DB. I ran a few test queries as well as the explain analyse tool. It seems like the query is fairly performant. I got results that varied from a couple tenths of a millisecond to two milliseconds.

Copy link
Contributor

@kneal kneal Feb 17, 2022

Choose a reason for hiding this comment

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

It's probably worth calling out the indexes are visible from the application code or at least the default indexes. An admin could add more outside this code.

).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0).
AddRow(2, 1, 2, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 1, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0).
AddRow(2, 1, 2, 0, "", "", "", 0, 0, 2, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

nothing to do here right now, but a comment...this many params is hard to understand and prone to errors. named arguments would be better, or a type passed that contains the parameters. something to think about for future development.

Copy link
Contributor

Choose a reason for hiding this comment

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

Named params isn't quite a thing in Go, you can pass a struct or call a function on a struct to access it's fields but in this particular instance we are handicapped by the library. If you look up the NewRow in godoc you'll see it takes that array of strings to define what the columns are and then AddRow takes an array of a custom type which narrows down it down to primitives.

Copy link
Contributor

@jbrockopp jbrockopp left a comment

Choose a reason for hiding this comment

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

The code LGTM but had a question on created vs started.

@jbrockopp jbrockopp added the enhancement Indicates an improvement to a feature label Feb 21, 2022
Copy link
Contributor

@jbrockopp jbrockopp left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Indicates an improvement to a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API : GET builds by amount or time window

7 participants