plots2 icon indicating copy to clipboard operation
plots2 copied to clipboard

Wrong wiki count in Dropdown by type

Open keshavsethi opened this issue 6 years ago • 35 comments

In https://publiclab.org/contributors/newsletter dropdown by type Wiki pages is showing 3 but on clicking it shows only 1 Image is shown below Screenshot from 2020-01-23 17-47-30

keshavsethi avatar Jan 23 '20 12:01 keshavsethi

If we click on by Views button then it shows all 3 but again if try to sort all 3 of them by title it again shows 1 newsletter

keshavsethi avatar Jan 23 '20 12:01 keshavsethi

Can I try fix this?

gabrielbaldao avatar Jan 24 '20 20:01 gabrielbaldao

yes, you can try this but before doing this get it reviewed by @SidharthBansal @nstjean @jywarren

keshavsethi avatar Jan 25 '20 08:01 keshavsethi

Sure. Please provide a fix

SidharthBansal avatar Jan 25 '20 14:01 SidharthBansal

@gabrielbaldao are you doing this or can I proceed?

NitinBhasneria avatar Feb 12 '20 12:02 NitinBhasneria

Go ahead

SidharthBansal avatar Feb 21 '20 09:02 SidharthBansal

Note we should make this match the code and tests here: https://github.com/publiclab/plots2/pull/7476

and the idea for a "single source of truth" here: https://github.com/publiclab/plots2/issues/6855#issuecomment-578331475

Thank you!

jywarren avatar Jun 16 '20 19:06 jywarren

And noting a more detailed investigation here -- I think we need to be sure all counts are from a unified body of code maintained in the models, and unit tested!

https://github.com/publiclab/plots2/issues/7965#issuecomment-644980326

jywarren avatar Jun 16 '20 20:06 jywarren

So we should confirm that all counts are coming from the respective Tag.count style methods. Key areas are here:

https://github.com/ckchu8/plots2/blob/0fbcc07bd61fd238d88779e49a3fbd2e300ef041/app/controllers/tag_controller.rb#L542-L561

And on tag cards, for example on https://publiclab.org/tags

jywarren avatar Jul 21 '20 21:07 jywarren

Just noting that the original issue at top may be a special case. I see in the DB that we're fetching these three pages:

  • https://publiclab.org/n/3928 https://publiclab.org/wiki/newsletter (Public Lab Community Newsletter)
  • https://publiclab.org/n/4965 https://publiclab.org/newsletter (Newsletter)
  • https://publiclab.org/wiki/subscribe

Only the middle one is the correct one. The first may be some kind of strange clone... not sure. From the early days of the site? And the last, doesn't look like a page anymore?

Here are the full records:

irb(main):005:0> Node.for_tagname_and_type('newsletter', 'page', wildcard: false).last => #<Node nid: 4965, vid: 18544, type: "page", language: "", title: "Newsletter", uid: 1, status: 1, created: 1353450804, changed: 1484959036, comment: 0, promote: 0, moderate: 0, sticky: 0, tnid: 0, translate: 0, cached_likes: 1, comments_count: 0, drupal_node_revisions_count: 5, path: "/wiki/newsletter", main_image_id: nil, slug: "newsletter", views: 1471, latitude: nil, longitude: nil, precision: nil>

irb(main):006:0> Node.for_tagname_and_type('newsletter', 'page', wildcard: false).first => #<Node nid: 308, vid: 20121, type: "page", language: "", title: "Subscribe", uid: 1, status: 1, created: 1306885186, changed: 1484959009, comment: 0, promote: 0, moderate: 0, sticky: 0, tnid: 0, translate: 0, cached_likes: 0, comments_count: 0, drupal_node_revisions_count: 13, path: "/subscribe", main_image_id: nil, slug: "subscribe", views: 25, latitude: nil, longitude: nil, precision: nil>

irb(main):007:0> Node.for_tagname_and_type('newsletter', 'page', wildcard: false)[1] => #<Node nid: 4965, vid: 18544, type: "page", language: "", title: "Newsletter", uid: 1, status: 1, created: 1353450804, changed: 1484959036, comment: 0, promote: 0, moderate: 0, sticky: 0, tnid: 0, translate: 0, cached_likes: 1, comments_count: 0, drupal_node_revisions_count: 5, path: "/wiki/newsletter", main_image_id: nil, slug: "newsletter", views: 1471, latitude: nil, longitude: nil, precision: nil>

Very mysterious!

jywarren avatar Jul 21 '20 21:07 jywarren

Jeanette from PL noted this page also has an elevated number of wiki pages listed on the tab count:

image

83 is a much bigger error than what @keshavsethi had found, as only 6 wiki pages are shown. We can go into the database directly to just skim what those wiki pages actually are, in the query returning 86, and compare the queries too.

jywarren avatar Jul 28 '20 14:07 jywarren

OK! The disparity, for the method tag, is these different queries:

irb(main):002:0> Node.for_tagname_and_type('method', 'page', wildcard: false).count
=> 83
irb(main):001:0> Tag.find_nodes_by_type('method','page').count
=> 5

I'll look at the code behind them to confirm what's going on.

Here is the tab count: https://github.com/publiclab/plots2/blob/92834ead6c53d6d9431bb99024d4044b282518be/app/controllers/tag_controller.rb#L542-L561

It references Node.for_tagname_and_type:

https://github.com/publiclab/plots2/blob/122e8a73b440bb752f080375aad3f24a02b7c8f9/app/models/node.rb#L621-L630

By contrast, Tag.find_nodes_by_type is here:

https://github.com/publiclab/plots2/blob/122e8a73b440bb752f080375aad3f24a02b7c8f9/app/models/tag.rb#L94-L116

Noting that the first 6 items on both https://publiclab.org/wiki/tag/method and https://publiclab.org/methods are the same, but /wiki/tag/method cuts off after 6. A clue? 🕵️

jywarren avatar Aug 04 '20 18:08 jywarren

Both segments of code are significantly complicated by parent tag references.

jywarren avatar Aug 04 '20 18:08 jywarren

OK, so the difference is in the limit term:

irb(main):010:0> Tag.find_nodes_by_type('method','page', false).count
=> 83
irb(main):011:0> Tag.find_nodes_by_type('method','page').count
=> 5

Let's track where the limit is being set on https://publiclab.org/wiki/tag/method

But, a follow-up could be to consolidate this code and/or remove the parent functionality.

jywarren avatar Aug 04 '20 18:08 jywarren

So, the query for the listing of wiki pages in the table below on /wiki/tag/method is: https://github.com/publiclab/plots2/blob/92834ead6c53d6d9431bb99024d4044b282518be/app/controllers/tag_controller.rb#L117

I /believe/ none of these have parent tag issues, having checked in the db. So we're left with a strange mystery of pagination, which I can't quite figure out.

Each of these pages show a different collection, with some overlapping items:

  • https://publiclab.org/wiki/tag/method
  • https://publiclab.org/wiki/tag/method?page=2
  • https://publiclab.org/wiki/tag/method?page=3
  • https://publiclab.org/wiki/tag/method?page=4

We can probably get all 83 pages this way. So it could be pagination being missing -- we could activate by adding @pagination = true in the controller, to activate this line:

https://github.com/publiclab/plots2/blob/92834ead6c53d6d9431bb99024d4044b282518be/app/views/wiki/_wikis.html.erb#L30

But there's something else -- why is Odor Logging and Stormwater Monitoring the top entry for both the first couple pages? And why does Facilitate your meetings show up again on pages 3 and 4? Something is going on with our query.

jywarren avatar Aug 04 '20 18:08 jywarren

Let's fix pagination, and then return here to see what we can do about the repeated display of some items, as well as figure out why page 1 shows only 6, but later pages show a full 24 (i think?)???

jywarren avatar Aug 04 '20 19:08 jywarren

VERY strange -- https://github.com/publiclab/plots2/pull/8243#issuecomment-668782325 --- pagination mysteries persist...

jywarren avatar Aug 04 '20 19:08 jywarren

OK, noting from this comment that the /tag/____ page is cached every 5 mins.

jywarren avatar Aug 11 '20 21:08 jywarren

Indeed:

> Node.for_tagname_and_type('method','page',question:false).paginate(page: 1,per_page: 24).order('node_revisions.timestamp DESC').length
=> 4

jywarren avatar Aug 11 '20 21:08 jywarren

It seems to be the mix of .order('node_revisions.timestamp DESC') and .paginate() that is causing this. If I remove one, it starts to return 24 items:

irb(main):089:0> Node.for_tagname_and_type('method','page',question:false).paginate(page: 1,per_page: 24).order('node_revisions.timestamp DESC').length
=> 4
irb(main):090:0> Node.for_tagname_and_type('method','page',question:false).paginate(page: 1,per_page: 24).length
=> 24
irb(main):091:0> Node.for_tagname_and_type('method','page',question:false).order('node_revisions.timestamp DESC').paginate(page: 1,per_page: 24).length
=> 4

jywarren avatar Aug 11 '20 21:08 jywarren