2
0
Эх сурвалжийг харах

field parser for diagnostics (#3241)

Co-authored-by: Alex Meijer <ameijer@users.noreply.github.com>
Ishaan Mittal 9 сар өмнө
parent
commit
3ea4b9aaa0

+ 15 - 0
core/pkg/filter/diagnostics/fields.go

@@ -0,0 +1,15 @@
+package diagnostics
+
+import (
+	"github.com/opencost/opencost/core/pkg/filter/fieldstrings"
+)
+
+// DiagnosticsField is an enum that represents Diagnostics-specific fields that can be
+// filtered on (cluster)
+type DiagnosticsField string
+
+// If you add a DiagnosticsField, make sure to update field maps to return the correct
+// Diagnostic value does not enforce exhaustive pattern matching on "enum" types.
+const (
+	FieldClusterID  DiagnosticsField = DiagnosticsField(fieldstrings.FieldClusterID)
+)

+ 36 - 0
core/pkg/filter/diagnostics/parser.go

@@ -0,0 +1,36 @@
+package diagnostics
+
+import "github.com/opencost/opencost/core/pkg/filter/ast"
+
+// a slice of all the diagnostics field instances the lexer should recognize as
+// valid left-hand comparators
+var diagnosticsFilterFields []*ast.Field = []*ast.Field{
+	ast.NewField(FieldClusterID),
+}
+
+// fieldMap is a lazily loaded mapping from DiagnosticsField to ast.Field
+var fieldMap map[DiagnosticsField]*ast.Field
+
+func init() {
+	fieldMap = make(map[DiagnosticsField]*ast.Field, len(diagnosticsFilterFields))
+	for _, f := range diagnosticsFilterFields {
+		ff := *f
+		fieldMap[DiagnosticsField(ff.Name)] = &ff
+	}
+}
+
+// DefaultFieldByName returns only default diagnostics filter fields by name.
+func DefaultFieldByName(field DiagnosticsField) *ast.Field {
+	if af, ok := fieldMap[field]; ok {
+		afcopy := *af
+		return &afcopy
+	}
+
+	return nil
+}
+
+// NewDiagnosticFilterParser creates a new `ast.FilterParser` implementation
+// which uses diagnostics specific fields
+func NewDiagnosticsFilterParser() ast.FilterParser {
+	return ast.NewFilterParser(diagnosticsFilterFields)
+}