Skip to content

Commit

Permalink
fix(lib): fix validation in teads-curve
Browse files Browse the repository at this point in the history
- update readme file
  • Loading branch information
manushak committed Feb 28, 2024
1 parent 4b0c6ef commit b052d49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 42 deletions.
21 changes: 9 additions & 12 deletions src/lib/teads-curve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
Teads Engineering team has built a plugin that is capable of estimating CPU usages across varying type of CPUs using a curve commonly known as Teads Curve.

## Plugin name

IF recognizes the Teads CPU plugin as `teads-curve`.

## Parameters

### Plugin config
### Plugin global config

- `cpu/thermal-design-power`: the TDp of the processor
- `interpolation`: the interpolation method to apply to the TDP data

### Inputs

- `cpu/thermal-design-power`: the TDp of the processor
- `cpu/utilization`: percentage CPU utilization for the input
- `duration`: the amount of time the observation covers, in seconds

## Returns

Expand Down Expand Up @@ -48,8 +45,8 @@ const teads = TeadsCurve({
const results = teads.execute([
{
duration: 3600, // duration institute
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
'cpu/utilization': 100, // CPU usage as a value between 0 to 100 in percentage
datetime: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
},
]);
```
Expand All @@ -73,8 +70,8 @@ const results = teads.execute(
[
{
duration: 3600, // duration institute
timestamp: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
'cpu/utilization': 100, // CPU usage as a value between 0 to 100 in percentage
datetime: '2021-01-01T00:00:00Z', // ISO8601 / RFC3339 timestamp
},
],
{
Expand All @@ -83,7 +80,7 @@ const results = teads.execute(
);
```

## Example `impl`
## Example `manifest`

```yaml
name: teads-curve
Expand All @@ -92,15 +89,15 @@ tags:
initialize:
plugins:
teads-curve:
function: TeadsCurve
method: TeadsCurve
path: '@grnsft/if-unofficial-plugins'
global-config:
interpolation: spline
tree:
children:
child:
pipeline:
- teads-cpu
- teads-curve
inputs:
- timestamp: 2023-07-06T00:00
duration: 3600
Expand All @@ -113,5 +110,5 @@ You can run this by passing it to `if`. Run impact using the following command r
```sh
npm i -g @grnsft/if
npm i -g @grnsft/if-unofficial-plugins
if --impl ./examples/impls/test/teads-cpu.yml --ompl ./examples/ompls/teads-cpu.yml
if --manifest ./examples/manifests/test/teads-curve.yml --output ./examples/outputs/teads-curve.yml
```
49 changes: 19 additions & 30 deletions src/lib/teads-curve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ export const TeadsCurve = (globalConfig?: ConfigParams): PluginInterface => {
/**
* Calculate the total emissions for a list of inputs.
*/
const execute = async (
inputs: PluginParams[],
config?: ConfigParams
): Promise<PluginParams[]> => {
const mergedConfig = Object.assign({}, globalConfig, config);
const validatedConfig = validateConfig(mergedConfig);
const execute = async (inputs: PluginParams[]) => {
const validatedConfig = validateConfig(globalConfig || {});

return inputs.map((input, index) => {
const safeInput = getValidatedInput(input);
const inputWithConfig: PluginParams = Object.assign(
const safeInput = validateInput(input);
const inputWithConfig = Object.assign(
{},
input,
safeInput,
Expand Down Expand Up @@ -80,16 +76,17 @@ export const TeadsCurve = (globalConfig?: ConfigParams): PluginInterface => {
* (wattage * duration) / (seconds in an hour) / 1000 = kWh
*/
const calculateEnergy = (input: PluginParams) => {
const {duration, 'cpu/utilization': cpu} = input;
const {
duration,
'cpu/utilization': cpu,
'cpu/thermal-design-power': cpuThermalDesignPower,
} = input;
const spline: any = new Spline(POINTS, CURVE);

const wattage =
input.interpolation === Interpolation.SPLINE
? spline.at(cpu) * input['cpu/thermal-design-power']
: calculateLinearInterpolationWattage(
cpu,
input['cpu/thermal-design-power']
);
? spline.at(cpu) * cpuThermalDesignPower
: calculateLinearInterpolationWattage(cpu, cpuThermalDesignPower);

return (wattage * duration) / 3600 / 1000;
};
Expand Down Expand Up @@ -161,34 +158,26 @@ export const TeadsCurve = (globalConfig?: ConfigParams): PluginInterface => {
interpolation: z.nativeEnum(Interpolation).optional(),
});

//Manually add default value
config.interpolation = config.interpolation ?? Interpolation.SPLINE;
// Manually set default value
const interpolation = config.interpolation ?? Interpolation.SPLINE;

return validate<z.infer<typeof schema>>(schema, config);
return validate<z.infer<typeof schema>>(schema, {...config, interpolation});
};

/**
* Validates parameters.
*/
const validateParams = (params: object) => {
const validateInput = (input: PluginParams) => {
const schema = z.object({
duration: z.number().gt(0),
'cpu/utilization': z.number().min(0).max(100),
'cpu/thermal-design-power': z.number().min(1),
});

return validate<z.infer<typeof schema>>(schema, params);
};

/**
* Sets validated parameters for the class instance.
*/
const getValidatedInput = (params: object) => {
const safeParams = Object.assign({}, params, validateParams(params));
// Manually set default value if the property is missing.
const cpuTDP = input['cpu/thermal-design-power'] ?? 0;

return {
'cpu/thermal-design-power': safeParams['cpu/thermal-design-power'] ?? 0,
'cpu/utilization': safeParams['cpu/utilization'],
};
return validate<z.infer<typeof schema>>(schema, {...input, cpuTDP});
};

return {
Expand Down

0 comments on commit b052d49

Please sign in to comment.