Skip to content

Commit

Permalink
Merge pull request #390 from Green-Software-Foundation/aggregation-pe…
Browse files Browse the repository at this point in the history
…r-unit

Aggregation per unit
  • Loading branch information
jmcook1186 committed Jan 15, 2024
2 parents c380f62 + 7c09832 commit 3fbcb2b
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 683 deletions.
4 changes: 2 additions & 2 deletions examples/impls/test/aggregation-test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: nesting-demo
description:
aggregation:
aggregation-metrics: ['carbon', 'energy']
aggregation-method: 'sum'
metrics: ['carbon', 'energy']
type: 'horizontal'
tags:
kind: web
complexity: moderate
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/unit/config/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const {
NOT_INITIALIZED_MODEL,
NOT_CONSTRUCTABLE_MODEL,
INVALID_MODULE_PATH,
INVALID_AGGREGATION_METHOD,
METRIC_MISSING,
} = STRINGS;

describe('config/strings: ', () => {
Expand Down Expand Up @@ -47,4 +49,25 @@ describe('config/strings: ', () => {
expect(INVALID_MODULE_PATH(param)).toEqual(expectedMessage);
});
});

describe('INVALID_AGGREGATION_METHOD(): ', () => {
it('successfully appends given param to message.', () => {
const param = 'mock-param';

const expectedMessage = `Aggregation is not possible for given ${param} since method is 'none'.`;

expect(INVALID_AGGREGATION_METHOD(param)).toEqual(expectedMessage);
});
});

describe('METRIC_MISSING(): ', () => {
it('successfully appends given param to message.', () => {
const metric = 'mock-metric';
const index = 0;

const expectedMessage = `Aggregation metric ${metric} is not found in inputs[${index}].`;

expect(METRIC_MISSING(metric, index)).toEqual(expectedMessage);
});
});
});
93 changes: 93 additions & 0 deletions src/__tests__/unit/lib/aggregator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {aggregate} from '../../../lib/aggregator';

import {STRINGS} from '../../../config';

import {ERRORS} from '../../../util/errors';

import {UnitKeyName} from '../../../types/units';

const {INVALID_AGGREGATION_METHOD, METRIC_MISSING} = STRINGS;

const {InvalidAggregationParams} = ERRORS;

describe('lib/aggregator: ', () => {
describe('aggregate(): ', () => {
it('throws error if aggregation method is none.', async () => {
const inputs = [{}];
const metrics = ['total-resources'] as UnitKeyName[];

const expectedMessage = INVALID_AGGREGATION_METHOD('none');

expect.assertions(1);

try {
await aggregate(inputs, metrics);
} catch (error) {
expect(error).toEqual(new InvalidAggregationParams(expectedMessage));
}
});

it('throws error if metric is not found while aggregation.', async () => {
const inputs = [
{
'ram-util': 10,
},
];
const metrics = ['cpu-util'] as UnitKeyName[];

const expectedMessage = METRIC_MISSING(metrics[0], 0);

expect.assertions(1);

try {
await aggregate(inputs, metrics);
} catch (error) {
expect(error).toEqual(new InvalidAggregationParams(expectedMessage));
}
});

it('should successfully calculate avg.', async () => {
const inputs = [
{
'cpu-util': 10,
},
{
'cpu-util': 20,
},
];
const metrics = ['cpu-util'] as UnitKeyName[];

const expectedKey = `aggregated-${Object.keys(inputs[0])[0]}`;
const expectedValue = (inputs[0]['cpu-util'] + inputs[1]['cpu-util']) / 2;
const expectedResult = {
[`${expectedKey}`]: expectedValue,
};

const aggregatedResult = await aggregate(inputs, metrics);

expect(aggregatedResult).toEqual(expectedResult);
});

it('should successfully calculate sum.', async () => {
const inputs = [
{
'disk-io': 10,
},
{
'disk-io': 20,
},
];
const metrics = ['disk-io'] as UnitKeyName[];

const expectedKey = `aggregated-${Object.keys(inputs[0])[0]}`;
const expectedValue = inputs[0]['disk-io'] + inputs[1]['disk-io'];
const expectedResult = {
[`${expectedKey}`]: expectedValue,
};

const aggregatedResult = await aggregate(inputs, metrics);

expect(aggregatedResult).toEqual(expectedResult);
});
});
});
140 changes: 0 additions & 140 deletions src/__tests__/unit/lib/planet-aggregator.test.ts

This file was deleted.

Loading

0 comments on commit 3fbcb2b

Please sign in to comment.