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

Add a "Calendar view" #732

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

justin-jiajia
Copy link
Contributor

image

Implement #338 .

@justin-jiajia justin-jiajia marked this pull request as ready for review January 10, 2025 01:58
@muety muety self-requested a review January 10, 2025 16:36
Copy link
Owner

@muety muety left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this, great contribution!

Regarding the overall implementation, I'd probably suggest a slightly different approach. You current implementation comes with a few drawbacks:

  • "Duplicate" logic to aggregate durations
  • Inefficient, because operating on raw heartbeats for each request instead of leveraging pre-aggregated summaries
  • Not respected custom mapping or project aliases

To overcome these (and thus also improve performance), I'd probably utilize the already existing summary computation logic. I'd (very roughly speaking) do approximately this:

  • Use SplitRangeByDays (search code for examples), to break down the requested interval into single days
  • Use SummaryService.Aliased() (just like in many other places throughout Wakapi) to fetch fetch summaries for each day
  • Construct the DailyProjectStat view model from an array of summaries, i.e. introduce methods func NewDailyProjectStats(summaries []*models.Summaries) []*DailyProjectStat { ... } or something, e.g. similar to here.

views/summary.tpl.html Outdated Show resolved Hide resolved
models/summary.go Outdated Show resolved Hide resolved
services/summary.go Outdated Show resolved Hide resolved
services/summary.go Outdated Show resolved Hide resolved
@justin-jiajia justin-jiajia requested a review from muety January 14, 2025 10:00
Copy link
Owner

@muety muety left a comment

Choose a reason for hiding this comment

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

Thanks for implementing my change requests!

Do you think we should limit the calendar view component to a maximum number of days? Because when choosing a very large – if not All Time – interval, the chart becomes unreadable and super inefficient (blocked my browser tab for almost 30 seconds). Would love to hear your thoughts on that.

image

dailyProjects := make([]*DailyProjectViewModel, 0)
intervals := utils.SplitRangeByDays(s.FromTime.T(), s.ToTime.T())
for _, cur := range intervals {
cur_summary, err := srv.Aliased(cur[0], cur[1].Add(-1*time.Second), s.User, srv.Retrieve, filters, false)
Copy link
Owner

Choose a reason for hiding this comment

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

Can you explain what's the point of Add(-1*time.Second)?

Copy link
Contributor Author

@justin-jiajia justin-jiajia Jan 15, 2025

Choose a reason for hiding this comment

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

I searched the repo for the example of SplitRangeByDays. I got the following code snippet:

intervals := utils.SplitRangeByDays(overallParams.From, overallParams.To)
summaries := make([]*models.Summary, len(intervals))
// filtering
filters := helpers.ParseSummaryFilters(r)
for i, interval := range intervals {
summary, err := h.summarySrvc.Aliased(interval[0], interval[1], user, h.summarySrvc.Retrieve, filters, end.After(time.Now()))
if err != nil {
return nil, err, http.StatusInternalServerError
}
// wakatime returns requested instead of actual summary range
summary.FromTime = models.CustomTime(interval[0])
summary.ToTime = models.CustomTime(interval[1].Add(-1 * time.Second))
summaries[i] = summary
}

As it includes both SplitRangeByDays and summarySrvc.Aliased, I wrote my code according to it.

I didn't know whether the Aliased takes account of the heartbeats exactly sent at the last second or not, so I thought this line was better as the last second (or the first second on the next day) won't be calculated twice.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for explaining. You should be good to go with using the exact end date, summary calculation logic will take care of the rest 👍.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please tell me a bit more? What is exact? Should I remove the add(-1s) or not?

}

// get a day-by-day summary of **each** project
func (srv *SummaryService) NewDailyProjectStats(s *models.Summary, filters *models.Filters) ([]*DailyProjectViewModel, error) {
Copy link
Owner

Choose a reason for hiding this comment

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

Sorry for being very picky about code architecture here, but: following the notion of a "layered architecture" / adhering to the "dependency rule", the service layer shouldn't have any referenced to view models. They rather belong to what you could call "presentation layer" or so and thus only be used at the level of routes.

I'd prefer to go for this implementation:

  • Move the DailyProjectViewModel definition to models/view
  • In that same file, add a method NewDailyProjectStats([]*models.Summary summaries)
  • Move this function here to inside SummaryHandler and change it in a way that it (1) retrieves the required list of summaries from the service and (2) calls the just mentioned NewDailyProjectStats to have it construct the actual view model from it

So in summary:

  • Service remains mostly unchanged
  • Route is responsible for fetching summaries and calling to construct view model
  • View model construction (from a list of summaries) is implemented next to the view model itself

Again, sorry for dragging this out so much! :-|

@justin-jiajia justin-jiajia requested a review from muety January 16, 2025 13:23
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