zeit/z/entry.go

189 lines
5.9 KiB
Go
Raw Normal View History

2020-10-11 14:12:34 +00:00
package z
import (
2020-10-11 20:14:22 +00:00
"errors"
2020-10-15 21:44:04 +00:00
"strings"
2020-10-11 14:12:34 +00:00
"time"
2020-10-15 20:38:03 +00:00
"fmt"
"github.com/gookit/color"
2020-10-11 14:12:34 +00:00
)
type Entry struct {
ID string `json:"-"`
Begin time.Time `json:"begin,omitempty"`
Finish time.Time `json:"finish,omitempty"`
Project string `json:"project,omitempty"`
Task string `json:"task,omitempty"`
2020-10-17 12:17:01 +00:00
Notes string `json:"notes,omitempty"`
2020-10-11 14:12:34 +00:00
User string `json:"user,omitempty"`
2020-10-15 22:52:27 +00:00
SHA1 string `json:"-"`
2020-10-11 14:12:34 +00:00
}
func NewEntry(
id string,
begin string,
finish string,
project string,
task string,
user string) (Entry, error) {
2020-10-15 09:54:11 +00:00
var err error
newEntry := Entry{}
newEntry.ID = id
newEntry.Project = project
newEntry.Task = task
newEntry.User = user
_, err = newEntry.SetBeginFromString(begin)
if err != nil {
return Entry{}, err
}
_, err = newEntry.SetFinishFromString(finish)
if err != nil {
return Entry{}, err
}
if newEntry.IsFinishedAfterBegan() == false {
return Entry{}, errors.New("beginning time of tracking cannot be after finish time")
}
return newEntry, nil
}
2020-10-15 21:44:04 +00:00
func (entry *Entry) SetIDFromDatabaseKey(key string) (error) {
splitKey := strings.Split(key, ":")
if len(splitKey) < 3 || len(splitKey) > 3 {
return errors.New("not a valid database key")
}
entry.ID = splitKey[2]
return nil
}
2020-10-15 09:54:11 +00:00
func (entry *Entry) SetBeginFromString(begin string) (time.Time, error) {
2020-10-11 14:12:34 +00:00
var beginTime time.Time
var err error
if begin == "" {
beginTime = time.Now()
} else {
beginTime, err = ParseTime(begin)
if err != nil {
2020-10-15 09:54:11 +00:00
return beginTime, err
2020-10-11 14:12:34 +00:00
}
}
2020-10-15 09:54:11 +00:00
entry.Begin = beginTime
return beginTime, nil
}
func (entry *Entry) SetFinishFromString(finish string) (time.Time, error) {
var finishTime time.Time
var err error
2020-10-11 14:12:34 +00:00
if finish != "" {
finishTime, err = ParseTime(finish)
if err != nil {
2020-10-15 09:54:11 +00:00
return finishTime, err
2020-10-11 14:12:34 +00:00
}
}
2020-10-15 09:54:11 +00:00
entry.Finish = finishTime
return finishTime, nil
}
2020-10-11 20:14:36 +00:00
2020-10-15 09:54:11 +00:00
func (entry *Entry) IsFinishedAfterBegan() (bool) {
2020-10-15 20:38:03 +00:00
return (entry.Finish.IsZero() || entry.Begin.Before(entry.Finish))
}
2020-10-15 20:56:04 +00:00
func (entry *Entry) GetOutputForTrack(isRunning bool, wasRunning bool) (string) {
var outputPrefix string = ""
var outputSuffix string = ""
now := time.Now()
trackDiffNow := now.Sub(entry.Begin)
trackDiffNowOut := time.Time{}.Add(trackDiffNow)
if isRunning == true && wasRunning == false {
outputPrefix = "began tracking"
} else if isRunning == true && wasRunning == true {
outputPrefix = "tracking"
outputSuffix = fmt.Sprintf(" for %sh", color.FgLightWhite.Render(trackDiffNowOut.Format("15:04")))
} else if isRunning == false && wasRunning == false {
2020-10-15 20:38:03 +00:00
outputPrefix = "tracked"
}
if entry.Task != "" && entry.Project != "" {
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s %s %s on %s%s\n", CharTrack, outputPrefix, color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), outputSuffix)
2020-10-15 20:38:03 +00:00
} else if entry.Task != "" && entry.Project == "" {
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s %s %s%s\n", CharTrack, outputPrefix, color.FgLightWhite.Render(entry.Task), outputSuffix)
2020-10-15 20:38:03 +00:00
} else if entry.Task == "" && entry.Project != "" {
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s %s task on %s%s\n", CharTrack, outputPrefix, color.FgLightWhite.Render(entry.Project), outputSuffix)
2020-10-15 20:38:03 +00:00
}
2020-10-15 20:56:04 +00:00
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s %s task%s\n", CharTrack, outputPrefix, outputSuffix)
2020-10-15 20:38:03 +00:00
}
func (entry *Entry) GetOutputForFinish() (string) {
2020-10-15 20:56:04 +00:00
var outputSuffix string = ""
2020-10-15 20:38:03 +00:00
trackDiff := entry.Finish.Sub(entry.Begin)
trackDiffOut := time.Time{}.Add(trackDiff)
2020-10-15 20:56:04 +00:00
outputSuffix = fmt.Sprintf(" for %sh", color.FgLightWhite.Render(trackDiffOut.Format("15:04")))
2020-10-15 20:38:03 +00:00
if entry.Task != "" && entry.Project != "" {
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s finished tracking %s on %s%s\n", CharFinish, color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), outputSuffix)
2020-10-15 20:38:03 +00:00
} else if entry.Task != "" && entry.Project == "" {
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s finished tracking %s%s\n", CharFinish, color.FgLightWhite.Render(entry.Task), outputSuffix)
2020-10-15 20:38:03 +00:00
} else if entry.Task == "" && entry.Project != "" {
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s finished tracking task on %s%s\n", CharFinish, color.FgLightWhite.Render(entry.Project), outputSuffix)
2020-10-15 20:38:03 +00:00
}
2020-10-15 21:07:36 +00:00
return fmt.Sprintf("%s finished tracking task%s\n", CharFinish, outputSuffix)
2020-10-11 14:12:34 +00:00
}
2020-10-15 21:44:12 +00:00
2020-10-17 20:14:00 +00:00
func (entry *Entry) GetOutput(full bool) (string) {
var output string = ""
2020-10-15 21:44:12 +00:00
trackDiff := entry.Finish.Sub(entry.Begin)
trackDiffOut := time.Time{}.Add(trackDiff)
2020-10-17 20:14:00 +00:00
if full == false {
output = fmt.Sprintf("%s %s on %s from %s to %s (%sh)", color.FgGray.Render(entry.ID), color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(entry.Finish.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(trackDiffOut.Format("15:04")))
} else {
output = fmt.Sprintf("%s\n %s on %s\n %sh from %s to %s\n\n Notes:\n %s\n", color.FgGray.Render(entry.ID), color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), color.FgLightWhite.Render(trackDiffOut.Format("15:04")), color.FgLightWhite.Render(entry.Begin.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(entry.Finish.Format("2006-01-02 15:04 -0700")), color.FgLightWhite.Render(strings.Replace(entry.Notes, "\n", "\n ", -1)) )
}
return output
2020-10-15 21:44:12 +00:00
}
2020-10-17 14:50:21 +00:00
func GetFilteredEntries(entries []Entry, project string, task string, since time.Time, until time.Time) ([]Entry, error) {
var filteredEntries []Entry
2020-10-17 14:50:21 +00:00
for _, entry := range entries {
if project != "" && GetIdFromName(entry.Project) != GetIdFromName(project) {
continue
}
if task != "" && GetIdFromName(entry.Task) != GetIdFromName(task) {
continue
}
if since.IsZero() == false && since.Before(entry.Begin) == false && since.Equal(entry.Begin) == false {
continue
}
if until.IsZero() == false && until.After(entry.Finish) == false && until.Equal(entry.Finish) == false {
continue
}
2020-10-17 14:50:21 +00:00
filteredEntries = append(filteredEntries, entry)
}
return filteredEntries, nil
}