Adding support for User Stats import to the generic importer

This commit is contained in:
cocococosti 2022-03-07 14:50:38 -04:00
parent a4f84257e4
commit 88448c7236
No known key found for this signature in database
GPG Key ID: 5F9EF216A49B6699
8 changed files with 52 additions and 7 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
app/.DS_Store vendored Normal file

Binary file not shown.

BIN
app/assets/.DS_Store vendored Normal file

Binary file not shown.

BIN
app/assets/javascripts/.DS_Store vendored Normal file

Binary file not shown.

BIN
public/.DS_Store vendored Normal file

Binary file not shown.

BIN
public/images/.DS_Store vendored Normal file

Binary file not shown.

BIN
public/images/emoji/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -23,14 +23,15 @@ class BulkImport::Generic < BulkImport::Base
end
def execute
import_categories
import_users
import_user_emails
# import_categories
# import_users
# import_user_emails
#import_single_sign_on_records
import_topics
import_posts
import_topic_allowed_users
import_likes
# import_topics
# import_posts
# import_topic_allowed_users
# import_likes
import_user_stats
end
def import_categories
@ -229,6 +230,50 @@ class BulkImport::Generic < BulkImport::Base
end
end
def import_user_stats
puts "Importing user stats..."
users = @db.execute(<<~SQL)
WITH posts_counts AS (SELECT COUNT(p.id) as count, p.user_id FROM posts p GROUP BY p.user_id),
topic_counts AS (SELECT COUNT(t.id) as count, t.user_id FROM topics t GROUP BY t.user_id),
first_post AS (SELECT MIN(p.created_at) as created_at, p.user_id FROM posts p GROUP BY p.user_id ORDER BY p.created_at ASC)
SELECT u.id as user_id, u.created_at, pc.count as posts, tc.count as topics, fp.created_at as first_post
FROM users u
JOIN posts_counts pc ON u.id = pc.user_id
JOIN topic_counts tc ON u.id = tc.user_id
JOIN first_post fp on u.id = fp.user_id
SQL
create_user_stats(users) do |row|
user = {
imported_id: row["user_id"],
imported_user_id: row["user_id"],
new_since: to_datetime(row["created_at"]),
post_count: row["posts"],
topic_count: row["topics"],
first_post_created_at: to_datetime(row["first_post"])
}
likes_received = @db.execute(<<~SQL)
SELECT COUNT(l.id) as likes_received FROM likes l join posts p on l.post_id = p.id WHERE p.user_id = #{row["user_id"]}
SQL
if likes_received
user[:likes_received] = row["likes_received"]
end
likes_given = @db.execute(<<~SQL)
SELECT COUNT(l.id) as likes_given FROM likes l WHERE l.user_id = #{row["user_id"]}
SQL
if likes_given
user[:likes_given] = row["likes_given"]
end
user
end
end
def create_connection(path)
sqlite = SQLite3::Database.new(path, results_as_hash: true)
sqlite.busy_timeout = 60000 # 60 seconds