timetracker

package module
v1.4.7 Latest Latest
Warning

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

Go to latest
Published: May 27, 2023 License: MIT Imports: 22 Imported by: 3

README

Go Reference GitHub go.mod Go version GitHub release (latest by date) Go Report Card Actions Status

HomeOffice Button - TimeTracker

Use a AWS IOT 1-Click button to track working time, illness or vacation in your home office and generate monthly reports.
IOT Time Tracking Architecture

AWS IOT 1-Click Button

This project uses a Seeed IoT button for AWS IoT 1-Click to capture time tracking events, e.g. start/end of work or illness and vacation.

Actions

A AWS IOT 1-Click button usually supports three types of click. Each click type will capture a different time tracking record.

Click Type Description
Single Click Captures time tracking record for working day.
Double Click  Use double click to capture illness.
 Long Press This click type is used to capture start of vacation.

Contents

This package includes different types of repositories, a report generator, an Excel output formatter and a publisher which uploads generated reports to a S3 bucket.

Repositories

Repository Description
LocaLRepository An in memory repositorie, e.g. for testing.
S3Repository  A repository which persists time tracking records in a S3 bucket.

Report Generator

The report generator creates a montly summary with working hours and breaks per day for a list of passed time tracking records.

Applied Rules

Report generating uses different rules to interpret time tracking records.

More then 2 events per working day

Each pair of WORKDAY events is used to calcualte working time. Time between is intepreted as a break.

Missing end of workday

If a workday has an odd number of captured events the end of a working day will be estimated, e.g. by default working hours defined in locale settings.

Fill days of illness or vacation

It's not necessary to click each day on the button if you're sick or on vacation. You only have to capture start of illness or vacation using corresponding click type. For monthly report this type will be used until next differing type occurs.

Order of types

If a day belongs to more than one type (WORKDAY.ILLNESS.VACATION) of time tracking events, they'll be used to determine type of the entire day in floowing order.

  • ILLNESS, has highest priority, overwrites all other
  • VACATION, will overwrite WORKDAY
  • WORKDAY, lowest priority

Report Formatter

A formatter takes a generated report to create an putput for it. You can pass a list of public holidays, the formatter will highlight them in its output.

Excel File

This formatter generates monthly report as an Excel file.

Report Publisher

S3 Publisher

This publisher uploads a generated report output to a S3 bucket.

File Publisher

Simply writes a report to a given local file.

eMail Publisher

Uses AWS SES to send an email with generated report attached.

Calendar

A calendar uses an external service to fetch public holidays.

Locale

In a locale you can define some settings e.g. country for holidays, time zone or default wokring time per day.

Following repoditories are using this time tracker package and contain AWS Lambda functions to capture click events and generate reports.

Repository Description
hob-core Contains core components for this project, e.g. event definitions and helper to serialize/deserialize events.
hob-iot-handler Handler to process events from AWS IOT 1-Click.
hob-apigw-handler  Handler for capture request send to AWs API Gateway.
 hob-report-generator Kambda function to generate and send monthly reports.

AWS IoT 1-Click
Seeed IoT button for AWS

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Calendar

type Calendar interface {

	// GetHolidays returns a list of holiday for given year and month.
	GetHolidays(int, int) ([]Holiday, error)
}

Calendar is used to get holidays or non-working days.

type CalendarApi

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

CalendarApi is used to retrieve holidays from an online service.

func NewCalendarApi added in v1.1.0

func NewCalendarApi(apiKey string, location Locale) *CalendarApi

newCalendarApi returns a new api to get holidays.

func (*CalendarApi) GetHolidays

func (api *CalendarApi) GetHolidays(year, month int) ([]Holiday, error)

GetHolidays try to fetch holidays for given month.

type Date

type Date struct {

	// Year this date belongs to.
	Year int

	// Month this date belongs to.
	Month int

	// Day this date belongs to.
	Day int
}

Date is a single calendar day.

func (Date) AsTime

func (d Date) AsTime() time.Time

AsTime returns a time object, at 00:00:00 UtC, for current date.

func (Date) Before

func (d Date) Before(d2 Date) bool

Before returns true if passed date is before current date.

func (Date) Equal added in v1.4.4

func (d Date) Equal(d2 Date) bool

Equal returns true if passed date is equal to current.

func (Date) Format

func (d Date) Format(layout string) string

Format returns a date as string in passed layout.

func (Date) IsEqual

func (d Date) IsEqual(t time.Time) bool

IsEqual return true if given date matched with current.

func (Date) String

func (d Date) String() string

String returns current date formatted with YYYY-MM-DD/2006-01-02.

type Day

type Day struct {

	// Date of this day.
	Date

	// Type of a time tracking event.
	Type RecordType

	// WorkingTime is the total time of work for a day.
	WorkingTime time.Duration

	// BreakTime is total time of breaks for a day.
	BreakTime time.Duration

	// Events is a list of captured time tracking events.
	Events []TimeTrackingRecord
}

Day is a single day of working, illness or vacations. It contains all time tracking events occurred for this date and calculated working/break time based on this events.

type EMailPublisher added in v1.3.0

type EMailPublisher struct {
	Source, Destination, Subject, Message string
}

EMailPublisher delivers time tracking reports via email.

func NewEMailPublisher added in v1.3.0

func NewEMailPublisher(source, destination, subject, message string) *EMailPublisher

NewEMailPublisher creates a new publsher to send time tracking reports via email.

func (*EMailPublisher) Send added in v1.3.0

func (publisher *EMailPublisher) Send(content []byte, fileName string) error

Send will deliver given time tracking report via email.

type ExcelReportFormatter

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

ExcelReportFormatter will generate Excel files for reports

func NewExcelReportFormatter

func NewExcelReportFormatter(logger log.Logger) *ExcelReportFormatter

NewExcelReportFormatter returns a new formatter to generate an excel file for a report.

func (*ExcelReportFormatter) FileExtension added in v1.1.0

func (formatter *ExcelReportFormatter) FileExtension() string

FileExtension reurns file extension for Exce files: xlsx.

func (*ExcelReportFormatter) WithHolidays

func (formatter *ExcelReportFormatter) WithHolidays(holidays []Holiday)

WithHolidays will assign give list of holidays for output formatting.

func (*ExcelReportFormatter) WriteMonthlyReportToBuffer

func (formatter *ExcelReportFormatter) WriteMonthlyReportToBuffer(report *MonthlyReport) (*bytes.Buffer, error)

WriteMonthlyReportToBuffer returns a buffer for gemerated report output.

func (*ExcelReportFormatter) WriteMonthlyReportToFile

func (formatter *ExcelReportFormatter) WriteMonthlyReportToFile(report *MonthlyReport, filename string) error

WriteMonthlyReportToFile will generate a report outout an writes it to given file.

type FilePublisher added in v1.1.0

type FilePublisher struct {
	FileMode os.FileMode
	Path     string
	// contains filtered or unexported fields
}

FilePublisher wirtes contents to files.

func NewFilePublisher added in v1.1.0

func NewFilePublisher(path *string, logger log.Logger) *FilePublisher

NewFilePublisher returns a new publisher which writes report content to files.

func (*FilePublisher) Send added in v1.1.0

func (publisher *FilePublisher) Send(content []byte, fileName string) error

Send will write passed content to given file name.

type Holiday

type Holiday struct {

	// Date of this day.
	Date

	// Description of a public holiday.
	Description string
}

Holiday is a single, public holiday.

type LocaLRepository

type LocaLRepository struct {
	Records map[string]map[Date][]TimeTrackingRecord
}

LocaLRepository is an in memory time tracker.

func NewLocaLRepository

func NewLocaLRepository() *LocaLRepository

NewLocaLRepository create a new, in menory, time tracker.

func (*LocaLRepository) Add added in v1.4.0

Add creates a new time tracking record with given values. Same time tacking record will be returned together with a generated key.

func (*LocaLRepository) Capture

func (repo *LocaLRepository) Capture(deviceId string, recordType RecordType) error

Capture will create a time tracking record with passed type at time this method has been called.

func (*LocaLRepository) Captured

func (repo *LocaLRepository) Captured(deviceId string, recordType RecordType, timestamp time.Time) error

Captured creates a time tracking record for passed point in time.

func (*LocaLRepository) Delete added in v1.4.0

func (repo *LocaLRepository) Delete(key string) error

Delete will remove given time tracking record.

func (*LocaLRepository) ListRecords

func (repo *LocaLRepository) ListRecords(deviceId string, start time.Time, end time.Time) ([]TimeTrackingRecord, error)

ListRecords returns available time tracking records for given range.

type Locale

type Locale struct {

	// ISO 3166-1 country code.
	Country string

	// Timezone, used to format time in reports.
	Timezone *string

	// DateFormat, used tp write dates in given format to report outputs.
	DateFormat *string

	// DefaultWorkTime is used if there's no end of work for a day, including breaks.
	DefaultWorkTime time.Duration

	// Breaks is a map of working durations and breaks which have to be applied for this time.
	Breaks map[time.Duration]time.Duration
}

Locale contains settings like country, region, time zone and working breaks.

type MonthlyReport

type MonthlyReport struct {

	// Year this report belongs to.
	Year int

	// Month this reprt has been created for.
	Month int

	// Location a report should be generated for.
	Location Locale

	// Days is the list of days in a momth.
	Days []Day

	// TotalWorkingTine is the entire working time of a month.
	TotalWorkingTime time.Duration
}

MonthlyReport included total amount of work for a month and details about each single day.

type RecordType

type RecordType string

RecordType defines with kind of event has been tracked.

const (

	// WORKDAY is used to track start and end of a usual workday.
	WORKDAY RecordType = "workday"

	// ILLNESS is used to track sick leave.
	ILLNESS RecordType = "illness"

	// VACATION to track holiday absence.
	VACATION RecordType = "vacation"

	// WEEKEND used for non-working days in a week.
	WEEKEND RecordType = "weekend"
)

type ReportCalculator

type ReportCalculator interface {

	// WithTimeTrackingRecords applies a list of reords for report calculation.
	WithTimeTrackingRecords([]TimeTrackingRecord)

	// MonthlyReport calculates a report for given year and month.
	MonthlyReport(int, int, RecordType) (*MonthlyReport, error)
}

ReportCalculator creates a time tracking summary based on captured records.

type ReportCalulator

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

ReportCalulator calulates working and break time for all day given by time tracking records.

func NewReportCalulator

func NewReportCalulator(records []TimeTrackingRecord, location Locale) *ReportCalulator

NewReportCalulator returns a new calulator using given time tracking records and local.

func (*ReportCalulator) MonthlyReport

func (calculator *ReportCalulator) MonthlyReport(year, month int, latestType RecordType) (*MonthlyReport, error)

MonthlyReport generates a report for given year and month from existing time tracking records.

func (*ReportCalulator) WithTimeTrackingRecords added in v1.0.4

func (calculator *ReportCalulator) WithTimeTrackingRecords(records []TimeTrackingRecord)

WithTimeTrackingRecords will apply given records for calculation.

type ReportFormatter

type ReportFormatter interface {

	// WithHolidays will assign give list of holidays for output formatting.
	WithHolidays(holidays []Holiday)

	// WriteMonthlyReportToFile will generate a report outout an writes it to given file.
	WriteMonthlyReportToFile(*MonthlyReport, string) error

	// WriteMonthlyReportToBuffer returns a buffer for gemerated report output.
	WriteMonthlyReportToBuffer(*MonthlyReport) (*bytes.Buffer, error)

	// FileExtension returns an extenstion for a report file.
	FileExtension() string
}

ReportFormatter generates an output for passed reports.

type ReportPublisher

type ReportPublisher interface {

	// Send publishes given report data to a target.
	Send([]byte, string) error
}

ReportPublisher sends given report to a defined target.

type S3Publisher

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

S3Publisher uploads given report to an AWS S3 bucket.

func NewS3Publisher

func NewS3Publisher(awsRegion, bucket, basePath *string, logger log.Logger) *S3Publisher

NewS3Publisher returns a new publisher to upload reports to AWS S3.

func (*S3Publisher) Send

func (publisher *S3Publisher) Send(data []byte, name string) error

Send will upload given report data to AWS S3.

type S3Repository

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

S3Repository uses AWS S3 bucket to persist time tracking records.

func NewS3Repository

func NewS3Repository(awsRegion, bucket, basePath *string) *S3Repository

NewS3Repository create a new repository to store time tracking records in AWS S3.

func (*S3Repository) Add added in v1.4.0

Add creates a new time tracking record with given values. Same time tacking record will be returned together with a generated key.

func (*S3Repository) Capture

func (repo *S3Repository) Capture(deviceId string, recordType RecordType) error

Capture will create a time tracking record with passed type at time this method has been called.

func (*S3Repository) Captured

func (repo *S3Repository) Captured(deviceId string, recordType RecordType, timestamp time.Time) error

Captured creates a time tracking record for passed point in time.

func (*S3Repository) Delete added in v1.4.0

func (repo *S3Repository) Delete(key string) error

Delete will remove given time tracking record.

func (*S3Repository) ListRecords

func (repo *S3Repository) ListRecords(deviceId string, start time.Time, end time.Time) ([]TimeTrackingRecord, error)

ListRecords returns all records captured for given device id and time range.

type TimeTracker

type TimeTracker interface {

	// Capture will create a time tracking record with passed type at time this method has been called.
	Capture(string, RecordType) error

	// Captured creates a time tracking record for passed point in time.
	Captured(string, RecordType, time.Time) error

	// ListRecords returns available time tracking records for given range.
	ListRecords(string, time.Time, time.Time) ([]TimeTrackingRecord, error)
}

TimeTracker is used to persist event, e.g start/end of a workday, illness and vacations. All timestamps are collected in UTC.

type TimeTrackingRecord

type TimeTrackingRecord struct {

	// Key is an unique identifier of a time tracking record.
	Key string

	// DeviceId is an identifier of a device which captures a time tracking record.
	DeviceId string

	// Type of a time tracking event.
	Type RecordType

	// Timestamp is the point in time a time tracking event has occurred.
	Timestamp time.Time

	// Estimated time tracking report a used to fill missing events. e.g. workday end if it not has been captured.
	Estimated bool
}

TimeTrackingReport os a single captured time tracking event.

type TimeTrackingRecordManager added in v1.4.0

type TimeTrackingRecordManager interface {

	// Add creates a new time tracking record with given values. Same time tacking record will be
	// returned together with a generated key.
	Add(TimeTrackingRecord) (TimeTrackingRecord, error)

	// Delete will remove time tracking record by passed key.
	Delete(string) error
}

TimeTrackingRecordManager is used to create, update or delete single time tracking records.

Jump to

Keyboard shortcuts

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