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

MF-916 - Fix Things and Channels counters #947

Merged
merged 9 commits into from
Nov 13, 2019
Merged

MF-916 - Fix Things and Channels counters #947

merged 9 commits into from
Nov 13, 2019

Conversation

manuio
Copy link
Contributor

@manuio manuio commented Nov 12, 2019

Signed-off-by: Manuel Imperiale manuel.imperiale@gmail.com

Closes #916

@manuio manuio requested a review from a team as a code owner November 12, 2019 00:16
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
@codecov-io
Copy link

codecov-io commented Nov 12, 2019

Codecov Report

Merging #947 into master will decrease coverage by 0.44%.
The diff coverage is 52.94%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #947      +/-   ##
==========================================
- Coverage   84.07%   83.63%   -0.45%     
==========================================
  Files          75       75              
  Lines        5383     5248     -135     
==========================================
- Hits         4526     4389     -137     
- Misses        588      590       +2     
  Partials      269      269
Impacted Files Coverage Δ
things/postgres/things.go 79.8% <50%> (-2.62%) ⬇️
things/postgres/channels.go 79.76% <53.84%> (-6.67%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67d5188...22ebbb4. Read the comment docs.

return things.ChannelsPage{}, err
switch metadata {
case nil:
cq = fmt.Sprintf("%s %s", cq, ";")
Copy link
Contributor

Choose a reason for hiding this comment

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

You can write it like this: cq = fmt.Sprintf("%s;", cq)

return things.ThingsPage{}, err
switch metadata {
case nil:
cq = fmt.Sprintf("%s %s", cq, ";")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, you can write it as cq = fmt.Sprintf("%s;", cq)

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
blokovi
blokovi previously approved these changes Nov 12, 2019
switch name {
case "":
if err := cr.db.GetContext(ctx, &total, q, owner); err != nil {
return things.ChannelsPage{}, err
switch metadata {
Copy link
Contributor

Choose a reason for hiding this comment

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

this looks like lot of cases
cannot this be done like here
https://github.com/mainflux/mainflux/blob/22f1e851cd334f30c33f314eff76f6af23088425/things/postgres/channels.go#L125 using getNameQuery and getMetadataQuery
is total different than number of rows we get from the query above?

Copy link
Contributor Author

@manuio manuio Nov 12, 2019

Choose a reason for hiding this comment

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

@mteodor The first one returns you one page after to apply filters. The second one counts the total of Things with a certain filter in all pages.

Copy link
Contributor

Choose a reason for hiding this comment

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

and this would not work
cq := fmt.Sprintf(`SELECT COUNT(*) FROM things WHERE owner = $1 %s%s`, nq, mq) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
Channel string `db:"channel"`
Thing string `db:"thing"`
Owner string `db:"owner"`
func (cr channelRepository) total(ctx context.Context, query string, params map[string]interface{}) (uint64, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you create single helper function for this? (maybe this method can receive db as parameter as well)

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
q = fmt.Sprintf(`SELECT COUNT(*) FROM channels WHERE owner = $1 %s;`, cq)

total := uint64(0)
cq := `SELECT COUNT(*) FROM channels WHERE owner = :owner`
Copy link
Contributor

@mteodor mteodor Nov 13, 2019

Choose a reason for hiding this comment

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

I would do it similar like above since we alread have subqueries for metadata and name

cq := fmt.Sprintf(`SELECT COUNT(*) FROM channels WHERE owner = :owner %s%s`, nq, mq )

and no need for case at all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right! I would even say that we don't need to do this checks since this name and metadata queries are returned by getNameQuery and getMetadatQuery

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
@@ -418,7 +412,7 @@ func getNameQuery(name string) (string, string) {
nq := ""
if name != "" {
name = fmt.Sprintf(`%%%s%%`, name)
nq = ` AND LOWER(name) LIKE :name`
nq = ` AND name LIKE :name`
Copy link
Contributor

Choose a reason for hiding this comment

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

actually we need LOWER here, sorry

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
Copy link
Contributor

@mteodor mteodor left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@anovakovic01 anovakovic01 left a comment

Choose a reason for hiding this comment

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

LGTM!

@manuio manuio merged commit afeec81 into absmach:master Nov 13, 2019
@manuio manuio deleted the things branch November 13, 2019 14:27
manuio added a commit that referenced this pull request Oct 12, 2020
* MF-916 - Fix Things and Channels counters

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use NamedQueryContext to count rows

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Create single helper total() func

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rm useless LOWER

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Revert previous commit

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
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.

Total Things or Channels are wrong if filtered by metadata
5 participants