mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
tests: handle JSON in TestHTTPEndpoints
This commit is contained in:
@@ -4,7 +4,9 @@ package helpers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -14,6 +16,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kylelemons/godebug/pretty"
|
||||
)
|
||||
|
||||
@@ -35,22 +38,48 @@ func Diff(a, b interface{}) string {
|
||||
|
||||
// HTTPEndpointCases describes case for TestHTTPEndpoints
|
||||
type HTTPEndpointCases []struct {
|
||||
Description string
|
||||
URL string
|
||||
ContentType string
|
||||
StatusCode int
|
||||
FirstLines []string
|
||||
JSONInput interface{}
|
||||
JSONOutput interface{}
|
||||
}
|
||||
|
||||
// TestHTTPEndpoints test a few HTTP endpoints
|
||||
func TestHTTPEndpoints(t *testing.T, serverAddr net.Addr, cases HTTPEndpointCases) {
|
||||
t.Helper()
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.URL, func(t *testing.T) {
|
||||
desc := tc.Description
|
||||
if desc == "" {
|
||||
desc = tc.URL
|
||||
}
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
t.Helper()
|
||||
resp, err := http.Get(fmt.Sprintf("http://%s%s", serverAddr, tc.URL))
|
||||
if tc.FirstLines != nil && tc.JSONOutput != nil {
|
||||
t.Fatalf("Cannot have both FirstLines and JSONOutput")
|
||||
}
|
||||
var resp *http.Response
|
||||
var err error
|
||||
if tc.JSONInput == nil {
|
||||
resp, err = http.Get(fmt.Sprintf("http://%s%s", serverAddr, tc.URL))
|
||||
if err != nil {
|
||||
t.Fatalf("GET %s:\n%+v", tc.URL, err)
|
||||
}
|
||||
} else {
|
||||
payload := new(bytes.Buffer)
|
||||
err = json.NewEncoder(payload).Encode(tc.JSONInput)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error:\n%+v", err)
|
||||
}
|
||||
resp, err = http.Post(fmt.Sprintf("http://%s%s", serverAddr, tc.URL),
|
||||
"application/json", payload)
|
||||
if err != nil {
|
||||
t.Fatalf("POST %s:\n%+v", tc.URL, err)
|
||||
}
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
if tc.StatusCode == 0 {
|
||||
tc.StatusCode = 200
|
||||
@@ -59,11 +88,15 @@ func TestHTTPEndpoints(t *testing.T, serverAddr net.Addr, cases HTTPEndpointCase
|
||||
t.Fatalf("GET %s: got status code %d, not %d", tc.URL,
|
||||
resp.StatusCode, tc.StatusCode)
|
||||
}
|
||||
if tc.JSONOutput != nil {
|
||||
tc.ContentType = "application/json; charset=utf-8"
|
||||
}
|
||||
gotContentType := resp.Header.Get("Content-Type")
|
||||
if gotContentType != tc.ContentType {
|
||||
t.Errorf("GET %s Content-Type (-got, +want):\n-%s\n+%s",
|
||||
tc.URL, gotContentType, tc.ContentType)
|
||||
}
|
||||
if tc.JSONOutput == nil {
|
||||
reader := bufio.NewScanner(resp.Body)
|
||||
got := []string{}
|
||||
for reader.Scan() && len(got) < len(tc.FirstLines) {
|
||||
@@ -72,6 +105,16 @@ func TestHTTPEndpoints(t *testing.T, serverAddr net.Addr, cases HTTPEndpointCase
|
||||
if diff := Diff(got, tc.FirstLines); diff != "" {
|
||||
t.Errorf("GET %s (-got, +want):\n%s", tc.URL, diff)
|
||||
}
|
||||
} else {
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
var got gin.H
|
||||
if err := decoder.Decode(&got); err != nil {
|
||||
t.Fatalf("POST %s:\n%+v", tc.URL, err)
|
||||
}
|
||||
if diff := Diff(got, tc.JSONOutput); diff != "" {
|
||||
t.Fatalf("POST %s (-got, +want):\n%s", tc.URL, diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ func TestGinRouter(t *testing.T) {
|
||||
URL: "/api/v0/test",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{`{"message":"ping"}`},
|
||||
}, {
|
||||
URL: "/api/v0/test",
|
||||
JSONOutput: gin.H{"message": "ping"},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
"akvorado/common/helpers"
|
||||
)
|
||||
|
||||
// graphQuery describes the input for the /graph endpoint.
|
||||
type graphQuery struct {
|
||||
// graphHandlerInput describes the input for the /graph endpoint.
|
||||
type graphHandlerInput struct {
|
||||
Start time.Time `json:"start" binding:"required"`
|
||||
End time.Time `json:"end" binding:"required,gtfield=Start"`
|
||||
Points int `json:"points" binding:"required,min=5,max=2000"` // minimum number of points
|
||||
@@ -23,12 +23,23 @@ type graphQuery struct {
|
||||
Units string `json:"units" binding:"required,oneof=pps bps"`
|
||||
}
|
||||
|
||||
// graphQueryToSQL converts a graph query to an SQL request
|
||||
func (query graphQuery) toSQL() (string, error) {
|
||||
interval := int64((query.End.Sub(query.Start).Seconds())) / int64(query.Points)
|
||||
// graphHandlerOutput describes the output for the /graph endpoint.
|
||||
type graphHandlerOutput struct {
|
||||
Rows [][]string `json:"rows"`
|
||||
Time []time.Time `json:"t"`
|
||||
Points [][]int `json:"points"` // t → row → xps
|
||||
Average []int `json:"average"` // row → xps
|
||||
Min []int `json:"min"`
|
||||
Max []int `json:"max"`
|
||||
NinetyFivePercentile []int `json:"95th"`
|
||||
}
|
||||
|
||||
// graphHandlerInputToSQL converts a graph input to an SQL request
|
||||
func (input graphHandlerInput) toSQL() (string, error) {
|
||||
interval := int64((input.End.Sub(input.Start).Seconds())) / int64(input.Points)
|
||||
|
||||
// Filter
|
||||
where := query.Filter.filter
|
||||
where := input.Filter.filter
|
||||
if where == "" {
|
||||
where = "{timefilter}"
|
||||
} else {
|
||||
@@ -39,7 +50,7 @@ func (query graphQuery) toSQL() (string, error) {
|
||||
fields := []string{
|
||||
`toStartOfInterval(TimeReceived, INTERVAL slot second) AS time`,
|
||||
}
|
||||
if query.Units == "pps" {
|
||||
if input.Units == "pps" {
|
||||
fields = append(fields, `SUM(Packets*SamplingRate/slot) AS xps`)
|
||||
} else {
|
||||
fields = append(fields, `SUM(Bytes*SamplingRate*8/slot) AS xps`)
|
||||
@@ -47,7 +58,7 @@ func (query graphQuery) toSQL() (string, error) {
|
||||
selectFields := []string{}
|
||||
dimensions := []string{}
|
||||
others := []string{}
|
||||
for _, column := range query.Dimensions {
|
||||
for _, column := range input.Dimensions {
|
||||
field := column.toSQLSelect()
|
||||
selectFields = append(selectFields, field)
|
||||
dimensions = append(dimensions, column.String())
|
||||
@@ -70,7 +81,7 @@ func (query graphQuery) toSQL() (string, error) {
|
||||
strings.Join(dimensions, ", "),
|
||||
where,
|
||||
strings.Join(dimensions, ", "),
|
||||
query.Limit))
|
||||
input.Limit))
|
||||
}
|
||||
|
||||
sqlQuery := fmt.Sprintf(`
|
||||
@@ -85,35 +96,25 @@ ORDER BY time`, strings.Join(with, ",\n "), strings.Join(fields, ",\n "), where)
|
||||
return sqlQuery, nil
|
||||
}
|
||||
|
||||
type graphHandlerOutput struct {
|
||||
Rows [][]string `json:"rows"`
|
||||
Time []time.Time `json:"t"`
|
||||
Points [][]int `json:"points"` // t → row → xps
|
||||
Average []int `json:"average"` // row → xps
|
||||
Min []int `json:"min"`
|
||||
Max []int `json:"max"`
|
||||
NinetyFivePercentile []int `json:"95th"`
|
||||
}
|
||||
|
||||
func (c *Component) graphHandlerFunc(gc *gin.Context) {
|
||||
ctx := c.t.Context(gc.Request.Context())
|
||||
var query graphQuery
|
||||
if err := gc.ShouldBindJSON(&query); err != nil {
|
||||
var input graphHandlerInput
|
||||
if err := gc.ShouldBindJSON(&input); err != nil {
|
||||
gc.JSON(http.StatusBadRequest, gin.H{"message": helpers.Capitalize(err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
sqlQuery, err := query.toSQL()
|
||||
sqlQuery, err := input.toSQL()
|
||||
if err != nil {
|
||||
gc.JSON(http.StatusBadRequest, gin.H{"message": helpers.Capitalize(err.Error())})
|
||||
return
|
||||
}
|
||||
resolution := time.Duration(int64(query.End.Sub(query.Start).Nanoseconds()) / int64(query.Points))
|
||||
resolution := time.Duration(int64(input.End.Sub(input.Start).Nanoseconds()) / int64(input.Points))
|
||||
if resolution < time.Second {
|
||||
resolution = time.Second
|
||||
}
|
||||
sqlQuery = c.queryFlowsTable(sqlQuery,
|
||||
query.Start, query.End, resolution)
|
||||
input.Start, input.End, resolution)
|
||||
gc.Header("X-SQL-Query", strings.ReplaceAll(sqlQuery, "\n", " "))
|
||||
|
||||
results := []struct {
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
netHTTP "net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -22,12 +18,12 @@ import (
|
||||
func TestGraphQuerySQL(t *testing.T) {
|
||||
cases := []struct {
|
||||
Description string
|
||||
Input graphQuery
|
||||
Input graphHandlerInput
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
Description: "no dimensions, no filters, bps",
|
||||
Input: graphQuery{
|
||||
Input: graphHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Points: 100,
|
||||
@@ -48,7 +44,7 @@ GROUP BY time, dimensions
|
||||
ORDER BY time`,
|
||||
}, {
|
||||
Description: "no dimensions, no filters, pps",
|
||||
Input: graphQuery{
|
||||
Input: graphHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Points: 100,
|
||||
@@ -69,7 +65,7 @@ GROUP BY time, dimensions
|
||||
ORDER BY time`,
|
||||
}, {
|
||||
Description: "no dimensions",
|
||||
Input: graphQuery{
|
||||
Input: graphHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Points: 100,
|
||||
@@ -90,7 +86,7 @@ GROUP BY time, dimensions
|
||||
ORDER BY time`,
|
||||
}, {
|
||||
Description: "no filters",
|
||||
Input: graphQuery{
|
||||
Input: graphHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Points: 100,
|
||||
@@ -160,7 +156,24 @@ func TestGraphHandler(t *testing.T) {
|
||||
{base.Add(2 * time.Minute), 100, []string{"router2", "provider4"}},
|
||||
{base.Add(2 * time.Minute), 100, []string{"Other", "Other"}},
|
||||
}
|
||||
expected := gin.H{
|
||||
mockConn.EXPECT().
|
||||
Select(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
SetArg(1, expectedSQL).
|
||||
Return(nil)
|
||||
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/graph",
|
||||
JSONInput: gin.H{
|
||||
"start": time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
"end": time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
"points": 100,
|
||||
"limit": 20,
|
||||
"dimensions": []string{"ExporterName", "InIfProvider"},
|
||||
"filter": "DstCountry = 'FR' AND SrcCountry = 'US'",
|
||||
"units": "bps",
|
||||
},
|
||||
JSONOutput: gin.H{
|
||||
// Sorted by sum of bps
|
||||
"rows": [][]string{
|
||||
{"router1", "provider2"}, // 10000
|
||||
@@ -215,50 +228,7 @@ func TestGraphHandler(t *testing.T) {
|
||||
500,
|
||||
1000,
|
||||
},
|
||||
}
|
||||
mockConn.EXPECT().
|
||||
Select(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
SetArg(1, expectedSQL).
|
||||
Return(nil)
|
||||
|
||||
input := graphQuery{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Points: 100,
|
||||
Limit: 20,
|
||||
Dimensions: []queryColumn{
|
||||
queryColumnExporterName,
|
||||
queryColumnInIfProvider,
|
||||
},
|
||||
Filter: queryFilter{"DstCountry = 'FR' AND SrcCountry = 'US'"},
|
||||
Units: "bps",
|
||||
}
|
||||
payload := new(bytes.Buffer)
|
||||
err = json.NewEncoder(payload).Encode(input)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error:\n%+v", err)
|
||||
}
|
||||
resp, err := netHTTP.Post(fmt.Sprintf("http://%s/api/v0/console/graph", h.Address),
|
||||
"application/json", payload)
|
||||
if err != nil {
|
||||
t.Fatalf("POST /api/v0/console/graph:\n%+v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("POST /api/v0/console/graph: got status code %d, not 200", resp.StatusCode)
|
||||
}
|
||||
gotContentType := resp.Header.Get("Content-Type")
|
||||
if gotContentType != "application/json; charset=utf-8" {
|
||||
t.Errorf("POST /api/v0/console/graph Content-Type (-got, +want):\n-%s\n+%s",
|
||||
gotContentType, "application/json; charset=utf-8")
|
||||
}
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
var got gin.H
|
||||
if err := decoder.Decode(&got); err != nil {
|
||||
t.Fatalf("POST /api/v0/console/graph error:\n%+v", err)
|
||||
}
|
||||
|
||||
if diff := helpers.Diff(got, expected); diff != "" {
|
||||
t.Fatalf("POST /api/v0/console/graph (-got, +want):\n%s", diff)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ func (c *Component) Start() error {
|
||||
c.d.HTTP.GinRouter.GET("/api/v0/console/widget/graph", c.widgetGraphHandlerFunc)
|
||||
c.d.HTTP.GinRouter.POST("/api/v0/console/graph", c.graphHandlerFunc)
|
||||
c.d.HTTP.GinRouter.POST("/api/v0/console/sankey", c.sankeyHandlerFunc)
|
||||
c.d.HTTP.GinRouter.POST("/api/v0/console/filter/validate", c.filterValidateHandlerFunc)
|
||||
|
||||
c.t.Go(func() error {
|
||||
ticker := time.NewTicker(10 * time.Second)
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
"akvorado/common/helpers"
|
||||
)
|
||||
|
||||
// sankeyQuery describes the input for the /sankey endpoint.
|
||||
type sankeyQuery struct {
|
||||
// sankeyHandlerInput describes the input for the /sankey endpoint.
|
||||
type sankeyHandlerInput struct {
|
||||
Start time.Time `json:"start" binding:"required"`
|
||||
End time.Time `json:"end" binding:"required,gtfield=Start"`
|
||||
Dimensions []queryColumn `json:"dimensions" binding:"required,min=2"` // group by ...
|
||||
@@ -22,57 +22,7 @@ type sankeyQuery struct {
|
||||
Units string `json:"units" binding:"required,oneof=pps bps"`
|
||||
}
|
||||
|
||||
// sankeyQueryToSQL converts a sankey query to an SQL request
|
||||
func (query sankeyQuery) toSQL() (string, error) {
|
||||
// Filter
|
||||
where := query.Filter.filter
|
||||
if where == "" {
|
||||
where = "{timefilter}"
|
||||
} else {
|
||||
where = fmt.Sprintf("{timefilter} AND (%s)", where)
|
||||
}
|
||||
|
||||
// Select
|
||||
arrayFields := []string{}
|
||||
dimensions := []string{}
|
||||
for _, column := range query.Dimensions {
|
||||
arrayFields = append(arrayFields, fmt.Sprintf(`if(%s IN (SELECT %s FROM rows), %s, 'Other')`,
|
||||
column.String(),
|
||||
column.String(),
|
||||
column.toSQLSelect()))
|
||||
dimensions = append(dimensions, column.String())
|
||||
}
|
||||
fields := []string{}
|
||||
if query.Units == "pps" {
|
||||
fields = append(fields, `SUM(Packets*SamplingRate/range) AS xps`)
|
||||
} else {
|
||||
fields = append(fields, `SUM(Bytes*SamplingRate*8/range) AS xps`)
|
||||
}
|
||||
fields = append(fields, fmt.Sprintf("[%s] AS dimensions", strings.Join(arrayFields, ",\n ")))
|
||||
|
||||
// With
|
||||
with := []string{
|
||||
fmt.Sprintf(`(SELECT MAX(TimeReceived) - MIN(TimeReceived) FROM {table} WHERE %s) AS range`, where),
|
||||
fmt.Sprintf(
|
||||
"rows AS (SELECT %s FROM {table} WHERE %s GROUP BY %s ORDER BY SUM(Bytes) DESC LIMIT %d)",
|
||||
strings.Join(dimensions, ", "),
|
||||
where,
|
||||
strings.Join(dimensions, ", "),
|
||||
query.Limit),
|
||||
}
|
||||
|
||||
sqlQuery := fmt.Sprintf(`
|
||||
WITH
|
||||
%s
|
||||
SELECT
|
||||
%s
|
||||
FROM {table}
|
||||
WHERE %s
|
||||
GROUP BY dimensions
|
||||
ORDER BY xps DESC`, strings.Join(with, ",\n "), strings.Join(fields, ",\n "), where)
|
||||
return sqlQuery, nil
|
||||
}
|
||||
|
||||
// sankeyHandlerOutput describes the output for the /sankey endpoint.
|
||||
type sankeyHandlerOutput struct {
|
||||
// Unprocessed data for table view
|
||||
Rows [][]string `json:"rows"`
|
||||
@@ -87,29 +37,80 @@ type sankeyLink struct {
|
||||
Xps int `json:"xps"`
|
||||
}
|
||||
|
||||
// sankeyHandlerInputToSQL converts a sankey query to an SQL request
|
||||
func (input sankeyHandlerInput) toSQL() (string, error) {
|
||||
// Filter
|
||||
where := input.Filter.filter
|
||||
if where == "" {
|
||||
where = "{timefilter}"
|
||||
} else {
|
||||
where = fmt.Sprintf("{timefilter} AND (%s)", where)
|
||||
}
|
||||
|
||||
// Select
|
||||
arrayFields := []string{}
|
||||
dimensions := []string{}
|
||||
for _, column := range input.Dimensions {
|
||||
arrayFields = append(arrayFields, fmt.Sprintf(`if(%s IN (SELECT %s FROM rows), %s, 'Other')`,
|
||||
column.String(),
|
||||
column.String(),
|
||||
column.toSQLSelect()))
|
||||
dimensions = append(dimensions, column.String())
|
||||
}
|
||||
fields := []string{}
|
||||
if input.Units == "pps" {
|
||||
fields = append(fields, `SUM(Packets*SamplingRate/range) AS xps`)
|
||||
} else {
|
||||
fields = append(fields, `SUM(Bytes*SamplingRate*8/range) AS xps`)
|
||||
}
|
||||
fields = append(fields, fmt.Sprintf("[%s] AS dimensions", strings.Join(arrayFields, ",\n ")))
|
||||
|
||||
// With
|
||||
with := []string{
|
||||
fmt.Sprintf(`(SELECT MAX(TimeReceived) - MIN(TimeReceived) FROM {table} WHERE %s) AS range`, where),
|
||||
fmt.Sprintf(
|
||||
"rows AS (SELECT %s FROM {table} WHERE %s GROUP BY %s ORDER BY SUM(Bytes) DESC LIMIT %d)",
|
||||
strings.Join(dimensions, ", "),
|
||||
where,
|
||||
strings.Join(dimensions, ", "),
|
||||
input.Limit),
|
||||
}
|
||||
|
||||
sqlQuery := fmt.Sprintf(`
|
||||
WITH
|
||||
%s
|
||||
SELECT
|
||||
%s
|
||||
FROM {table}
|
||||
WHERE %s
|
||||
GROUP BY dimensions
|
||||
ORDER BY xps DESC`, strings.Join(with, ",\n "), strings.Join(fields, ",\n "), where)
|
||||
return sqlQuery, nil
|
||||
}
|
||||
|
||||
func (c *Component) sankeyHandlerFunc(gc *gin.Context) {
|
||||
ctx := c.t.Context(gc.Request.Context())
|
||||
var query sankeyQuery
|
||||
if err := gc.ShouldBindJSON(&query); err != nil {
|
||||
var input sankeyHandlerInput
|
||||
if err := gc.ShouldBindJSON(&input); err != nil {
|
||||
gc.JSON(http.StatusBadRequest, gin.H{"message": helpers.Capitalize(err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
sqlQuery, err := query.toSQL()
|
||||
sqlQuery, err := input.toSQL()
|
||||
if err != nil {
|
||||
gc.JSON(http.StatusBadRequest, gin.H{"message": helpers.Capitalize(err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
// We need to select a resolution allowing us to have a somewhat accurate timespan
|
||||
resolution := time.Duration(int64(query.End.Sub(query.Start).Nanoseconds()) / 20)
|
||||
resolution := time.Duration(int64(input.End.Sub(input.Start).Nanoseconds()) / 20)
|
||||
if resolution < time.Second {
|
||||
resolution = time.Second
|
||||
}
|
||||
|
||||
// Prepare and execute query
|
||||
sqlQuery = c.queryFlowsTable(sqlQuery,
|
||||
query.Start, query.End, resolution)
|
||||
input.Start, input.End, resolution)
|
||||
gc.Header("X-SQL-Query", strings.ReplaceAll(sqlQuery, "\n", " "))
|
||||
results := []struct {
|
||||
Xps float64 `ch:"xps"`
|
||||
@@ -132,7 +133,7 @@ func (c *Component) sankeyHandlerFunc(gc *gin.Context) {
|
||||
if name != "Other" {
|
||||
return name
|
||||
}
|
||||
return fmt.Sprintf("Other %s", query.Dimensions[index].String())
|
||||
return fmt.Sprintf("Other %s", input.Dimensions[index].String())
|
||||
}
|
||||
addedNodes := map[string]bool{}
|
||||
addNode := func(name string) {
|
||||
@@ -154,7 +155,7 @@ func (c *Component) sankeyHandlerFunc(gc *gin.Context) {
|
||||
output.Rows = append(output.Rows, result.Dimensions)
|
||||
output.Xps = append(output.Xps, int(result.Xps))
|
||||
// Consider each pair of successive dimensions
|
||||
for i := 0; i < len(query.Dimensions)-1; i++ {
|
||||
for i := 0; i < len(input.Dimensions)-1; i++ {
|
||||
dimension1 := completeName(result.Dimensions[i], i)
|
||||
dimension2 := completeName(result.Dimensions[i+1], i+1)
|
||||
addNode(dimension1)
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
netHTTP "net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -22,12 +18,12 @@ import (
|
||||
func TestSankeyQuerySQL(t *testing.T) {
|
||||
cases := []struct {
|
||||
Description string
|
||||
Input sankeyQuery
|
||||
Input sankeyHandlerInput
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
Description: "two dimensions, no filters, bps",
|
||||
Input: sankeyQuery{
|
||||
Input: sankeyHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Dimensions: []queryColumn{queryColumnSrcAS, queryColumnExporterName},
|
||||
@@ -49,7 +45,7 @@ GROUP BY dimensions
|
||||
ORDER BY xps DESC`,
|
||||
}, {
|
||||
Description: "two dimensions, no filters, pps",
|
||||
Input: sankeyQuery{
|
||||
Input: sankeyHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Dimensions: []queryColumn{queryColumnSrcAS, queryColumnExporterName},
|
||||
@@ -71,7 +67,7 @@ GROUP BY dimensions
|
||||
ORDER BY xps DESC`,
|
||||
}, {
|
||||
Description: "two dimensions, with filter",
|
||||
Input: sankeyQuery{
|
||||
Input: sankeyHandlerInput{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Dimensions: []queryColumn{queryColumnSrcAS, queryColumnExporterName},
|
||||
@@ -147,7 +143,23 @@ func TestSankeyHandler(t *testing.T) {
|
||||
{621, []string{"Other", "Other", "Other"}},
|
||||
{159, []string{"Other", "provider1", "router1"}},
|
||||
}
|
||||
expected := gin.H{
|
||||
mockConn.EXPECT().
|
||||
Select(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
SetArg(1, expectedSQL).
|
||||
Return(nil)
|
||||
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/sankey",
|
||||
JSONInput: gin.H{
|
||||
"start": time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
"end": time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
"dimensions": []string{"SrcAS", "InIfProvider", "ExporterName"},
|
||||
"limit": 10,
|
||||
"filter": "DstCountry = 'FR'",
|
||||
"units": "bps",
|
||||
},
|
||||
JSONOutput: gin.H{
|
||||
// Raw data
|
||||
"rows": [][]string{
|
||||
{"AS100", "Other", "router1"},
|
||||
@@ -235,46 +247,7 @@ func TestSankeyHandler(t *testing.T) {
|
||||
{"source": "provider2", "target": "Other ExporterName", "xps": 2234},
|
||||
{"source": "Other InIfProvider", "target": "Other ExporterName", "xps": 975 + 621},
|
||||
},
|
||||
}
|
||||
mockConn.EXPECT().
|
||||
Select(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
SetArg(1, expectedSQL).
|
||||
Return(nil)
|
||||
|
||||
input := sankeyQuery{
|
||||
Start: time.Date(2022, 04, 10, 15, 45, 10, 0, time.UTC),
|
||||
End: time.Date(2022, 04, 11, 15, 45, 10, 0, time.UTC),
|
||||
Dimensions: []queryColumn{queryColumnSrcAS, queryColumnInIfProvider, queryColumnExporterName},
|
||||
Limit: 10,
|
||||
Filter: queryFilter{"DstCountry = 'FR'"},
|
||||
Units: "bps",
|
||||
}
|
||||
payload := new(bytes.Buffer)
|
||||
err = json.NewEncoder(payload).Encode(input)
|
||||
if err != nil {
|
||||
t.Fatalf("Encode() error:\n%+v", err)
|
||||
}
|
||||
resp, err := netHTTP.Post(fmt.Sprintf("http://%s/api/v0/console/sankey", h.Address),
|
||||
"application/json", payload)
|
||||
if err != nil {
|
||||
t.Fatalf("POST /api/v0/console/sankey:\n%+v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("POST /api/v0/console/sankey: got status code %d, not 200", resp.StatusCode)
|
||||
}
|
||||
gotContentType := resp.Header.Get("Content-Type")
|
||||
if gotContentType != "application/json; charset=utf-8" {
|
||||
t.Errorf("POST /api/v0/console/sankey Content-Type (-got, +want):\n-%s\n+%s",
|
||||
gotContentType, "application/json; charset=utf-8")
|
||||
}
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
var got gin.H
|
||||
if err := decoder.Decode(&got); err != nil {
|
||||
t.Fatalf("POST /api/v0/console/sankey error:\n%+v", err)
|
||||
}
|
||||
|
||||
if diff := helpers.Diff(got, expected); diff != "" {
|
||||
t.Fatalf("POST /api/v0/console/sankey (-got, +want):\n%s", diff)
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
|
||||
"github.com/benbjohnson/clock"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"akvorado/common/clickhousedb"
|
||||
@@ -91,17 +92,14 @@ func TestWidgetLastFlow(t *testing.T) {
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/widget/flow-last",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{`,
|
||||
` "InIfBoundary": "external",`,
|
||||
` "InIfName": "Hu0/0/1/10",`,
|
||||
` "InIfSpeed": 100000,`,
|
||||
` "SamplingRate": 10000,`,
|
||||
` "SrcAddr": "2001:db8::22",`,
|
||||
` "SrcCountry": "FR",`,
|
||||
` "TimeReceived": "2022-04-04T08:36:11.00000001Z"`,
|
||||
`}`,
|
||||
JSONOutput: gin.H{
|
||||
"InIfBoundary": "external",
|
||||
"InIfName": "Hu0/0/1/10",
|
||||
"InIfSpeed": 100000,
|
||||
"SamplingRate": 10000,
|
||||
"SrcAddr": "2001:db8::22",
|
||||
"SrcCountry": "FR",
|
||||
"TimeReceived": "2022-04-04T08:36:11.00000001Z",
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -133,12 +131,9 @@ func TestFlowRate(t *testing.T) {
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/widget/flow-rate",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{`,
|
||||
` "period": "second",`,
|
||||
` "rate": 100.1`,
|
||||
`}`,
|
||||
JSONOutput: gin.H{
|
||||
"period": "second",
|
||||
"rate": 100.1,
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -174,15 +169,12 @@ func TestWidgetExporters(t *testing.T) {
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/widget/exporters",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{`,
|
||||
` "exporters": [`,
|
||||
` "exporter1",`,
|
||||
` "exporter2",`,
|
||||
` "exporter3"`,
|
||||
` ]`,
|
||||
`}`,
|
||||
JSONOutput: gin.H{
|
||||
"exporters": []string{
|
||||
"exporter1",
|
||||
"exporter2",
|
||||
"exporter3",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -236,28 +228,32 @@ func TestWidgetTop(t *testing.T) {
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/widget/top/src-port",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{"top":[{"name":"TCP/443","percent":51},{"name":"UDP/443","percent":20},{"name":"TCP/80","percent":18}]}`,
|
||||
},
|
||||
JSONOutput: gin.H{
|
||||
"top": []gin.H{
|
||||
gin.H{"name": "TCP/443", "percent": 51},
|
||||
gin.H{"name": "UDP/443", "percent": 20},
|
||||
gin.H{"name": "TCP/80", "percent": 18}}},
|
||||
}, {
|
||||
URL: "/api/v0/console/widget/top/protocol",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{"top":[{"name":"TCP","percent":75},{"name":"UDP","percent":24},{"name":"ESP","percent":1}]}`,
|
||||
},
|
||||
JSONOutput: gin.H{
|
||||
"top": []gin.H{
|
||||
gin.H{"name": "TCP", "percent": 75},
|
||||
gin.H{"name": "UDP", "percent": 24},
|
||||
gin.H{"name": "ESP", "percent": 1}}},
|
||||
}, {
|
||||
URL: "/api/v0/console/widget/top/exporter",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{"top":[{"name":"exporter1","percent":20},{"name":"exporter3","percent":10},{"name":"exporter5","percent":3}]}`,
|
||||
},
|
||||
JSONOutput: gin.H{
|
||||
"top": []gin.H{
|
||||
gin.H{"name": "exporter1", "percent": 20},
|
||||
gin.H{"name": "exporter3", "percent": 10},
|
||||
gin.H{"name": "exporter5", "percent": 3}}},
|
||||
}, {
|
||||
URL: "/api/v0/console/widget/top/src-as",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{"top":[{"name":"2906: Netflix","percent":12},{"name":"36040: Youtube","percent":10},{"name":"20940: Akamai","percent":9}]}`,
|
||||
},
|
||||
JSONOutput: gin.H{
|
||||
"top": []gin.H{
|
||||
gin.H{"name": "2906: Netflix", "percent": 12},
|
||||
gin.H{"name": "36040: Youtube", "percent": 10},
|
||||
gin.H{"name": "20940: Akamai", "percent": 9}}},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -309,9 +305,14 @@ ORDER BY Time`).
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/console/widget/graph?points=100",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{"data":[{"t":"2009-11-10T23:00:00Z","gbps":25.3},{"t":"2009-11-10T23:01:00Z","gbps":27.8},{"t":"2009-11-10T23:02:00Z","gbps":26.4},{"t":"2009-11-10T23:03:00Z","gbps":29.2},{"t":"2009-11-10T23:04:00Z","gbps":21.3},{"t":"2009-11-10T23:05:00Z","gbps":24.7}]}`,
|
||||
JSONOutput: gin.H{
|
||||
"data": []gin.H{
|
||||
gin.H{"t": "2009-11-10T23:00:00Z", "gbps": 25.3},
|
||||
gin.H{"t": "2009-11-10T23:01:00Z", "gbps": 27.8},
|
||||
gin.H{"t": "2009-11-10T23:02:00Z", "gbps": 26.4},
|
||||
gin.H{"t": "2009-11-10T23:03:00Z", "gbps": 29.2},
|
||||
gin.H{"t": "2009-11-10T23:04:00Z", "gbps": 21.3},
|
||||
gin.H{"t": "2009-11-10T23:05:00Z", "gbps": 24.7}},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
|
||||
"akvorado/common/helpers"
|
||||
"akvorado/common/reporter"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestHTTPEndpoints(t *testing.T) {
|
||||
@@ -21,15 +23,12 @@ func TestHTTPEndpoints(t *testing.T) {
|
||||
},
|
||||
}, {
|
||||
URL: "/api/v0/inlet/flow/schemas.json",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
`{`,
|
||||
` "current-version": 1,`,
|
||||
` "versions": {`,
|
||||
` "0": "/api/v0/inlet/flow/schema-0.proto",`,
|
||||
` "1": "/api/v0/inlet/flow/schema-1.proto"`,
|
||||
` }`,
|
||||
`}`,
|
||||
JSONOutput: gin.H{
|
||||
"current-version": 1,
|
||||
"versions": gin.H{
|
||||
"0": "/api/v0/inlet/flow/schema-0.proto",
|
||||
"1": "/api/v0/inlet/flow/schema-1.proto",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"akvorado/common/helpers"
|
||||
"akvorado/common/http"
|
||||
"akvorado/common/reporter"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestConfigurationEndpoint(t *testing.T) {
|
||||
@@ -25,12 +27,9 @@ func TestConfigurationEndpoint(t *testing.T) {
|
||||
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
|
||||
{
|
||||
URL: "/api/v0/orchestrator/broker/configuration/inlet",
|
||||
ContentType: "application/json; charset=utf-8",
|
||||
FirstLines: []string{
|
||||
"{",
|
||||
` "bye": "Goodbye world!",`,
|
||||
` "hello": "Hello world!"`,
|
||||
"}",
|
||||
JSONOutput: gin.H{
|
||||
"bye": "Goodbye world!",
|
||||
"hello": "Hello world!",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user