Skip to content

Commit

Permalink
Add a weekly report and make it the default report
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmci committed Sep 10, 2024
1 parent b1adf37 commit 71bb369
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
18 changes: 16 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,25 @@ func getAllDaysOfMonth() []string {
return days
}

func getAllDaysOfWeek() []string {
var days []string
now := time.Now()

for i := 0; i < 7; i++ {
day := now.AddDate(0, 0, -int(now.Weekday())+i)
days = append(days, day.Format("2006-01-02"))
}

return days
}

func getYearlyData(db *sql.DB) ([]DayAggregate, error) {
query := `
SELECT
DATE(datetime(created_at, 'localtime')) as day,
SUM(actual) as total_actual
SUM(actual) as total_actual,
SUM(estimate) as total_estimate,
SUM(done) as total_done
FROM tasks
WHERE strftime('%Y', datetime(created_at, 'localtime')) = strftime('%Y', 'now', 'localtime')
GROUP BY day
Expand All @@ -108,7 +122,7 @@ func getYearlyData(db *sql.DB) ([]DayAggregate, error) {
var dayAggregates []DayAggregate
for rows.Next() {
var dayAggregate DayAggregate
err := rows.Scan(&dayAggregate.Day, &dayAggregate.TotalActual)
err := rows.Scan(&dayAggregate.Day, &dayAggregate.TotalActual, &dayAggregate.TotalEstimate, &dayAggregate.TotalDone)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,17 @@ func handleEditCommand(args []string) {
// Helper function to handle the 'report' command
func handleReportCommand(args []string) {
reportFlag := flag.NewFlagSet("report", flag.ExitOnError)
reportType := reportFlag.String("type", "monthly", "Report type: 'monthly' or 'yearly'")
reportType := reportFlag.String("type", "monthly", "Report type: 'monthly','yearly' or 'weekly'")
// add a short version of the flag
reportFlag.StringVar(reportType, "t", "monthly", "Report type: 'monthly' or 'yearly'")
reportFlag.StringVar(reportType, "t", "weekly", "Report type: 'monthly' or 'yearly' or 'weekly'")
reportFlag.Parse(args)

if *reportType == "yearly" {
generateYearlyCalendarReport(db)
} else {
} else if *reportType == "monthly" {
generateMonthlyReport(db)
} else {
generateWeeklyReport(db)
}
}

Expand Down
30 changes: 30 additions & 0 deletions reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import (
"time"
)

func generateWeeklyReport(db *sql.DB) {
data, err := getYearlyData(db)
if err != nil {
log.Fatal(err)
}

// print the weekly report from Sunday to Saturday
fmt.Println("Weekly Report")
fmt.Println("Day | Done | Actual ")
fmt.Println("------------|------|----------------")

allDays := getAllDaysOfWeek()
dataMap := make(map[string]DayAggregate)

// Map data to dates
for _, dayAggregate := range data {
dataMap[dayAggregate.Day] = dayAggregate
}

for _, day := range allDays {
if aggregate, found := dataMap[day]; found {
actualTomatoes := generateEmojis(aggregate.TotalActual, "🍅")
fmt.Printf("%-11s | %-4d | %-7s\n", day, aggregate.TotalDone, actualTomatoes)
} else {
fmt.Printf("%-11s | %-4d | %-7s\n", day, 0, "")
}
}

}

func generateMonthlyReport(db *sql.DB) {
data, err := getMonthlyData(db)
if err != nil {
Expand Down

0 comments on commit 71bb369

Please sign in to comment.