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
86 changes: 42 additions & 44 deletions src/alert/alert.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
function Alert(opts) {
opts = opts || {};
this.conditions = [];

this.state = {
name: 'Panel Title alert',
for: '15m',
frequency: '5m',
conditions: [],
message: '',
notifications: [],
executionErrorState: 'keep_state',
noDataState: 'keep_state',
alertRuleTags: {},
handler: 1,
};

this._init(opts);
this._initConditions(opts);
}
class Alert {
constructor(opts = {}) {
this.conditions = [];

this.state = {
name: 'Panel Title alert',
for: '15m',
frequency: '5m',
conditions: [],
message: '',
notifications: [],
executionErrorState: 'keep_state',
noDataState: 'keep_state',
alertRuleTags: {},
handler: 1,
};

this._init(opts);
this._initConditions(opts);
}

Alert.prototype._init = function _init(opts) {
const self = this;
_init(opts) {
Object.keys(opts).forEach((opt) => {
this.state[opt] = opts[opt];
});
}

Object.keys(opts).forEach(function eachOpt(opt) {
self.state[opt] = opts[opt];
});
};
_initConditions(opts) {
this.state.conditions = this.state.conditions || [];

Alert.prototype._initConditions = function _initConditions(opts) {
var self = this;
this.state.conditions = this.state.conditions || [];
if (opts.conditions) {
this.conditions = this.conditions.concat(opts.conditions);
}
}

if (opts.conditions) {
self.conditions = self.conditions.concat(opts.conditions);
addCondition(condition) {
this.conditions.push(condition);
return this;
}
};

Alert.prototype.addCondition = function addCondition(condition) {
this.conditions.push(condition);
return this;
};

Alert.prototype.generate = function generate() {
this.state.conditions = this.conditions.map((condition) =>
condition.generate()
);
return this.state;
};

generate() {
this.state.conditions = this.conditions.map((condition) =>
condition.generate()
);
return this.state;
}
}

module.exports = Alert;
114 changes: 48 additions & 66 deletions src/alert/condition.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,77 @@
function Condition(opts) {
opts = opts || {};

const that = this;
that.state = {};

this._evaluator = {
params: [],
type: 'gt',
};

this._operator = {
type: 'and',
};

this._query = {
params: ['A', '5m', 'now'],
};

this._reducer = {
params: [],
type: 'avg',
};

Object.keys(opts).forEach(function eachOpt(opt) {
that.state[opt] = opts[opt];
});

function withEvaluator(value, type) {
class Condition {
constructor(opts = {}) {
this.state = {};

this._evaluator = {
params: [],
type: 'gt',
};

this._operator = {
type: 'and',
};

this._query = {
params: ['A', '5m', 'now'],
};

this._reducer = {
params: [],
type: 'avg',
};

Object.keys(opts).forEach((opt) => {
this.state[opt] = opts[opt];
});
}
withEvaluator(value, type) {
const types = ['gt', 'lt', 'within_range'];

if (!types.includes(type)) {
throw Error(`Evaluator type must be one of [${types.toString()}]`);
}

that._evaluator.type = type;
this._evaluator.type = type;

if (['gt', 'lt'].includes(type)) {
that._evaluator.params = [value];
this._evaluator.params = [value];
} else if (Array.isArray(value)) {
that._evaluator.params = value;
this._evaluator.params = value;
}

return this;
}

function withOperator(operator) {
withOperator(operator) {
const types = ['and', 'or'];

if (!types.includes(operator)) {
throw Error(`Operator must be one of [${types.toString}]`);
}

that._operator.type = operator;
this._operator.type = operator;
return this;
}

function orCondition() {
that._operator.type = 'or';
orCondition() {
this._operator.type = 'or';
return this;
}

function andCondition() {
that._operator.type = 'and';
andCondition() {
this._operator.type = 'and';
return this;
}

function onQuery(query, duration, from) {
onQuery(query, duration, from) {
if (typeof query !== 'string') {
throw Error(
'Query identifier must be a string. eg. "A" or "B", etc...'
);
}
that._query.params[0] = query;
that._query.params[1] = duration;
that._query.params[2] = from;
this._query.params[0] = query;
this._query.params[1] = duration;
this._query.params[2] = from;

return this;
}

function withReducer(type) {
withReducer(type) {
const types = [
'min',
'max',
Expand All @@ -94,33 +87,22 @@ function Condition(opts) {
throw Error(`Reducer has to be one of [${types.toString()}]`);
}

that._reducer.type = type;
this._reducer.type = type;
return this;
}

function generate() {
generate() {
return Object.assign(
{},
{
evaluator: that._evaluator,
operator: that._operator,
query: that._query,
reducer: that._reducer,
evaluator: this._evaluator,
operator: this._operator,
query: this._query,
reducer: this._reducer,
type: 'query',
},
that.state
this.state
);
}

return {
withEvaluator,
withOperator,
withReducer,
orCondition,
andCondition,
onQuery,
generate,
};
}

module.exports = Condition;
79 changes: 39 additions & 40 deletions src/annotations/graphite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,49 @@

var errors = require('../errors');

function Graphite(opts) {
opts = opts || {};
var self = this;
class Graphite {
constructor(opts = {}) {
if (!opts.name) {
throw errors.UnfulfilledRequirement.create(
'{component} missing requirement: {unfulfilledArg}',
{
component: 'grafana.annotations.Graphite',
unfulfilledArg: 'name',
}
);
}

if (!opts.name) {
throw errors.UnfulfilledRequirement.create(
'{component} missing requirement: {unfulfilledArg}',
{
component: 'grafana.annotations.Graphite',
unfulfilledArg: 'name',
}
);
}
if (!opts.target) {
throw errors.UnfulfilledRequirement.create(
'{component} missing requirement: {unfulfilledArg}',
{
component: 'grafana.annotations.Graphite',
unfulfilledArg: 'target',
}
);
}

if (!opts.target) {
throw errors.UnfulfilledRequirement.create(
'{component} missing requirement: {unfulfilledArg}',
{
component: 'grafana.annotations.Graphite',
unfulfilledArg: 'target',
}
);
}
var defaults = {
name: 'no name',
datasource: 'graphite',
showLine: true,
iconColor: 'rgb(255, 234, 0)',
lineColor: 'rgba(165, 161, 70, 0.59)',
iconSize: 10,
enable: true,
target: '',
};
this.state = defaults;

var defaults = {
name: 'no name',
datasource: 'graphite',
showLine: true,
iconColor: 'rgb(255, 234, 0)',
lineColor: 'rgba(165, 161, 70, 0.59)',
iconSize: 10,
enable: true,
target: '',
};
self.state = defaults;
// Overwrite defaults with custom values
Object.keys(opts).forEach((opt) => {
this.state[opt] = opts[opt];
});
}

// Overwrite defaults with custom values
Object.keys(opts).forEach(function eachOpt(opt) {
self.state[opt] = opts[opt];
});
generate() {
return this.state;
}
}

Graphite.prototype.generate = function generate() {
return this.state;
};

module.exports = Graphite;
Loading