-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.js
60 lines (49 loc) · 2.17 KB
/
posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { appendFile } from "node:fs/promises";
import fastLoremIpsum from "fast-lorem-ipsum";
const DEFAULT_POST_COUNT = 1000;
const DEFAULT_TITLE_CHARACTER_COUNT = 7;
const DEFAULT_CONTENT_CHARACTER_COUNT = 256;
/**
* Generate a list of synthetic posts to be loaded into Postgres
*
* @param {object} args
* @param {number} [args.userCount] number of users that implies contiguous user IDs (0 to userCount-1)
* @param {number} [args.aboutHTMLWordCount] number of words to generate (lorem ipsum) for about_html (serves to add heft to tuples)
* @param {string} [args.outputFilePath] output file path, if present this functoin returns void
* @returns {any[][]} List of generated synthetic posts
*/
export async function generatePosts(args) {
const userCount = args.userCount;
if (!userCount) { throw new Error("user count is missing"); }
const postCount = args.postCount ?? DEFAULT_POST_COUNT;
const titleCharacterCount = args.titleCharacterCount || DEFAULT_TITLE_CHARACTER_COUNT;
const contentCharacterCount = args.contentCharacterCount || DEFAULT_CONTENT_CHARACTER_COUNT;
const outputFilePath = args.outputFilePath;
if (!outputFilePath) { throw new Error("output file path must be specified"); }
let creatorId;
const first20 = userCount * 0.20;
const last80 = userCount * 0.80;
// Generate all the posts with randomized creators
for (var id = 0; id < postCount; id++) {
if (Math.random() * 100 < 80) {
// 80% of the time, one of the first 20 people make the posts
creatorId = Math.floor(Math.random() * first20);
} else {
// 20% of the time, one of the later posters post
creatorId = (Math.random() * last80) + first20;
}
creatorId = Math.floor(creatorId);
const post = {
id,
title: fastLoremIpsum(titleCharacterCount, 'c'),
content: fastLoremIpsum(contentCharacterCount, 'w'),
main_image_src: `http://example.com/${fastLoremIpsum(10, 'w')}.png`,
main_link_src: `http://example.com/${fastLoremIpsum(4, 'w')}?ref=supabook`,
created_by: creatorId,
};
await appendFile(outputFilePath, `${JSON.stringify(post)}\n`);
}
// Write the entries to disk (returning nothing)
if (outputFilePath) {
}
}