Skip to content

Commit

Permalink
✨ added support for named ranges + upgraded deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohland committed Nov 1, 2023
1 parent 6a9f45d commit 2c4dd4d
Show file tree
Hide file tree
Showing 7 changed files with 1,489 additions and 1,285 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ The trigger.**rule** object has the following additional properties that can be
- *days* - the days of the week to apply the rule to (example: [mon, tue, wed, thu, fri]) - defaults to every day
- *time* - the time of day to apply the rule to (example: 00:00-06:00) - defaults to all hours of the day

Example period formats

- `-10m to -5m` is 10 minutes ago to 5 minutes ago
- `-1h to 0h` is hour ago to now
- `-1d to -1h` is 24 hours ago to 1 hour ago
- `today` is from 00:00AM until now
- `yesterday` is from 00:00AM yesterday until 00:00AM today

##### MySQL Configuration

The example below will execute the given mysql query, and iterate over the result set.
Expand Down
2,641 changes: 1,377 additions & 1,264 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "barky",
"version": "1.0.46",
"version": "1.0.47",
"description": "A simple cloud services watchdog with digest notification support & no external dependencies",
"homepage": "https://github.com/Rohland/barky#readme",
"main": "dist/cli.js",
Expand All @@ -16,26 +16,26 @@
"author": "",
"license": "MIT",
"dependencies": {
"axios": "^1.4.0",
"axios": "^1.6.0",
"dotenv": "^16.3.1",
"knex": "^2.5.1",
"mysql2": "^3.6.0",
"knex": "^3.0.1",
"mysql2": "^3.6.2",
"sqlite3": "^5.1.6",
"tslib": "^2.6.2",
"yaml": "^2.3.1",
"yaml": "^2.3.3",
"yargs": "^17.7.2"
},
"devDependencies": {
"@babel/core": "^7.22.10",
"@babel/preset-env": "^7.22.10",
"@types/jest": "^29.5.3",
"@types/node": "^20.5.0",
"babel-jest": "^29.6.2",
"jest": "^29.6.2",
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@types/jest": "^29.5.7",
"@types/node": "^20.8.10",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"jest-mock-console": "^2.0.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
},
"bin": {
"barky": "./dist/cli.js"
Expand Down
4 changes: 2 additions & 2 deletions src/evaluators/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export abstract class BaseEvaluator {
private _skippedApps: IUniqueKey[] = [];

constructor(config: any) {
this._globalConfig = config;
this._globalConfig = config || {};
}

public async evaluateApps(): Promise<EvaluatorResult> {
Expand All @@ -44,7 +44,7 @@ export abstract class BaseEvaluator {
abstract get type(): EvaluatorType;

public get config(): any {
return this._globalConfig[this.type];
return this._globalConfig[this.type] || {};
}

public get skippedApps(): IApp[] {
Expand Down
24 changes: 19 additions & 5 deletions src/evaluators/sumo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ async function tryEvaluate(app) {
app.timeTaken = stopClock(timer);
return validateResults(app, results, log);
} catch (err) {
log(`error executing sumo evaluator for '${ app.name }': ${ err.message }`, err);
const errorInfo = new Error(err.message);
errorInfo.stack = err.stack;
// @ts-ignore
errorInfo.response = {
status: err?.response?.status,
data: err?.response.data
};
log(`error executing sumo evaluator for '${ app.name }': ${ err.message }`, errorInfo);
return new MonitorFailureResult(
"sumo",
app.name,
Expand Down Expand Up @@ -131,7 +138,7 @@ function generateValueForVariable(value) {
}
}

async function startSearch(app, _log) {
async function startSearch(app, log) {
const search = {
query: app.query,
from: toSumoTime(app.period.from),
Expand All @@ -143,6 +150,7 @@ async function startSearch(app, _log) {
SumoUrl,
search,
getHeaders(app.token));
log(`started sumo job search for '${ app.name }'`, result.data);
return result.data.id;
}

Expand All @@ -162,9 +170,15 @@ async function isJobComplete(app, log) {
}
}

async function getSearchResult(app, _log) {
const result = await axios.get(`${ SumoUrl }/${ app.jobId }/records?offset=0&limit=100`, getHeaders(app.token));
return result.data;
async function getSearchResult(app, log) {
try {
const result = await axios.get(`${ SumoUrl }/${ app.jobId }/records?offset=0&limit=100`, getHeaders(app.token));
log(`successfully completed sumo job search for '${ app.name }', result:`, result.data);
return result.data;
} catch(err) {
log(`failed to complete sumo job search for '${ app.name }', result:`, err.response?.data);
throw err;
}
}

async function deleteJob(app, log) {
Expand Down
36 changes: 36 additions & 0 deletions src/lib/period-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,42 @@ describe("period parsing", () => {
expect(new Date().valueOf() - +result.to).toBeLessThan(1000);
});
});
describe("with named ranges", () => {
describe("today", () => {
it("should return value from 00:00AM to now", async () => {
// arrange
const period = "today";

// act
const result = parsePeriodRange(period);

// assert
const today = new Date();
today.setHours(0,0,0,0);
expect(result.from).toEqual(today);
const fuzzyDeltaInMillis = 100;
const now = new Date();
expect(+now - +result.to).toBeLessThanOrEqual(fuzzyDeltaInMillis);
});
});

describe("yesterday", () => {
it("should return value from 00:00AM yesterday to 00:00AM today", async () => {
// arrange
const period = "yesterday";

// act
const result = parsePeriodRange(period);

// assert
const today = new Date();
today.setHours(0,0,0,0);
const yesterday = new Date(today.setDate(today.getDate() - 1));
expect(result.from).toEqual(yesterday);
expect(result.to).toEqual(today);
});
});
});
});

});
37 changes: 35 additions & 2 deletions src/lib/period-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,41 @@ export function parsePeriodToMinutes(input): number {
return parsePeriodToSeconds(input) / 60;
}

export function parsePeriodRange(input): IPeriod {
if (!input || !ValidPeriodRangeRegex.test(input.trim())) {
export function parsePeriodRange(input: string): IPeriod {
if (!input) {
throw new Error(InvalidPeriodRangeError);
}
const namedRangeResult = tryParseNamedPeriodRange(input);
if (namedRangeResult) {
return namedRangeResult;
}
return tryParseNumericPeriodRange(input);
}

function tryParseNamedPeriodRange(input: string): IPeriod {
const lowered = input.trim().toLowerCase();
const today = new Date();
switch(lowered) {
case "today":
today.setHours(0, 0, 0, 0);
return {
from: today,
to: new Date()
};
case "yesterday":
today.setHours(0, 0, 0, 0);
const yesterday = new Date(today.setDate(today.getDate() - 1));
return {
from: yesterday,
to: today
};
default:
return null;
}
}

function tryParseNumericPeriodRange(input: string): IPeriod {
if (!ValidPeriodRangeRegex.test(input.trim())){
throw new Error(InvalidPeriodRangeError);
}
const match = ValidPeriodRangeRegex.exec(input.trim());
Expand Down

0 comments on commit 2c4dd4d

Please sign in to comment.