Skip to content
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

Merged
merged 7 commits into from
Jun 28, 2023

Conversation

brunocorrea23
Copy link
Contributor

targets #294

Copy link
Contributor

@iuioiua iuioiua left a 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()?

@brunocorrea23
Copy link
Contributor Author

brunocorrea23 commented Jun 24, 2023

Thanks!

  • I have added tests for the analytics inside the tests for createUser() and createItem(). I didn't do it for createVote() since this test has not been created yet and is part of an ongoing PR. We can add that later.
  • I've removed the incrementVisitsPerDay() in favor of a more generic incrementAnalyticsMetricPerDay() that can be reused if more cases in the future. Also did some refactoring around that. Let me know if this makes sense. I can undo these changes if it doens't.

Copy link
Contributor

@iuioiua iuioiua left a 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 Show resolved Hide resolved
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)
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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
Comment on lines 75 to 77
const itemsCount =
(await getAnalyticsMetricPerDay("items_count", new Date()))?.valueOf() ??
0n;
Copy link
Contributor

@iuioiua iuioiua Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const itemsCount =
(await getAnalyticsMetricPerDay("items_count", new Date()))?.valueOf() ??
0n;
const itemsCount = await getAnalyticsMetricPerDay("items_count", new Date());
assertEquals(itemsCount, null);

Copy link
Contributor Author

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?

Copy link
Contributor

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?

utils/db_test.ts Outdated Show resolved Hide resolved
@brunocorrea23
Copy link
Contributor Author

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?

Thanks for your input here 🙏 I'll do some refactoring around this so that we end up with 3 functions per metric:

  • increment[X]ByDay(date: Date)
  • get[X]ByDay(date: Date)
  • get[X]ListByDay(options?: Deno.KvListOptions)

@brunocorrea23
Copy link
Contributor Author

@iuioiua , couple of comments here:

  • I've created the analytics functions I've mentioned above.
  • I haven't changed the way /stats uses the analytics metrics as I believe this is outside the scope of this PR and might overlap with the work on fix(stats): populate empty chart axis #289 . If the changes are not handled by that PR, I can create another one soon after it.
  • 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.

Copy link
Contributor

@iuioiua iuioiua left a 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.

utils/db.ts Outdated Show resolved Hide resolved
utils/db.ts Outdated Show resolved Hide resolved
utils/db.ts Outdated Show resolved Hide resolved
utils/db.ts Outdated Show resolved Hide resolved
utils/db.ts Outdated Show resolved Hide resolved
@brunocorrea23
Copy link
Contributor Author

Addressed the previous comments. Please take a look.

Copy link
Contributor

@iuioiua iuioiua left a 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.

@iuioiua
Copy link
Contributor

iuioiua commented Jun 28, 2023

Closes #294

@iuioiua iuioiua merged commit c06c4ad into denoland:main Jun 28, 2023
@brunocorrea23
Copy link
Contributor Author

@iuioiua , thank you for all the help and input! 🙏

@brunocorrea23 brunocorrea23 deleted the analytics-into-atomic-ops branch June 28, 2023 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants