-
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
refactor: analytics into atomic operations #296
Conversation
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.
Great! Thanks for this. Please also check that their respective functions have increased the analytics data. E.g. that users_count
has been incremented in the createUser()
test.
Can we also investigate removing incrementAnalyticsMetricPerDay()
?
Thanks!
|
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.
IMO, we should drop getAnalyticsMetricPerDay()
in favour of, say getItemsCountByDate()
, and the same for the others. I'd rather have more lines of code that are easier to understand rather than fewer lines of code that require more thinking. WDYT?
utils/db.ts
Outdated
@@ -308,6 +306,7 @@ export async function createUser(user: User) { | |||
.set(usersKey, user) | |||
.set(usersByLoginKey, user) | |||
.set(usersBySessionKey, user) | |||
.sum(analyticsKey("users_count"), 1n) |
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 using analyticsKey()
. It gives a more immediate idea of what's going on, IMO. At most, maybe a toDate()
that returns date.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!
utils/db_test.ts
Outdated
const itemsCount = | ||
(await getAnalyticsMetricPerDay("items_count", new Date()))?.valueOf() ?? | ||
0n; |
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.
const itemsCount = | |
(await getAnalyticsMetricPerDay("items_count", new Date()))?.valueOf() ?? | |
0n; | |
const itemsCount = await getAnalyticsMetricPerDay("items_count", new Date()); | |
assertEquals(itemsCount, null); |
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.
@iuioiua , can you pls explain this suggestion here?
I believe that if you do it like this, the assertion is going to fail. When it gets to this point, the itemsCount is probably equal to 2n
because of the two createItem()
that were run on the test above (" [db] getAllItems()"
)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, we should move the line that defines itemsCount
elsewhere such that it doesn't require a default value. WDYT?
Thanks for your input here 🙏 I'll do some refactoring around this so that we end up with 3 functions per metric:
|
@iuioiua , couple of comments here:
|
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.
Few nits. Could you please also rebase? Sorry, I only just pushed a DB-related change.
- As for the default value on the db_test.ts you've mentioned above: the way I see to remove the need for a default value is simply to move the function to the top of the file, where we can guarantee that no one has increased that metrics count before. But I do think that the current implementation is more robust, as it doesn't assume anything about order of functions within that file. I can imagine in the future someone trying to changing that file, the test starts failing and it will be hard to debug why.
Well-justified. Let's go with your suggestion then.
Addressed the previous comments. Please take a look. |
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.
Great work! Nice cleanups and simplifications! Thanks for this one, @brunocorrea23.
Closes #294 |
@iuioiua , thank you for all the help and input! 🙏 |
targets #294