Skip to content

Conversation

@kharijarrett
Copy link
Collaborator

This feature was requested mainly for customer reporting. Made it so the time was configurable. Successfully tested on Mac. This module still could use some standardization but that's another problem for another PR.

@kharijarrett kharijarrett requested a review from bhaney December 9, 2024 19:08
Copy link
Contributor

@bhaney bhaney left a comment

Choose a reason for hiding this comment

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

Nice change, a few things to make it more configurable and to prevent race conditions

func GetTimestamp() string {
currTime := time.Now()
return fmt.Sprintf(currTime.Format("20060102_150405"))
return string(currTime.Format("20060102_150405"))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a cool way of specifying what format you want something in! Didn't know about it

h, m, s := start.Hour(), start.Minute(), start.Second()
if itsBeen >= 24*time.Hour ||
(h == t.lastReset.Hour() && m == t.lastReset.Minute() && s == t.lastReset.Second()) {
t.classCounter = make(map[string]int)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is running in a go routine, you want to make sure about race conditions that might appear. One thing to really prevent this would be to consider using a sync.Map object instead of a simple map.

Copy link
Contributor

Choose a reason for hiding this comment

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

// Reset if it's the exact time or it's been more than 24 hours since last reset
itsBeen := start.Sub(t.lastReset)
h, m, s := start.Hour(), start.Minute(), start.Second()
if itsBeen >= 24*time.Hour ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this a variable, not jsut 24 hours. Make it default to 24, but have it be settable by an optional config. And rather than hours, use the golang ParseDuration option to interpret time strings time.ParseDuration("5h3m").

so the new optional config should default to 24 hours, but be settable with a config like

"reset_duration": "2h5m"

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to add a test -- once you add the reset duration option, set the reset duration to something like 10 s and then see if it works like expected

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.

3 participants