cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dashboard tip, combine DQL (with time selector) with a REST call in one Dashboard ***almost an app (-;***

henk_stobbe
DynaMight Leader
DynaMight Leader

Hello,

As an example I have created a Dashboard that retrieves all host names from Dynatrace, using the current time selector. And based on these results asks ServiceNow to return the  ID's based on the that name.

henk_stobbe_0-1738494355633.png

Maybe it can give someone some inspiration (-;

Source will be in the reply, you need to add ServiceNow url and token, I love comments and remarks,

KR Henk

1 REPLY 1

henk_stobbe
DynaMight Leader
DynaMight Leader

import { queryExecutionClient } from '@dynatrace-sdk/client-query';
export default async function () {
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function removeMilliseconds(isoString) {
const date = new Date(isoString);
return date.toISOString().split('.')[0] + 'Z';
}
const from = removeMilliseconds($dt_timeframe_from);
const to = removeMilliseconds($dt_timeframe_to);

// DQL fetching all hosts in Dt
const data = await queryExecutionClient.queryExecute({
body: {
query: `fetch dt.entity.host,timeframe: "${from}/${to}"`
}
});

let result;
// Wait for DQL to finish
while (true) {
result = await queryExecutionClient.queryPoll({
requestToken: data.requestToken
});

if (result.state !== "RUNNING") {
break;
}

// Wait for 1 second before polling again
await delay(1000);
}

// Check if result.result.records exists and is an array
if (result && result.result && Array.isArray(result.result.records)) {
// Extract entity names from the result
const entityNames = result.result.records.map(record => record['entity.name']);

// Create an array to hold the final results
const finalResults = [];
const requestOptions = {
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": "Basic Pleasaddyourowntokenhere"
}
};

// Iterate over each entity name and find the corresponding value in SNOW
for (const entityName of entityNames) {
try {
// Sending the request using fetch
const response = await fetch(`https://please.use.your.snow.url/api/now/table/cmdb_ci?sysparm_fields=u_number&sysparm_query=company...)}`, requestOptions);
const data = await response.json(); // Expecting JSON response

const values = data.result.map(item => item.u_number);

if (values.length === 0) {
finalResults.push({ DynatraceHost: entityName, ServiceNowID: 'no records found' });
} else if (values.length === 1) {
finalResults.push({ DynatraceHost: entityName, ServiceNowID: values[0] });
} else {
finalResults.push({ DynatraceHost: entityName, ServiceNowID: 'multiple recodrs found' });
}
} catch (error) {
console.error(`Error fetching data for ${entityName}:`, error);
finalResults.push({ DynatraceHost: entityName, ServiceNowID: 'error fetching data' });
}
}

return finalResults;
} else {
throw new Error('No records found in the result.');
}
}

Featured Posts