Skip to content

Commit

Permalink
Show UI's SQL queries in debug mode (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian authored Sep 8, 2023
1 parent 4e9a7e7 commit 593c564
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/methods/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions api/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 593c564

Please sign in to comment.