Skip to content

Commit 15f9051

Browse files
chore: Refactor the slice filter for entity search
1 parent 8854972 commit 15f9051

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,13 +474,16 @@ const main = async () => {
474474
const smartscapeResult = await findMonitoredEntityViaSmartscapeByName(dtClient, entityNames);
475475

476476
if (smartscapeResult && smartscapeResult.records && smartscapeResult.records.length > 0) {
477-
let resp = `Found ${smartscapeResult.records.length} monitored entities via Smartscape! Displaying the first ${maxEntitiesToDisplay} entities:\n`;
477+
// Filter valid entities first, to ensure we display up to maxEntitiesToDisplay entities
478+
const validSmartscapeEntities = smartscapeResult.records.filter(
479+
(entity): entity is { id: string; type: string; name: string; [key: string]: any } =>
480+
!!(entity && entity.id && entity.type && entity.name),
481+
);
478482

479-
// iterate over dqlResponse and create a string with the problem details, but only show the top maxEntitiesToDisplay problems
480-
smartscapeResult.records.slice(0, maxEntitiesToDisplay).forEach((entity) => {
481-
if (entity && entity.id && entity.type && entity.name) {
482-
resp += `- Entity '${entity.name}' of entity-type '${entity.type}' has entity id '${entity.id}' and tags ${entity['tags'] ? JSON.stringify(entity['tags']) : 'none'} - DQL Filter: '| filter dt.smartscape.${String(entity.type).toLowerCase()} == "${entity.id}"'\n`;
483-
}
483+
let resp = `Found ${validSmartscapeEntities.length} monitored entities via Smartscape! Displaying the first ${Math.min(maxEntitiesToDisplay, validSmartscapeEntities.length)} valid entities:\n`;
484+
485+
validSmartscapeEntities.slice(0, maxEntitiesToDisplay).forEach((entity) => {
486+
resp += `- Entity '${entity.name}' of entity-type '${entity.type}' has entity id '${entity.id}' and tags ${entity['tags'] ? JSON.stringify(entity['tags']) : 'none'} - DQL Filter: '| filter dt.smartscape.${String(entity.type).toLowerCase()} == "${entity.id}"'\n`;
484487
});
485488

486489
// ToDo: Refine next-steps, this is not working properly yet.
@@ -499,10 +502,16 @@ const main = async () => {
499502
const result = await findMonitoredEntitiesByName(dtClient, entityNames, extendedSearch);
500503

501504
if (result && result.records && result.records.length > 0) {
502-
let resp = `Found ${result.records.length} monitored entities! Displaying the first ${maxEntitiesToDisplay} entities:\n`;
505+
// Filter valid entities first, to ensure we display up to maxEntitiesToDisplay entities
506+
const validClassicEntities = result.records.filter(
507+
(entity): entity is { id: string; type: string; name: string; [key: string]: any } =>
508+
!!(entity && entity.id && entity.type && entity.name),
509+
);
510+
511+
let resp = `Found ${validClassicEntities.length} monitored entities! Displaying the first ${Math.min(maxEntitiesToDisplay, validClassicEntities.length)} entities:\n`;
503512

504513
// iterate over dqlResponse and create a string with the problem details, but only show the top maxEntitiesToDisplay problems
505-
result.records.slice(0, maxEntitiesToDisplay).forEach((entity) => {
514+
validClassicEntities.slice(0, maxEntitiesToDisplay).forEach((entity) => {
506515
if (entity && entity.id) {
507516
const entityType = getEntityTypeFromId(String(entity.id));
508517
resp += `- Entity '${entity['entity.name']}' of entity-type '${entity['entity.type']}' has entity id '${entity.id}' and tags ${entity['tags'] ? entity['tags'] : 'none'} - DQL Filter: '| filter ${entityType} == "${entity.id}"'\n`;

0 commit comments

Comments
 (0)