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 eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineConfig([
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-this-alias': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-namespace': 'off',
},
},
]);
39 changes: 24 additions & 15 deletions src/alert/condition.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { GrafanaCondition } from '../grafana';
import type {
GrafanaCondition,
GrafanaEvaluatorType,
GrafanaOperatorType,
GrafanaReducerType,
} from '../grafana';

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

this._evaluator = {
Expand All @@ -32,7 +36,12 @@ class Condition {
this.state[opt] = opts[opt];
});
}
withEvaluator(value, type) {
withEvaluator(value: (string | number)[], type: 'within_range'): Condition;
withEvaluator(value: string | number, type: 'gt' | 'lt'): Condition;
withEvaluator(
value: (string | number) | (string | number)[],
type: GrafanaEvaluatorType
) {
const types = ['gt', 'lt', 'within_range'];

if (!types.includes(type)) {
Expand All @@ -42,15 +51,15 @@ class Condition {
this._evaluator.type = type;

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

return this;
}
withOperator(operator) {
const types = ['and', 'or'];
withOperator(operator: GrafanaOperatorType) {
const types = ['and', 'or'] as const;

if (!types.includes(operator)) {
throw Error(`Operator must be one of [${types.toString}]`);
Expand All @@ -67,7 +76,7 @@ class Condition {
this._operator.type = 'and';
return this;
}
onQuery(query, duration, from) {
onQuery(query: string, duration?: string, from?: string) {
if (typeof query !== 'string') {
throw Error(
'Query identifier must be a string. eg. "A" or "B", etc...'
Expand All @@ -79,7 +88,7 @@ class Condition {

return this;
}
withReducer(type) {
withReducer(type: GrafanaReducerType) {
const types = [
'min',
'max',
Expand All @@ -89,7 +98,7 @@ class Condition {
'last',
'median',
'diff',
];
] satisfies GrafanaReducerType[];

if (!types.includes(type)) {
throw Error(`Reducer has to be one of [${types.toString()}]`);
Expand All @@ -98,7 +107,7 @@ class Condition {
this._reducer.type = type;
return this;
}
generate() {
generate(): GrafanaCondition {
return Object.assign(
{},
{
Expand Down
16 changes: 10 additions & 6 deletions src/alert/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Alert = require('./alert');
import Condition = require('./condition');
import _Alert = require('./alert');
import _Condition = require('./condition');

export = {
Alert: Alert,
Condition: Condition,
};
namespace alert {
export type Alert = _Alert;
export const Alert = _Alert;
export type Condition = _Condition;
export const Condition = _Condition;
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an idiom in typescript that I'm familiar with. This works to export both the type and the var under the same name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit of a weird feature - as long as it does not conflict, TypeScript allows that
https://www.typescriptlang.org/docs/handbook/declaration-merging.html

In this case, the re-exported class is both a type (for instances of it) and a variable (a constructor function), and this is a workaround to have both (having just export const does not export the type)

}

export = alert;
11 changes: 7 additions & 4 deletions src/annotations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Graphite = require('./graphite');
import _Graphite = require('./graphite');

export = {
Graphite: Graphite,
};
namespace annotations {
export type Graphite = _Graphite;
export const Graphite = _Graphite;
}

export = annotations;
Loading