Skip to content

Commit

Permalink
fix and log a bunch of issues; get to rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-he committed Jul 27, 2024
1 parent 2145e2b commit b1d8c40
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 235 deletions.
9 changes: 7 additions & 2 deletions internal/control/cli/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/ja-he/dayplan/internal/control"
Expand Down Expand Up @@ -57,6 +58,8 @@ type Controller struct {

// TODO: try to get rid of this
ensureEventsPaneTimestampWithinVisibleScroll func(time.Time)

log zerolog.Logger
}

// NewController creates a new Controller.
Expand All @@ -66,7 +69,10 @@ func NewController(
categoryStyling styling.CategoryStyling,
stylesheet styling.Stylesheet,
) (*Controller, error) {
controller := Controller{}
controller := Controller{
log: log.With().Str("component", "controller").Logger(),
}
defer controller.goToDay(date)

categoryGetter := func(name string) model.Category {
cat, ok := categoryStyling.GetKnownCategoriesByName()[name]
Expand All @@ -85,7 +91,6 @@ func NewController(
var err error
dp, err = providers.NewFilesDataProvider(
path.Join(envData.BaseDirPath, "days"),
controller.data.Categories,
)
if err != nil {
return nil, fmt.Errorf("cannot initialize data provider (%w)", err)
Expand Down
67 changes: 30 additions & 37 deletions internal/control/cli/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"log"
"os"
"path"
"strings"
Expand All @@ -11,15 +10,16 @@ import (
"github.com/ja-he/dayplan/internal/config"
"github.com/ja-he/dayplan/internal/control"
"github.com/ja-he/dayplan/internal/model"
"github.com/ja-he/dayplan/internal/storage"
"github.com/ja-he/dayplan/internal/storage/providers"
"github.com/ja-he/dayplan/internal/styling"
)

// Flags for the `summarize` command line command, for `go-flags` to parse
// command line args into.
type SummarizeCommand struct {
FromDay string `short:"f" long:"from" description:"the day from which to start summarizing" value-name:"<yyyy-mm-dd>" required:"true"`
TilDay string `short:"t" long:"til" description:"the day til which to summarize (inclusive)" value-name:"<yyyy-mm-dd>" required:"true"`
From time.Time `short:"f" long:"from" description:"the timestamp from which to start summarizing" value-name:"<TIME>" required:"true"`
Til time.Time `short:"t" long:"til" description:"the timestamp until which to summarize" value-name:"<TIME>" required:"true"`

HumanReadable bool `long:"human-readable" description:"format times as hours and minutes"`
CategoryFilterString string `long:"category-filter" description:"a filter for categories; any named categories included; all included if omitted" value-name:"<cat1>,<cat2>,..."`
Expand Down Expand Up @@ -73,14 +73,8 @@ func (command *SummarizeCommand) Execute(args []string) error {
styledCategories.Add(cat, style)
}

startDate, err := model.FromString(Opts.SummarizeCommand.FromDay)
if err != nil {
log.Fatalf("from date '%s' invalid", Opts.SummarizeCommand.FromDay)
}
currentDate := startDate
finalDate, err := model.FromString(Opts.SummarizeCommand.TilDay)
if err != nil {
log.Fatalf("til date '%s' invalid", Opts.SummarizeCommand.TilDay)
if command.Til.Before(command.From) {
return fmt.Errorf("from-time must be before til-time")
}

filterCategories := len(Opts.SummarizeCommand.CategoryFilterString) > 0
Expand All @@ -92,44 +86,43 @@ func (command *SummarizeCommand) Execute(args []string) error {
}

// TODO: can probably make this mostly async?
days := make([]model.EventList, 0)
for currentDate != finalDate.Next() {
fh := providers.NewFileHandler(path.Join(envData.BaseDirPath, "days"), currentDate)
categories := make([]model.Category, 0)
for _, cat := range styledCategories.GetAll() {
categories = append(categories, cat.Cat)
}
days = append(days, *fh.Read(categories))

currentDate = currentDate.Next()
var dataProvider storage.DataProvider
dataProvider, err = providers.NewFilesDataProvider(path.Join(envData.BaseDirPath, "days"))
if err != nil {
return fmt.Errorf("can't create file data provider (%w)", err)
}

categoryIncluded := func(cat model.Category) bool {
_, ok := includeCategoriesByName[cat.Name]
categoryIncluded := func(categoryName string) bool {
_, ok := includeCategoriesByName[categoryName]
return ok
}

totalSummary := make(map[model.Category]time.Duration)
for _, day := range days {
daySummary := day.SumUpByCategory()
for category, duration := range daySummary {
totalSummary[category] += duration
}
}
totalSummary := dataProvider.SumUpTimespanByCategory(command.From, command.Til)

if Opts.SummarizeCommand.Verbose {
fmt.Println("dayplan time summary:")

fmt.Println("from: ", Opts.SummarizeCommand.FromDay)
fmt.Println("til: ", Opts.SummarizeCommand.TilDay)
fmt.Println("category filter: ", Opts.SummarizeCommand.CategoryFilterString)
fmt.Println("from: ", command.From)
fmt.Println("til: ", command.Til)
fmt.Println("category filter: ", command.CategoryFilterString)

fmt.Println("read", len(days), "days")
fmt.Println("total summary:")
}

for category, duration := range totalSummary {
if filterCategories && !categoryIncluded(category) {
categoriesByName := styledCategories.GetKnownCategoriesByName()
for categoryName, duration := range totalSummary {
category, ok := categoriesByName[categoryName]
if !ok {
fmt.Fprint(os.Stderr, "warning: category '", categoryName, "' not found in config\n")
category = &model.Category{
Name: categoryName,
Priority: 0,
Goal: nil,
Deprecated: false,
}
}

if filterCategories && !categoryIncluded(categoryName) {
continue
}

Expand All @@ -142,7 +135,7 @@ func (command *SummarizeCommand) Execute(args []string) error {

var goalStr string = ""
if category.Goal != nil {
goal := model.GoalForRange(category.Goal, startDate, finalDate)
goal := model.GoalForRange(category.Goal, command.From, command.Til)
actual := time.Duration(duration) * time.Minute
deficit := goal - actual
deficitStr := fmt.Sprint(deficit - (deficit % time.Minute))
Expand Down
24 changes: 14 additions & 10 deletions internal/control/cli/timesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package cli

import (
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
"time"

"github.com/ja-he/dayplan/internal/config"
"github.com/ja-he/dayplan/internal/control"
"github.com/ja-he/dayplan/internal/model"
"github.com/ja-he/dayplan/internal/storage/providers"
"github.com/ja-he/dayplan/internal/storage"
"github.com/ja-he/dayplan/internal/styling"
"github.com/ja-he/dayplan/internal/util"
"github.com/rs/zerolog/log"
)

// TimesheetCommand is the command `timesheet`, which produces a timesheet for
Expand Down Expand Up @@ -91,27 +90,32 @@ func (command *TimesheetCommand) Execute(args []string) error {

startDate, err := model.FromString(command.FromDay)
if err != nil {
log.Fatalf("from date '%s' invalid", command.FromDay)
log.Fatal().Msgf("from date '%s' invalid", command.FromDay)
}
currentDate := startDate
finalDate, err := model.FromString(command.TilDay)
if err != nil {
log.Fatalf("til date '%s' invalid", command.TilDay)
log.Fatal().Msgf("til date '%s' invalid", command.TilDay)
}

type dateAndDay struct {
model.Date
model.EventList
}

var dataProvider storage.DataProvider
log.Fatal().Msg("TODO: initialize data provider")

data := make([]dateAndDay, 0)
for currentDate != finalDate.Next() {
fh := providers.NewFileHandler(path.Join(envData.BaseDirPath+"days"), currentDate)
categories := make([]model.Category, 0)
for _, cat := range styledCategories.GetAll() {
categories = append(categories, cat.Cat)
events, err := dataProvider.GetEventsCoveringTimerange(currentDate.ToGotime(), currentDate.ToGotime().Add(24*time.Hour))
if err != nil {
return fmt.Errorf("error while getting events for %s (%w)", currentDate.String(), err)
}
data = append(data, dateAndDay{currentDate, *fh.Read(categories)})
data = append(data, dateAndDay{
currentDate,
model.EventList{Events: events},
})

currentDate = currentDate.Next()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// ...
type Event struct {
Name string `dpedit:"name"`
Cat Category `dpedit:"category"`
Cat Category `dpedit:"category"` // TODO: change to just category name
Start time.Time `dpedit:",ignore"`
End time.Time `dpedit:",ignore"`
}
Expand Down
14 changes: 4 additions & 10 deletions internal/model/goal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/ja-he/dayplan/internal/config"
"github.com/rs/zerolog/log"
)

// Goal defines a time goal.
Expand Down Expand Up @@ -169,14 +170,7 @@ func NewWorkweekGoalFromConfig(cfg config.WorkweekGoal) (*WorkweekGoal, error) {

// GoalForRange is a helper to sum up the duration for the given range expected
// by the given Goal.
func GoalForRange(goal Goal, startDate, endDate Date) time.Duration {
sum := time.Duration(0)

currentDate := startDate
for currentDate != endDate.Next() {
sum += goal.Requires(currentDate)
currentDate = currentDate.Next()
}

return sum
func GoalForRange(goal Goal, start, end time.Time) time.Duration {
log.Fatal().Msgf("TODO: GoalForRange not implemented")
return 0
}
2 changes: 2 additions & 0 deletions internal/storage/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type DataProvider interface {
SetEventCategory(EventIdentifier, model.Category) error
SetEventAllData(EventIdentifier, model.Event) error

SumUpTimespanByCategory(start time.Time, end time.Time) map[string]time.Duration

// need something here for mutability, e.g. constructing an editor...

CommitState() error
Expand Down
Loading

0 comments on commit b1d8c40

Please sign in to comment.