-
Notifications
You must be signed in to change notification settings - Fork 49
feat: Color Map pass the colorMapping object to giraffe
#3473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
539c3d6
feat: adding colorMapping utils
ChitlangeSahas c71f238
chore: minor cleanup of variable names
ChitlangeSahas 00ad1bd
chore: prettier
ChitlangeSahas d4bbdfc
test: added tests for the colorMappingUtils.ts
ChitlangeSahas 91a40a9
chore: prettier
ChitlangeSahas 54bc75e
chore: openAPI SHA
ChitlangeSahas 7c319cd
chore: prettier
ChitlangeSahas a1ebcae
feat: add feature flag
ChitlangeSahas 71f7be5
fix: address PR comments
ChitlangeSahas 4d203cc
test: more tests
ChitlangeSahas c04bb10
chore: prettier
ChitlangeSahas fe0c4cc
fix: better feature flag use
ChitlangeSahas 89a4ac1
chore: prettier
ChitlangeSahas 192bb99
chore: ✂️
ChitlangeSahas eac7e40
chore: openAPI SHA
ChitlangeSahas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // tests will go here | ||
|
||
|
|
||
| import {getColorMappingObjects} from './colorMappingUtils' | ||
| import {XYViewProperties} from '../../client' | ||
|
|
||
| const MOCK_START = 1639775162126 | ||
| const MOCK_STOP = 1639778762126 | ||
| const dataFromIDPE_noColorMapping = { | ||
| columnGroup: { | ||
| mappings: [ | ||
| { | ||
| _start: `${MOCK_START}`, | ||
| _stop: `${MOCK_STOP}`, | ||
| _field: 'co', | ||
| _measurement: 'airSensors', | ||
| sensor_id: 'TLM0100', | ||
| result: 'mean', | ||
| }, | ||
| { | ||
| _start: `${MOCK_START}`, | ||
| _stop: `${MOCK_STOP}`, | ||
| _field: 'co', | ||
| _measurement: 'airSensors', | ||
| sensor_id: 'TLM0101', | ||
| result: 'mean', | ||
| }, | ||
| { | ||
| _start: `${MOCK_START}`, | ||
| _stop: `${MOCK_STOP}`, | ||
| _field: 'co', | ||
| _measurement: 'airSensors', | ||
| sensor_id: 'TLM0102', | ||
| result: 'mean', | ||
| }, | ||
| ], | ||
| columnKeys: [ | ||
| '_start', | ||
| '_stop', | ||
| '_field', | ||
| '_measurement', | ||
| 'sensor_id', | ||
| 'result', | ||
| ], | ||
| }, | ||
| properties: { | ||
| // no colorMapping present | ||
| colors: [ | ||
| { | ||
| id: '2f2d8b62-5e53-46b2-bde9-a291bcc5f9e7', | ||
| type: 'scale', | ||
| hex: '#31C0F6', | ||
| name: 'Nineteen Eighty Four', | ||
| value: 0, | ||
| }, | ||
| { | ||
| id: '4b0d1e17-2177-40cf-b524-980fe80f6ca8', | ||
| type: 'scale', | ||
| hex: '#A500A5', | ||
| name: 'Nineteen Eighty Four', | ||
| value: 0, | ||
| }, | ||
| { | ||
| id: 'd90d4be9-14f8-48da-a309-8d2cb50f6961', | ||
| type: 'scale', | ||
| hex: '#FF7E27', | ||
| name: 'Nineteen Eighty Four', | ||
| value: 0, | ||
| }, | ||
| ], | ||
| }, | ||
| } | ||
|
|
||
| const expectedData = { | ||
| mappingForGiraffe: { | ||
| mappings: [ | ||
| { | ||
| _start: `${MOCK_START}`, | ||
| _stop: `${MOCK_STOP}`, | ||
| _field: 'co', | ||
| _measurement: 'airSensors', | ||
| sensor_id: 'TLM0100', | ||
| result: 'mean', | ||
| color: '#31C0F6', | ||
| }, | ||
| { | ||
| _start: `${MOCK_START}`, | ||
| _stop: `${MOCK_STOP}`, | ||
| _field: 'co', | ||
| _measurement: 'airSensors', | ||
| sensor_id: 'TLM0101', | ||
| result: 'mean', | ||
| color: '#A500A5', | ||
| }, | ||
| { | ||
| _start: `${MOCK_START}`, | ||
| _stop: `${MOCK_STOP}`, | ||
| _field: 'co', | ||
| _measurement: 'airSensors', | ||
| sensor_id: 'TLM0102', | ||
| result: 'mean', | ||
| color: '#FF7E27', | ||
| }, | ||
| ], | ||
| columnKeys: [ | ||
| '_start', | ||
| '_stop', | ||
| '_field', | ||
| '_measurement', | ||
| 'sensor_id', | ||
| 'result', | ||
| ], | ||
| seriesToColorIndexMap: { | ||
| 'co-airSensors-TLM0100-mean-': 0, | ||
| 'co-airSensors-TLM0101-mean-': 1, | ||
| 'co-airSensors-TLM0102-mean-': 2, | ||
| }, | ||
| }, | ||
| mappingForIDPE: { | ||
| colorMapping: { | ||
| 'co-airSensors-TLM0100-mean-': 0, | ||
| 'co-airSensors-TLM0101-mean-': 1, | ||
| 'co-airSensors-TLM0102-mean-': 2, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| describe('color mapping utils', function() { | ||
| it('should generate a color mapping when view properties from the IDPE do not have colorMapping', function() { | ||
| const { | ||
| colorMappingForGiraffe, | ||
| colorMappingForIDPE, | ||
| needsToSaveToIDPE, | ||
| } = getColorMappingObjects( | ||
| dataFromIDPE_noColorMapping.columnGroup, | ||
| dataFromIDPE_noColorMapping.properties as XYViewProperties | ||
| ) | ||
|
|
||
| expect(colorMappingForGiraffe).toStrictEqual(expectedData.mappingForGiraffe) | ||
| expect(needsToSaveToIDPE).toBe(true) | ||
| expect(colorMappingForIDPE).toStrictEqual( | ||
| expectedData.mappingForIDPE.colorMapping | ||
| ) | ||
| }) | ||
| }) | ||
ChitlangeSahas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import {XYViewProperties} from 'src/types' | ||
|
|
||
| /** | ||
| * Evaluates deeper equality for two map objects based on their key:value pair | ||
| * @param map1 | ||
| * @param map2 | ||
| */ | ||
| const areMappingsSame = (map1, map2) => { | ||
| if (!map1 || !map2) { | ||
| return false | ||
| } | ||
|
|
||
| // Create arrays of property names | ||
| const aProps = Object.getOwnPropertyNames(map1) | ||
| const bProps = Object.getOwnPropertyNames(map2) | ||
|
|
||
| // If number of properties is different, | ||
| // objects are not equivalent | ||
| if (aProps.length != bProps.length) { | ||
ChitlangeSahas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false | ||
| } | ||
|
|
||
| for (let i = 0; i < aProps.length; i++) { | ||
| const propName = aProps[i] | ||
|
|
||
| // If values of same property are not equal, | ||
| // objects are not equivalent | ||
| if (map1[propName] !== map2[propName]) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * Returns the colorMapping objects for UI, based on the state of the current view it will return the existing or generate a new one and request trigger saving to IDPE | ||
| * @param columnGroupMap | ||
| * @param properties | ||
| * @returns an object shaped : {colorMappingForIDPE?, colorMappingForGiraffe?, needsToSaveToIDPE} | ||
| */ | ||
|
|
||
| export const getColorMappingObjects = ( | ||
| columnGroupMap, | ||
| properties: XYViewProperties | ||
| ) => { | ||
| const seriesToColorIndexMap = generateSeriesToColorIndexMap( | ||
| columnGroupMap, | ||
| properties | ||
| ) | ||
| let needsToSaveToIDPE = false | ||
|
|
||
| // if the mappings from the IDPE and the *required* one's for the current view are the same, we don't need to generate new mappings | ||
| if (areMappingsSame(properties.colorMapping, seriesToColorIndexMap)) { | ||
| const columnKeys = columnGroupMap.columnKeys | ||
| const mappings = {...columnGroupMap} | ||
|
|
||
| mappings.mappings.forEach(graphLine => { | ||
| const seriesID = getSeriesId(graphLine, columnKeys) | ||
|
|
||
| const colors = properties.colors | ||
|
|
||
| // this is needed for giraffe | ||
| graphLine.color = colors[properties.colorMapping[seriesID]].hex | ||
| }) | ||
|
|
||
| needsToSaveToIDPE = false | ||
ChitlangeSahas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| colorMappingForGiraffe: {columnKeys, ...mappings}, | ||
| needsToSaveToIDPE, | ||
| } | ||
| } else { | ||
| const columnKeys = columnGroupMap.columnKeys | ||
| const mappings = {...columnGroupMap} | ||
|
|
||
| mappings.mappings.forEach(graphLine => { | ||
| const seriesID = getSeriesId(graphLine, columnKeys) | ||
|
|
||
| const colors = properties.colors | ||
|
|
||
| // this is needed for giraffe | ||
| graphLine.color = colors[seriesToColorIndexMap[seriesID]].hex | ||
| }) | ||
|
|
||
| const newColorMappingForGiraffe = { | ||
| ...mappings, | ||
| seriesToColorIndexMap, | ||
| } | ||
| needsToSaveToIDPE = true | ||
ChitlangeSahas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| colorMappingForIDPE: seriesToColorIndexMap, | ||
| colorMappingForGiraffe: newColorMappingForGiraffe, | ||
| needsToSaveToIDPE, | ||
| } | ||
| } | ||
| } | ||
|
|
||
ChitlangeSahas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const getSeriesId = (graphLine, columnKeys) => { | ||
| let id = '' | ||
| for (const key of columnKeys) { | ||
| // ignore the '_start' and '_stop' columns when generating the ID. | ||
| if (key !== '_start' && key !== '_stop') { | ||
| id += graphLine[key] + '-' | ||
| } | ||
| } | ||
| return id | ||
| } | ||
|
|
||
| /** | ||
| * This function generates a map that maps the series ID to the color Index. | ||
| * @param columnGroupMap - generated using the createGroupIDColumn function | ||
| * @param properties - XYViewProperties | ||
| * @returns - a map that contains the series ID and it's color index (value bounded by the properties.colors array) | ||
| */ | ||
|
|
||
| export const generateSeriesToColorIndexMap = ( | ||
| columnGroupMap, | ||
| properties: XYViewProperties | ||
| ) => { | ||
| const seriesToColorIndexMap = {} | ||
| const cgMap = {...columnGroupMap} | ||
| cgMap.mappings.forEach((graphLine, colorIndex) => { | ||
| const id = getSeriesId(graphLine, columnGroupMap.columnKeys) | ||
| seriesToColorIndexMap[id] = colorIndex % properties.colors.length | ||
| }) | ||
|
|
||
| return {...seriesToColorIndexMap} | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this
SHAlives in a branch, maybe we should wait for this PR influxdata/openapi#248