newrelic / nr1-pathpoint

Pathpoint is an enterprise platform tracker that models system health in relation to actual user-impacting business stages.
Apache License 2.0
8 stars 19 forks source link

Error updating Synthetic Flame Script #77

Closed rsamanez closed 5 months ago

rsamanez commented 10 months ago

Description

It was found that when the client has a large number of synthetic scripts the algorithm fails because it only performs a search on the first 50 elements.

Steps to Reproduce

Reproducing the bug requires a customer with more than 50 Synthetic Scripts in their account. The search algorithm fails, and instead of updating the existing script, it tries to create a new one with the same name, which is why the creation attempt fails.

Solution

file to fix: /nerdlets/pathpoint-nerdlet/services/SynConnector.js

add in the header

import { AccountStorageQuery, AccountStorageMutation } from 'nr1';

Add the next function

SetFlameScript(scriptID, scriptName) {
    try {
      AccountStorageMutation.mutate({
        accountId: this.accountId,
        actionType: AccountStorageMutation.ACTION_TYPE.WRITE_DOCUMENT,
        collection: 'pathpoint',
        documentId: 'flameScript',
        document: {
          flameScriptId: scriptID,
          flameScriptName: scriptName
        }
      });
    } catch (error) {
      /* istanbul ignore next */
      throw new Error(error);
    }
  }

Update this function ExistFlameScript()

async ExistFlameScript() {
    let searchResult = false;
    let monitorID = '';
    let error = null;
    try {
      if (this.flameScriptId === null) {
        const { data } = await AccountStorageQuery.query({
          accountId: this.accountId,
          collection: 'pathpoint',
          documentId: 'flameScript'
        });
        if (data) {
          this.flameScriptId = data.flameScriptId;
          this.flameScriptName = data.flameScriptName;
        }
      }
      // const name = `Pathpoint-${this.pathpoint_id} Flame Script`;
      if (this.flameScriptId) {
        // validate if monitor already exist
        const response = await this.axiosInstance.get(
          `${this.uriSyntetic}/${this.flameScriptId}`,
          {
            headers: {
              contentType: 'application/json',
              'Api-Key': this.userApiKey
            }
          }
        );
        if (
          response &&
          response.status &&
          response.status === 200 &&
          response.data &&
          response.data.name
        ) {
          monitorID = this.flameScriptId;
          searchResult = true;
        } else {
          error = true;
        }
      }
    } catch (error) {
      throw new Error(error);
    }
    return { searchResult, monitorID, error };
  }

Add the. SetFlameScript function to CreateFlameScript()

async CreateFlameScript() {
...
...
        const monitorID = arrayLocation[1];
  --> this.SetFlameScript(monitorID, FlameMonitor.name);
        return monitorID;