From 2d1157131bfb20adcee4ee5632c81027cbdc976c Mon Sep 17 00:00:00 2001 From: kekskurse Date: Sun, 16 Jan 2022 00:41:43 +0100 Subject: [PATCH] Template --- .gitignore | 4 +++- main.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- webpage.tmpl | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 webpage.tmpl diff --git a/.gitignore b/.gitignore index beff2fc..1ca683a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ eventlog -.idea/ \ No newline at end of file +.idea/ +ett +tmp/ \ No newline at end of file diff --git a/main.go b/main.go index 809d10d..c6375aa 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + _ "embed" "encoding/json" "flag" "github.com/rs/zerolog/log" @@ -9,6 +10,7 @@ import ( "os" "strconv" "strings" + "text/template" "time" ) @@ -19,6 +21,9 @@ type Event struct { var eventlog string +//go:embed webpage.tmpl +var templateContent []byte + //Current State var lastEvent Event var currentState map[string]map[string]int64 //Per Name per Day in Secounds @@ -48,7 +53,49 @@ func main() { func runHttpServer() { http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { - w.Write([]byte("

Time Tracking

")) + + var taglist []string + for s, _ := range currentState { + taglist = append(taglist, s) + } + + var daylist []string + for i := 0; i <= 7; i++ { + g := -1 * 24 * i + t := time.Now().Add(time.Duration(g) * time.Hour) + daylist = append(daylist, t.Format("02.01.06")) + } + + fm := template.FuncMap{"tominute": func(a int64) int64 { + return a / 60 + }} + + week := map[string]interface{}{} + for tag, _ := range currentState { + var gi int64 + for i := 0; i <= 7; i++ { + g := -1 * 24 * i + t := time.Now().Add(time.Duration(g) * time.Hour) + secounds := currentState[tag][t.Format("02.01.06")] + gi = gi + secounds + } + gi = gi / 60 + s := strconv.FormatInt(gi, 10) + week[tag] = s + } + + + templ := template.Must(template.New("page").Funcs(fm).Parse(string(templateContent))) + templ.Execute(w, map[string]interface{}{ + "lastEvent": lastEvent, + "taglist": taglist, + "daylist": daylist, + "currentState": currentState, + "weeklist": week, + }) + + + /*w.Write([]byte("

Time Tracking

")) w.Write([]byte("Doing "+lastEvent.Name+" since "+lastEvent.EventTimestamp.Format(time.RFC3339)+"
")) w.Write([]byte("

Today

")) for tag, _ := range currentState { @@ -72,6 +119,7 @@ func runHttpServer() { line := tag+": "+s+"
" w.Write([]byte(line)) } + */ }) http.HandleFunc("/current", func(w http.ResponseWriter, r *http.Request) { res, err := json.Marshal(lastEvent) diff --git a/webpage.tmpl b/webpage.tmpl new file mode 100644 index 0000000..94105ca --- /dev/null +++ b/webpage.tmpl @@ -0,0 +1,40 @@ + +

Time Tracking

+

Currently doing {{ .lastEvent.Name }} since {{ .lastEvent.EventTimestamp }}

+ +

Per Day

+ + + + {{ range .daylist }} + + {{ end }} + +{{ range $val := .taglist }} + + + {{ range $v := $.daylist }} + {{ $secounds := index $.currentState $val $v }} + + {{ end }} + +{{ end }} +
Tag{{ . }}
{{ $val }}{{tominute $secounds }}
+

Last 7 Days

+{{ range $a, $b := .weeklist }} +
  • {{ $a }}: {{ $b }}
  • +{{ end }} +

    Start

    +
    + +
    + +
    \ No newline at end of file