Skip to content

Commit

Permalink
fix(asset): data link creation on deployed envs (#108)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Aradei <sorin.robert.aradei@ni.com>
  • Loading branch information
saradei-ni and Robert Aradei authored Oct 29, 2024
1 parent c33f477 commit 11125b3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,7 @@ describe('Time based data links', () => {
const from = new Date('2022-01-01T00:00:00.0000000Z');

expect(dataLink.title).toBe(`View ${AssetCalibrationForecastKey.Day}`);
expect(dataLink.targetBlank).toBe(true);
expect(dataLink.url).toContain('/d/${__dashboard.uid}/${__dashboard}?orgId=${__org.id}');
expect(dataLink.targetBlank).toBe(false);

const builtUrl = dataLink.onBuildUrl({
replaceVariables: (value: string) => value.replace('${__data.fields.Day}', from.toISOString())
Expand All @@ -655,8 +654,7 @@ describe('Time based data links', () => {
const weekEndDate = new Date('2022-01-09T23:59:59.999Z');

expect(dataLink.title).toBe(`View ${AssetCalibrationForecastKey.Week}`);
expect(dataLink.targetBlank).toBe(true);
expect(dataLink.url).toContain('/d/${__dashboard.uid}/${__dashboard}?orgId=${__org.id}');
expect(dataLink.targetBlank).toBe(false);

const builtUrl = dataLink.onBuildUrl({
replaceVariables: (value: string) => value.replace('${__data.fields.Week}', `${weekStartDate.toISOString()} : ${weekEndDate.toISOString()}`)
Expand All @@ -678,8 +676,7 @@ describe('Time based data links', () => {
const monthDate = new Date('2022-01-01T00:00:00.0000000Z');

expect(dataLink.title).toBe(`View ${AssetCalibrationForecastKey.Month}`);
expect(dataLink.targetBlank).toBe(true);
expect(dataLink.url).toContain('/d/${__dashboard.uid}/${__dashboard}?orgId=${__org.id}');
expect(dataLink.targetBlank).toBe(false);

const builtUrl = dataLink.onBuildUrl({
replaceVariables: (value: string) => value.replace('${__data.fields.Month}', monthDate.toISOString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataQueryRequest, DataFrameDTO, DataSourceInstanceSettings, FieldDTO, TestDataSourceResponse, DataLink } from '@grafana/data';
import { AssetDataSourceOptions, AssetQuery, AssetQueryType, AssetType, AssetTypeOptions, BusType, BusTypeOptions } from '../../types/types';
import { BackendSrv, getBackendSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
import { BackendSrv, config, getBackendSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
import { AssetDataSourceBase } from '../AssetDataSourceBase';
import { AssetCalibrationForecastKey, AssetCalibrationTimeBasedGroupByType, CalibrationForecastQuery, CalibrationForecastResponse, ColumnDescriptorType, FieldDTOWithDescriptor } from '../../types/CalibrationForecastQuery.types';
import { transformComputedFieldsQuery } from '../../../../core/query-builder.utils';
Expand Down Expand Up @@ -102,9 +102,6 @@ export class CalibrationForecastDataSource extends AssetDataSourceBase {
}

processResultsGroupedByTime(result: DataFrameDTO, timeGrouping: AssetCalibrationForecastKey) {
const timeFieldFirstValue = result.fields.find(field => field.name === timeGrouping)?.values?.at(0);
const startDate = this.forecastDateFormatterMap.get(timeGrouping)!(timeFieldFirstValue);

result.fields.forEach(field => {
field.name = this.createColumnNameFromDescriptor(field as FieldDTOWithDescriptor);
const formatter = this.forecastDateFormatterMap.get(field.name as AssetCalibrationForecastKey);
Expand All @@ -114,17 +111,17 @@ export class CalibrationForecastDataSource extends AssetDataSourceBase {
}

if (!formatter) {
field.config = { links: this.createDataLinks(timeGrouping, startDate) };
field.config = { links: this.createDataLinks(timeGrouping) };
}
});
}

private createDataLinks(timeGrouping: AssetCalibrationForecastKey, startDate: string): DataLink[] {
const url = window.location.href.split('/d/')[0] + '/d/${__dashboard.uid}/${__dashboard}?orgId=${__org.id}&${__all_variables}';
private createDataLinks(timeGrouping: AssetCalibrationForecastKey): DataLink[] {
const url = config.appUrl + 'd/${__dashboard.uid}/${__dashboard}?orgId=${__org.id}&${__all_variables}';

return [
{
title: `View ${timeGrouping}`, targetBlank: true, url: `${url}`,
title: `View ${timeGrouping}`, targetBlank: false, url: `${url}`,
onBuildUrl: function (options) {
if (!options.replaceVariables) {
return url;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Time based data links creates data links for Day grouping 1`] = `"http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}&from=1640995200000&to=1641081600000"`;
exports[`Time based data links creates data links for Day grouping 1`] = `"d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}&from=1640995200000&to=1641081600000"`;

exports[`Time based data links creates data links for Month grouping 1`] = `"http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}&from=1641081600000&to=1643760000000"`;
exports[`Time based data links creates data links for Month grouping 1`] = `"d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}&from=1641081600000&to=1643760000000"`;

exports[`Time based data links creates data links for Week grouping 1`] = `"http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}&from=1641168000000&to=1641689999999"`;
exports[`Time based data links creates data links for Week grouping 1`] = `"d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}&from=1641168000000&to=1641689999999"`;

exports[`queries asset calibration forecast with day groupBy 1`] = `
[
Expand Down Expand Up @@ -35,9 +35,9 @@ exports[`queries asset calibration forecast with day groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Day",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -83,9 +83,9 @@ exports[`queries asset calibration forecast with month groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -131,9 +131,9 @@ exports[`queries asset calibration forecast with week groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Week",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -351,9 +351,9 @@ exports[`queries calibration forecast with month and assetType groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -375,9 +375,9 @@ exports[`queries calibration forecast with month and assetType groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -399,9 +399,9 @@ exports[`queries calibration forecast with month and assetType groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -447,9 +447,9 @@ exports[`queries calibration forecast with month and busType groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -471,9 +471,9 @@ exports[`queries calibration forecast with month and busType groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -495,9 +495,9 @@ exports[`queries calibration forecast with month and busType groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -543,9 +543,9 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -567,9 +567,9 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -591,9 +591,9 @@ exports[`queries calibration forecast with month and location groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -639,9 +639,9 @@ exports[`queries calibration forecast with month and vendor groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -663,9 +663,9 @@ exports[`queries calibration forecast with month and vendor groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -687,9 +687,9 @@ exports[`queries calibration forecast with month and vendor groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down Expand Up @@ -735,9 +735,9 @@ exports[`queries calibration forecast with month and workspace groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -759,9 +759,9 @@ exports[`queries calibration forecast with month and workspace groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand All @@ -783,9 +783,9 @@ exports[`queries calibration forecast with month and workspace groupBy 1`] = `
"links": [
{
"onBuildUrl": [Function],
"targetBlank": true,
"targetBlank": false,
"title": "View Month",
"url": "http://localhost//d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
"url": "d/\${__dashboard.uid}/\${__dashboard}?orgId=\${__org.id}&\${__all_variables}",
},
],
},
Expand Down

0 comments on commit 11125b3

Please sign in to comment.