Skip to content

Commit

Permalink
Merge pull request #40 from Klimatbyran/feat/imageTable
Browse files Browse the repository at this point in the history
CO2 table image
  • Loading branch information
irony authored Mar 14, 2024
2 parents 1e053e0 + d6bf6df commit dca8342
Show file tree
Hide file tree
Showing 10 changed files with 2,262 additions and 52 deletions.
1,893 changes: 1,855 additions & 38 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"@bull-board/express": "^5.12.0",
"@elastic/elasticsearch": "^8.12.1",
"@google/generative-ai": "^0.1.3",
"@hugocxl/react-to-image": "^0.0.9",
"bullmq": "^5.1.1",
"chromadb": "^1.7.3",
"discord.js": "^14.14.1",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jest": "^29.7.0",
"node-html-to-image": "^4.0.0",
"openai": "^4.24.3",
"pdf-parse": "^1.1.1",
"typescript": "^5.3.3"
Expand Down
127 changes: 127 additions & 0 deletions src/data/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"emissions": [
{
"year": "2018",
"scope1": {
"emissions": "1544000",
"unit": "ton CO2e",
"baseYear": "2018"
},
"scope2": {
"emissions": "1408000",
"unit": "ton CO2e",
"locationBased": "1408000",
"marketBased": "1838000",
"baseYear": "2018"
},
"scope3": {
"emissions": null,
"unit": null,
"baseYear": "2018",
"categories": {}
},
"totalEmissions": "Error 500",
"totalUnit": "ton CO2e"
},
{
"year": "2019",
"scope1": {
"emissions": "1537000",
"unit": "ton CO2e",
"baseYear": "2019"
},
"scope2": {
"emissions": "1292000",
"unit": "ton CO2e",
"locationBased": "1292000",
"marketBased": "1661000",
"baseYear": "2019"
},
"scope3": {
"emissions": "3595000",
"unit": "ton CO2e",
"baseYear": "2019",
"categories": {}
},
"totalEmissions": "2834000",
"totalUnit": "ton CO2e"
},
{
"year": "2020",
"scope1": {
"emissions": "1445000",
"unit": "ton CO2e",
"baseYear": "2020"
},
"scope2": {
"emissions": "1217000",
"unit": "ton CO2e",
"locationBased": "1217000",
"marketBased": "1436000",
"baseYear": "2020"
},
"scope3": {
"emissions": "3410000",
"unit": "ton CO2e",
"baseYear": "2020",
"categories": {}
},
"totalEmissions": "2662000",
"totalUnit": "ton CO2e"
},
{
"year": "2021",
"scope1": {
"emissions": "1410000",
"unit": "ton CO2e",
"baseYear": "2021"
},
"scope2": {
"emissions": "1216000",
"unit": "ton CO2e",
"locationBased": "1216000",
"marketBased": "1462000",
"baseYear": "2021"
},
"scope3": {
"emissions": null,
"unit": null,
"baseYear": "2021",
"categories": {}
},
"totalEmissions": "2626000",
"totalUnit": "ton CO2e"
},
{
"year": "2022",
"scope1": {
"emissions": "1395000",
"unit": "ton CO2e",
"baseYear": "2022"
},
"scope2": {
"emissions": "1254000",
"unit": "ton CO2e",
"locationBased": "1254000",
"marketBased": "1499000",
"baseYear": "2022"
},
"scope3": {
"emissions": null,
"unit": null,
"baseYear": "2022",
"categories": {}
},
"totalEmissions": "2649000",
"totalUnit": "ton CO2e"
}
],
"companyName": "Essity",
"bransch": "Hygiene and Health Company",
"baseYear": "2016",
"url": "https://essity.com",
"reliability": "High",
"needsReview": true,
"reviewComment": "Scope 3 emissions data is not available for 2018, 2021, and 2022, which impacted the accuracy of the total emissions for those years. Prior year adjustments for Scope 2 are mentioned but not detailed enough to be factored into the analysis. This requires clarification from the company for accurate annual accounting and should be reviewed by the reporting company.",
"reviewStatusCode": "412"
}
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import express from 'express'
import { createBullBoard } from '@bull-board/api'
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter'
import { ExpressAdapter } from '@bull-board/express'
import fs from 'fs/promises'

import discord from './discord'
import elastic from './elastic'
Expand All @@ -20,6 +21,7 @@ import {
searchVectors,
splitText,
} from './queues'
import { scope2Image } from './lib/imageCreator'

// add dummy job
// downloadPDF.add('dummy', {
Expand Down Expand Up @@ -65,3 +67,18 @@ app.listen(3000, () => {
console.log('Running on 3000...')
console.log('For the UI, open http://localhost:3000/admin/queues')
})

app.get('/', (req, res) => {
res.send(`Hi I'm Garbo!`)
})

app.get(`/api/companies`, async function (req, res) {
res.writeHead(200, { 'Content-Type': 'image/png' })
const exampleString = (
await fs.readFile('./src/data/example.json')
).toString()
console.log('exampleString', exampleString)
const example = JSON.parse(exampleString)
const image = await scope2Image(example)
res.end(image, 'binary')
})
50 changes: 50 additions & 0 deletions src/lib/imageCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import nodeHtmlToImage from 'node-html-to-image'
import fs from 'fs'

type YearEmissions = {
year: number
scope1: {
emissions: string
unit: string
baseYear: string
}
scope2: {
emissions: string
unit: string
mb: string
lb: string
baseYear: string
}
scope3: {
emissions: string
unit: string
baseYear: string
categories: {
[key: string]: string
}
}
totalEmissions?: string
totalUnit?: string
}

type CompanyData = {
companyName: string
bransch?: string
baseYear?: string
url?: string
emissions: Array<YearEmissions>
reliability?: string
needsReview?: boolean
reviewComment?: string
reviewStatusCode?: string
}

export const scope2Image = async (company: CompanyData) => {
const emissions = company.emissions.sort((a, b) => b.year - a.year)
const template = fs.readFileSync('src/lib/scope2.handlebars', 'utf8')
const image = await nodeHtmlToImage({
html: template,
content: { ...company, emissions },
})
return image
}
119 changes: 119 additions & 0 deletions src/lib/scope2.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<style>
body{
font-family: sans-serif, sans-serif;
font-size: 16px;
line-height: 1.5;
padding: 10px;
color: #333;
}
.container { display: flex;
flex-direction: column;
gap: 32px;
}
.title {
font-size: 1.125rem;
font-weight: bold;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 8px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) td {
background-color: #f2f2f2;
}
.text-left {
text-align: left;
}
td[rowspan] {
vertical-align: top;
}
</style>
<div class='container'>
<div>
<h2 class='title'>
Koldioxidutsläpp (CO<sub>2</sub>e)
</h2>
<table>
<thead>
<tr>
<th colspan='2' class='text-left'>Utsläpp inom scope 1, 2 och 3 (CO<sub
>2</sub>e) och biogena utsläpp (utanför scope) genererade av
{{companyName}}s verksamhet.</th>
{{#each emissions as |emission|}}
<th>{{emission.year}}</th>
{{/each}}
</tr>
</thead>
<tbody>
<tr>
<td rowspan='5'>Ton CO<sub>2</sub>e</td>
<td>Scope 1</td>
{{#each emissions as |emission|}}
<td>{{emission.scope1.emissions}}</td>
{{/each}}
</tr>
<tr>
<td>Scope 2<sup>3</sup></td>
{{#each emissions as |emission|}}
<td>{{emission.scope2.emissions}}</td>
{{/each}}
</tr>
<tr>
<td>Platsbaserat tillvägagångssätt</td>
{{#each emissions as |emission|}}
<td>{{emission.scope2.lb}}</td>
{{/each}}
</tr>
<!--<tr>
<td colspan='2'>Förändring jämfört med basår (scope 1 och 2), %</td>
<td>-55</td>
<td>-46</td>
<td>-34</td>
<td>-28</td>
<td>—</td>
</tr>
<tr>
<td colspan='2'>Koldioxidintensitet<sup>4</sup></td>
<td>1,13</td>
<td>1,46</td>
<td>1,67</td>
<td>1,64</td>
<td>2,60</td>
</tr>-->
<tr>
<td>Scope 3</td>

{{#each emissions as |emission|}}
<td>{{emission.scope3.emissions}}</td>
{{/each}}
</tr>
<!--<tr>
<td colspan='2'>Förändring jämfört med basår (scope 3), %</td>
<td>-13</td>
<td>-12</td>
<td>—</td>
<td>—</td>
<td>—</td>
</tr>
<tr>
<td colspan='2'>Utanför scope<sup>5</sup></td>
<td>33&nbsp;000</td>
<td>22&nbsp;000</td>
<td>17&nbsp;000</td>
<td>20&nbsp;000</td>
<td>—</td>
</tr>-->
</tbody>
</table>
</div>
<div>
</div>
Loading

0 comments on commit dca8342

Please sign in to comment.