Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get logs method refactor #12

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions application/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
89 changes: 24 additions & 65 deletions application/backend/app/containerdb/containerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -185,26 +133,37 @@ 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)
continue
} 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
}
Expand Down
10 changes: 8 additions & 2 deletions application/backend/app/containerdb/containerdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
23 changes: 11 additions & 12 deletions application/backend/app/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions application/build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions application/frontend/src/Views/Logs/NewLogsV2.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading