Skip to content

Commit

Permalink
chore(src): update from refactor-to-pure-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
narekhovhannisyan committed Mar 2, 2024
2 parents 898c24c + 186702f commit c2283bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib/boavizta/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Boavizta exposes a [REST API](https://doc.api.boavizta.org/). If the `boavizta`

### Plugin global config

- `allocation`: manufacturing impacts can be reported with two allocation strategies: `TOTAL` is the total impact without adjusting for usage. `LINEAR` distrbutes the impact linearly over the lifespan of a device. See [Boavizta docs](https://doc.api.boavizta.org/Explanations/manufacture_methodology/#hover-a-specific-duration-allocation-linear) for more info.
- `verbose`: determines how much information the API response contains (optional)

### Inputs
Expand All @@ -23,6 +22,7 @@ Boavizta exposes a [REST API](https://doc.api.boavizta.org/). If the `boavizta`
- `cpu/expected-lifespan`: the lifespan of the component, in seconds
- `country`: the country used to lookup grid carbon intensity, e.g. "USA" (optional - falls back to Boavizta default)
- `cpu/utilization`: percentage CPU utilization for a given observation
- `instance-type`: the name of the specific instance

## Returns

Expand Down
39 changes: 32 additions & 7 deletions src/lib/boavizta/boavizta-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import axios from 'axios';

import {KeyValuePair} from '../../types/common';

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

import {ICountryCodes} from './types';

const {APIRequestError} = ERRORS;

export const BoaviztaAPI = () => {
const BASE_URL = 'https://api.boavizta.org/v1';

const errorBuilder = buildErrorMessage(BoaviztaAPI.name);

/**
* Fetches CPU output data from Boavizta API for a specific component type.
*/
Expand All @@ -27,16 +34,34 @@ export const BoaviztaAPI = () => {
* Fetches cloud instance data from Boavizta API.
*/
const fetchCloudInstanceData = async (
dataCast: KeyValuePair,
data: KeyValuePair,
verbose: boolean
): Promise<object> => {
const updatedDataCast = replaceHyphensWithUnderscores(dataCast);
const updatedDataCast = replaceHyphensWithUnderscores(data);

const response = await axios.post(
`${BASE_URL}/cloud/instance?verbose=${verbose}&duration=${updatedDataCast['usage']['hours_use_time']}`,
dataCast
);
return response.data;
const response = await axios
.post(
`${BASE_URL}/cloud/instance?verbose=${verbose}&duration=${updatedDataCast['usage']['hours_use_time']}`,
{
provider: data.provider,
instance_type: data['instance-type'],
usage: data.usage,
}
)
.catch(error => {
throw new APIRequestError(
errorBuilder({
message: `Error fetching data from Boavizta API. ${JSON.stringify(
(error.response &&
error.response.data &&
error.response.data.detail) ||
error
)}`,
})
);
});

return response?.data;
};

/**
Expand Down
8 changes: 4 additions & 4 deletions src/lib/boavizta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const BoaviztaCpuOutput = (
if (!(metricType in input)) {
throw new InputValidationError(
errorBuilder({
message: 'Invalid input parameter',
message: `${metricType} is not provided in input`,
})
);
}
Expand All @@ -74,7 +74,7 @@ export const BoaviztaCpuOutput = (
usage: BoaviztaUsageType | undefined
): Promise<BoaviztaCpuOutputType> => {
const data = Object.assign({}, input, {usage});
const verbose = globalConfig.verbose || false;
const verbose = (globalConfig && globalConfig.verbose) || false;
const response = await boaviztaAPI.fetchCpuOutputData(
data,
componentType,
Expand Down Expand Up @@ -138,7 +138,7 @@ export const BoaviztaCloudOutput = (
if (!(metricType in input)) {
throw new InputValidationError(
errorBuilder({
message: 'Invalid input parameter',
message: `${metricType} is not provided in input`,
})
);
}
Expand All @@ -162,7 +162,7 @@ export const BoaviztaCloudOutput = (
usage: BoaviztaUsageType
): Promise<BoaviztaCloudInstanceType> => {
const data = Object.assign({}, input, {usage});
const verbose = globalConfig.verbose || false;
const verbose = (globalConfig && globalConfig.verbose) || false;
const response = await boaviztaAPI.fetchCloudInstanceData(data, verbose);

return baseOutput.formatResponse(response);
Expand Down

0 comments on commit c2283bf

Please sign in to comment.