Added comments to data generator

ref ENG-1219

Includes a minor fix to ensure posts are published in the past, so
that comments can be created after.
This commit is contained in:
Sam Lord 2024-06-11 11:41:04 +01:00
parent 63c674828a
commit 0aa36b901d
3 changed files with 76 additions and 2 deletions

View File

@ -0,0 +1,73 @@
const {faker} = require('@faker-js/faker');
const TableImporter = require('./TableImporter');
const {luck} = require('../utils/random');
const generateEvents = require('../utils/event-generator');
const dateToDatabaseString = require('../utils/database-date');
class CommentsImporter extends TableImporter {
static table = 'comments';
static dependencies = ['posts'];
constructor(knex, transaction) {
super(CommentsImporter.table, knex, transaction);
}
async import(quantity) {
const posts = await this.transaction.select('id', 'published_at').from('posts')
.where('status', 'published');
this.members = await this.transaction.select('id', 'created_at').from('members');
this.commentsPerPost = quantity ? quantity / posts.length : 10;
await this.importForEach(posts, this.commentsPerPost);
}
setReferencedModel(model) {
this.model = model;
this.commentIds = [];
this.timestamps = generateEvents({
shape: 'ease-out',
trend: 'negative',
// Steady readers login more, readers who lose interest read less overall.
// ceil because members will all have logged in at least once
total: faker.datatype.number({min: 0, max: this.commentsPerPost}),
startTime: new Date(model.published_at),
endTime: new Date()
});
this.possibleMembers = this.members.filter(member => new Date(member.created_at) < new Date(model.published_at));
}
generate() {
const timestamp = this.timestamps.pop();
if (!timestamp) {
// Out of events for this post
return null;
}
if (this.possibleMembers.length === 0) {
return null;
}
const isReply = luck(30) && this.commentIds.length > 0; // 30% of comments are replies
const event = {
id: this.fastFakeObjectId(),
post_id: this.model.id,
member_id: this.possibleMembers[faker.datatype.number(this.possibleMembers.length - 1)].id,
parent_id: isReply ? this.commentIds[faker.datatype.number(this.commentIds.length - 1)] : undefined,
status: 'published',
created_at: dateToDatabaseString(timestamp),
updated_at: dateToDatabaseString(timestamp),
html: `<p>${faker.lorem.sentence().replace(/[&<>"']/g, c => `&#${c.charCodeAt(0)};`)}</p>`
};
this.commentIds.push(event.id);
return event;
}
}
module.exports = CommentsImporter;

View File

@ -55,7 +55,7 @@ class PostsImporter extends TableImporter {
created_at: dateToDatabaseString(timestamp),
created_by: '1',
updated_at: dateToDatabaseString(timestamp),
published_at: status === 'published' || status === 'scheduled' ? dateToDatabaseString(faker.date.soon(5, timestamp)) : null,
published_at: status === 'published' ? dateToDatabaseString(timestamp) : status === 'scheduled' ? dateToDatabaseString(faker.date.soon(5, timestamp)) : null,
uuid: faker.datatype.uuid(),
title: title,
type: this.type,

View File

@ -36,5 +36,6 @@ module.exports = [
require('./MembersFeedbackImporter'),
require('./RecommendationsImporter'),
require('./RecommendationClickEventsImporter'),
require('./RecommendationSubscribeEventsImporter')
require('./RecommendationSubscribeEventsImporter'),
require('./CommentsImporter')
];