Skip to content

Commit

Permalink
fix: remove extra report ambigous structure
Browse files Browse the repository at this point in the history
  • Loading branch information
irony committed Mar 29, 2024
1 parent a5410bc commit 438b85d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 169 deletions.
2 changes: 1 addition & 1 deletion src/data/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
}
],
"companyName": "Essity",
"bransch": "Hygiene and Health Company",
"industry": "Hygiene and Health Company",
"baseYear": "2016",
"url": "https://essity.com",
"reliability": "High",
Expand Down
114 changes: 68 additions & 46 deletions src/elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Elastic {
properties: {
companyName: { type: 'keyword' },
orgranizationNumber: { type: 'keyword' },
bransch: { type: 'keyword' },
industry: { type: 'keyword' },
baseYear: { type: 'keyword' },
url: { type: 'keyword' },
emissions: {
Expand Down Expand Up @@ -165,51 +165,73 @@ class Elastic {
}

public isValidEmissionReport(report): boolean {
const isString = (value) => typeof value === 'string';
const isBoolean = (value) => typeof value === 'boolean';
const isDouble = (value) => typeof value === 'number';

const hasValidTopLevelProps = isString(report.companyName) &&
isString(report.bransch) &&
isString(report.baseYear) &&
isString(report.url) &&
isString(report.reliability) &&
isBoolean(report.needsReview) &&
isString(report.reviewComment) &&
isString(report.reviewStatusCode);
if (!hasValidTopLevelProps) return false;

const isString = (value) => typeof value === 'string'
const isBoolean = (value) => typeof value === 'boolean'
const isDouble = (value) => typeof value === 'number'

const hasValidTopLevelProps =
isString(report.companyName) &&
isString(report.industry) &&
isString(report.baseYear) &&
isString(report.url) &&
isString(report.reliability) &&
isBoolean(report.needsReview) &&
isString(report.reviewComment) &&
isString(report.reviewStatusCode)
if (!hasValidTopLevelProps) return false

// Validate emissions object structure
if (typeof report.emissions !== 'object' || report.emissions === null) return false;

if (typeof report.emissions !== 'object' || report.emissions === null)
return false

for (const year of Object.keys(report.emissions)) {
const yearData = report.emissions[year];
if (typeof yearData !== 'object' || yearData === null) return false;
if (!isString(yearData.year) || !isDouble(yearData.scope1.emissions) ||
!isDouble(yearData.totalEmissions) || !isDouble(yearData.totalUnit) ||
!isString(yearData.scope1.unit) || !isDouble(yearData.scope2.emissions) ||
!isString(yearData.scope2.unit) || !isDouble(yearData.scope2.mb) ||
!isDouble(yearData.scope2.lb) || !isDouble(yearData.scope3.emissions) ||
!isString(yearData.scope3.unit) || !isString(yearData.scope3.baseYear)) return false;

const yearData = report.emissions[year]
if (typeof yearData !== 'object' || yearData === null) return false
if (
!isString(yearData.year) ||
!isDouble(yearData.scope1.emissions) ||
!isDouble(yearData.totalEmissions) ||
!isDouble(yearData.totalUnit) ||
!isString(yearData.scope1.unit) ||
!isDouble(yearData.scope2.emissions) ||
!isString(yearData.scope2.unit) ||
!isDouble(yearData.scope2.mb) ||
!isDouble(yearData.scope2.lb) ||
!isDouble(yearData.scope3.emissions) ||
!isString(yearData.scope3.unit) ||
!isString(yearData.scope3.baseYear)
)
return false

// Validate categories within scope3
const categories = yearData.scope3.categories;
if (typeof categories !== 'object' || categories === null) return false;

const validCategories = ['1_purchasedGoods', '2_capitalGoods', '3_fuelAndEnergyRelatedActivities',
'4_upstreamTransportationAndDistribution', '5_wasteGeneratedInOperations',
'6_businessTravel', '7_employeeCommuting', '8_upstreamLeasedAssets',
'9_downstreamTransportationAndDistribution', '10_processingOfSoldProducts',
'11_useOfSoldProducts', '12_endOfLifeTreatmentOfSoldProducts',
'13_downstreamLeasedAssets', '14_franchises', '15_investments', '16_other'];

const categories = yearData.scope3.categories
if (typeof categories !== 'object' || categories === null) return false

const validCategories = [
'1_purchasedGoods',
'2_capitalGoods',
'3_fuelAndEnergyRelatedActivities',
'4_upstreamTransportationAndDistribution',
'5_wasteGeneratedInOperations',
'6_businessTravel',
'7_employeeCommuting',
'8_upstreamLeasedAssets',
'9_downstreamTransportationAndDistribution',
'10_processingOfSoldProducts',
'11_useOfSoldProducts',
'12_endOfLifeTreatmentOfSoldProducts',
'13_downstreamLeasedAssets',
'14_franchises',
'15_investments',
'16_other',
]

for (const category of validCategories) {
if (!isDouble(categories[category])) return false;
if (!isDouble(categories[category])) return false
}
}
return true;
return true
}


public hashPdf(pdfBuffer: Buffer): string {
return crypto.createHash('sha256').update(pdfBuffer).digest('hex')
Expand Down Expand Up @@ -240,17 +262,17 @@ class Elastic {

async indexReport(pdfHash: string, reportData: any, url: string) {
try {
let parsed;
let parsed
if (typeof reportData === 'string') {
console.log("Parsing report data");
parsed = JSON.parse(reportData);
console.log('Parsing report data')
parsed = JSON.parse(reportData)
} else if (typeof reportData === 'object') {
console.log("Report data is already parsed");
parsed = reportData;
console.log('Report data is already parsed')
parsed = reportData
} else {
throw new Error("reportData is neither a string nor an object");
throw new Error('reportData is neither a string nor an object')
}

// Convert from array to object for easier access in elastic
const emissions = parsed.emissions.reduce((acc, curr) => {
acc[curr.year] = curr
Expand Down
40 changes: 2 additions & 38 deletions src/lib/discordTable.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
import { Table } from 'embed-table'
import { EmbedBuilder } from 'discord.js'
import { CompanyData } from '../models/companyEmissions'

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)
Expand Down Expand Up @@ -77,7 +41,7 @@ export const summaryTable = async (company: CompanyData) => {
],
]

return table.map((t) => t.join(' ')).join('\n> ')
return table.map((t) => t.join(' ')).join('\n ')
}

export const scope3Table = async (company: CompanyData) => {
Expand Down
37 changes: 37 additions & 0 deletions src/models/companyEmissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export type YearEmissions = {
year: number
scope1: {
emissions: number
unit: string
baseYear: string
}
scope2: {
emissions: string
unit: number
mb: string
lb: string
baseYear: string
}
scope3: {
emissions: string
unit: number
baseYear: string
categories: {
[key: string]: number
}
}
totalEmissions?: string
totalUnit?: string
}

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

0 comments on commit 438b85d

Please # to comment.