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

Add an optional gramsOfCO2ePerKWh field to the meta data of processed profiles #4672

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/tooltip/TrackPower.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getCommittedRange,
getPreviewSelection,
getProfileInterval,
getMeta,
} from 'firefox-profiler/selectors/profile';
import { getSampleIndexRangeForSelection } from 'firefox-profiler/profile-logic/profile-data';

Expand All @@ -26,6 +27,7 @@ import type {
Milliseconds,
PreviewSelection,
StartEndRange,
ProfileMeta,
} from 'firefox-profiler/types';

import type { ConnectedProps } from 'firefox-profiler/utils/connect';
Expand All @@ -37,6 +39,7 @@ type OwnProps = {|

type StateProps = {|
interval: Milliseconds,
meta: ProfileMeta,
committedRange: StartEndRange,
previewSelection: PreviewSelection,
|};
Expand Down Expand Up @@ -68,8 +71,9 @@ class TooltipTrackPowerImpl extends React.PureComponent<Props> {
_computeCO2eFromPower(power: number): number {
// total energy Wh to kWh
const energy = power / 1000;
const { WORLD } = averageIntensity.data;
return energy * WORLD;
const intensity =
this.props.meta.gramsOfCO2ePerKWh || averageIntensity.data.WORLD;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use ?? instead of || so that we test for its absence rather than it being falsy. Indeed I guess it's theorically possible to have it set to 0 in case of 100% renewable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

0 doesn't make sense, even for renewable. As an example, my renewable electricity provider estimates the carbon intensity of its electricity at 18.5g per kWh for production plus 5.15g per kWh for distribution. Using solar panels on my roof would have about the same footprint. If we wanted to support the 0 value, I would expect its meaning to be "hide everything in the UI that talks about CO2eq".

return energy * intensity;
}

_computePowerSumForCommittedRange = memoize(
Expand Down Expand Up @@ -187,6 +191,7 @@ class TooltipTrackPowerImpl extends React.PureComponent<Props> {
export const TooltipTrackPower = explicitConnect<OwnProps, StateProps, {||}>({
mapStateToProps: (state) => ({
interval: getProfileInterval(state),
meta: getMeta(state),
committedRange: getCommittedRange(state),
previewSelection: getPreviewSelection(state),
}),
Expand Down
4 changes: 4 additions & 0 deletions src/types/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,10 @@ export type ProfileMeta = {|
initialSelectedThreads?: ThreadIndex[],
// Keep the defined thread order
keepProfileThreadOrder?: boolean,

// Grams of CO2 equivalent per kWh. Used to display power track tooltips.
// Will fallback to the global average if this is missing.
gramsOfCO2ePerKWh?: number,
|};

/**
Expand Down