Skip to content

Commit

Permalink
add logs_import and logs_web modules for log importing and web API
Browse files Browse the repository at this point in the history
  • Loading branch information
abaldeweg authored Jan 23, 2025
1 parent 3580927 commit bbc7606
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 67 deletions.
2 changes: 2 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ use (
./framework
./gateway
./logs
./logs_import
./logs_web
./static
)
35 changes: 0 additions & 35 deletions logs/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package controller
import (
"net/http"
"strconv"
"time"

"github.com/abaldeweg/warehouse-server/logs/db"
"github.com/abaldeweg/warehouse-server/logs/parser"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -38,36 +36,3 @@ func GetLogs(c *gin.Context) {

c.JSON(http.StatusOK, d)
}

// CreateLog handles the POST request to parse and store logs.
func ImportLogs() {
entries, err := parser.ReadLogEntries()
if err != nil {
print("Internal Server Error")
return
}

h, err := db.NewDBHandler()
if err != nil {
print("Internal Server Error")
return
}
defer h.Close()

for _, entry := range entries {
date, _ := strconv.Atoi(time.Time(entry.Time).Format("20060102"))
exists, err := h.Exists(date, entry)
if err != nil {
print("Internal Server Error")
return
}
if !exists {
if err := h.Write(date, entry); err != nil {
print("Internal Server Error")
return
}
}
}

print("success")
}
35 changes: 33 additions & 2 deletions logs_import/cmd/cli.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
package cmd

import (
"github.com/abaldeweg/warehouse-server/logs/controller"
"fmt"
"log"
"strconv"
"time"

"github.com/spf13/cobra"

"github.com/abaldeweg/warehouse-server/logs/db"
"github.com/abaldeweg/warehouse-server/logs/parser"
)

// ImportLogsCmd reads logs from the log file and imports them into the database.
var ImportLogsCmd = &cobra.Command{
Use: "import",
Short: "Read logs from the log file and import them into the database",
Run: func(cmd *cobra.Command, args []string) {
controller.ImportLogs()
entries, err := parser.ReadLogEntries()
if err != nil {
log.Fatal(err)
}

h, err := db.NewDBHandler()
if err != nil {
log.Fatal(err)
}
defer h.Close()

for _, entry := range entries {
date, _ := strconv.Atoi(time.Time(entry.Time).Format("20060102"))
exists, err := h.Exists(date, entry)
if err != nil {
log.Fatal(err)
}
if !exists {
if err := h.Write(date, entry); err != nil {
log.Fatal(err)
}
}
}

fmt.Println("\033[32mLogs successfully imported!\033[0m")
},
}

Expand Down
78 changes: 78 additions & 0 deletions logs_web/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,81 @@
module github.com/abaldeweg/warehouse-server/logs_web

go 1.23.2

require github.com/abaldeweg/warehouse-server/framework v0.12.1

require github.com/mattn/go-sqlite3 v1.14.24 // indirect

require (
cel.dev/expr v0.16.2 // indirect
cloud.google.com/go v0.116.0 // indirect
cloud.google.com/go/auth v0.11.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
cloud.google.com/go/iam v1.2.2 // indirect
cloud.google.com/go/monitoring v1.21.2 // indirect
cloud.google.com/go/storage v1.48.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.3 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.3 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.3 // indirect
github.com/abaldeweg/warehouse-server/logs v0.0.0-20250123164018-3580927a4a21
github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
github.com/envoyproxy/go-control-plane v0.13.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.14.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.31.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
golang.org/x/arch v0.12.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/api v0.210.0 // indirect
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241113202542-65e8d215514f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/grpc v1.67.2 // indirect
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241018153737-98959d9a4904 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit bbc7606

Please sign in to comment.