Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update JSDocs to match behaviour #198

Merged
Merged
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions src/co2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

/**
* @typedef {Object} CO2EstimateTraceResultPerByte
* @property {number} co2 - The CO2 estimate in grams/kilowatt-hour
* @property {number|CO2EstimateComponents} co2 - The CO2 estimate in grams or its separate components
* @property {boolean} green - Whether the domain is green or not
* @property {TraceResultVariables} variables - The variables used to calculate the CO2 estimate
* @property {TraceResultVariablesPerByte} variables - The variables used to calculate the CO2 estimate
*/

/**
* @typedef {Object} CO2EstimateTraceResultPerVisit
* @property {number} co2 - The CO2 estimate in grams/kilowatt-hour
* @property {number|CO2EstimateComponents} co2 - The CO2 estimate in grams or its separate components
* @property {boolean} green - Whether the domain is green or not
* @property {TraceResultVariables} variables - The variables used to calculate the CO2 estimate
* @property {TraceResultVariablesPerVisit} variables - The variables used to calculate the CO2 estimate
*/

/**
Expand All @@ -35,6 +35,15 @@
* @property {number} production - The production grid intensity set by the user or the default
*/

/**
* @typedef {Object} CO2EstimateComponents
* @property {number} networkCO2 - The CO2 estimate for networking in grams
* @property {number} dataCenterCO2 - The CO2 estimate for data centers in grams
* @property {number} consumerDeviceCO2 - The CO2 estimate for consumer devices in grams
* @property {number} productionCO2 - The CO2 estimate for device production in grams
* @property {number} total - The total CO2 estimate in grams
*/

import OneByte from "./1byte.js";
import SustainableWebDesign from "./sustainable-web-design.js";

Expand Down Expand Up @@ -70,7 +79,7 @@ class CO2 {
*
* @param {number} bytes
* @param {boolean} green
* @return {number} the amount of CO2 in grammes
* @return {number|CO2EstimateComponents} the amount of CO2 in grammes or its separate components
*/
perByte(bytes, green = false) {
return this.model.perByte(bytes, green, this._segment);
Expand All @@ -83,7 +92,7 @@ class CO2 {
*
* @param {number} bytes
* @param {boolean} green
* @return {number} the amount of CO2 in grammes
* @return {number|CO2EstimateComponents} the amount of CO2 in grammes or its separate components
Copy link
Contributor

Choose a reason for hiding this comment

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

i think there are cases where perVisit has a different return type than perByte:

const co2Emission = new co2({
  results: "segment",
});

const bytesSent = 1000 * 1000 * 1000;
const greenHost = false;

console.log(co2Emission.perVisit(bytesSent, greenHost));

the result i'm seeing looks like this:

{
    "consumerDeviceCO2 - first": 154.825749,
    "consumerDeviceCO2 - subsequent": 1.0321716600000002,
    "networkCO2 - first": 41.68385550000001,
    "networkCO2 - subsequent": 0.27789237000000006,
    "productionCO2 - first": 56.570946750000004,
    "productionCO2 - subsequent": 0.3771396450000001,
    "dataCenterCO2 - first": 44.66127375,
    "dataCenterCO2 - subsequent": 0.297741825,
    "total": 299.7267705
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good spot, I'll have a look at that - might need a unique type for perVisit.
Is there possibly also a bug there? Adding up all of the first values only gives ~297.74, so I assume the total is including the subsequent visit information too. In terms of what you might want to use this for it would make more sense to have "total - first" and "total - subsequent" properties. Or use sub-objects with first and subsequent values for all of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NVM, I see that both parts of the component are reduced by a percentage so the total is correct. Having separated total components would be a feature request/matter of taste.

*/
perVisit(bytes, green = false) {
if (this.model?.perVisit) {
Expand Down
Loading