Skip to content

Commit

Permalink
feat: update hideFromReadData field to accept an array of strings and…
Browse files Browse the repository at this point in the history
… implement condition comparison logic
  • Loading branch information
BenElferink committed Dec 23, 2024
1 parent 0479097 commit 35717ca
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 46 deletions.
4 changes: 3 additions & 1 deletion destinations/data/elasticsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ spec:
required: false
tooltip: 'Username used for HTTP Basic Authentication'
renderCondition: ['ELASTICSEARCH_BASIC_AUTH_ENABLED', '==', 'true']
hideFromReadData: ['ELASTICSEARCH_BASIC_AUTH_ENABLED', '==', 'false']
- name: ELASTICSEARCH_PASSWORD
displayName: Password
componentType: input
Expand All @@ -66,6 +67,7 @@ spec:
tooltip: 'Password used for HTTP Basic Authentication'
secret: true
renderCondition: ['ELASTICSEARCH_BASIC_AUTH_ENABLED', '==', 'true']
hideFromReadData: ['ELASTICSEARCH_BASIC_AUTH_ENABLED', '==', 'false']
- name: ELASTICSEARCH_TLS_ENABLED
displayName: Enable TLS
componentType: checkbox
Expand All @@ -89,4 +91,4 @@ spec:
placeholder: '-----BEGIN CERTIFICATE-----'
tooltip: 'When using TLS, provide the CA certificate to verify the server. If empty uses system root CA'
renderCondition: ['ELASTICSEARCH_TLS_ENABLED', '==', 'true']
hideFromReadData: true
hideFromReadData: ['true']
2 changes: 1 addition & 1 deletion destinations/data/jaeger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ spec:
placeholder: '-----BEGIN CERTIFICATE-----'
tooltip: 'When using TLS, provide the CA certificate to verify the server. If empty uses system root CA'
renderCondition: ['JAEGER_TLS_ENABLED', '==', 'true']
hideFromReadData: true
hideFromReadData: ['true']
testConnectionSupported: true
2 changes: 1 addition & 1 deletion destinations/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ type Field struct {
Secret bool `yaml:"secret"`
InitialValue string `yaml:"initialValue"`
RenderCondition []string `yaml:"renderCondition"`
HideFromReadData bool `yaml:"hideFromReadData"`
HideFromReadData []string `yaml:"hideFromReadData"`
CustomReadDataLabels []*CustomReadDataLabel `yaml:"customReadDataLabels"`
}
2 changes: 1 addition & 1 deletion frontend/endpoints/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type Field struct {
Secret bool `json:"secret,omitempty"`
InitialValue string `json:"initial_value,omitempty"`
RenderCondition []string `json:"render_condition,omitempty"`
HideFromReadData bool `json:"hide_from_read_data,omitempty"`
HideFromReadData []string `json:"hide_from_read_data,omitempty"`
CustomReadDataLabels []*CustomReadDataLabel `json:"custom_read_data_labels,omitempty"`
}

Expand Down
6 changes: 3 additions & 3 deletions frontend/graph/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ type Field {
secret: Boolean!
initialValue: String!
renderCondition: [String!]!
hideFromReadData: Boolean!
hideFromReadData: [String!]!
customReadDataLabels: [CustomReadDataLabel!]!
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DISPLAY_TITLES, safeJsonParse } from '@/utils';
import { compareCondition, DISPLAY_TITLES, safeJsonParse } from '@/utils';
import { DataCardRow, DataCardFieldTypes } from '@/reuseable-components';
import type { ActualDestination, DestinationDetailsResponse, ExportedSignals } from '@/types';

Expand All @@ -24,9 +24,16 @@ const buildCard = (destination: ActualDestination, destinationTypeDetails?: Dest
sortedParsedFields.map(({ key, value }) => {
const { displayName, secret, componentProperties, hideFromReadData, customReadDataLabels } = destinationTypeDetails?.fields?.find((field) => field.name === key) || {};

if (!hideFromReadData) {
const shouldHide = !!hideFromReadData?.length
? compareCondition(
hideFromReadData,
(destinationTypeDetails?.fields || []).map((field) => ({ name: field.name, value: parsedFields[field.name] ?? null })),
)
: false;

if (!shouldHide) {
const { type } = safeJsonParse(componentProperties, { type: '' });
const isSecret = secret || type === 'password' ? new Array(10).fill('•').join('') : '';
const isSecret = (secret || type === 'password') && !!value.length ? new Array(10).fill('•').join('') : '';

if (!!customReadDataLabels?.length) {
customReadDataLabels.forEach(({ condition, ...custom }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { INPUT_TYPES } from '@/utils';
import type { DynamicField } from '@/types';
import { compareCondition, INPUT_TYPES } from '@/utils';
import { Dropdown, Input, TextArea, InputList, KeyValueInputsList, Checkbox } from '@/reuseable-components';

interface Props {
Expand All @@ -9,38 +9,6 @@ interface Props {
formErrors: Record<string, string>;
}

const compareCondition = (renderCondition: DynamicField['renderCondition'], fields: DynamicField[]) => {
if (!renderCondition || !renderCondition.length) return true;

const [key, cond, val] = renderCondition;
const field = fields.find((field) => field.name === key);

if (!field) {
console.warn(`Field with name ${key} not found, render condition will be ignored`);
return true;
}

switch (cond) {
case '===':
case '==':
return field.value === val;
case '!==':
case '!=':
return field.value !== val;
case '>':
return field.value > val;
case '<':
return field.value < val;
case '>=':
return field.value >= val;
case '<=':
return field.value <= val;
default:
console.warn(`Invalid condition ${cond}, render condition will be ignored`);
return true;
}
};

export const DestinationDynamicFields: React.FC<Props> = ({ fields, onChange, formErrors }) => {
return fields?.map((field) => {
const { componentType, renderCondition, ...rest } = field;
Expand Down
2 changes: 1 addition & 1 deletion frontend/webapp/types/destinations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface DestinationDetailsField {
secret: boolean;
initialValue: string;
renderCondition: string[];
hideFromReadData: boolean;
hideFromReadData: string[];
customReadDataLabels: {
condition: string;
title: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const compareCondition = (renderCondition: string[], fields: { name: string; value: any }[]) => {
if (!renderCondition || !renderCondition.length) return true;
if (renderCondition.length === 1) return renderCondition[0] == 'true';

const [key, cond, val] = renderCondition;
const field = fields.find((field) => field.name === key);

if (!field) {
console.warn(`Field with name ${key} not found, condition will be skipped`);
return true;
}

switch (cond) {
case '===':
case '==':
return field.value === val;
case '!==':
case '!=':
return field.value !== val;
case '>':
return field.value > val;
case '<':
return field.value < val;
case '>=':
return field.value >= val;
case '<=':
return field.value <= val;
default:
console.warn(`Invalid condition ${cond}, condition will be skipped`);
return true;
}
};
1 change: 1 addition & 0 deletions frontend/webapp/utils/functions/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './compare-condition';
export * from './get-value-for-range';

0 comments on commit 35717ca

Please sign in to comment.