Skip to content

Commit

Permalink
Add co2 version to the HTML output (#3954)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Aug 31, 2023
1 parent 745d8eb commit 6b245ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
25 changes: 18 additions & 7 deletions lib/plugins/sustainable/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { resolve, join } from 'node:path';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
import zlib from 'node:zlib';
Expand All @@ -10,9 +9,15 @@ import { co2, hosting } from '@tgwf/co2';
import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import { Aggregator } from './aggregator.js';

const readFile = promisify(fs.readFile);
const gunzip = promisify(zlib.gunzip);
const fsp = fs.promises;
const __dirname = fileURLToPath(new URL('.', import.meta.url));

const packageJson = JSON.parse(
await fsp.readFile(resolve(join(__dirname, '..', '..', '..', 'package.json')))
);
const co2Version = packageJson.dependencies['@tgwf/co2'];

const gunzip = promisify(zlib.gunzip);
const log = intel.getLogger('sitespeedio.plugin.sustainable');

const DEFAULT_METRICS_PAGE_SUMMARY = [
Expand Down Expand Up @@ -41,7 +46,7 @@ async function getGzippedFileAsJson(jsonPath) {
async function loadJSON(jsonPath) {
const jsonBuffer = jsonPath.endsWith('.gz')
? await getGzippedFileAsJson(jsonPath)
: await readFile(jsonPath);
: await fsp.readFile(jsonPath);
return JSON.parse(jsonBuffer);
}

Expand All @@ -50,12 +55,15 @@ export default class SustainablePlugin extends SitespeedioPlugin {
super({ name: 'sustainable', options, context, queue });
}

open(context, options) {
async open(context, options) {
this.storageManager = context.storageManager;
this.options = options;
this.sustainableOptions = options.sustainable || {};
this.make = context.messageMaker('sustainable').make;
this.pug = readFileSync(resolve(__dirname, 'pug', 'index.pug'), 'utf8');
this.pug = await fsp.readFile(
resolve(__dirname, 'pug', 'index.pug'),
'utf8'
);
this.aggregator = new Aggregator(options);
this.firstRunsData = {};
context.filterRegistry.registerFilterForType(
Expand Down Expand Up @@ -204,7 +212,8 @@ export default class SustainablePlugin extends SitespeedioPlugin {
co2ThirdParty: co2PerParty.thirdParty,
hostingGreenCheck,
dirtiestResources,
co2PerContentType
co2PerContentType,
co2Version
},
{
url: message.url,
Expand All @@ -226,6 +235,8 @@ export default class SustainablePlugin extends SitespeedioPlugin {
const extras = this.firstRunsData[url];
// Attach first run so we can show that extra data that we don't collect stats for
summaries.urls[url].firstRun = extras;

summaries.urls[url].co2Version = co2Version;
queue.postMessage(
make('sustainable.pageSummary', summaries.urls[url], {
url: url,
Expand Down
7 changes: 6 additions & 1 deletion lib/plugins/sustainable/pug/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ h1 Sustainable Web

- let sustainable = pageInfo.data.sustainable.run ? pageInfo.data.sustainable.run : pageInfo.data.sustainable.pageSummary

p The sustainable metrics is calculated using
a(href='https://github.com/thegreenwebfoundation/co2.js') @tgwf/co2
|  version #{sustainable.co2Version}.

p We know that using the internet means using electricity, and because most of that electricity comes from fossil fuels, it means that more data we send, the more fossil fuels we end up burning.

p This is terrible news for the climate, and
Expand All @@ -27,7 +31,8 @@ if !options.sustainable.pageViews
if (sustainable.hostingInfo)
p #{sustainable.hostingInfo.url} #{sustainable.hostingInfo.green === true ? 'is hosted green! This means the servers running the site use green energy' : 'does not appear to be running on green energy.'}

p This information comes from The Green Web Foundation's datasets, which are published as open data. If you have questions, corrections or want to use this data yourself, please visit (href='https://www.thegreenwebfoundation.org') The Green Web Foundation website.
p This information comes from The Green Web Foundation's datasets, which are published as open data. If you have questions, corrections or want to use this data yourself, please visit
a(href='https://www.thegreenwebfoundation.org') The Green Web Foundation website.

- const co2FirstParty = sustainable.co2FirstParty.median || sustainable.co2FirstParty
- const co2ThirdParty = sustainable.co2ThirdParty.median === 0 ? 0 : sustainable.co2ThirdParty.median || sustainable.co2ThirdParty
Expand Down

0 comments on commit 6b245ad

Please sign in to comment.