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

Release v5.3.0 #374

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

.eslintcache
.eslintcache
# Local Netlify folder
.netlify
5 changes: 5 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ package = "@algolia/netlify-plugin-crawler"
[plugins.inputs]
branches = ['main', 'develop']
renderJavaScript = true

[functions]
[functions."collectCarbon"]
external_node_modules = ["jsdom"]
schedule = "0 0 * * 1"
67 changes: 67 additions & 0 deletions netlify/functions/collectCarbon.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { MongoClient } from 'mongodb';
import { Handler } from '@netlify/functions';
import { CarbonData, CarbonResult } from '../../src/shared/types/docs';
const { default: axios } = require('axios');
const jsdom = require('jsdom');

async function getCarbon(url): Promise<CarbonResult> {
const api = 'https://api.websitecarbon.com/b?url=';
try {
const res = await axios.get(api + url);
return {
url,
carbon: res.data.c,
percent: res.data.p
};
} catch (e) {
return {
url,
carbon: -1,
percent: -1,
error: {
status: e.response.status,
statusText: e.response.statusText
}
};
}
}

async function parseSitemap(): Promise<string[]> {
const res = await axios.get('https://raw.githubusercontent.com/bsc-xdc/anatomy/master/public/sitemap.xml');
const dom = new jsdom.JSDOM(res.data, { contentType: 'application/xml' });
return Array.from(dom.window.document.querySelectorAll('loc'), (loc: Element) => loc.textContent || '');
}

async function collectData(): Promise<CarbonData> {
const urls = await parseSitemap();
const filteredUrls = urls.map((url) => url.replace(/\/$/m, ''));
const promises = filteredUrls.map((url) => getCarbon(url));
const results = Array.from(await Promise.all(promises));
return {
date: new Date().toISOString(),
results
};
}

const handler: Handler = async () => {
const uri = process.env.MONGO_CONNECTION as string;
const client = new MongoClient(uri);

try {
const database = client.db('carbon-metrics');
const carbon = database.collection<CarbonData>('metrics');
const carbonData = await collectData();
await carbon.insertOne(carbonData);
return {
statusCode: 200
};
} catch (error) {
return {
statusCode: 500
};
} finally {
await client.close();
}
};

export { handler };
Loading