Add Current endpoint
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Kekskurse 2022-01-14 22:48:29 +01:00
parent 68b4166c8d
commit 4e75f15ab3
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4

41
main.go
View file

@ -13,8 +13,8 @@ import (
)
type Event struct {
name string
eventTimestamp time.Time
Name string
EventTimestamp time.Time
}
var eventlog string
@ -35,8 +35,8 @@ func main() {
defer func() {
e := Event{
name: "Nothing",
eventTimestamp: time.Now(),
Name: "Nothing",
EventTimestamp: time.Now(),
}
addEvent(e)
}()
@ -71,6 +71,13 @@ func runHttpServer() {
w.Write([]byte(line))
}
})
http.HandleFunc("/current", func(w http.ResponseWriter, r *http.Request) {
res, err := json.Marshal(currentState)
if err != nil {
log.Fatal().Err(err).Msg("Cant Marshal Current State")
}
w.Write(res)
})
http.HandleFunc("/tags", func(w http.ResponseWriter, r *http.Request) {
@ -117,8 +124,8 @@ func runHttpServer() {
name := r.FormValue("name")
e := Event{
name: name,
eventTimestamp: eventTimestamp,
Name: name,
EventTimestamp: eventTimestamp,
}
addEvent(e)
@ -147,8 +154,8 @@ func readStateFromFile() {
log.Fatal().Err(err).Str("TimeString", details[0]).Msg("Cant parse time from eventlog")
}
e := Event{
name: details[1],
eventTimestamp: t,
Name: details[1],
EventTimestamp: t,
}
handleEvent(e)
@ -158,7 +165,7 @@ func readStateFromFile() {
func saveEvent(e Event) {
var line string
line = e.eventTimestamp.Format(time.RFC3339)+";"+e.name+"\r\n"
line = e.EventTimestamp.Format(time.RFC3339)+";"+e.Name +"\r\n"
f, err := os.OpenFile(eventlog, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
@ -180,17 +187,17 @@ func addEvent(e Event) {
func handleEvent(e Event) {
log.Debug().Msg("Handle New Event")
if lastEvent.name != "" {
duration := e.eventTimestamp.Sub(lastEvent.eventTimestamp)
log.Debug().Int64("Duration", int64(duration.Seconds())).Str("LastEventName", lastEvent.name).Msg("Calculatet Duration for last event")
if lastEvent.Name != "" {
duration := e.EventTimestamp.Sub(lastEvent.EventTimestamp)
log.Debug().Int64("Duration", int64(duration.Seconds())).Str("LastEventName", lastEvent.Name).Msg("Calculatet Duration for last event")
//Add to state
currentDuration := currentState[lastEvent.name][lastEvent.eventTimestamp.Format("02.01.06")]
if len(currentState[lastEvent.name]) == 0 {
currentState[lastEvent.name] = make(map[string]int64)
currentDuration := currentState[lastEvent.Name][lastEvent.EventTimestamp.Format("02.01.06")]
if len(currentState[lastEvent.Name]) == 0 {
currentState[lastEvent.Name] = make(map[string]int64)
}
currentDuration += int64(duration.Seconds())
log.Debug().Int64("Day Duration", currentDuration).Str("Day", lastEvent.eventTimestamp.Format("02.01.06")).Str("typ", lastEvent.name).Msg("Add Duration to Day")
currentState[lastEvent.name][lastEvent.eventTimestamp.Format("02.01.06")] = currentDuration
log.Debug().Int64("Day Duration", currentDuration).Str("Day", lastEvent.EventTimestamp.Format("02.01.06")).Str("typ", lastEvent.Name).Msg("Add Duration to Day")
currentState[lastEvent.Name][lastEvent.EventTimestamp.Format("02.01.06")] = currentDuration
}
lastEvent = e