From 8477ed25c9a8283fbe436fde155070e17c3e03dd Mon Sep 17 00:00:00 2001 From: sunface Date: Wed, 25 Oct 2023 16:51:11 +0800 Subject: [PATCH] feat: add some options to getLogs api in datav datasource #290 --- .../plugins/builtin/datav/api/logs.go | 31 +++++++++++++++++-- .../plugins/builtin/datav/utils/params.go | 20 ++++++++++++ .../built-in/datasource/datav/QueryEditor.tsx | 9 ++++-- 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 query/internal/plugins/builtin/datav/utils/params.go diff --git a/query/internal/plugins/builtin/datav/api/logs.go b/query/internal/plugins/builtin/datav/api/logs.go index 067a2e303..dfb3c7a3b 100644 --- a/query/internal/plugins/builtin/datav/api/logs.go +++ b/query/internal/plugins/builtin/datav/api/logs.go @@ -7,6 +7,7 @@ import ( ch "github.com/ClickHouse/clickhouse-go/v2" datavmodels "github.com/DataObserve/datav/query/internal/plugins/builtin/datav/models" + datavutils "github.com/DataObserve/datav/query/internal/plugins/builtin/datav/utils" pluginUtils "github.com/DataObserve/datav/query/internal/plugins/utils" "github.com/DataObserve/datav/query/pkg/config" "github.com/DataObserve/datav/query/pkg/models" @@ -67,10 +68,34 @@ func GetLogs(c *gin.Context, ds *models.Datasource, conn ch.Conn, params map[str return models.GenPluginResult(models.PluginStatusError, "Parse search query error: "+err.Error(), nil) } } + + orderI := params["orderByTimestamp"] + order := "desc" + if orderI != nil { + order = orderI.(string) + } + + namespace, service, host := datavutils.GetNamespaceServiceHostFromParams(params) + + var domainQuery string + var domainArgs []interface{} + if namespace != "" { + domainQuery += " AND namespace = ?" + domainArgs = append(domainArgs, namespace) + } + if service != "" { + domainQuery += " AND service = ?" + domainArgs = append(domainArgs, service) + } + if host != "" { + domainQuery += " AND host = ?" + domainArgs = append(domainArgs, host) + } + // query logs - logsQuery := fmt.Sprintf(datavmodels.LogsSelectSQL+" FROM %s.%s where (timestamp >= ? AND timestamp <= ? %s) order by timestamp desc LIMIT %d OFFSET %d", config.Data.Observability.DefaultLogDB, datavmodels.DefaultLogsTable, searchQuery, perPageLogs, page*int64(perPageLogs)) + logsQuery := fmt.Sprintf(datavmodels.LogsSelectSQL+" FROM %s.%s where (timestamp >= ? AND timestamp <= ? %s %s) order by timestamp %s LIMIT %d OFFSET %d", config.Data.Observability.DefaultLogDB, datavmodels.DefaultLogsTable, domainQuery, searchQuery, order, perPageLogs, page*int64(perPageLogs)) - args := append([]interface{}{start * 1e9, (end) * 1e9}, searchArgs...) + args := append([]interface{}{start * 1e9, (end) * 1e9}, append(domainArgs, searchArgs...)...) rows, err := conn.Query(c.Request.Context(), logsQuery, args...) if err != nil { logger.Warn("Error Query logs", "query", logsQuery, "error", err) @@ -89,7 +114,7 @@ func GetLogs(c *gin.Context, ds *models.Datasource, conn ch.Conn, params map[str var res1 *models.PluginResultData if page == 0 { // query metrics - metricsQuery := fmt.Sprintf("SELECT toStartOfInterval(fromUnixTimestamp64Nano(timestamp), INTERVAL %d SECOND) AS ts_bucket, if(multiSearchAny(severity, ['error', 'err', 'emerg', 'alert', 'crit', 'fatal']), 'errors', 'others') as severity_group, count(*) as count from %s.%s where (timestamp >= ? AND timestamp <= ? %s) group by ts_bucket,severity_group order by ts_bucket", step, config.Data.Observability.DefaultLogDB, datavmodels.DefaultLogsTable, searchQuery) + metricsQuery := fmt.Sprintf("SELECT toStartOfInterval(fromUnixTimestamp64Nano(timestamp), INTERVAL %d SECOND) AS ts_bucket, if(multiSearchAny(severity, ['error', 'err', 'emerg', 'alert', 'crit', 'fatal']), 'errors', 'others') as severity_group, count(*) as count from %s.%s where (timestamp >= ? AND timestamp <= ? %s %s) group by ts_bucket,severity_group order by ts_bucket", step, config.Data.Observability.DefaultLogDB, datavmodels.DefaultLogsTable, domainQuery, searchQuery) rows, err = conn.Query(c.Request.Context(), metricsQuery, args...) if err != nil { diff --git a/query/internal/plugins/builtin/datav/utils/params.go b/query/internal/plugins/builtin/datav/utils/params.go new file mode 100644 index 000000000..deee9fd10 --- /dev/null +++ b/query/internal/plugins/builtin/datav/utils/params.go @@ -0,0 +1,20 @@ +package utils + +func GetNamespaceServiceHostFromParams(params map[string]interface{}) (namespace string, service string, host string) { + namespaceI := params["namespace"] + if namespaceI != nil { + namespace = namespaceI.(string) + } + + serviceI := params["service"] + if serviceI != nil { + service = serviceI.(string) + } + + hostI := params["host"] + if hostI != nil { + host = hostI.(string) + } + + return +} diff --git a/ui/src/views/dashboard/plugins/built-in/datasource/datav/QueryEditor.tsx b/ui/src/views/dashboard/plugins/built-in/datasource/datav/QueryEditor.tsx index 2e5d3dd54..86f6f1940 100644 --- a/ui/src/views/dashboard/plugins/built-in/datasource/datav/QueryEditor.tsx +++ b/ui/src/views/dashboard/plugins/built-in/datasource/datav/QueryEditor.tsx @@ -76,6 +76,7 @@ const HttpQueryEditor = ({ panel, datasource, query, onChange }: DatasourceEdito Param Desc + Default value @@ -83,6 +84,7 @@ const HttpQueryEditor = ({ panel, datasource, query, onChange }: DatasourceEdito api?.paramsDesc?.map(desc => {desc[0]} {desc[1]} + {desc[2]} ) } @@ -147,9 +149,10 @@ const apiList = [{ params: `{ }`, paramsDesc: [ - ["env", "environment name, such as dev, test, prod etc"], - ["service", "filter by service names, e.g datav|driver"], - ["perPage", "page size of logs when query from datasource, default is 100"] + ["env", "environment name, such as dev, test, prod etc", ""], + ["service", "filter by service names, e.g datav|driver", ""], + ["perPage", "page size of logs when query from datasource", "100"], + ["orderByTimestamp", "order by timestamp, default is desc", "asc | desc"], ], format: DataFormat.Logs }