Skip to content
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ jobs:
- run: npm run build --if-present
- run: npm test
- run: npm run lint
- run: npm run typecheck
21 changes: 15 additions & 6 deletions src/alert/alert.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import type { GrafanaAlert } from '../grafana';
import type Condition from './condition';

type AlertOptions = Partial<
Omit<GrafanaAlert, 'conditions'> & {
conditions: Condition[];
}
>;

class Alert {
private conditions: any[];
private conditions: Condition[];

private state: any;
private state: GrafanaAlert;

constructor(opts: any = {}) {
constructor(opts: AlertOptions = {}) {
this.conditions = [];

this.state = {
Expand All @@ -23,21 +32,21 @@ class Alert {
this._initConditions(opts);
}

_init(opts) {
_init(opts: AlertOptions) {
Object.keys(opts).forEach((opt) => {
this.state[opt] = opts[opt];
});
}

_initConditions(opts) {
_initConditions(opts: AlertOptions) {
this.state.conditions = this.state.conditions || [];

if (opts.conditions) {
this.conditions = this.conditions.concat(opts.conditions);
}
}

addCondition(condition) {
addCondition(condition: Condition) {
this.conditions.push(condition);
return this;
}
Expand Down
9 changes: 6 additions & 3 deletions src/alert/condition.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { GrafanaCondition } from '../grafana';

class Condition {
private state: object;
private state: GrafanaCondition;
private _evaluator: { params: any[]; type: string };
private _operator: { type: string };
private _query: { params: any[] };
private _query: { params: string[] };
private _reducer: { params: any[]; type: string };
constructor(opts = {}) {
constructor(opts: Partial<GrafanaCondition> = {}) {
// @ts-expect-error todo: should fields be optional?
this.state = {};

this._evaluator = {
Expand Down
7 changes: 4 additions & 3 deletions src/annotations/graphite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
// THE SOFTWARE.

import errors = require('../errors');
import type { GrafanaGraphiteAnnotation } from '../grafana';

class Graphite {
private state: any;
constructor(opts: any = {}) {
state: GrafanaGraphiteAnnotation;
constructor(opts: Partial<GrafanaGraphiteAnnotation> = {}) {
if (!opts.name) {
throw errors.UnfulfilledRequirement.create(
'{component} missing requirement: {unfulfilledArg}',
Expand All @@ -43,7 +44,7 @@ class Graphite {
);
}

const defaults = {
const defaults: GrafanaGraphiteAnnotation = {
name: 'no name',
datasource: 'graphite',
showLine: true,
Expand Down
14 changes: 11 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ import xtend = require('xtend');
* `cookie` - Key/value pair for auth, defaults to"auth-openid="
* `headers` - Map of header keys/values, defaults to no headers
*/
let configurations = {
let configurations: Config = {
user: 'guest',
group: 'guest',
url: 'https://your.graphite.url.com/elasticsearch/grafana-dash/dashboard/',
cookie: 'auth-openid=',
headers: {},
};

function configure(opts) {
type Config = {
user: string;
group: string;
url: string;
cookie: string;
headers: Record<string, string>;
};

function configure(opts: Partial<Config> = {}) {
configurations = xtend(configurations, opts);
}

function getConfig() {
function getConfig(): Config {
return configurations;
}

Expand Down
48 changes: 32 additions & 16 deletions src/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@
import Templates = require('./templates');
import Annotations = require('./annotations');
import ExternalLink = require('./external-link');
import type {
GrafanaCustomTemplate,
GrafanaDashboard,
GrafanaGraphiteAnnotation,
} from './grafana';
import type Row from './row';
import type Graphite from './annotations/graphite';
import type Query from './templates/query';
import type Custom from './templates/custom';

type DashboardOptions = Partial<
Omit<GrafanaDashboard, 'rows' | 'templating' | 'annotations'> & {
rows: Row[];
templating: GrafanaCustomTemplate[];
annotations: GrafanaGraphiteAnnotation[];
}
>;

class Dashboard {
state: any;
rows: any[];
links: any[];
constructor(opts = {}) {
state: GrafanaDashboard;
rows: Row[];
links: ExternalLink[];
constructor(opts: DashboardOptions = {}) {
// @ts-expect-error initialized in init calls below
this.state = {};
this._init(opts);
this._initRows(opts);
Expand All @@ -35,7 +53,7 @@ class Dashboard {
this._initAnnotations(opts);
}

_init(opts) {
_init(opts: DashboardOptions) {
this.state.id = opts.id || null;
this.state.title = opts.title || 'Generated Grafana Dashboard';
this.state.originalTitle = opts.originalTitle || 'Generated Dashboard';
Expand All @@ -54,7 +72,7 @@ class Dashboard {
}
}

_initRows(opts) {
_initRows(opts: DashboardOptions) {
this.rows = [];
this.state.rows = [];

Expand All @@ -65,48 +83,46 @@ class Dashboard {
}
}

_initLinks(opts) {
_initLinks(opts: DashboardOptions) {
this.links = opts.links || [];
this.state.links = [];
}

_initTemplating(opts) {
_initTemplating(opts: DashboardOptions) {
this.state.templating = {
list: [],
enable: true,
};

if (opts.templating) {
opts.templating.forEach((template) => {
template = new Templates.Custom(template);
this.addTemplate(template);
this.addTemplate(new Templates.Custom(template));
});
}
}

_initAnnotations(opts) {
_initAnnotations(opts: DashboardOptions) {
this.state.annotations = {
list: [],
enable: true,
};

if (opts.annotations) {
opts.annotations.forEach((annotation) => {
annotation = new Annotations.Graphite(annotation);
this.addAnnotation(annotation);
this.addAnnotation(new Annotations.Graphite(annotation));
});
}
}

addRow(row) {
addRow(row: Row) {
this.rows.push(row);
}

addTemplate(template) {
addTemplate(template: Custom | Query) {
this.state.templating.list.push(template.generate());
}

addAnnotation(annotation) {
addAnnotation(annotation: Graphite) {
this.state.annotations.list.push(annotation.generate());
}

Expand Down
10 changes: 6 additions & 4 deletions src/external-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import type { GrafanaExternalLink } from './grafana';

class ExternalLink {
private state: any;
constructor(opts = {}) {
const defaults = {
private state: GrafanaExternalLink;
constructor(opts: Partial<GrafanaExternalLink> = {}) {
const defaults: GrafanaExternalLink = {
title: '',
tooltip: '',
url: '',
Expand Down Expand Up @@ -56,7 +58,7 @@ class ExternalLink {
return this;
}

withIcon(iconName) {
withIcon(iconName: string) {
this.state.icon = iconName;
return this;
}
Expand Down
Loading