Updating topic view count from import script

(Gerhard Schlager) #1

I’m trying to update the view count of topics imported by the phpBB3 importer.
I’ve come up with the following simple solution:

topic = Topic.find_by(id: post.topic_id)
topic.views = opts[:views]

You can see more on GitHub in my implementation of this feature for the phpBB3 importer.

It works. Now I’m wondering if this is really that simple.
I’ve seen that there is a TopicViewItem class which saves the views for each user. I’m a little bit worried that some background task will aggregate those items and overwrites the value of views some time later.

Am I missing something or is my solution sufficient?

How many views are counted per IP?
(Sam Saffron) #2

Hacking numbers there is a bit risky, eventually a consistency job may nuke it.

You really want a record in the topic_views table for every view.

(Kevin McKinney) #3

I know this is an old topic, but was the only one I could find that was applicable.

I’m also interested in importing a view count. I couldn’t find any instance of topic_views in any of the existing import scripts. Is there a best practice for how to accomplish this? It seems if I insert into the topic_views table I might also have to set the views field on the topics table, right? I’m worried about what else I might be missing.

How often do said “consistency jobs” run? Should I just be inserting into topic_views and let a job set the rest of the data for me?

I found that the Lithium import script covers importing ‘likes’ so I was going to model that part of my script after that one, but nothing seems to import ‘viewed’ counts anywhere.

(Itkin) #4

+1 i’m looking into importing view counts too, did you find a nice way to proceed ?

(Gerhard Schlager) #5

As far as I can remember that should work. The main problem I faced with the phpBB3 importer was the difference in how Discourse and phpBB count views.

  • Discourse increases the topic view count per user or, in case of anonymous users, IP address every 8 hours (at least that’s the default value of the site setting topic_view_duration_hours)
  • phpBB3 increases the topic view count every time a user visits a topic. Navigating between topic pages does not count as additional views, but there’s no other limit. A user can generate hundreds of views per hour for the same topic simply by navigating between two different topics.

I can’t think of a method to approximate a view count that would look correct after the import.
For example, it’s quite possible that every imported topic has a view count of 10k or more whereas all your new topics created in Discourse have only about 1000 views because of the different counting algorithms.
0 views, 10k views, both would look wrong. So, why even bother importing view counts. :wink:

I’m not sure how Lithium counts views, but I’d advise against importing view counts if it’s similar to phpBB3.

But, if you really need to import view counts, creating views as seen in TopicView and setting the views attribute of topics should work.

(Kevin McKinney) #6

Thanks for confirming! Glad that’s how it worked because that’s basically what I already did. :stuck_out_tongue: What I was importing from (a custom internal tool we wrote a couple of years ago) wasn’t really a forum, but Discourse covers almost all the functionality we had, plus a lot more, so once I set up a different internal forum and got familiar with it, I knew I wanted to port this tool to Discourse as well.

The way we counted views was actually at the other end of the spectrum - it just counted whether someone viewed it once. So the view counts being imported were really low, and while Discourse will add to the view count differently over time, I’m totally fine with that. It was just nice to have ‘view’ and ‘like’ counts ported over so that the switch for users felt more natural.