Skip to content

Commit 6937a6e

Browse files
author
Lucas Hosseini
committed
[examples/hackernews] Add button to simulate activity.
Add a special API end point that simulate series of 1 to 20 upvotes to a random post every second.
1 parent 065cd26 commit 6937a6e

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

examples/hackernews/web_service/app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import secrets
33
import psycopg2
44
import requests
5+
import random
6+
import time
57

68
REACTIVE_SERVICE_URL = "http://reactive_cache:8081/v1"
79

@@ -250,3 +252,24 @@ def deupvote_post(post_id):
250252
@app.get("/healthcheck")
251253
def healthcheck():
252254
return "ok", 200
255+
256+
257+
@app.post("/simulate_activity")
258+
def simulate_activity():
259+
with get_db() as db:
260+
with db.cursor() as cur:
261+
cur.execute("SELECT id FROM posts")
262+
post_ids = [x[0] for x in cur.fetchall()]
263+
264+
for i in range(1000):
265+
post_id = random.choice(post_ids)
266+
for j in range(random.randrange(20)):
267+
user_id = random.randint(0, 1000)
268+
cur.execute(
269+
"INSERT INTO upvotes(user_id, post_id) VALUES (%s, %s)",
270+
(user_id, post_id),
271+
)
272+
db.commit()
273+
time.sleep(1)
274+
275+
return "ok", 200

examples/hackernews/www/src/App.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ function Feed(props: { posts: Post[]; session: Session | null }) {
203203
);
204204
}
205205

206+
function simulateActivity() {
207+
void fetch("/api/simulate_activity", { method: "POST" }).catch(
208+
(err: unknown) => {
209+
console.error(err);
210+
},
211+
);
212+
}
213+
206214
return (
207215
<>
208216
<Header session={session} />
@@ -233,6 +241,14 @@ function Feed(props: { posts: Post[]; session: Session | null }) {
233241
))}
234242
</ol>
235243
</div>
244+
<button
245+
onClick={(e) => {
246+
e.preventDefault();
247+
simulateActivity();
248+
}}
249+
>
250+
Simulate activity
251+
</button>
236252
</>
237253
);
238254
}

0 commit comments

Comments
 (0)