diff --git a/application/Dockerfile b/application/Dockerfile index 1f47cf2..15c0a63 100644 --- a/application/Dockerfile +++ b/application/Dockerfile @@ -28,6 +28,3 @@ FROM alpine COPY --from=frontbuilder /code/dist/ /dist/ COPY --from=backendbuilder /backend/main /backend/main CMD ["/backend/main"] - -# docker run -v /var/run/docker.sock:/var/run/docker.sock --rm -it $(docker build -f Dockerfile .) -# docker build . -t devforth/onlogs && docker push devforth/onlogs diff --git a/application/backend/app/containerdb/containerdb.go b/application/backend/app/containerdb/containerdb.go index 0fd573b..86f7cce 100644 --- a/application/backend/app/containerdb/containerdb.go +++ b/application/backend/app/containerdb/containerdb.go @@ -95,65 +95,6 @@ func getDateTimeFromKey(key string) string { return strings.Split(key, " +")[0] } -// # TODO: should be merged with GetLogs function -/* -Get logs line by line with filtering by logline status. - - getPrev - if true, will get logs from latest to oldest. - - include - if true, will include logs with startWith key. - -returns json obj same to GetLogs function. -*/ -func GetLogsByStatus(host string, container string, message string, status string, limit int, startWith string, getPrev bool, include bool, caseSensetivity bool) map[string]interface{} { - logs_db := util.GetDB(host, container, "logs") - db := util.GetDB(host, container, "statuses") - iter := db.NewIterator(nil, nil) - defer iter.Release() - to_return := map[string]interface{}{} - to_return["logs"] = [][]string{} - move_direction := getMoveDirection(getPrev, iter) - - if !searchInit(iter, startWith, getPrev, include, move_direction) { - to_return["is_end"] = true - return to_return - } - - counter := 0 - iteration := 0 - last_processed_key := []string{} - for counter < limit && iteration < 1000000 { - iteration += 1 - key := iter.Key() - if len(key) == 0 { - to_return["is_end"] = true - increaseAndMove(&counter, move_direction) - continue - } else { - to_return["is_end"] = false - } - - value := string(iter.Value()) - if value != status { - move_direction() - continue - } - - last_processed_key = []string{string(key), value} - res, _ := logs_db.Get(key, nil) - logLine := string(res) - - if !fitsForSearch(logLine, message, caseSensetivity) { - move_direction() - continue - } - - to_return["logs"] = append(to_return["logs"].([][]string), []string{getDateTimeFromKey(string(key)), logLine}) - increaseAndMove(&counter, move_direction) - } - - to_return["last_processed_key"] = last_processed_key - return to_return -} - /* Get logs line by line from container. - getPrev - if true, will get logs from latest to oldest. @@ -167,12 +108,19 @@ returns json obj like this: "is_end": false } */ -func GetLogs(getPrev bool, include bool, host string, container string, message string, limit int, startWith string, caseSensetivity bool) map[string]interface{} { +func GetLogs(getPrev bool, include bool, host string, container string, message string, limit int, startWith string, caseSensetivity bool, status *string) map[string]interface{} { logs_db := util.GetDB(host, container, "logs") + var statusDb *leveldb.DB + if status != nil { + statusDb = util.GetDB(host, container, "statuses") + } iter := logs_db.NewIterator(nil, nil) defer iter.Release() - to_return := map[string]interface{}{} - to_return["logs"] = [][]string{} + + to_return := map[string]interface{}{ + "logs": [][]string{}, + } + logs := [][]string{} move_direction := getMoveDirection(getPrev, iter) if !searchInit(iter, startWith, getPrev, include, move_direction) { @@ -185,7 +133,7 @@ func GetLogs(getPrev bool, include bool, host string, container string, message last_processed_key := "" for counter < limit && iteration < 1000000 { iteration += 1 - key := string(iter.Key()) + key := iter.Key() if len(key) == 0 { to_return["is_end"] = true increaseAndMove(&counter, move_direction) @@ -193,18 +141,29 @@ func GetLogs(getPrev bool, include bool, host string, container string, message } else { to_return["is_end"] = false } - last_processed_key = key + + keyStr := string(key) value := string(iter.Value()) + if status != nil { + statusValue, err := statusDb.Get(key, nil) + if err != nil || string(statusValue) != *status { + move_direction() + continue + } + } + if !fitsForSearch(value, message, caseSensetivity) { move_direction() continue } - to_return["logs"] = append(to_return["logs"].([][]string), []string{getDateTimeFromKey(key), value}) + logs = append(logs, []string{getDateTimeFromKey(keyStr), value}) increaseAndMove(&counter, move_direction) + last_processed_key = keyStr } + to_return["logs"] = logs to_return["last_processed_key"] = last_processed_key return to_return } diff --git a/application/backend/app/containerdb/containerdb_test.go b/application/backend/app/containerdb/containerdb_test.go index cf92fec..4be7472 100644 --- a/application/backend/app/containerdb/containerdb_test.go +++ b/application/backend/app/containerdb/containerdb_test.go @@ -65,19 +65,25 @@ func TestGetLogs(t *testing.T) { db.Close() var logs [][]string - logs = GetLogs(false, true, "Test", "TestGetLogsCont", "", 30, vars.Year+"-02-10T12:57:09.230421754Z", false)["logs"].([][]string) + logs = GetLogs(false, true, "Test", "TestGetLogsCont", "", 30, vars.Year+"-02-10T12:57:09.230421754Z", false, nil)["logs"].([][]string) if len(logs) != 5 { t.Error("5 logItems must be returned!") } if logs[0][0] != vars.Year+"-02-10T12:57:09.230421754Z" { t.Error("Invalid first logItem datetime: ", logs[0][0]) } + if logs[4][0] != vars.Year+"-02-10T12:51:09.230421754Z" { + t.Error("Invalid last logItem datetime: ", logs[4][0]) + } - logs = GetLogs(true, false, "Test", "TestGetLogsCont", "", 30, vars.Year+"-02-10T12:51:09.230421754Z", false)["logs"].([][]string) + logs = GetLogs(true, false, "Test", "TestGetLogsCont", "", 30, vars.Year+"-02-10T12:51:09.230421754Z", false, nil)["logs"].([][]string) if len(logs) != 4 { t.Error("4 logItems must be returned!") } if logs[0][0] != vars.Year+"-02-10T12:52:09.230421754Z" { t.Error("Invalid first logItem datetime: ", logs[0][0]) } + if logs[3][0] != vars.Year+"-02-10T12:57:09.230421754Z" { + t.Error("Invalid last logItem datetime: ", logs[3][0]) + } } diff --git a/application/backend/app/routes/routes.go b/application/backend/app/routes/routes.go index dc4dad9..9308e75 100644 --- a/application/backend/app/routes/routes.go +++ b/application/backend/app/routes/routes.go @@ -415,7 +415,7 @@ func GetPrevLogs(w http.ResponseWriter, req *http.Request) { if params.Get("host") == "" { panic("Host is not mentioned!") } - json.NewEncoder(w).Encode(containerdb.GetLogs(true, false, params.Get("host"), params.Get("id"), params.Get("search"), limit, params.Get("startWith"), caseSensetive)) + json.NewEncoder(w).Encode(containerdb.GetLogs(true, false, params.Get("host"), params.Get("id"), params.Get("search"), limit, params.Get("startWith"), caseSensetive, nil)) } func GetLogs(w http.ResponseWriter, req *http.Request) { @@ -434,17 +434,16 @@ func GetLogs(w http.ResponseWriter, req *http.Request) { panic("Host is not mentioned!") } - if params.Get("status") != "" { - json.NewEncoder(w).Encode(containerdb.GetLogsByStatus( - params.Get("host"), params.Get("id"), params.Get("search"), params.Get("status"), - limit, params.Get("startWith"), false, true, caseSensetive, - )) - } else { - json.NewEncoder(w).Encode(containerdb.GetLogs( - false, false, params.Get("host"), params.Get("id"), params.Get("search"), - limit, params.Get("startWith"), caseSensetive, - )) + status := params.Get("status") + var statusPtr *string + if status != "" { + statusPtr = &status } + + json.NewEncoder(w).Encode(containerdb.GetLogs( + false, false, params.Get("host"), params.Get("id"), params.Get("search"), + limit, params.Get("startWith"), caseSensetive, statusPtr, + )) } func GetLogWithPrev(w http.ResponseWriter, req *http.Request) { @@ -458,7 +457,7 @@ func GetLogWithPrev(w http.ResponseWriter, req *http.Request) { if params.Get("host") == "" { panic("Host is not mentioned!") } - json.NewEncoder(w).Encode(containerdb.GetLogs(false, true, params.Get("host"), params.Get("id"), "", limit, params.Get("startWith"), false)) + json.NewEncoder(w).Encode(containerdb.GetLogs(false, true, params.Get("host"), params.Get("id"), "", limit, params.Get("startWith"), false, nil)) } // TODO return {"error": "Invalid host!"} when host is not exists diff --git a/application/build.sh b/application/build.sh old mode 100644 new mode 100755 index 0808271..1e7bc23 --- a/application/build.sh +++ b/application/build.sh @@ -1,2 +1,4 @@ -docker buildx create --use -# docker buildx build --platform=linux/amd64,linux/arm64 --tag "devforth/onlogs:latest" --tag "devforth/onlogs:1.1.1" --push . +# docker buildx create --use +docker buildx build --platform=linux/amd64,linux/arm64 --tag "devforth/onlogs:latest" --tag "devforth/onlogs:1.1.2" --push . +# docker run -v /var/run/docker.sock:/var/run/docker.sock --rm -it $(docker build -q -f Dockerfile .) +# docker build . -t devforth/onlogs && docker push devforth/onlogs diff --git a/application/frontend/src/Views/Logs/NewLogsV2.svelte b/application/frontend/src/Views/Logs/NewLogsV2.svelte index c258478..5fd59d8 100644 --- a/application/frontend/src/Views/Logs/NewLogsV2.svelte +++ b/application/frontend/src/Views/Logs/NewLogsV2.svelte @@ -157,8 +157,8 @@ total_logs_amount += data.logs.length; if (initialService === $lastChosenService) { - setLastLogTime(data.logs.reverse()?.at(0)?.at(0)); - allLogs = [...allLogs, ...data.logs]; + setLastLogTime(data.logs?.at(0)?.at(0)); + allLogs = [...allLogs, ...data.logs.reverse()]; let allLogsCopy = [...allLogs]; newLogs = allLogsCopy.splice(0, limit);