...

/

Displaying a Summary to the Users

Displaying a Summary to the Users

Learn how to use the historical data to display a summary of activities to the users.

One of the benefits of having the data stored in a SQL database is that we can use its expressive power to query and summarize the data in many ways. Let’s use that to present a summary of the users’ activities in the application. To display the data to the users, we’ll add two new sections to our application’s interface, as shown in the figures below.

The Daily Summary section presents a summary of the current day’s activities in minutes broken down by Pomodoro and Breaks.

We’ll implement the Daily Summary interface using the BarChart Termdash widget.

The Weekly Summary section displays the current week’s activities broken down by Pomodoro and Breaks, using the Termdash LineChart widget.

These widgets require data to display. We can extract the required data using a single SQL query with the appropriate filters. We’ll add a single method to the Repository interface to query the data and then use a pair of functions to transform the data according to each widget’s requirements. Let’s start by modifying the Repository interface.

Updating the interval.go file

We edit the file interval.go and add a new method called CategorySummary() to the Repository interface:

type Repository interface {
Create(i Interval) (int64, error)
Update(i Interval) error
ByID(id int64) (Interval, error)
Last() (Interval, error)
Breaks(n int) ([]Interval, error)
CategorySummary(day time.Time, filter string) (time.Duration, error)
}

This method takes two inputs: a time.Time type representing the day to summarize and a string filter to filter the category. It returns a value of type time.Duration as a sum of the time spent on that category for a given day.

We need to implement this new method on both repositories, so we start with the inMemory repository. Let’s open the file repository/inMemory.go and add the ...