From b900fead753504246f49596c684ee0a03e8df112 Mon Sep 17 00:00:00 2001 From: ebb-tide Date: Mon, 2 Jul 2018 17:45:44 -0700 Subject: [PATCH 1/9] Add map as a template variable value type to the backend --- bolt/internal/internal.proto | 2 +- chronograf.go | 4 ++-- server/swagger.json | 2 +- server/templates.go | 2 +- server/templates_test.go | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bolt/internal/internal.proto b/bolt/internal/internal.proto index 27dec2513c..f11b3bc833 100644 --- a/bolt/internal/internal.proto +++ b/bolt/internal/internal.proto @@ -97,7 +97,7 @@ message Template { } message TemplateValue { - string type = 1; // Type can be tagKey, tagValue, fieldKey, csv, measurement, database, constant + string type = 1; // Type can be tagKey, tagValue, fieldKey, csv, map, measurement, database, constant string value = 2; // Value is the specific value used to replace a template in an InfluxQL query bool selected = 3; // Selected states that this variable has been picked to use for replacement string key = 4; // Key is the key for a specific Value if the Template Type is map (optional) diff --git a/chronograf.go b/chronograf.go index 8a1c54f47d..3f83f8f950 100644 --- a/chronograf.go +++ b/chronograf.go @@ -159,7 +159,7 @@ type Range struct { // TemplateValue is a value use to replace a template in an InfluxQL query type TemplateValue struct { Value string `json:"value"` // Value is the specific value used to replace a template in an InfluxQL query - Type string `json:"type"` // Type can be tagKey, tagValue, fieldKey, csv, measurement, database, constant, influxql + Type string `json:"type"` // Type can be tagKey, tagValue, fieldKey, csv, map, measurement, database, constant, influxql Selected bool `json:"selected"` // Selected states that this variable has been picked to use for replacement Key string `json:"key,omitempty"` // Key is the key for the Value if the Template Type is 'map' } @@ -177,7 +177,7 @@ type TemplateID string type Template struct { TemplateVar ID TemplateID `json:"id"` // ID is the unique ID associated with this template - Type string `json:"type"` // Type can be fieldKeys, tagKeys, tagValues, CSV, constant, measurements, databases, map, influxql + Type string `json:"type"` // Type can be fieldKeys, tagKeys, tagValues, csv, constant, measurements, databases, map, influxql Label string `json:"label"` // Label is a user-facing description of the Template Query *TemplateQuery `json:"query,omitempty"` // Query is used to generate the choices for a template } diff --git a/server/swagger.json b/server/swagger.json index bb591d4927..968ef9b2c8 100644 --- a/server/swagger.json +++ b/server/swagger.json @@ -4178,7 +4178,7 @@ }, "type": { "type": "string", - "enum": ["csv", "tagKey", "tagValue", "fieldKey", "timeStamp"], + "enum": ["csv", "tagKey", "tagValue", "fieldKey", "timeStamp", "map"], "description": "The type will change the format of the output value. tagKey/fieldKey are double quoted; tagValue are single quoted; csv and timeStamp are not quoted." }, diff --git a/server/templates.go b/server/templates.go index 6f158ac3c3..fcb7a365e1 100644 --- a/server/templates.go +++ b/server/templates.go @@ -22,7 +22,7 @@ func ValidTemplateRequest(template *chronograf.Template) error { switch v.Type { default: return fmt.Errorf("Unknown template variable type %s", v.Type) - case "csv", "fieldKey", "tagKey", "tagValue", "measurement", "database", "constant", "influxql": + case "csv", "map", "fieldKey", "tagKey", "tagValue", "measurement", "database", "constant", "influxql": } if template.Type == "map" && v.Key == "" { diff --git a/server/templates_test.go b/server/templates_test.go index cd775acea9..4a23fefefd 100644 --- a/server/templates_test.go +++ b/server/templates_test.go @@ -69,7 +69,7 @@ func TestValidTemplateRequest(t *testing.T) { { Key: "key", Value: "value", - Type: "constant", + Type: "map", }, }, }, @@ -84,7 +84,7 @@ func TestValidTemplateRequest(t *testing.T) { Values: []chronograf.TemplateValue{ { Value: "value", - Type: "constant", + Type: "map", }, }, }, From 45875c7840b3110fa7aa97d7c53d9eec0fc5cf0a Mon Sep 17 00:00:00 2001 From: ebb-tide Date: Mon, 2 Jul 2018 17:49:00 -0700 Subject: [PATCH 2/9] Add a map template variable builder --- ui/src/shared/copy/notifications.ts | 6 + .../components/MapTemplateBuilder.tsx | 150 ++++++++++++++++++ .../components/TemplatePreviewListItem.tsx | 1 + .../components/TemplateVariableEditor.tsx | 2 + ui/src/tempVars/constants/index.ts | 15 ++ ui/src/types/tempVars.ts | 3 + 6 files changed, 177 insertions(+) create mode 100644 ui/src/tempVars/components/MapTemplateBuilder.tsx diff --git a/ui/src/shared/copy/notifications.ts b/ui/src/shared/copy/notifications.ts index 879ed8e090..6ed110f0b1 100644 --- a/ui/src/shared/copy/notifications.ts +++ b/ui/src/shared/copy/notifications.ts @@ -541,6 +541,12 @@ export const notifyInvalidTimeRangeValueInURLQuery = (): Notification => ({ message: `Invalid URL query value supplied for lower or upper time range.`, }) +export const notifyInvalidMapType = (): Notification => ({ + ...defaultErrorNotification, + icon: 'cube', + message: `Template Variables of map type accept two comma separated values per line`, +}) + export const notifyInvalidZoomedTimeRangeValueInURLQuery = (): Notification => ({ ...defaultErrorNotification, icon: 'cube', diff --git a/ui/src/tempVars/components/MapTemplateBuilder.tsx b/ui/src/tempVars/components/MapTemplateBuilder.tsx new file mode 100644 index 0000000000..9ef8b735de --- /dev/null +++ b/ui/src/tempVars/components/MapTemplateBuilder.tsx @@ -0,0 +1,150 @@ +import React, {PureComponent, ChangeEvent} from 'react' +import {getDeep} from 'src/utils/wrappers' +import Papa from 'papaparse' +import _ from 'lodash' + +import {ErrorHandling} from 'src/shared/decorators/errors' + +import TemplatePreviewList from 'src/tempVars/components/TemplatePreviewList' +import DragAndDrop from 'src/shared/components/DragAndDrop' +import { + notifyCSVUploadFailed, + notifyInvalidMapType, +} from 'src/shared/copy/notifications' + +import {TemplateBuilderProps, TemplateValueType} from 'src/types' +import {trimAndRemoveQuotes} from 'src/tempVars/utils/parsing' + +interface State { + templateValuesString: string +} + +@ErrorHandling +class MapTemplateBuilder extends PureComponent { + public constructor(props: TemplateBuilderProps) { + super(props) + const templateValues = props.template.values.map(v => v.value) + + this.state = { + templateValuesString: templateValues.join(', '), + } + } + + public render() { + const {onUpdateDefaultTemplateValue, template} = this.props + const {templateValuesString} = this.state + const pluralizer = template.values.length === 1 ? '' : 's' + + return ( + <> +
+ + +
+
+ +
+