From 593c564686f0e9c38bbff1ab5d443d26c4224028 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 8 Sep 2023 11:52:12 +0200 Subject: [PATCH] Show UI's SQL queries in debug mode (#192) --- api/methods/queries.go | 8 +++++++- api/utils/utils.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/api/methods/queries.go b/api/methods/queries.go index accdcd46..0d4e487a 100644 --- a/api/methods/queries.go +++ b/api/methods/queries.go @@ -337,6 +337,12 @@ func executeSqlQuery(filter models.Filter, report string, section string, view s return "", errors.Wrap(errTpl, "invalid query template compiling") } + // print executed sql query + // ...it can be useful for debug + if gin.Mode() == gin.DebugMode { + utils.LogInfo(queryFile + " | " + utils.InlineQuery(queryString.String())) + } + // execute query db := source.CDRInstance() results, errQuery := db.Query(queryString.String()) @@ -526,7 +532,7 @@ func buildCdrQuery(queryFile string, filter models.Filter) (string, error) { } // append limit to query - + queryBuilder.WriteString(" LIMIT " + queryLimit) return queryBuilder.String(), nil diff --git a/api/utils/utils.go b/api/utils/utils.go index 99442d7d..e684654e 100644 --- a/api/utils/utils.go +++ b/api/utils/utils.go @@ -119,6 +119,22 @@ func LogError(err error) { os.Stderr.WriteString(err.Error() + "\n") } +func LogInfo(message string) { + currentTime := time.Now().Format("2006/01/02 - 15:04:05") + _, _ = os.Stderr.WriteString("[INFO] " + currentTime + " | " + message + "\n") +} + +func InlineQuery(query string) string { + // Remove SQL comments (lines starting with --) + commentPattern := regexp.MustCompile(`--.*$`) + query = commentPattern.ReplaceAllString(query, "") + + // Remove line breaks and extra spaces + query = strings.ReplaceAll(query, "\n", " ") + query = strings.Join(strings.Fields(query), " ") + return query +} + func Contains(a string, values []string) bool { for _, b := range values { if b == a {