mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-21 21:33:18 +01:00
Adds analytics for requests made (#1999)
This commit is contained in:
@@ -21,7 +21,25 @@ func SendStartEvent(se StartEvent) error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonValue, err := json.Marshal(postBody)
|
return doRequest(postBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendRequestEvent(re RequestEvent) error {
|
||||||
|
postBody := map[string]interface{}{
|
||||||
|
"client_id": re.ClientId,
|
||||||
|
"events": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"name": "request",
|
||||||
|
"params": re,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return doRequest(postBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doRequest(body map[string]interface{}) error {
|
||||||
|
jsonValue, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,3 +9,9 @@ type StartEvent struct {
|
|||||||
Protected bool `json:"protected"`
|
Protected bool `json:"protected"`
|
||||||
HasHostname bool `json:"hasHostname"`
|
HasHostname bool `json:"hasHostname"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RequestEvent struct {
|
||||||
|
ClientId string `json:"-"`
|
||||||
|
TotalContainers int `json:"totalContainers"`
|
||||||
|
RunningContainers int `json:"runningContainers"`
|
||||||
|
}
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -126,6 +126,7 @@ func main() {
|
|||||||
Username: args.Username,
|
Username: args.Username,
|
||||||
Password: args.Password,
|
Password: args.Password,
|
||||||
Hostname: args.Hostname,
|
Hostname: args.Hostname,
|
||||||
|
NoAnalytics: args.NoAnalytics,
|
||||||
}
|
}
|
||||||
|
|
||||||
assets, err := fs.Sub(content, "dist")
|
assets, err := fs.Sub(content, "dist")
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/amir20/dozzle/analytics"
|
||||||
"github.com/amir20/dozzle/docker"
|
"github.com/amir20/dozzle/docker"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@@ -22,6 +24,7 @@ type Config struct {
|
|||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
Hostname string
|
Hostname string
|
||||||
|
NoAnalytics bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
@@ -78,6 +81,27 @@ func (h *handler) index(w http.ResponseWriter, req *http.Request) {
|
|||||||
_, err := h.content.Open(req.URL.Path)
|
_, err := h.content.Open(req.URL.Path)
|
||||||
if err == nil && req.URL.Path != "" && req.URL.Path != "/" {
|
if err == nil && req.URL.Path != "" && req.URL.Path != "/" {
|
||||||
fileServer.ServeHTTP(w, req)
|
fileServer.ServeHTTP(w, req)
|
||||||
|
if !h.config.NoAnalytics {
|
||||||
|
go func() {
|
||||||
|
host, _ := os.Hostname()
|
||||||
|
|
||||||
|
if containers, err := h.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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if !isAuthorized(req) && req.URL.Path != "login" {
|
if !isAuthorized(req) && req.URL.Path != "login" {
|
||||||
http.Redirect(w, req, path.Clean(h.config.Base+"/login"), http.StatusTemporaryRedirect)
|
http.Redirect(w, req, path.Clean(h.config.Base+"/login"), http.StatusTemporaryRedirect)
|
||||||
|
|||||||
@@ -434,6 +434,7 @@ func Test_createRoutes_username_password_invalid_session(t *testing.T) {
|
|||||||
func createHandler(client docker.Client, content fs.FS, config Config) *mux.Router {
|
func createHandler(client docker.Client, content fs.FS, config Config) *mux.Router {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
client = new(MockedClient)
|
client = new(MockedClient)
|
||||||
|
client.(*MockedClient).On("ListContainers").Return([]docker.Container{}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if content == nil {
|
if content == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user