Skip to content

Commit

Permalink
feat: removes request analytics on page load (#2464)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 authored Nov 1, 2023
1 parent ebc0290 commit 6c1f59c
Showing 1 changed file with 17 additions and 51 deletions.
68 changes: 17 additions & 51 deletions internal/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"sort"

"net/http"
"os"
"path"

"github.com/amir20/dozzle/internal/analytics"
"github.com/amir20/dozzle/internal/auth"
"github.com/amir20/dozzle/internal/docker"
"github.com/amir20/dozzle/internal/profile"
Expand All @@ -27,35 +25,15 @@ func (h *handler) index(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, path.Clean(h.config.Base+"/login"), http.StatusTemporaryRedirect)
return
}
go h.sendRequestEvent()
h.executeTemplate(w, req)
}
}

func (h *handler) executeTemplate(w http.ResponseWriter, req *http.Request) {
file, err := h.content.Open("index.html")
if err != nil {
log.Panic(err)
}
bytes, err := io.ReadAll(file)
if err != nil {
log.Panic(err)
}
tmpl, err := template.New("index.html").Funcs(template.FuncMap{
"marshal": func(v interface{}) template.JS {
a, _ := json.Marshal(v)
return template.JS(a)
},
}).Parse(string(bytes))
if err != nil {
log.Panic(err)
}

base := ""
if h.config.Base != "/" {
base = h.config.Base
}

hosts := make([]*docker.Host, 0, len(h.clients))
for _, v := range h.clients {
hosts = append(hosts, v.Host())
Expand Down Expand Up @@ -102,6 +80,23 @@ func (h *handler) executeTemplate(w http.ResponseWriter, req *http.Request) {
"Manifest": h.readManifest(),
"Base": base,
}
file, err := h.content.Open("index.html")
if err != nil {
log.Panic(err)
}
bytes, err := io.ReadAll(file)
if err != nil {
log.Panic(err)
}
tmpl, err := template.New("index.html").Funcs(template.FuncMap{
"marshal": func(v interface{}) template.JS {
a, _ := json.Marshal(v)
return template.JS(a)
},
}).Parse(string(bytes))
if err != nil {
log.Panic(err)
}

err = tmpl.Execute(w, data)
if err != nil {
Expand Down Expand Up @@ -131,32 +126,3 @@ func (h *handler) readManifest() map[string]interface{} {
return manifest
}
}

func (h *handler) sendRequestEvent() {
if !h.config.NoAnalytics {
host, _ := os.Hostname()

var client DockerClient
for _, v := range h.clients {
client = v
break
}

if containers, err := client.ListContainers(); err == nil {
totalContainers := len(containers)
runningContainers := 0
for _, container := range containers {
if container.State == "running" {
runningContainers++
}
}

re := analytics.RequestEvent{
ClientId: host,
TotalContainers: totalContainers,
RunningContainers: runningContainers,
}
analytics.SendRequestEvent(re)
}
}
}

0 comments on commit 6c1f59c

Please sign in to comment.