Template
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Kekskurse 2022-01-16 00:41:43 +01:00
parent cea9efd133
commit 2d1157131b
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
3 changed files with 92 additions and 2 deletions

4
.gitignore vendored
View file

@ -1,2 +1,4 @@
eventlog
.idea/
.idea/
ett
tmp/

50
main.go
View file

@ -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("<h1>Time Tracking</h1>"))
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("<h1>Time Tracking</h1>"))
w.Write([]byte("Doing "+lastEvent.Name+" since "+lastEvent.EventTimestamp.Format(time.RFC3339)+"<br>"))
w.Write([]byte("<h3>Today</h3>"))
for tag, _ := range currentState {
@ -72,6 +119,7 @@ func runHttpServer() {
line := tag+": "+s+"<br>"
w.Write([]byte(line))
}
*/
})
http.HandleFunc("/current", func(w http.ResponseWriter, r *http.Request) {
res, err := json.Marshal(lastEvent)

40
webpage.tmpl Normal file
View file

@ -0,0 +1,40 @@
<style type="text/css">
table, th, td {
border: 1px solid black;
}
</style>
<h1>Time Tracking</h1>
<p>Currently doing {{ .lastEvent.Name }} since {{ .lastEvent.EventTimestamp }}</p>
<h3>Per Day</h3>
<table>
<tr>
<td>Tag</td>
{{ range .daylist }}
<td>{{ . }}</td>
{{ end }}
</tr>
{{ range $val := .taglist }}
<tr>
<td>{{ $val }}</td>
{{ range $v := $.daylist }}
{{ $secounds := index $.currentState $val $v }}
<td>{{tominute $secounds }}</td>
{{ end }}
</tr>
{{ end }}
</table>
<h3>Last 7 Days</h3>
{{ range $a, $b := .weeklist }}
<li>{{ $a }}: {{ $b }}</li>
{{ end }}
<h3>Start</h3>
<form method="get" action="/event">
<select name="name">
{{ range $val := .taglist }}
<option>{{$val}}</option>
{{ end }}
</select>
<br/>
<input type="submit"></input>
</form>