-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: analytics into atomic operations #296
Changes from 2 commits
d4f1955
ad91d4a
ff67ac0
3e2a394
f1b4d04
19167fb
f04517d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,11 +1,13 @@ | ||||||||||||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||||||||||||
import { | ||||||||||||
analyticsKey, | ||||||||||||
type Comment, | ||||||||||||
createComment, | ||||||||||||
createItem, | ||||||||||||
createUser, | ||||||||||||
deleteUserBySession, | ||||||||||||
getAllItems, | ||||||||||||
getAnalyticsMetricPerDay, | ||||||||||||
getCommentsByItem, | ||||||||||||
getItem, | ||||||||||||
getItemsByUser, | ||||||||||||
|
@@ -15,8 +17,7 @@ import { | |||||||||||
getUserByLogin, | ||||||||||||
getUserBySession, | ||||||||||||
getUserByStripeCustomer, | ||||||||||||
getVisitsPerDay, | ||||||||||||
incrementVisitsPerDay, | ||||||||||||
incrementAnalyticsMetricPerDay, | ||||||||||||
type Item, | ||||||||||||
kv, | ||||||||||||
newCommentProps, | ||||||||||||
|
@@ -71,7 +72,14 @@ Deno.test("[db] (get/create)Item()", async () => { | |||||||||||
|
||||||||||||
assertEquals(await getItem(item.id), null); | ||||||||||||
|
||||||||||||
const itemsCount = | ||||||||||||
(await getAnalyticsMetricPerDay("items_count", new Date()))?.valueOf() ?? | ||||||||||||
0n; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iuioiua , can you pls explain this suggestion here? We can't assume anything about the value o itemsCount, as we don't know how many tests will use it before in the code. We can only test that the itemsCount_after = itemsCount_before + 1. Does it make sense? Did I understand correctly your suggestion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, we should move the line that defines |
||||||||||||
await createItem(item); | ||||||||||||
assertEquals( | ||||||||||||
(await getAnalyticsMetricPerDay("items_count", new Date()))!.valueOf(), | ||||||||||||
iuioiua marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
itemsCount + 1n, | ||||||||||||
); | ||||||||||||
await assertRejects(async () => await createItem(item)); | ||||||||||||
assertEquals(await getItem(item.id), item); | ||||||||||||
}); | ||||||||||||
|
@@ -139,8 +147,14 @@ Deno.test("[db] user", async () => { | |||||||||||
assertEquals(await getUserBySession(user.sessionId), null); | ||||||||||||
assertEquals(await getUserByStripeCustomer(user.stripeCustomerId!), null); | ||||||||||||
|
||||||||||||
const usersCount = (await getAnalyticsMetricPerDay("users_count", new Date())) | ||||||||||||
?.valueOf() ?? 0n; | ||||||||||||
await createUser(user); | ||||||||||||
await assertRejects(async () => await createUser(user)); | ||||||||||||
assertEquals( | ||||||||||||
(await getAnalyticsMetricPerDay("users_count", new Date()))!.valueOf(), | ||||||||||||
usersCount + 1n, | ||||||||||||
); | ||||||||||||
assertEquals(await getUser(user.id), user); | ||||||||||||
assertEquals(await getUserByLogin(user.login), user); | ||||||||||||
assertEquals(await getUserBySession(user.sessionId), user); | ||||||||||||
|
@@ -164,17 +178,20 @@ Deno.test("[db] user", async () => { | |||||||||||
); | ||||||||||||
}); | ||||||||||||
|
||||||||||||
Deno.test("[db] visit", async () => { | ||||||||||||
Deno.test("[db] analytics increment/get", async () => { | ||||||||||||
const date = new Date("2023-01-01"); | ||||||||||||
const visitsKey = [ | ||||||||||||
"visits", | ||||||||||||
const dummyKey = [ | ||||||||||||
"example", | ||||||||||||
`${date.toISOString().split("T")[0]}`, | ||||||||||||
]; | ||||||||||||
await incrementVisitsPerDay(date); | ||||||||||||
assertEquals((await kv.get(visitsKey)).key[1], "2023-01-01"); | ||||||||||||
assertEquals((await getVisitsPerDay(date))!.valueOf(), 1n); | ||||||||||||
await kv.delete(visitsKey); | ||||||||||||
assertEquals(await getVisitsPerDay(date), null); | ||||||||||||
await incrementAnalyticsMetricPerDay("example", date); | ||||||||||||
assertEquals((await kv.get(dummyKey)).key[1], "2023-01-01"); | ||||||||||||
assertEquals( | ||||||||||||
(await getAnalyticsMetricPerDay("example", date))!.valueOf(), | ||||||||||||
1n, | ||||||||||||
); | ||||||||||||
await kv.delete(dummyKey); | ||||||||||||
assertEquals(await getAnalyticsMetricPerDay("example", date), null); | ||||||||||||
}); | ||||||||||||
|
||||||||||||
Deno.test("[db] newCommentProps()", () => { | ||||||||||||
|
@@ -205,3 +222,10 @@ Deno.test("[db] createComment() + getCommentsByItem()", async () => { | |||||||||||
await assertRejects(async () => await createComment(comment2)); | ||||||||||||
assertArrayIncludes(await getCommentsByItem(itemId), [comment1, comment2]); | ||||||||||||
}); | ||||||||||||
|
||||||||||||
Deno.test("[db] analyticsKey()", () => { | ||||||||||||
assertEquals(analyticsKey("metric_test"), [ | ||||||||||||
"metric_test", | ||||||||||||
`${new Date().toISOString().split("T")[0]}`, | ||||||||||||
]); | ||||||||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... It might be better to define a
const usersCount
at the start of the function with the others without usinganalyticsKey()
. It gives a more immediate idea of what's going on, IMO. At most, maybe atoDate()
that returnsdate.toISOString().split("T")[0]
might be useful. Ditto for other similar scenarios. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I.e. check out
formatDate()
in #289.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Will adjust that!