added gitignore file and updated some stuff

This commit is contained in:
Daniel Sommer 2023-01-30 10:31:47 +01:00
parent 4998e24dfc
commit 2437680c29
4 changed files with 47 additions and 10 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
__debug_bin
worklog

View file

@ -58,9 +58,27 @@ func StopTimestamp() (bool, Timestamp) {
} }
func GetFirstTimestamp(user string, date time.Time) Timestamp { func GetFirstTimestamp(user string, date time.Time) Timestamp {
return GetTodaysTimestamp(user, date, true)
}
func GetLastTimestamp(user string, date time.Time) Timestamp {
return GetTodaysTimestamp(user, date, false)
}
func GetTodaysTimestamp(user string, date time.Time, first bool) Timestamp {
var timestamp Timestamp var timestamp Timestamp
date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC) var where string
connection.Where("start > ? and user = ?", date, user).First(&timestamp) var order string
if first {
date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)
order = "asc"
where = "start > ?"
} else {
date = time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, time.UTC)
order = "desc"
where = "start < ?"
}
connection.Where(where+" and user = ?", date, user).Order("start " + order).First(&timestamp)
return timestamp return timestamp
} }

View file

@ -27,6 +27,9 @@ func TimeToDDMMYYYY(time time.Time) string {
} }
func TimeToHHMMSS(time time.Time) string { func TimeToHHMMSS(time time.Time) string {
if time == ZeroDate {
return ""
}
tmp := strconv.Itoa(time.Second()) tmp := strconv.Itoa(time.Second())
if len(tmp) < 2 { if len(tmp) < 2 {
tmp = "0" + tmp tmp = "0" + tmp

View file

@ -41,6 +41,7 @@ func ToJSON(from time.Time, to time.Time, user string) (string, error) {
Stop: tools.TimeToHHMMSS(workday.Stop), Stop: tools.TimeToHHMMSS(workday.Stop),
Duration: tools.DurationToHHMMSS(workday.Duration), Duration: tools.DurationToHHMMSS(workday.Duration),
} }
workdayJson.Open = workday.isOpen()
if workday.Pause.Duration > 0 { if workday.Pause.Duration > 0 {
pauseJson := pauseJson{ pauseJson := pauseJson{
Start: tools.TimeToHHMMSS(workday.Pause.Start), Start: tools.TimeToHHMMSS(workday.Pause.Start),
@ -63,6 +64,7 @@ func (workday *Workday) ToJSON(user string) (string, error) {
Stop: tools.TimeToHHMMSS(workday.Stop), Stop: tools.TimeToHHMMSS(workday.Stop),
Duration: tools.DurationToHHMMSS(workday.Duration), Duration: tools.DurationToHHMMSS(workday.Duration),
} }
workdayJson.Open = workday.isOpen()
if workday.Pause.Duration > 0 { if workday.Pause.Duration > 0 {
pauseJson := pauseJson{ pauseJson := pauseJson{
Start: tools.TimeToHHMMSS(workday.Pause.Start), Start: tools.TimeToHHMMSS(workday.Pause.Start),
@ -77,17 +79,24 @@ func (workday *Workday) ToJSON(user string) (string, error) {
func GetToday(user string) (Workday, error) { func GetToday(user string) (Workday, error) {
var workday Workday var workday Workday
timestamp := database.GetFirstTimestamp(user, time.Now()) firstTimestamp := database.GetFirstTimestamp(user, time.Now())
if timestamp.ID == 0 { if firstTimestamp.ID == 0 {
return workday, errors.New("no workday for user '" + user + "' started today") return workday, errors.New("no workday for user '" + user + "' started today")
} }
workday.Date = tools.TimeToDDMMYYYY(timestamp.Start) workday.Date = tools.TimeToDDMMYYYY(firstTimestamp.Start)
workday.Start = timestamp.Start workday.Start = firstTimestamp.Start
workday.Stop = timestamp.End workday.Stop = firstTimestamp.End
if workday.Stop == tools.ZeroDate {
workday.Stop = time.Now()
}
workday.Duration = workday.Stop.Sub(workday.Start) workday.Duration = workday.Stop.Sub(workday.Start)
if workday.isOpen() {
return workday, nil
}
currentTimestamp := database.GetLastTimestamp(user, time.Now())
workday.Stop = currentTimestamp.End
if workday.Stop == tools.ZeroDate {
workday.Duration = time.Since(workday.Start)
} else {
workday.Duration = workday.Stop.Sub(workday.Start)
}
return workday, nil return workday, nil
} }
@ -144,6 +153,10 @@ func aggregatedTimestampsToWorkdays(aggregatedTimestamps map[string][]database.T
return workdays return workdays
} }
func (workday *Workday) isOpen() bool {
return workday.Stop == tools.ZeroDate
}
func (workday *Workday) insertFakePause() { func (workday *Workday) insertFakePause() {
var pause Pause var pause Pause
deviationStart := rand.Intn(15-(-15)) + (-15) deviationStart := rand.Intn(15-(-15)) + (-15)
@ -177,6 +190,7 @@ type workdayJson struct {
Start string Start string
Stop string Stop string
Duration string Duration string
Open bool
Pause pauseJson Pause pauseJson
} }