8000 Only calculate time for current record if it hasn't ended by dominikbraun · Pull Request #7 · dominikbraun/timetrace · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
10 changes: 8 additions & 2 deletions cli/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ func statusCommand(t *core.Timetrace) *cobra.Command {

project := defaultString

if report.Current.Project != nil {
if report.Current != nil {
project = report.Current.Project.Key
}

trackedTimeCurrent := defaultString

if report.TrackedTimeCurrent != nil {
trackedTimeCurrent = report.TrackedTimeCurrent.String()
}

rows := [][]string{
{
project,
report.TrackedTimeCurrent.String(),
trackedTimeCurrent,
report.TrackedTimeToday.String(),
},
}
Expand Down
8 changes: 2 additions & 6 deletions core/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ func (t *Timetrace) DeleteRecord(record Record) error {
// loadLatestRecord loads the youngest record. This may also be a record from
// another day. If there is no latest record, nil and no error will be returned.
func (t *Timetrace) loadLatestRecord() (*Record, error) {
latestDirs, err := t.fs.RecordDirs(func(a, b string) bool {
dateA, _ := time.Parse("2006-01-02", a)
dateB, _ := time.Parse("2006-01-02", a)
return dateA.Before(dateB)
})
latestDirs, err := t.fs.RecordDirs()
if err != nil {
return nil, err
}
Expand All @@ -87,7 +83,7 @@ func (t *Timetrace) loadLatestRecord() (*Record, error) {
return nil, nil
}

dir := latestDirs[0]
dir := latestDirs[len(latestDirs)-1]

latestRecords, err := t.fs.RecordFilepaths(dir, func(a, b string) bool {
timeA, _ := time.Parse(recordLayout, a)
Expand Down
44 changes: 29 additions & 15 deletions core/timetrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (

type Report struct {
Current *Record
TrackedTimeCurrent time.Duration
TrackedTimeCurrent *time.Duration
TrackedTimeToday time.Duration
}

Expand All @@ -24,7 +24,7 @@ type Filesystem interface {
ProjectFilepaths() ([]string, error)
RecordFilepath(start time.Time) string
RecordFilepaths(dir string, less func(a, b string) bool) ([]string, error)
RecordDirs(less func(a, b string) bool) ([]string, error)
RecordDirs() ([]string, error)
RecordDirFromDate(date time.Time) string
EnsureDirectories() error
EnsureRecordDir(date time.Time) error
Expand Down Expand Up @@ -74,33 +74,47 @@ func (t *Timetrace) Start(projectKey string, isBillable bool) error {
return t.SaveRecord(record, false)
}

// Status creates and returns a report including the time worked since the last
// start and the overall time worked today.
// Status calculates and returns a status report.
//
// If the user isn't tracking time at the moment of calling this function, the
// Report.Current and Report.TrackedTimeCurrent fields will be nil. If the user
// hasn't tracked time today, ErrTrackingNotStarted will be returned.
func (t *Timetrace) Status() (*Report, error) {
latestRecord, err := t.loadLatestRecord()
now := time.Now()

firstRecord, err := t.loadOldestRecord(time.Now())
if err != nil {
return nil, err
}

if latestRecord == nil {
if firstRecord == nil {
return nil, ErrTrackingNotStarted
}

now := time.Now()

firstRecord, err := t.loadOldestRecord(now)
latestRecord, err := t.loadLatestRecord()
if err != nil {
return nil, err
}

var report Report

// If the latest record has been stopped, there is no active time tracking.
// Therefore, just calculate the tracked time of today and return.
if latestRecord.End != nil {
return &Report{
TrackedTimeToday: latestRecord.End.Sub(firstRecord.Start),
}, nil
}

report.Current = latestRecord

// If the latest record has not been stopped yet, time tracking is active.
// Calculate the time tracked for the current record and for today.
trackedTimeCurrent := now.Sub(latestRecord.Start)
trackedTimeToday := now.Sub(firstRecord.Start)
report.TrackedTimeCurrent = &trackedTimeCurrent
report.TrackedTimeToday = now.Sub(firstRecord.Start)

return &Report{
Current: latestRecord,
TrackedTimeCurrent: trackedTimeCurrent,
TrackedTimeToday: trackedTimeToday,
}, nil
return &report, nil
}

// Stop stops the time tracking and marks the current record as ended.
Expand Down
28 changes: 5 additions & 23 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,12 @@ func (fs *Fs) RecordFilepaths(dir string, less func(a, b string) bool) ([]string
return filepaths, nil
}

// RecordDirs returns all record directories sorted by the given function.
//
// For example, assume three directories containing record files:
//
// - timetrace/records/2021-05-01
// - timetrace/records/2021-05-02
// - timetrace/records/2021-05-03
//
// The following call to RecordDirs will return those directories sorted from
// newest to oldest:
//
// latestRecordDirs, err := RecordDirs(func (a, b string) bool {
// dateA, _ := time.Parse("2006-01-02", a)
// dateB, _ := time.Parse("2006-01-02", b)
// return dateA.Before(dateB)
// })
//
// This can be used to determine the latest record directory and obtain the
// latest record within that directory using RecordFilepaths.
// RecordDirs returns all record directories sorted alphabetically. This can be
// used to determine the latest record directory and obtain the latest record
// within that directory using RecordFilepaths.
//
// Note that all timetrace directories have to exist for RecordDirs to work.
func (fs *Fs) RecordDirs(less func(a, b string) bool) ([]string, error) {
func (fs *Fs) RecordDirs() ([]string, error) {
items, err := ioutil.ReadDir(fs.recordsDir())
if err != nil {
return nil, err
Expand All @@ -148,9 +132,7 @@ func (fs *Fs) RecordDirs(less func(a, b string) bool) ([]string, error) {
dirs = append(dirs, filepath.Join(fs.recordsDir(), item.Name()))
}

sort.Slice(dirs, func(i, j int) bool {
return less(dirs[i], dirs[j])
})
sort.Strings(dirs)

return dirs, nil
}
Expand Down
0