Skip to content
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

Variables: Fix assetId field variable replacement #172

Conversation

idastambuk
Copy link
Contributor

What this PR does / why we need it:
When selecting variables for assetIds field, if a variable is a string, the JSON.parse() fails, so the variable is added with the value of its ID instead of being correctly interpolated. This fixes that and adds some tests for assetIds.

Which issue(s) this PR fixes:

Fixes #158

Special notes for your reviewer:

This can be reproduced by following the steps from the original PR and with Sitewise from datasource provisioning. The assetId field in the query object in the network tab should contain all the values defined for the variables.

Screenshot 2023-03-22 at 3 16 03 PM

Screenshot 2023-03-22 at 3 14 35 PM

This also fixes the child not appearing when a parent is selected in the List associated assets.
Screenshot 2023-03-22 at 3 19 42 PM

Note: There's another variable interpolation issue which comes from core grafana, the fix for which will be released in 9.5

@idastambuk idastambuk requested a review from a team as a code owner March 22, 2023 14:23
@idastambuk idastambuk requested review from fridgepoet and sarahzinger and removed request for a team March 22, 2023 14:23
@github-actions
Copy link

Backend code coverage report for PR #172
No changes

@github-actions
Copy link

github-actions bot commented Mar 22, 2023

Frontend code coverage report for PR #172

Plugin Main PR Difference
src 1.82% 6.97% 5.15%

@github-actions
Copy link

github-actions bot commented Mar 22, 2023

Levitate is-compatible report:

🔍 Resolving @grafana/data@latest...
🔍 Resolving @grafana/ui@latest...
🔍 Resolving @grafana/runtime@latest...
🔍 Resolving @grafana/e2e-selectors@latest...

🔬 Checking compatibility between ./src/module.ts and @grafana/data@9.4.7...
✔ Found @grafana/data version 9.2.5 locally

🔬 Checking compatibility between ./src/module.ts and @grafana/ui@9.4.7...
✔ Found @grafana/ui version 9.2.5 locally

🔬 Checking compatibility between ./src/module.ts and @grafana/runtime@9.4.7...
✔ Found @grafana/runtime version 9.2.5 locally

🔬 Checking compatibility between ./src/module.ts and @grafana/e2e-selectors@9.4.7...
✔ Found @grafana/e2e-selectors version 9.2.5 locally

✔️ ./src/module.ts appears to be compatible with @grafana/data,@grafana/ui,@grafana/runtime,@grafana/e2e-selectors

Copy link
Member

@sarahzinger sarahzinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff! Couple of ideas/questions, let me know whatcha think!

})
: null;
// Sitewise doesn't support adhoc vars so this should be fine
if (variableValue && variableValue.type !== 'adhoc') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's an adhoc var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ad hoc vars are key-value pairs that can be added on the go, directly from the panel view. Im not entirely sure how one gets values from them, but Sitewise doesn't support them (at least that's what it was when I try to add one), so we don't need to account for them anyway here.
This specific if block is mostly a type guard, as getVariables can return adHoc variable type too, and those objects don't have a value.current that we can get the value from.

@@ -143,7 +149,7 @@ export class DataSource extends DataSourceWithBackend<SitewiseQuery, SitewiseOpt
propertyAlias: templateSrv.replace(query.propertyAlias, scopedVars),
region: templateSrv.replace(query.region || '', scopedVars),
propertyId: templateSrv.replace(query.propertyId || '', scopedVars),
assetIds,
assetIds: [...new Set(assetIds)],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to convert the array into a set and then back to an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I didn't do this, but when I was testing I realized if I had two variables that contained the same id, they would get repeated in the assetId array, so doing this removes the duplicate values. Duplicates are probably fine and it won't break anything, but just in case, maybe?

getTemplateSrv: () => {
return {
getVariableName(variableId: string) {
return variableId.slice(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I don't follow this slice, is it just generating a string? Would it work the same as if we returned the variableId?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Youre totally right, this was initially gonna be a more complicated test so this can definitely be simplified now! Just pushed

Copy link
Contributor

@kevinwcyu kevinwcyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@sarahzinger sarahzinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@idastambuk idastambuk merged commit a7ea122 into main Mar 27, 2023
5 checks passed
@idastambuk idastambuk deleted the 158-cannot-select-defined-hierarchies-when-using-list-associated-assets-query-type-with-another-variable branch March 27, 2023 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot select defined hierarchies when using "List associated assets" query type with another variable
3 participants