core

package
v0.14.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2022 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package core provides high-level functions for interacting with core entities like projects and records. This API can be used by the CLI and other clients.

Index

Constants

View Source
const (
	BakFileExt = ".bak"
)

Variables

View Source
var (
	ErrProjectNotFound       = errors.New("project not found")
	ErrBackupProjectNotFound = errors.New("backup project not found")
	ErrProjectAlreadyExists  = errors.New("project already exists")
	ErrParentlessModule      = errors.New("no parent project for module exists, please create parent first")
)
View Source
var (
	ErrRecordNotFound       = errors.New("record not found")
	ErrBackupRecordNotFound = errors.New("backup record not found")
	ErrRecordAlreadyExists  = errors.New("record already exists")
)
View Source
var (
	ErrNoEndTime           = errors.New("no end time for last record")
	ErrTrackingNotStarted  = errors.New("start tracking first")
	ErrAllDirectoriesEmpty = errors.New("all directories empty")
)

Functions

func FilterBillable added in v0.11.0

func FilterBillable(display bool) func(*Record) bool

FilterBillable returns a record filter for the billable flag that filters based on the passed display value

func FilterByProject added in v0.11.0

func FilterByProject(key string) func(*Record) bool

FilterByProject returns true if the record matches the given project keys if a module is given "mod@project" filter checks if module and project match

func FilterByTimeRange added in v0.11.0

func FilterByTimeRange(start, end time.Time) func(*Record) bool

FilterByTimeRange allows to determine whether a given records is in-between a time-range. If "to" is nil the upper boundary is ignored and vice versa with "from". If both are nil returns true start and end time are both inclusive. Explanition for the `to.AddDate(0,0,1)`: the "to" input will be YYYY-MM-DD 00:00:00, hence the actual tracked records of that date will be ignored as they are all bigger since their hh:mm:ss will be grather then 00:00:00 of the "to" time. Adding one day to the "to" time will include records tracked on that date thus will make the "to" time inclusive

func FilterNoneNilEndTime added in v0.11.0

func FilterNoneNilEndTime(r *Record) bool

Types

type Filesystem

type Filesystem interface {
	ProjectFilepath(key string) string
	ProjectBackupFilepath(key string) string
	ProjectFilepaths() ([]string, error)
	ProjectBackupFilepaths() ([]string, error)
	RecordFilepath(start time.Time) string
	RecordBackupFilepath(start time.Time) string
	RecordFilepaths(dir string, less func(a, b string) bool) ([]string, error)
	RecordDirs() ([]string, error)
	ReportDir() string
	RecordDirFromDate(date time.Time) string
	EnsureDirectories() error
	EnsureRecordDir(date time.Time) error
	WriteReport(path string, data []byte) error
}

Filesystem represents a filesystem used for storing and loading resources.

type Formatter added in v0.4.0

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter represents a date- and time formatter. It provides all displayed date- and time layouts and is capable of parsing those layouts.

func (*Formatter) CombineDateAndTime added in v0.11.0

func (f *Formatter) CombineDateAndTime(d, t time.Time) time.Time

CombineDateAndTime takes a date and a time and combines them to the time struct that represents the given time on the given day

func (*Formatter) FormatDuration added in v0.9.0

func (f *Formatter) FormatDuration(duration time.Duration) string

formatDuration formats the passed duration into a string. The format is determined by value of UseDecimalHours in config file and will be "8h 24min", "8.4h, or "8h 24min 8.4h". seconds information is ignored.

func (*Formatter) FormatTags added in v0.14.0

func (f *Formatter) FormatTags(tags []string) string

func (*Formatter) ParseDate added in v0.6.0

func (f *Formatter) ParseDate(input string) (time.Time, error)

ParseDate parses a date from an input string in the form YYYY-MM-DD. It also supports the `today` and `yesterday` aliases for convenience.

func (*Formatter) ParseRecordKey added in v0.6.0

func (f *Formatter) ParseRecordKey(key string) (time.Time, error)

ParseRecordKey parses an input string in the form 2006-01-02-15-04 or 2006-01-02-03-04PM depending on the use12hours setting.

func (*Formatter) ParseTime added in v0.11.0

func (f *Formatter) ParseTime(input string) (time.Time, error)

ParseTime parses a time from an input string in the configured timeLayout

func (*Formatter) PrettyDateString added in v0.11.0

func (f *Formatter) PrettyDateString(input time.Time) string

PrettyDateString returns a nice representation of a given time example: Mon 31. May 2021

func (*Formatter) RecordKey added in v0.6.0

func (f *Formatter) RecordKey(record *Record) string

func (*Formatter) RecordKeyLayout added in v0.6.0

func (f *Formatter) RecordKeyLayout() string

func (*Formatter) TimeString added in v0.4.0

func (f *Formatter) TimeString(input time.Time) string

type Project

type Project struct {
	Key string `json:"key"`
}

func (*Project) IsModule added in v0.5.0

func (p *Project) IsModule() bool

func (*Project) Parent added in v0.5.0

func (p *Project) Parent() string

Parent returns the parent project of the current project or an empty string if there is no parent. If it has a parent, the current project is a module.

type Record

type Record struct {
	Start      time.Time  `json:"start"`
	End        *time.Time `json:"end"`
	Project    *Project   `json:"project"`
	IsBillable bool       `json:"is_billable"`
	Tags       []string   `json:"tags"`
}

func (*Record) Duration added in v0.13.0

func (r *Record) Duration() time.Duration

Duration calculates time duration for a specific record. If the record doesn't have an end time, then it is expected that time is still being tracked, and duration will be counted to a current time since start.

type Report

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

type Reporter added in v0.11.0

type Reporter struct {
	// contains filtered or unexported fields
}

Reporter holds map of projects with slice of project records

func (Reporter) Json added in v0.11.0

func (r Reporter) Json() ([]byte, error)

Json prepares the r.report and r.totals data so that it can be written to a json file

func (Reporter) Table added in v0.11.0

func (r Reporter) Table() ([][]string, string)

Table prepares the r.report and r.totals data in a way that it can be consumed by the out.Table It returns a [][]string where each []string represents one record of a project and the total sum of time for all projects

type Timetrace

type Timetrace struct {
	// contains filtered or unexported fields
}

func New

func New(config *config.Config, fs Filesystem) *Timetrace

func (*Timetrace) BackupProject added in v0.8.0

func (t *Timetrace) BackupProject(projectKey string) error

BackupProject creates a backup of the given project file.

func (*Timetrace) BackupRecord added in v0.8.0

func (t *Timetrace) BackupRecord(recordKey time.Time) error

BackupRecord creates a backup of the given record file

func (*Timetrace) Config

func (t *Timetrace) Config() *config.Config

func (*Timetrace) DeleteProject

func (t *Timetrace) DeleteProject(project Project) error

DeleteProject removes the given project and any associated submodules. Returns ErrProjectNotFound if the project doesn't exist.

func (*Timetrace) DeleteRecord

func (t *Timetrace) DeleteRecord(record Record) error

DeleteRecord removes the given record. Returns ErrRecordNotFound if the project doesn't exist.

func (*Timetrace) DeleteRecordsByProject added in v0.13.0

func (t *Timetrace) DeleteRecordsByProject(key string) error

func (*Timetrace) EditProject

func (t *Timetrace) EditProject(projectKey string) error

EditProject opens the project file in the preferred or default editor .

func (*Timetrace) EditRecord added in v0.6.0

func (t *Timetrace) EditRecord(recordTime time.Time, plus string, minus string) error

EditRecord loads the record internally, applies the option values and saves the record

func (*Timetrace) EditRecordManual added in v0.6.0

func (t *Timetrace) EditRecordManual(recordTime time.Time) error

EditRecordManual opens the record file in the preferred or default editor.

func (*Timetrace) EnsureDirectories

func (t *Timetrace) EnsureDirectories() error

func (*Timetrace) Formatter added in v0.4.0

func (t *Timetrace) Formatter() *Formatter

func (*Timetrace) ListProjectModules added in v0.7.0

func (t *Timetrace) ListProjectModules(project *Project) (string, error)

ListProjectModules loads all modules for a project and returns their keys as a concatenated string

func (*Timetrace) ListProjects

func (t *Timetrace) ListProjects() ([]*Project, error)

ListProjects loads and returns all stored projects sorted by their filenames. If no projects are found, an empty slice and no error will be returned.

func (*Timetrace) ListRecords added in v0.2.0

func (t *Timetrace) ListRecords(date time.Time) ([]*Record, error)

ListRecords loads and returns all records from the given date. If no records are found, an empty slice and no error will be returned.

func (*Timetrace) LoadBackupProject added in v0.8.0

func (t *Timetrace) LoadBackupProject(key string) (*Project, error)

func (*Timetrace) LoadBackupRecord added in v0.8.0

func (t *Timetrace) LoadBackupRecord(start time.Time) (*Record, error)

func (*Timetrace) LoadLatestRecord added in v0.6.0

func (t *Timetrace) LoadLatestRecord() (*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 (*Timetrace) LoadProject

func (t *Timetrace) LoadProject(key string) (*Project, error)

LoadProject loads the project with the given key. Returns ErrProjectNotFound if the project cannot be found.

func (*Timetrace) LoadRecord

func (t *Timetrace) LoadRecord(start time.Time) (*Record, error)

LoadRecord loads the record with the given start time. Returns ErrRecordNotFound if the record cannot be found.

func (*Timetrace) LoadRecordByID added in v0.14.0

func (t *Timetrace) LoadRecordByID(ID int) (*Record, error)

LoadRecordByID loads a record of the current day by the ID provided in list records (starting with the oldest as #1)

func (*Timetrace) RecordCollides added in v0.11.0

func (t *Timetrace) RecordCollides(toCheck Record) (bool, error)

RecordCollides checks if the time of a record collides with other records of the same day and returns a bool

func (*Timetrace) Report added in v0.11.0

func (t *Timetrace) Report(filter ...func(*Record) bool) (*Reporter, error)

Report generates a report of tracked times

The report can be filtered by the given Filter* funcs. By default all records of all projects will be collected. Interaction with the report can be done via the Reporter instance

func (*Timetrace) RevertProject added in v0.8.0

func (t *Timetrace) RevertProject(projectKey string) error

RevertProject reverts the given project to its latest backup

func (*Timetrace) RevertRecord added in v0.8.0

func (t *Timetrace) RevertRecord(recordKey time.Time) error

func (*Timetrace) RevertRecordsByProject added in v0.13.0

func (t *Timetrace) RevertRecordsByProject(key string) error

RevertRecordsByProject is a function called if user opts to also revert records when they revert a project.

func (*Timetrace) SaveProject

func (t *Timetrace) SaveProject(project Project, force bool) error

SaveProject persists the given project. Returns ErrProjectAlreadyExists if the project already exists and saving isn't forced.

func (*Timetrace) SaveRecord

func (t *Timetrace) SaveRecord(record Record, force bool) error

SaveRecord persists the given record. Returns ErrRecordAlreadyExists if the record already exists and saving isn't forced.

func (*Timetrace) Start

func (t *Timetrace) Start(projectKey string, isBillable bool, tags []string) error

Start starts tracking time for the given project key. This will create a new record with the current time as start time.

Since parallel work isn't supported, the previous work must be stopped first.

func (*Timetrace) Status

func (t *Timetrace) Status() (*Report, error)

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 (*Timetrace) Stop

func (t *Timetrace) Stop() error

Stop stops the time tracking and marks the current record as ended.

func (*Timetrace) WriteReport added in v0.11.0

func (t *Timetrace) WriteReport(path string, data []byte) error

WriteReport forwards the byte slice to the fs but checks in prior for the correct output path. If the user has not provided one the config.ReportPath will be used if not set path falls-back to $HOME/.timetrace/reports/report-<time.unix>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL