Repository: skupperproject/skupper
Pull request #1132 (2023-05-31 21:41:05)

add state query param


Patch:
diff --git a/pkg/flow/record.go b/pkg/flow/record.go
index b75c18c1..9e4c8ce9 100644
--- a/pkg/flow/record.go
+++ b/pkg/flow/record.go
@@ -440,6 +440,7 @@ type QueryParams struct {
 	TimeRangeStart     uint64            `json:"timeRangeStart"`
 	TimeRangeEnd       uint64            `json:"timeRangeEnd"`
 	TimeRangeOperation TimeRangeRelation `json:"timeRangeOperation"`
+	State              RecordState       `json:"state"`
 }
 
 func getQueryParams(url *url.URL) QueryParams {
@@ -453,6 +454,7 @@ func getQueryParams(url *url.URL) QueryParams {
 		TimeRangeStart:     now - (15 * oneMinute),
 		TimeRangeEnd:       now,
 		TimeRangeOperation: intersects,
+		State:              all,
 	}
 
 	for k, v := range url.Query() {
@@ -499,6 +501,18 @@ func getQueryParams(url *url.URL) QueryParams {
 			default:
 				qp.TimeRangeOperation = intersects
 			}
+		case "state":
+			recordState := v[0]
+			switch recordState {
+			case "all":
+				qp.State = all
+			case "active":
+				qp.State = active
+			case "terminated":
+				qp.State = terminated
+			default:
+				qp.State = all
+			}
 		default:
 			qp.FilterFields[cases.Title(language.Und, cases.NoLower).String(k)] = v[0]
 		}
@@ -533,16 +547,30 @@ const (
 	within
 )
 
+type RecordState int
+
+const (
+	all RecordState = iota
+	active
+	terminated
+)
+
 func (base *Base) TimeRangeValid(qp QueryParams) bool {
-	switch qp.TimeRangeOperation {
-	case intersects:
-		return !(base.EndTime != 0 && base.EndTime < qp.TimeRangeStart || base.StartTime > qp.TimeRangeEnd)
-	case contains:
-		return base.StartTime <= qp.TimeRangeStart && (base.EndTime == 0 || base.EndTime >= qp.TimeRangeEnd)
-	case within:
-		return base.StartTime >= qp.TimeRangeStart && (base.EndTime != 0 && base.EndTime <= qp.TimeRangeEnd)
-	default:
+	if qp.State == active && base.EndTime != 0 {
 		return false
+	} else if qp.State == terminated && base.EndTime == 0 {
+		return false
+	} else {
+		switch qp.TimeRangeOperation {
+		case intersects:
+			return !(base.EndTime != 0 && base.EndTime < qp.TimeRangeStart || base.StartTime > qp.TimeRangeEnd)
+		case contains:
+			return base.StartTime <= qp.TimeRangeStart && (base.EndTime == 0 || base.EndTime >= qp.TimeRangeEnd)
+		case within:
+			return base.StartTime >= qp.TimeRangeStart && (base.EndTime != 0 && base.EndTime <= qp.TimeRangeEnd)
+		default:
+			return false
+		}
 	}
 }
 


Test patch:
diff --git a/pkg/flow/record_test.go b/pkg/flow/record_test.go
index 70273074..00511dbc 100644
--- a/pkg/flow/record_test.go
+++ b/pkg/flow/record_test.go
@@ -270,6 +270,93 @@ func TestTimeRange(t *testing.T) {
 	}
 }
 
+func TestRecordState(t *testing.T) {
+	type test struct {
+		name               string
+		startTime          uint64
+		endTime            uint64
+		queryParams        QueryParams
+		timeRangeStart     uint64
+		timeRangeEnd       uint64
+		timeRangeOperation TimeRangeRelation
+		state              RecordState
+		result             bool
+	}
+
+	testTable := []test{
+		// A[2,3]
+		{
+			name:      "A-all",
+			startTime: 2,
+			endTime:   15,
+			queryParams: QueryParams{
+				TimeRangeStart:     1,
+				TimeRangeEnd:       20,
+				TimeRangeOperation: intersects,
+				State:              all,
+			},
+			result: true,
+		},
+		{
+			name:      "A-active",
+			startTime: 2,
+			endTime:   0,
+			queryParams: QueryParams{
+				TimeRangeStart:     1,
+				TimeRangeEnd:       20,
+				TimeRangeOperation: intersects,
+				State:              active,
+			},
+			result: true,
+		},
+		{
+			name:      "A-not-active",
+			startTime: 2,
+			endTime:   15,
+			queryParams: QueryParams{
+				TimeRangeStart:     1,
+				TimeRangeEnd:       20,
+				TimeRangeOperation: intersects,
+				State:              active,
+			},
+			result: false,
+		},
+		{
+			name:      "A-terminated",
+			startTime: 2,
+			endTime:   3,
+			queryParams: QueryParams{
+				TimeRangeStart:     1,
+				TimeRangeEnd:       20,
+				TimeRangeOperation: intersects,
+				State:              terminated,
+			},
+			result: true,
+		},
+		{
+			name:      "A-not-terminated",
+			startTime: 2,
+			endTime:   0,
+			queryParams: QueryParams{
+				TimeRangeStart:     1,
+				TimeRangeEnd:       20,
+				TimeRangeOperation: intersects,
+				State:              terminated,
+			},
+			result: false,
+		},
+	}
+
+	for _, test := range testTable {
+		base := Base{
+			StartTime: test.startTime,
+			EndTime:   test.endTime,
+		}
+		result := base.TimeRangeValid(test.queryParams)
+		assert.Equal(t, test.result, result, test.name)
+	}
+}
+
 func TestParameters(t *testing.T) {
 	type test struct {
 		values      map[string]string