Skip to content

Commit

Permalink
Fixed issues with element segments
Browse files Browse the repository at this point in the history
  • Loading branch information
coderReview committed Jun 28, 2022
1 parent fcd8d28 commit 48f2580
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
}
],
"screenshots": [],
"version": "2.0.1",
"updated": "2022-06-24"
"version": "2.1.0",
"updated": "2022-06-28"
},
"dependencies": {
"grafanaDependency": ">=8.4.0",
Expand Down
54 changes: 34 additions & 20 deletions src/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,15 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
};

// get a ui segment for the attributes
getElementSegments = (index: number): Promise<Array<SelectableValue<PIWebAPISelectableValue>>> => {
const { datasource, query } = this.props;
getElementSegments = (
index: number,
currentSegment?: Array<SelectableValue<PIWebAPISelectableValue>>
): Promise<Array<SelectableValue<PIWebAPISelectableValue>>> => {
const { datasource, query, data } = this.props;
var ctrl = this;
var findQuery = query.isPiPoint
? { type: 'dataserver' }
: { path: this.getSegmentPathUpTo(this.state.segments.slice(0), index) };
: { path: this.getSegmentPathUpTo(currentSegment ?? this.state.segments.slice(0), index) };

if (!query.isPiPoint) {
if (datasource.afserver?.name && index === 0) {
Expand Down Expand Up @@ -362,7 +365,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
// }
}
return datasource
.metricFindQuery(findQuery, { isPiPoint: query.isPiPoint })
.metricFindQuery(findQuery, Object.assign(data?.request?.scopedVars ?? {}, { isPiPoint: query.isPiPoint }))
.then((items: any[]) => {
var altSegments = map(items, (item: any) => {
let selectableValue: SelectableValue<PIWebAPISelectableValue> = {
Expand Down Expand Up @@ -411,7 +414,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {

// get the list of attributes for the user interface - PI
getAttributeSegmentsPI = (attributeText?: string): Promise<Array<SelectableValue<PIWebAPISelectableValue>>> => {
const { datasource, query } = this.props;
const { datasource, query, data } = this.props;
const ctrl = this;
const findQuery = {
path: '',
Expand All @@ -421,7 +424,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
};
let segments: Array<SelectableValue<PIWebAPISelectableValue>> = [];
return datasource
.metricFindQuery(findQuery, { isPiPoint: query.isPiPoint })
.metricFindQuery(findQuery, Object.assign(data?.request?.scopedVars ?? {}, { isPiPoint: query.isPiPoint }))
.then((items: any[]) => {
segments = map(items, (item: any) => {
let selectableValue: SelectableValue<PIWebAPISelectableValue> = {
Expand Down Expand Up @@ -494,7 +497,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
// remove element hierarchy from attribute collection
splitAttributes.splice(0, 1);

each(splitElements, function (item, index) {
each(splitElements, (item, _) => {
segmentsArray.push({
label: item,
value: {
Expand All @@ -503,7 +506,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
},
});
});
each(splitAttributes, function (item, index) {
each(splitAttributes, (item, _) => {
if (item !== '') {
// set current value
attributesArray.push({
Expand All @@ -515,7 +518,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
});
}
});
return this.getElementSegments(segmentsArray.length + 1).then((elements) => {
return this.getElementSegments(splitElements.length + 1, segmentsArray).then((elements) => {
if (elements.length > 0) {
segmentsArray.push({
label: 'Select Element',
Expand Down Expand Up @@ -567,14 +570,14 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
attributes: Array<SelectableValue<PIWebAPISelectableValue>>,
segments: Array<SelectableValue<PIWebAPISelectableValue>>
): Promise<any> {
const { datasource } = this.props;
const { datasource, data } = this.props;
var ctrl = this;
var findQuery = {
path: this.getSegmentPathUpTo(segments.slice(0), segments.length),
type: 'attributes',
};
return datasource
.metricFindQuery(findQuery, { isPiPoint: false })
.metricFindQuery(findQuery, Object.assign(data?.request?.scopedVars ?? {}, { isPiPoint: false }))
.then((attributesResponse: any) => {
var validAttributes: any = {};

Expand Down Expand Up @@ -607,7 +610,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
attribute: SelectableValue<PIWebAPISelectableValue>,
attributes: Array<SelectableValue<PIWebAPISelectableValue>>
) {
const { datasource } = this.props;
const { datasource, data } = this.props;
var ctrl = this;
var findQuery = {
path: attribute.path,
Expand All @@ -616,7 +619,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
type: 'pipoint',
};
return datasource
.metricFindQuery(findQuery, { isPiPoint: true })
.metricFindQuery(findQuery, Object.assign(data?.request?.scopedVars ?? {}, { isPiPoint: true }))
.then(() => {
ctrl.attributeChangeValue(attributes);
})
Expand Down Expand Up @@ -673,7 +676,7 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
},
});
});
this.getElementSegments(segments.length + 1).then((elements) => {
this.getElementSegments(splitElements.length + 1, segments).then((elements) => {
if (elements.length > 0) {
segments.push({
label: 'Select Element',
Expand Down Expand Up @@ -764,9 +767,9 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
* @memberOf PIWebAPIQueryEditor
*/
updateArray = (
segmentsArray: any[],
attributesArray: any[],
summariesArray: any[],
segmentsArray: Array<SelectableValue<PIWebAPISelectableValue>>,
attributesArray: Array<SelectableValue<PIWebAPISelectableValue>>,
summariesArray: Array<SelectableValue<PIWebAPISelectableValue>>,
isPiPoint: boolean,
cb?: (() => void) | undefined
) => {
Expand All @@ -786,14 +789,25 @@ export class PIWebAPIQueryEditor extends PureComponent<Props, State> {
);
};

// React action when component is mounted
// React action when component is initialized/updated
scopedVarsDone = false;
componentDidMount = () => {
this.initialLoad(false);
};
componentDidUpdate = () => {
console.log(this.props.data);
if (this.props.data?.state === 'Done' && !!this.props.data?.request?.scopedVars && !this.scopedVarsDone) {
this.scopedVarsDone = true;
this.initialLoad(true);
}
};
initialLoad = (force: boolean) => {
const { query } = this.props;
const metricsQuery = defaults(query, defaultQuery) as PIWebAPIQuery;
const { segments, attributes, summary, isPiPoint } = metricsQuery;

let segmentsArray: Array<SelectableValue<PIWebAPISelectableValue>> = segments?.slice(0) ?? [];
let attributesArray: Array<SelectableValue<PIWebAPISelectableValue>> = attributes?.slice(0) ?? [];
let segmentsArray: Array<SelectableValue<PIWebAPISelectableValue>> = force ? [] : segments?.slice(0) ?? [];
let attributesArray: Array<SelectableValue<PIWebAPISelectableValue>> = force ? [] : attributes?.slice(0) ?? [];
let summariesArray = summary?.types ?? [];

if (!isPiPoint && segmentsArray.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,14 @@ export class PiWebAPIDatasource extends DataSourceApi<PIWebAPIQuery, PIWebAPIDat
query = JSON.parse(query as string);
}
if (queryOptions.isPiPoint) {
query.path = this.templateSrv.replace(query.path);
query.path = this.templateSrv.replace(query.path, queryOptions);
} else {
if (query.path === '') {
query.type = querydepth[0];
} else if (query.type !== 'attributes') {
query.type = querydepth[Math.max(0, Math.min(query.path.split('\\').length, querydepth.length - 1))];
}
query.path = this.templateSrv.replace(query.path);
query.path = this.templateSrv.replace(query.path, queryOptions);
query.path = query.path.replace(/\{([^\\])*\}/gi, (r: string) => r.substring(1, r.length - 2).split(',')[0]);
}

Expand Down

0 comments on commit 48f2580

Please sign in to comment.