From 7f372578e54c73060c849635b1a9d9080e34fe62 Mon Sep 17 00:00:00 2001 From: dandimeo Date: Thu, 12 Oct 2023 17:57:56 +0200 Subject: [PATCH 1/6] debug field in config --- toolchain/arangoproxy/cmd/configs/local.yaml | 11 +---------- toolchain/arangoproxy/internal/models/config.go | 1 + 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/toolchain/arangoproxy/cmd/configs/local.yaml b/toolchain/arangoproxy/cmd/configs/local.yaml index 451bd39560..4b404ff903 100644 --- a/toolchain/arangoproxy/cmd/configs/local.yaml +++ b/toolchain/arangoproxy/cmd/configs/local.yaml @@ -1,13 +1,4 @@ -webserver: ':8080' -logFile: log.txt datasetsFile: '../internal/utils/datasets.json' cache: '/home/site/data/' +debug: false repositories: - - name: stable - type: single - version: "3.12" - url: http://192.168.129.2:8529 - - name: stable - type: cluster - version: "3.12" - url: http://192.168.129.3:8529 diff --git a/toolchain/arangoproxy/internal/models/config.go b/toolchain/arangoproxy/internal/models/config.go index a3695da42d..01ba75a4ae 100644 --- a/toolchain/arangoproxy/internal/models/config.go +++ b/toolchain/arangoproxy/internal/models/config.go @@ -10,6 +10,7 @@ type Config struct { Repositories []Repository `yaml:"repositories"` // ArangoDB instances Cache string `yaml:"cache"` // Cache configuration Datasets string `yaml:"datasetsFile"` + Debug bool `yaml:"debug"` Override string `yaml:"-"` } From ebf69bf22362edae4ff183b99572549dff7fe856 Mon Sep 17 00:00:00 2001 From: dandimeo Date: Thu, 12 Oct 2023 17:59:21 +0200 Subject: [PATCH 2/6] debug function in logger --- toolchain/arangoproxy/internal/models/logger.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/toolchain/arangoproxy/internal/models/logger.go b/toolchain/arangoproxy/internal/models/logger.go index 2a43363c8b..8a9acb8f41 100644 --- a/toolchain/arangoproxy/internal/models/logger.go +++ b/toolchain/arangoproxy/internal/models/logger.go @@ -27,6 +27,12 @@ func (l *ArangoproxyLogger) Printf(s string, args ...any) { l.logger.Printf(s, args...) } +func (l *ArangoproxyLogger) Debug(s string, args ...any) { + if Conf.Debug { + l.logger.Printf(s, args...) + } +} + func (l *ArangoproxyLogger) Summary(s string, args ...any) { s = strings.ReplaceAll(s, "\n", "\n
") l.summary.Printf(s+"
", args...) From 1ff7b220837d56cf43184c16ca4c7379117acdb2 Mon Sep 17 00:00:00 2001 From: dandimeo Date: Thu, 12 Oct 2023 18:02:55 +0200 Subject: [PATCH 3/6] prepend [DEBUG] to Debug messages, add arangosh code injection devug message --- toolchain/arangoproxy/internal/arangosh/arangosh.go | 1 + toolchain/arangoproxy/internal/models/logger.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/toolchain/arangoproxy/internal/arangosh/arangosh.go b/toolchain/arangoproxy/internal/arangosh/arangosh.go index 5cf5ff6772..6e97556eba 100644 --- a/toolchain/arangoproxy/internal/arangosh/arangosh.go +++ b/toolchain/arangoproxy/internal/arangosh/arangosh.go @@ -32,6 +32,7 @@ func ExecRoutine(example chan map[string]interface{}, outChannel chan string) { func Exec(exampleName string, code, filepath string, repository models.Repository) (output string) { code = format.AdjustCodeForArangosh(code) + models.Logger.Debug("[%s] [arangosh.Exec] Injecting Code:\n%s", exampleName, code) cmd := []byte(code) _, err := repository.StdinPipe.Write(cmd) diff --git a/toolchain/arangoproxy/internal/models/logger.go b/toolchain/arangoproxy/internal/models/logger.go index 8a9acb8f41..0c27cff33f 100644 --- a/toolchain/arangoproxy/internal/models/logger.go +++ b/toolchain/arangoproxy/internal/models/logger.go @@ -29,7 +29,7 @@ func (l *ArangoproxyLogger) Printf(s string, args ...any) { func (l *ArangoproxyLogger) Debug(s string, args ...any) { if Conf.Debug { - l.logger.Printf(s, args...) + l.logger.Printf("[DEBUG] "+s, args...) } } From 6f1227c6768b69d64ad6a5fc8a128e197fad21b7 Mon Sep 17 00:00:00 2001 From: dandimeo Date: Fri, 13 Oct 2023 17:37:07 +0200 Subject: [PATCH 4/6] debug mode working, more debug messages --- toolchain/arangoproxy/cmd/configs/local.yaml | 2 +- toolchain/arangoproxy/cmd/main.go | 13 +------------ toolchain/arangoproxy/internal/init.go | 3 +-- toolchain/arangoproxy/internal/models/config.go | 5 +++++ .../arangoproxy/internal/models/example.go | 17 ++++------------- toolchain/arangoproxy/internal/models/logger.go | 4 ++-- .../arangoproxy/internal/models/repository.go | 1 - .../arangoproxy/internal/service/service.go | 5 +++++ 8 files changed, 19 insertions(+), 31 deletions(-) diff --git a/toolchain/arangoproxy/cmd/configs/local.yaml b/toolchain/arangoproxy/cmd/configs/local.yaml index 4b404ff903..d4b28fe6aa 100644 --- a/toolchain/arangoproxy/cmd/configs/local.yaml +++ b/toolchain/arangoproxy/cmd/configs/local.yaml @@ -1,4 +1,4 @@ datasetsFile: '../internal/utils/datasets.json' cache: '/home/site/data/' debug: false -repositories: +repositories: [] \ No newline at end of file diff --git a/toolchain/arangoproxy/cmd/main.go b/toolchain/arangoproxy/cmd/main.go index 8036c0ec92..35df82f7eb 100644 --- a/toolchain/arangoproxy/cmd/main.go +++ b/toolchain/arangoproxy/cmd/main.go @@ -30,23 +30,12 @@ func init() { models.Conf.Override = strings.ReplaceAll(override, ",", "|") models.Logger.Printf(startupBanner) - models.Logger.Printf("./arangoproxy -help for help usage\n\n") - models.Logger.Printf("Init Setup\n") - if help { - models.Logger.Printf("Usage: ...\n") - os.Exit(0) - } - models.Logger.Printf("Configuration:\n%v\n", models.Conf) - - models.Logger.Printf("Setup Done\n---------\n") + models.Logger.Printf("Configuration:\n%s\n", models.Conf.String()) } func main() { - models.Logger.Printf("Available endpoints:\n - /js\n - /aql\n - /curl\n - /openapi\n") - models.Logger.Printf("Starting Server at :8080\n") - if useServers { internal.InitRepositories() models.Logger.Printf("[INIT] Repositories Init Done") diff --git a/toolchain/arangoproxy/internal/init.go b/toolchain/arangoproxy/internal/init.go index bf7223505b..b3bf51f36e 100644 --- a/toolchain/arangoproxy/internal/init.go +++ b/toolchain/arangoproxy/internal/init.go @@ -12,7 +12,6 @@ import ( func InitRepositories() { models.Repositories = make(map[string]models.Repository) - fmt.Printf("Repositories found in config file %s\n", models.Conf.Repositories) var wg sync.WaitGroup for _, repo := range models.Conf.Repositories { @@ -55,5 +54,5 @@ func openRepoStream(repository *models.Repository) { repository.StdinPipe = stdin repository.StdoutPipe = stdout - models.Logger.Printf("[openRepoStream] Done - Streams: \n%s\n%s", repository.StdinPipe, repository.StdoutPipe) + models.Logger.Printf("[openRepoStream] Opened Stream for %s succesfully", repository.Version) } diff --git a/toolchain/arangoproxy/internal/models/config.go b/toolchain/arangoproxy/internal/models/config.go index 01ba75a4ae..9536d4aa3d 100644 --- a/toolchain/arangoproxy/internal/models/config.go +++ b/toolchain/arangoproxy/internal/models/config.go @@ -1,6 +1,7 @@ package models import ( + "fmt" "os" "gopkg.in/yaml.v3" @@ -25,3 +26,7 @@ func LoadConfig(file string) error { err = yaml.Unmarshal(fileStream, &Conf) return err } + +func (c Config) String() string { + return fmt.Sprintf("Cache: %s\nDatasets: %s\nDebug: %t\nOverride: %s\nRepositories: %s", c.Cache, c.Datasets, c.Debug, c.Override, c.Repositories) +} diff --git a/toolchain/arangoproxy/internal/models/example.go b/toolchain/arangoproxy/internal/models/example.go index b53994dfff..346d032f7a 100644 --- a/toolchain/arangoproxy/internal/models/example.go +++ b/toolchain/arangoproxy/internal/models/example.go @@ -16,14 +16,9 @@ import ( "gopkg.in/yaml.v3" ) -type ExampleType string type RenderType string const ( - JS ExampleType = "js" - AQL ExampleType = "aql" - HTTP ExampleType = "http" - INPUT RenderType = "input" OUTPUT RenderType = "output" INPUT_OUTPUT RenderType = "input/output" @@ -31,7 +26,6 @@ const ( // @Example represents an example request to be supplied to an arango instance type Example struct { - Type ExampleType `json:"type"` Options ExampleOptions `json:"options"` // The codeblock yaml part Code string `json:"code"` Repository Repository `json:"-"` @@ -99,7 +93,9 @@ func ParseExample(request io.Reader, headers http.Header) (Example, error) { code := strings.Replace(string(decodedRequest), string(options), "", -1) - return Example{Type: "", Options: optionsYaml, Code: code, Base64Request: string(req)}, nil + Logger.Debug("[%s] Example Information:\n%s", optionsYaml.Name, optionsYaml.String()) + + return Example{Options: optionsYaml, Code: code, Base64Request: string(req)}, nil } func (r Example) String() string { @@ -112,12 +108,7 @@ func (r Example) String() string { } func (r ExampleOptions) String() string { - j, err := json.Marshal(r) - if err != nil { - return "" - } - - return string(j) + return fmt.Sprintf("Type: %s\nVersion: %s\nSaveCache: %s\nPosition: %s\n", r.Type, r.Version, r.SaveCache, r.Position) } type ExampleResponse struct { diff --git a/toolchain/arangoproxy/internal/models/logger.go b/toolchain/arangoproxy/internal/models/logger.go index 0c27cff33f..38cdb82696 100644 --- a/toolchain/arangoproxy/internal/models/logger.go +++ b/toolchain/arangoproxy/internal/models/logger.go @@ -19,7 +19,7 @@ func init() { summaryWriter := io.Writer(logFile) Logger = new(ArangoproxyLogger) - Logger.logger = log.New(os.Stdout, "", log.Ldate|log.Ltime) + Logger.logger = log.New(os.Stdout, "", 0) Logger.summary = log.New(summaryWriter, "", 0) } @@ -29,7 +29,7 @@ func (l *ArangoproxyLogger) Printf(s string, args ...any) { func (l *ArangoproxyLogger) Debug(s string, args ...any) { if Conf.Debug { - l.logger.Printf("[DEBUG] "+s, args...) + l.logger.Printf("[DEBUG] "+s+"\n", args...) } } diff --git a/toolchain/arangoproxy/internal/models/repository.go b/toolchain/arangoproxy/internal/models/repository.go index 56ede4df44..507c919af5 100644 --- a/toolchain/arangoproxy/internal/models/repository.go +++ b/toolchain/arangoproxy/internal/models/repository.go @@ -17,7 +17,6 @@ type Repository struct { var Repositories map[string]Repository func GetRepository(typ, version string) (Repository, error) { - Logger.Printf("Executing on server %s %s", typ, version) if repository, exists := Repositories[fmt.Sprintf("%s_%s", typ, version)]; exists { return repository, nil } diff --git a/toolchain/arangoproxy/internal/service/service.go b/toolchain/arangoproxy/internal/service/service.go index 95df939b49..5a081b2817 100644 --- a/toolchain/arangoproxy/internal/service/service.go +++ b/toolchain/arangoproxy/internal/service/service.go @@ -47,6 +47,7 @@ type JSService struct{} func (service JSService) Execute(request models.Example, cacheChannel chan map[string]interface{}, exampleChannel chan map[string]interface{}, outputChannel chan string) (res models.ExampleResponse) { repository, err := models.GetRepository(request.Options.Type, request.Options.Version) + models.Logger.Debug("[%s] Choosen Repository: %s", request.Options.Name, repository.Version) if err != nil { responseMsg := fmt.Sprintf("A server for version %s has not been used during generation", request.Options.Version) res = *models.NewExampleResponse(request.Code, responseMsg, request.Options) @@ -69,7 +70,9 @@ var curlFormatter = format.CurlFormatter{} func (service CurlService) Execute(request models.Example, cacheChannel chan map[string]interface{}, exampleChannel chan map[string]interface{}, outputChannel chan string) (res models.ExampleResponse, err error) { commands := curlFormatter.FormatCommand(request.Code) + repository, err := models.GetRepository(request.Options.Type, request.Options.Version) + models.Logger.Debug("[%s] Choosen Repository: %s", request.Options.Name, repository.Version) if err != nil { responseMsg := fmt.Sprintf("A server for version %s has not been used during generation", request.Options.Version) res = *models.NewExampleResponse(request.Code, responseMsg, request.Options) @@ -98,7 +101,9 @@ var AQLFormatter = format.AQLFormatter{} func (service AQLService) Execute(request models.Example, cacheChannel chan map[string]interface{}, exampleChannel chan map[string]interface{}, outputChannel chan string) (res models.AQLResponse) { commands := AQLFormatter.FormatRequestCode(request.Code, request.Options.BindVars) + repository, err := models.GetRepository(request.Options.Type, request.Options.Version) + models.Logger.Debug("[%s] Choosen Repository: %s", request.Options.Name, repository.Version) if err != nil { responseMsg := fmt.Sprintf("A server for version %s has not been used during generation", request.Options.Version) res.ExampleResponse.Input, res.ExampleResponse.Options, res.ExampleResponse.Output = request.Code, request.Options, responseMsg From 535ceeae0c981ca9499b70a682577847667fd01b Mon Sep 17 00:00:00 2001 From: dandimeo Date: Tue, 17 Oct 2023 19:29:41 +0200 Subject: [PATCH 5/6] add debug messages to openapi spec processing and save cache function --- toolchain/arangoproxy/internal/cache.go | 9 ++++++++- toolchain/arangoproxy/internal/service/service.go | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/toolchain/arangoproxy/internal/cache.go b/toolchain/arangoproxy/internal/cache.go index 5ec423fb3a..a6f944688a 100644 --- a/toolchain/arangoproxy/internal/cache.go +++ b/toolchain/arangoproxy/internal/cache.go @@ -19,6 +19,8 @@ func SaveCachedExampleResponse(chnl chan map[string]interface{}) error { entryName := fmt.Sprintf("%s_%s", exampleResponse.Options.Name, exampleResponse.Options.Type) responseHash, err := utils.EncodeToBase64(exampleResponse) + models.Logger.Debug("[%s] Saving To Cache:\nInput Hash: %s\nOutput Hash:%s", entryName, requestHash, responseHash) + newCacheEntry := make(map[string]map[string]string) newCacheEntry[entryName] = make(map[string]string) newCacheEntry[entryName]["request"] = requestHash @@ -34,7 +36,12 @@ func SaveCachedExampleResponse(chnl chan map[string]interface{}) error { cache[entryName] = newCacheEntry[entryName] cacheJson, _ := json.MarshalIndent(cache, "", "\t") err = os.WriteFile(cacheFilepath, cacheJson, 0644) - + if err != nil { + models.Logger.Printf("[%s] [ERROR] Error saving cache: %s", err.Error()) + models.Logger.Summary("
  • %s ERROR Saving Cache: %s", exampleResponse.Options.Name, err.Error()) + } else { + models.Logger.Debug("[%s] Cache Saved", err) + } } } } diff --git a/toolchain/arangoproxy/internal/service/service.go b/toolchain/arangoproxy/internal/service/service.go index 5a081b2817..efef119996 100644 --- a/toolchain/arangoproxy/internal/service/service.go +++ b/toolchain/arangoproxy/internal/service/service.go @@ -172,6 +172,9 @@ func (service OpenapiService) ProcessOpenapiSpec(spec map[string]interface{}, he spec["version"] = version + specDebug, _ := json.Marshal(spec) + models.Logger.Debug("[ProcessOpenapiSpec] Processing Spec %s", specDebug) + path := reflect.ValueOf(spec["paths"].(map[string]interface{})).MapKeys()[0].String() method := reflect.ValueOf(spec["paths"].(map[string]interface{})[path].(map[string]interface{})).MapKeys()[0].String() spec["paths"].(map[string]interface{})[path].(map[string]interface{})[method].(map[string]interface{})["summary"] = summary From 9f74342108981bc7a4ba59e6dca6d4460332c33e Mon Sep 17 00:00:00 2001 From: dandimeo Date: Tue, 17 Oct 2023 19:31:46 +0200 Subject: [PATCH 6/6] prettier configuration debug --- toolchain/arangoproxy/internal/models/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchain/arangoproxy/internal/models/config.go b/toolchain/arangoproxy/internal/models/config.go index 9536d4aa3d..64be621956 100644 --- a/toolchain/arangoproxy/internal/models/config.go +++ b/toolchain/arangoproxy/internal/models/config.go @@ -28,5 +28,5 @@ func LoadConfig(file string) error { } func (c Config) String() string { - return fmt.Sprintf("Cache: %s\nDatasets: %s\nDebug: %t\nOverride: %s\nRepositories: %s", c.Cache, c.Datasets, c.Debug, c.Override, c.Repositories) + return fmt.Sprintf(" Cache: %s\n Datasets: %s\n Debug: %t\n Override: %s\n Repositories: %s", c.Cache, c.Datasets, c.Debug, c.Override, c.Repositories) }