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

feat: entity existence check #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 54 additions & 47 deletions nerdlets/service-containers/service-containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,56 +52,63 @@ export default class ServiceContainers extends React.Component {

let result = await EntityByGuidQuery.query({ entityGuid });
const entity = result.data.entities[0];
const nrql = `SELECT uniques(containerId) FROM Transaction
WHERE entityGuid = '${entityGuid}' ${timeRange}`;

// get the container id's that this app runs in
result = await nrdbQuery(entity.accountId, nrql);
const containerIds = result.map((r) => r.member);
this.setState({ containerIds });

// look up the infrastucture account(s) that are associated with this entity.
// cache for performance.
const storageQuery = {
collection: 'GLOBAL',
entityGuid,
documentId: 'infraAccountsData',
};
const storageResult = await EntityStorageQuery.query(storageQuery);

let infraAccounts = storageResult.data && storageResult.data.infraAccounts;

// find the account(s) that are monitoring these containers. Hopefully there's exactly
// one, but not, take the account with the most matches.
if (!infraAccounts && containerIds && containerIds.length > 0) {
const where = `containerId IN (${containerIds
.map((cid) => `'${cid}'`)
.join(',')})`;
const find = { eventType: 'ProcessSample', where };

infraAccounts = await findRelatedAccountsWith(find);

// cache in entity storage
storageQuery.document = { infraAccounts };
storageQuery.actionType =
EntityStorageMutation.ACTION_TYPE.WRITE_DOCUMENT;

await EntityStorageMutation.mutate(storageQuery);
}
if (entity) {
const nrql = `SELECT uniques(containerId) FROM Transaction
WHERE entityGuid = '${entityGuid}' ${timeRange}`;

if (!infraAccounts || infraAccounts.length === 0) {
const searchedAccounts = await accountsWithData('ProcessSample');
this.setState({ accountDataNotFound: true, searchedAccounts, entity });
// get the container id's that this app runs in
result = await nrdbQuery(entity.accountId, nrql);
const containerIds = result.map((r) => r.member);
this.setState({ containerIds });

// look up the infrastucture account(s) that are associated with this entity.
// cache for performance.
const storageQuery = {
collection: 'GLOBAL',
entityGuid,
documentId: 'infraAccountsData',
};
const storageResult = await EntityStorageQuery.query(storageQuery);

let infraAccounts =
storageResult.data && storageResult.data.infraAccounts;

// find the account(s) that are monitoring these containers. Hopefully there's exactly
// one, but not, take the account with the most matches.
if (!infraAccounts && containerIds && containerIds.length > 0) {
const where = `containerId IN (${containerIds
.map((cid) => `'${cid}'`)
.join(',')})`;
const find = { eventType: 'ProcessSample', where };

infraAccounts = await findRelatedAccountsWith(find);

// cache in entity storage
storageQuery.document = { infraAccounts };
storageQuery.actionType =
EntityStorageMutation.ACTION_TYPE.WRITE_DOCUMENT;

await EntityStorageMutation.mutate(storageQuery);
}

if (!infraAccounts || infraAccounts.length === 0) {
const searchedAccounts = await accountsWithData('ProcessSample');
this.setState({ accountDataNotFound: true, searchedAccounts, entity });
} else {
// use the infra account with the most hits as the primary for this entity, and then
// store the others otherInfraAccounts so we can show an info box.
this.setState({
infraAccount: infraAccounts[0],
allInfraAccounts: infraAccounts,
containerIds,
entity,
timeRange,
});
}
} else {
// use the infra account with the most hits as the primary for this entity, and then
// store the others otherInfraAccounts so we can show an info box.
this.setState({
infraAccount: infraAccounts[0],
allInfraAccounts: infraAccounts,
containerIds,
entity,
timeRange,
});
console.log('entity data not found => ', result);
this.setState({ accountDataNotFound: true, entity });
}
}

Expand Down
2 changes: 1 addition & 1 deletion nr1.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaType": "NERDPACK",
"id": "5d870b85-07a5-4213-9bec-f9351fb9c08c",
"id": "3c4445bb-9ea4-4c72-b22f-6769c1c6cf87",
"displayName": "Container Explorer",
"description": "Global visibility of the containers across your infrastructure, easily segmented by tags."
}
Loading