Skip to content

Commit

Permalink
feat: add some options to getLogs api in datav datasource #290
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed Oct 25, 2023
1 parent 29764a0 commit 8477ed2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
31 changes: 28 additions & 3 deletions query/internal/plugins/builtin/datav/api/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions query/internal/plugins/builtin/datav/utils/params.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ const HttpQueryEditor = ({ panel, datasource, query, onChange }: DatasourceEdito
<Tr>
<Th>Param</Th>
<Th>Desc</Th>
<Th>Default value</Th>
</Tr>
</Thead>
<Tbody>
{
api?.paramsDesc?.map(desc => <Tr>
<Td>{desc[0]}</Td>
<Td>{desc[1]}</Td>
<Td>{desc[2]}</Td>
</Tr>)
}
</Tbody>
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8477ed2

Please sign in to comment.