Skip to content

Commit

Permalink
chore: Cleanup classes and add jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
josephgregoryii committed Oct 9, 2024
1 parent 12bc1a5 commit 20eddc1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 45 deletions.
36 changes: 29 additions & 7 deletions utils/lib/Alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
AlertType,
QuickstartAlertInput,
} from '../types/QuickstartMutationVariable';
import type { ArtifactAlertConfig } from '../types/Artifact';
import type { QuickstartConfigAlert } from '../types/QuickstartConfig';
import type { NerdGraphResponseWithLocalErrors } from '../types/nerdgraph';

Expand Down Expand Up @@ -79,7 +80,11 @@ export type SubmitSetRequiredDataSourcesMutationResult =
| NerdGraphResponseWithLocalErrors<AlertPolicySetRequiredDataSourcesMutationResults>
| { errors: ErrorOrNerdGraphError[] };

class Alert extends Component<QuickstartConfigAlert[], QuickstartAlertInput[]> {
class Alert extends Component<
QuickstartConfigAlert[],
QuickstartAlertInput[],
ArtifactAlertConfig
> {
constructor(identifier: string, basePath?: string) {
super(identifier, basePath);
this.isValid = this.validate();
Expand Down Expand Up @@ -134,7 +139,18 @@ class Alert extends Component<QuickstartConfigAlert[], QuickstartAlertInput[]> {
}
}

public transformForArtifact() {
/**
* Method extracts criteria from the config and returns an object appropriately
* structured for the artifact.
*/
transformForArtifact() {
if (!this.isValid) {
console.error(
`Alert is invalid.\nPlease check if the path at ${this.identifier} exists.`
);
return {};
}

const alertPolicy = this.config.map((condition) => {
const { description, name, type } = condition;

Expand All @@ -147,7 +163,7 @@ class Alert extends Component<QuickstartConfigAlert[], QuickstartAlertInput[]> {
};
});

return { [this.identifier]: alertPolicy }
return { [this.identifier]: alertPolicy };
}

getMutationVariables() {
Expand Down Expand Up @@ -206,13 +222,17 @@ class Alert extends Component<QuickstartConfigAlert[], QuickstartAlertInput[]> {

return true;
} catch (error) {
logger.error(`Alert Condition: Validaiton for ${displayName} failed with an error`);
logger.error(
`Alert Condition: Validaiton for ${displayName} failed with an error`
);

return false;
}
});

logger.debug(`Alert condition: Finished validation for alert at ${this.identifier}`);
logger.debug(
`Alert condition: Finished validation for alert at ${this.identifier}`
);

return validations.every(Boolean);
}
Expand Down Expand Up @@ -315,8 +335,10 @@ class Alert extends Component<QuickstartConfigAlert[], QuickstartAlertInput[]> {
}

static getAll() {
const alertPaths = glob.sync(path.join(__dirname, '..', '..', 'alert-policies', '**', '*.+(yml|yaml)'));
return alertPaths.map(alertPath => {
const alertPaths = glob.sync(
path.join(__dirname, '..', '..', 'alert-policies', '**', '*.+(yml|yaml)')
);
return alertPaths.map((alertPath) => {
// The identifier for alerts is the folder and the file name
// e.g. `node-js/HighCpuUtilization.yml`
const identifier = path.join(...alertPath.split('/').slice(-2, -1));
Expand Down
3 changes: 2 additions & 1 deletion utils/lib/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs';
import * as yaml from 'js-yaml';

import { GITHUB_REPO_BASE_URL } from '../constants';
abstract class Component<ConfigType, MutationVariablesType> {
abstract class Component<ConfigType, MutationVariablesType, ArtifactType> {
public identifier: string; // Local path to the component. Ex: python/flask
public configPath: string; // Absolute path to the config file within the repository
public config: ConfigType;
Expand All @@ -23,6 +23,7 @@ abstract class Component<ConfigType, MutationVariablesType> {
abstract getConfigFilePath(): string;
abstract getConfigContent(): ConfigType;
abstract getMutationVariables(): MutationVariablesType;
abstract transformForArtifact(): ArtifactType;

get fullPath() {
return path.join(this.basePath, this.configPath);
Expand Down
27 changes: 22 additions & 5 deletions utils/lib/Dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
fetchNRGraphqlResults,
ErrorOrNerdGraphError,
} from './nr-graphql-helpers';
import { ArtifactDashboardConfig } from '../types/Artifact';

export interface DashboardConfig {
name: string;
Expand Down Expand Up @@ -61,7 +62,11 @@ export type SubmitSetRequiredDataSourcesMutationResult =
| NerdGraphResponseWithLocalErrors<DashboardSetRequiredDataSourcesMutationResults>
| { errors: ErrorOrNerdGraphError[] };

class Dashboard extends Component<DashboardConfig, QuickstartDashboardInput> {
class Dashboard extends Component<
DashboardConfig,
QuickstartDashboardInput,
ArtifactDashboardConfig
> {
/**
* @returns - filepath from top level directory.
*/
Expand Down Expand Up @@ -104,7 +109,18 @@ class Dashboard extends Component<DashboardConfig, QuickstartDashboardInput> {
}
}

public transformForArtifact() {
/**
* Method extracts criteria from the config and returns an object appropriately
* structured for the artifact.
*/
transformForArtifact() {
if (!this.isValid) {
console.error(
`Dashboard is invalid.\nPlease check the dashboard at ${this.identifier}\n`
);
return {};
}

const { name, description } = this.config;
const screenshotPaths = this.getScreenshotPaths();

Expand All @@ -115,9 +131,10 @@ class Dashboard extends Component<DashboardConfig, QuickstartDashboardInput> {
rawConfiguration: JSON.stringify(this.config),
sourceUrl: Component.getAssetSourceUrl(this.configPath),
screenshots:
screenshotPaths && screenshotPaths.map((s) => this.getScreenshotUrl(s)),
}
};
screenshotPaths &&
screenshotPaths.map((s) => this.getScreenshotUrl(s)),
},
};
}

/**
Expand Down
17 changes: 13 additions & 4 deletions utils/lib/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ import type {
DataSourceInstallDirectiveInput,
DataSourceMutationVariable,
} from '../types/DataSourceMutationVariable';
import { ArtifactInstall } from '../types/Artifact';
import { ArtifactDataSourceConfig, ArtifactInstall } from '../types/Artifact';

export interface DataSourceMutationResponse {
dataSource: {
id: string;
};
}

class DataSource extends Component<DataSourceConfig, string> {
class DataSource extends Component<
DataSourceConfig,
string,
ArtifactDataSourceConfig
> {
/**
* @returns Filepath for the configuration file (from top-level directory).
*/
Expand Down Expand Up @@ -74,7 +78,11 @@ class DataSource extends Component<DataSourceConfig, string> {
return this._getYamlConfigContent();
}

public transformForArtifact() {
/**
* Method extracts criteria from the config and returns an object appropriately
* structured for the artifact.
*/
transformForArtifact() {
const { keywords, description, categoryTerms, icon, ...rest } = this.config;

return {
Expand All @@ -93,7 +101,8 @@ class DataSource extends Component<DataSourceConfig, string> {
return {
primary: this._parseInstallDirectiveForArtifact(install.primary),
fallback:
install.fallback && this._parseInstallDirectiveForArtifact(install.fallback),
install.fallback &&
this._parseInstallDirectiveForArtifact(install.fallback),
};
}

Expand Down
2 changes: 1 addition & 1 deletion utils/lib/Quickstart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class Quickstart {
};
}

public transformForArtifact(): ArtifactQuickstartConfig {
transformForArtifact(): ArtifactQuickstartConfig {
const {
authors,
description,
Expand Down
65 changes: 38 additions & 27 deletions utils/types/Artifact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* -- Data source -- */
export interface ArtifactDataSourceConfig {
id: string;
displayName: string;
Expand All @@ -9,11 +10,13 @@ export interface ArtifactDataSourceConfig {
}

export type ArtifactInstallDirective = {
primary: ArtifactInstall,
fallback?: ArtifactInstall
}
primary: ArtifactInstall;
fallback?: ArtifactInstall;
};

export type ArtifactInstall = ArtifactDataSourceConfigNerdletDirective | ArtifactDataSourceConfigLinkDirective;
export type ArtifactInstall =
| ArtifactDataSourceConfigNerdletDirective
| ArtifactDataSourceConfigLinkDirective;

interface ArtifactDataSourceConfigNerdletDirective {
nerdletId: string;
Expand All @@ -31,31 +34,21 @@ interface ArtifactDataSourceConfigNerdletDirective {
requiresAccount: boolean;
}

type DashboardScreenshot = {
url: string;
}

export interface ArtifactDashboardConfig {
[id: string]: {
description?: string;
displayName: string;
rawConfiguration: string;
sourceUrl?: string;
screenshots?: DashboardScreenshot[];
}
}
/* -- Quickstart -- */

type QuickstartConfigDocumentation = {
displayName: string;
description: string;
url: string;
}
};

export type QuickstartConfigSupportLevel =
|'NEW_RELIC'
|'COMMUNITY'
|'VERIFIED'
|'ENTERPRISE';
| 'NEW_RELIC'
| 'COMMUNITY'
| 'VERIFIED'
// Enterprise is deprecated. However some quickstarts still have this support
// level within their config.
| 'ENTERPRISE';

type QuickstartConfig = {
quickstartUuid: string;
Expand All @@ -70,22 +63,40 @@ type QuickstartConfig = {
alertPolicies?: string[];
dashboards?: string[];
dataSourceIds?: string[];
}
};

export interface ArtifactQuickstartConfig extends QuickstartConfig {
authors: Array<{ name: string; }>
authors: Array<{ name: string }>;
}

/* -- Dashboard -- */

type DashboardScreenshot = {
url: string;
};

export interface ArtifactDashboardConfig {
[id: string]: {
description?: string;
displayName: string;
rawConfiguration: string;
sourceUrl?: string;
screenshots?: DashboardScreenshot[];
};
}

/* --- Alert --- */

type AlertType = 'BASELINE' | 'STATIC';

type ArtifactAlert = {
type ArtifactAlert = {
description?: string;
displayName: string;
rawConfiguration: string;
sourceUrl?: string;
type: AlertType;
}
};

export interface ArtifactAlertConfig {
[id: string]: ArtifactAlert[]
[id: string]: ArtifactAlert[];
}

0 comments on commit 20eddc1

Please sign in to comment.