Spaces:
Runtime error
Runtime error
Commit
·
65dda73
1
Parent(s):
825aa76
trying to improve performance
Browse files- src/core/getAppPosts.mts +22 -6
- src/core/readPostFiles.mts +10 -3
- src/index.mts +37 -11
- src/utils/shuffleArray.mts +10 -0
src/core/getAppPosts.mts
CHANGED
@@ -1,14 +1,30 @@
|
|
1 |
import { postDirFilePath } from "../config.mts"
|
2 |
import { Post, PostVisibility } from "../types.mts"
|
|
|
|
|
3 |
|
4 |
import { readPostFiles } from "./readPostFiles.mts"
|
5 |
|
6 |
-
export const getAppPosts = async (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
const posts = await readPostFiles(postDirFilePath, appId)
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
}
|
|
|
1 |
import { postDirFilePath } from "../config.mts"
|
2 |
import { Post, PostVisibility } from "../types.mts"
|
3 |
+
import { getValidNumber } from "../utils/getValidNumber.mts"
|
4 |
+
import { shuffleArray } from "../utils/shuffleArray.mts"
|
5 |
|
6 |
import { readPostFiles } from "./readPostFiles.mts"
|
7 |
|
8 |
+
export const getAppPosts = async ({
|
9 |
+
appId,
|
10 |
+
limit,
|
11 |
+
visibility,
|
12 |
+
shuffle
|
13 |
+
}: {
|
14 |
+
appId: string
|
15 |
+
limit?: number
|
16 |
+
visibility?: PostVisibility
|
17 |
+
shuffle?: boolean
|
18 |
+
}): Promise<Post[]> => {
|
19 |
const posts = await readPostFiles(postDirFilePath, appId)
|
20 |
|
21 |
+
const visiblePosts = visibility
|
22 |
+
? posts.filter(post => post.visibility === visibility)
|
23 |
+
: posts
|
24 |
+
|
25 |
+
const sortedPosts = shuffle
|
26 |
+
? shuffleArray(visiblePosts)
|
27 |
+
: visiblePosts.sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt))
|
28 |
+
|
29 |
+
return sortedPosts.slice(0, getValidNumber(limit, 1, 80, 20))
|
30 |
}
|
src/core/readPostFiles.mts
CHANGED
@@ -6,7 +6,7 @@ import { readPostFile } from "./readPostFile.mts"
|
|
6 |
|
7 |
const cache = {} as { [directory: string]: { timestamp: number, files: Post[] } };
|
8 |
|
9 |
-
export const readPostFiles = async (postDirFilePath: string, appId?: string): Promise<Post[]> => {
|
10 |
|
11 |
const now = Date.now()
|
12 |
|
@@ -29,8 +29,15 @@ export const readPostFiles = async (postDirFilePath: string, appId?: string): Pr
|
|
29 |
}
|
30 |
|
31 |
const posts: Post[] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
for (const postFileName of postFiles) {
|
34 |
// console.log("postFileName:", postFileName)
|
35 |
const postFilePath = path.join(postDirFilePath, postFileName)
|
36 |
try {
|
@@ -49,6 +56,6 @@ export const readPostFiles = async (postDirFilePath: string, appId?: string): Pr
|
|
49 |
|
50 |
// store results in cache with current timestamp
|
51 |
cache[postDirFilePath] = { timestamp: now, files: posts }
|
52 |
-
|
53 |
return posts
|
54 |
}
|
|
|
6 |
|
7 |
const cache = {} as { [directory: string]: { timestamp: number, files: Post[] } };
|
8 |
|
9 |
+
export const readPostFiles = async (postDirFilePath: string, appId?: string, limit?: number): Promise<Post[]> => {
|
10 |
|
11 |
const now = Date.now()
|
12 |
|
|
|
29 |
}
|
30 |
|
31 |
const posts: Post[] = []
|
32 |
+
|
33 |
+
// TODO implement filtering at this level, using the date
|
34 |
+
|
35 |
+
// until then let's implement a hard limit
|
36 |
+
const postFilesToRead = postFiles.slice(0, 300)
|
37 |
+
|
38 |
+
|
39 |
+
for (const postFileName of postFilesToRead) {
|
40 |
|
|
|
41 |
// console.log("postFileName:", postFileName)
|
42 |
const postFilePath = path.join(postDirFilePath, postFileName)
|
43 |
try {
|
|
|
56 |
|
57 |
// store results in cache with current timestamp
|
58 |
cache[postDirFilePath] = { timestamp: now, files: posts }
|
59 |
+
|
60 |
return posts
|
61 |
}
|
src/index.mts
CHANGED
@@ -10,6 +10,7 @@ import { savePost } from "./core/savePost.mts"
|
|
10 |
import { getAppPosts } from "./core/getAppPosts.mts"
|
11 |
import { deletePost } from "./core/deletePost.mts"
|
12 |
import { getPost } from "./core/getPost.mts"
|
|
|
13 |
|
14 |
initFolders()
|
15 |
|
@@ -119,7 +120,7 @@ app.post("/posts/:appId", async (req, res) => {
|
|
119 |
res.end()
|
120 |
})
|
121 |
|
122 |
-
app.get("/posts/:appId/:
|
123 |
|
124 |
const appId = `${req.params.appId}`
|
125 |
|
@@ -131,16 +132,19 @@ app.get("/posts/:appId/:param", async (req, res) => {
|
|
131 |
return
|
132 |
}
|
133 |
|
134 |
-
const
|
135 |
-
|
|
|
|
|
|
|
136 |
|
137 |
-
if (isVisibility) {
|
138 |
-
const visibility = param
|
139 |
try {
|
140 |
-
const posts = await getAppPosts(
|
141 |
appId,
|
142 |
-
visibility === "all" ? undefined : visibility as PostVisibility
|
143 |
-
|
|
|
|
|
144 |
res.status(200)
|
145 |
console.log(`returning ${posts.length} community posts for app ${appId} (visibility: ${visibility})`)
|
146 |
res.write(JSON.stringify({ posts } as GetAppPostsResponse))
|
@@ -155,8 +159,31 @@ app.get("/posts/:appId/:param", async (req, res) => {
|
|
155 |
res.end()
|
156 |
return
|
157 |
}
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
try {
|
161 |
const post = await getPost(appId, postId)
|
162 |
res.status(200)
|
@@ -172,7 +199,6 @@ app.get("/posts/:appId/:param", async (req, res) => {
|
|
172 |
res.end()
|
173 |
return
|
174 |
}
|
175 |
-
}
|
176 |
})
|
177 |
|
178 |
// delete a post
|
|
|
10 |
import { getAppPosts } from "./core/getAppPosts.mts"
|
11 |
import { deletePost } from "./core/deletePost.mts"
|
12 |
import { getPost } from "./core/getPost.mts"
|
13 |
+
import { getValidBoolean } from "./utils/getValidBoolean.mts"
|
14 |
|
15 |
initFolders()
|
16 |
|
|
|
120 |
res.end()
|
121 |
})
|
122 |
|
123 |
+
app.get("/posts/:appId/firehose/:visibility/:maxNbItems/:shuffle", async (req, res) => {
|
124 |
|
125 |
const appId = `${req.params.appId}`
|
126 |
|
|
|
132 |
return
|
133 |
}
|
134 |
|
135 |
+
const visibility = `${req.params.visibility}`
|
136 |
+
|
137 |
+
const shuffle = getValidBoolean(`${req.params.shuffle}`, false)
|
138 |
+
|
139 |
+
const limit = getValidNumber(`${req.params.maxNbItems}`, 1, 80, 20)
|
140 |
|
|
|
|
|
141 |
try {
|
142 |
+
const posts = await getAppPosts({
|
143 |
appId,
|
144 |
+
visibility: visibility === "all" ? undefined : visibility as PostVisibility,
|
145 |
+
shuffle,
|
146 |
+
limit
|
147 |
+
})
|
148 |
res.status(200)
|
149 |
console.log(`returning ${posts.length} community posts for app ${appId} (visibility: ${visibility})`)
|
150 |
res.write(JSON.stringify({ posts } as GetAppPostsResponse))
|
|
|
159 |
res.end()
|
160 |
return
|
161 |
}
|
162 |
+
})
|
163 |
+
|
164 |
+
|
165 |
+
app.get("/posts/:appId/:postId", async (req, res) => {
|
166 |
+
|
167 |
+
const appId = `${req.params.appId}`
|
168 |
+
|
169 |
+
if (!uuidValidate(appId)) {
|
170 |
+
console.error("invalid appId")
|
171 |
+
res.status(400)
|
172 |
+
res.write(JSON.stringify({ error: `invalid appId` }))
|
173 |
+
res.end()
|
174 |
+
return
|
175 |
+
}
|
176 |
+
|
177 |
+
const postId = `${req.params.postId}`
|
178 |
+
|
179 |
+
if (!uuidValidate(postId)) {
|
180 |
+
console.error("invalid postId")
|
181 |
+
res.status(400)
|
182 |
+
res.write(JSON.stringify({ error: `invalid postId` }))
|
183 |
+
res.end()
|
184 |
+
return
|
185 |
+
}
|
186 |
+
|
187 |
try {
|
188 |
const post = await getPost(appId, postId)
|
189 |
res.status(200)
|
|
|
199 |
res.end()
|
200 |
return
|
201 |
}
|
|
|
202 |
})
|
203 |
|
204 |
// delete a post
|
src/utils/shuffleArray.mts
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export function shuffleArray<T>(items: T[]): T[] {
|
2 |
+
for (let i = items.length - 1; i > 0; i--) {
|
3 |
+
const j = Math.floor(Math.random() * (i + 1));
|
4 |
+
const temp = items[i];
|
5 |
+
items[i] = items[j];
|
6 |
+
items[j] = temp;
|
7 |
+
}
|
8 |
+
|
9 |
+
return items
|
10 |
+
}
|