Skip to content

Commit

Permalink
Merge pull request #459 from Klimatbyran/staging
Browse files Browse the repository at this point in the history
Update production
  • Loading branch information
Greenheart authored Dec 12, 2024
2 parents 629ba66 + 4917aa3 commit 2b84eca
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 23 deletions.
128 changes: 126 additions & 2 deletions scripts/import-spreadsheet-companies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import apiConfig from '../src/config/api'
import { CompanyInput, ReportingPeriodInput } from './import'
import { isMainModule } from './utils'
import { getReportingPeriodDates } from '../src/lib/reportingPeriodDates'
import { readFile, writeFile } from 'fs/promises'
import { resetDB } from '../src/lib/dev-utils'

const workbook = new ExcelJS.Workbook()
await workbook.xlsx.readFile(resolve('src/data/Company GHG data.xlsx'))
Expand Down Expand Up @@ -344,14 +346,23 @@ function getCompanyData(years: number[]) {

export async function upsertCompanies(companies: CompanyInput[]) {
for (const company of companies) {
const { wikidataId, name, tags, internalComment, reportingPeriods } =
company
const {
wikidataId,
name,
tags,
internalComment,
reportingPeriods,
description,
goals,
initiatives,
} = company

await postJSON(
`${baseURL}/companies`,
{
wikidataId,
name,
description,
tags,
internalComment,
metadata: {
Expand Down Expand Up @@ -415,6 +426,48 @@ export async function upsertCompanies(companies: CompanyInput[]) {
})
}
}

if (goals?.length) {
await postJSON(
`${baseURL}/companies/${wikidataId}/goals`,
{
goals,
metadata: {
...goals[0].metadata,
verifiedBy: undefined,
dataOrigin: undefined,
user: undefined,
},
},
'garbo'
).then(async (res) => {
if (!res.ok) {
const body = await res.text()
console.error(res.status, res.statusText, wikidataId, body)
}
})
}

if (initiatives?.length) {
await postJSON(
`${baseURL}/companies/${wikidataId}/initiatives`,
{
initiatives,
metadata: {
...initiatives[0].metadata,
verifiedBy: undefined,
dataOrigin: undefined,
user: undefined,
},
},
'garbo'
).then(async (res) => {
if (!res.ok) {
const body = await res.text()
console.error(res.status, res.statusText, wikidataId, body)
}
})
}
}
}

Expand All @@ -441,6 +494,77 @@ async function postJSON(
async function main() {
const companies = getCompanyData(range(2015, 2023).reverse())

await resetDB()

const apiCompaniesFile = resolve(
'src/data/2024-12-12-0337-garbo-companies.json'
)

const existing = await readFile(apiCompaniesFile, { encoding: 'utf-8' }).then(
JSON.parse
)

// await writeFile(apiCompaniesFile, JSON.stringify(existing), {
// encoding: 'utf-8',
// })

// process.exit(0)

const uniqueAPI = new Set<string>(existing.map((c) => c.wikidataId))
const uniqueSheets = new Set(companies.map((c) => c.wikidataId))

const existsInAPIButNotInSheets = uniqueAPI.difference(uniqueSheets)

console.log('exists in API but not in sheets')
console.dir(
Array.from(existsInAPIButNotInSheets).map(
(id) => existing.find((c) => c.wikidataId === id).name + ' - ' + id
)
)
console.log('exists in sheets but not in api')
console.dir(
Array.from(uniqueSheets.difference(uniqueAPI)).map(
(id) => companies.find((c) => c.wikidataId === id).name + ' - ' + id
)
)

// ## DÖLJ DESSA från API:et
const HIDDEN_FROM_API = new Set([
'Q22629259', // GARO
'Q37562781', // GARO
'Q489097', // Ernst & Young
'Q10432209', // Prisma Properties
'Q5168854', // Copperstone Resources AB
'Q115167497', // Specialfastigheter
'Q549624', // RISE AB
'Q34', // Swedish Logistic Property AB,

// OLD pages:

'Q8301325', // SJ
'Q112055015', // BONESUPPORT
'Q97858523', // Almi
'Q2438127', // Dynavox
'Q117352880', // BioInvent
'Q115167497', // Specialfastigheter
])

console.log('HIDDEN FROM API')
console.dir(
Array.from(HIDDEN_FROM_API).map(
(id) => existing.find((c) => c.wikidataId === id).name + ' - ' + id
)
)

const REMAINING_UNIQUE_IN_API =
existsInAPIButNotInSheets.difference(HIDDEN_FROM_API)
console.log('REMAINING_UNIQUE_IN_API')
console.dir(
Array.from(REMAINING_UNIQUE_IN_API).map(
(id) => existing.find((c) => c.wikidataId === id).name + ' - ' + id
)
)

console.log('Upserting companies based on spreadsheet data...')
await upsertCompanies(companies)

Expand Down
10 changes: 10 additions & 0 deletions scripts/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ export type DataOrigin = keyof typeof DATA_ORIGIN
export type CompanyInput = {
wikidataId: string
name: string
description?: string
tags?: string[]
internalComment?: string
initiatives?: InitiativeInput[]
goals?: GoalInput[]
reportingPeriods: ReportingPeriodInput[]
}

export type InitiativeInput = {
//
}
export type GoalInput = {
//
}

export type MetadataInput = {
comment?: string
source?: string
Expand Down
1 change: 1 addition & 0 deletions src/data/2024-12-12-0337-garbo-companies.json

Large diffs are not rendered by default.

105 changes: 84 additions & 21 deletions src/routes/readCompanies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ const metadata = {
},
}

// ## DÖLJ DESSA från API:et
const HIDDEN_FROM_API = new Set([
'Q22629259', // GARO
'Q37562781', // GARO
'Q489097', // Ernst & Young
'Q10432209', // Prisma Properties
'Q5168854', // Copperstone Resources AB
'Q115167497', // Specialfastigheter
'Q549624', // RISE AB
'Q34', // Swedish Logistic Property AB,

// OLD pages:

'Q8301325', // SJ
'Q112055015', // BONESUPPORT
'Q97858523', // Almi
'Q2438127', // Dynavox
'Q117352880', // BioInvent
'Q115167497', // Specialfastigheter
])

const unwantedWikidataIds = Array.from(HIDDEN_FROM_API)

function isNumber(n: unknown): n is number {
return Number.isFinite(n)
}
Expand Down Expand Up @@ -183,6 +206,11 @@ router.get(
},
},
},
where: {
wikidataId: {
notIn: unwantedWikidataIds,
},
},
})
res.json(
companies
Expand All @@ -207,13 +235,18 @@ router.get(
(reportingPeriod.emissions?.scope3 && {
...reportingPeriod.emissions.scope3,
calculatedTotalEmissions:
reportingPeriod.emissions.scope3.categories.reduce(
(total, category) =>
isNumber(category.total)
? category.total + total
: total,
0
),
reportingPeriod.emissions.scope3.categories.some((c) =>
Boolean(c.metadata.verifiedBy)
)
? reportingPeriod.emissions.scope3.categories.reduce(
(total, category) =>
isNumber(category.total)
? category.total + total
: total,
0
)
: reportingPeriod.emissions.scope3
.statedTotalEmissions?.total ?? 0,
}) ||
undefined,
},
Expand Down Expand Up @@ -243,9 +276,18 @@ router.get(
emissions: {
...reportingPeriod.emissions,
calculatedTotalEmissions:
(reportingPeriod.emissions?.scope1?.total || 0) +
(reportingPeriod.emissions?.scope2
?.calculatedTotalEmissions || 0) +
// If either scope 1 and scope 2 have verification, then we use them for the total.
// Otherwise, we use the combined scope1And2 if it exists
(Boolean(
reportingPeriod.emissions?.scope1?.metadata?.verifiedBy
) ||
Boolean(
reportingPeriod.emissions?.scope2?.metadata?.verifiedBy
)
? (reportingPeriod.emissions?.scope1?.total || 0) +
(reportingPeriod.emissions?.scope2
?.calculatedTotalEmissions || 0)
: reportingPeriod.emissions?.scope1And2?.total || 0) +
(reportingPeriod.emissions?.scope3
?.calculatedTotalEmissions || 0),
},
Expand All @@ -272,7 +314,14 @@ router.get(
try {
const { wikidataId } = req.params
const company = await prisma.company.findFirst({
where: { wikidataId },
where: {
wikidataId,
AND: {
wikidataId: {
notIn: unwantedWikidataIds,
},
},
},
select: {
wikidataId: true,
name: true,
Expand Down Expand Up @@ -438,13 +487,18 @@ router.get(
(reportingPeriod.emissions?.scope3 && {
...reportingPeriod.emissions.scope3,
calculatedTotalEmissions:
reportingPeriod.emissions.scope3.categories.reduce(
(total, category) =>
isNumber(category.total)
? category.total + total
: total,
0
),
reportingPeriod.emissions.scope3.categories.some((c) =>
Boolean(c.metadata.verifiedBy)
)
? reportingPeriod.emissions.scope3.categories.reduce(
(total, category) =>
isNumber(category.total)
? category.total + total
: total,
0
)
: reportingPeriod.emissions.scope3
.statedTotalEmissions?.total ?? 0,
}) ||
undefined,
},
Expand Down Expand Up @@ -474,9 +528,18 @@ router.get(
emissions: {
...reportingPeriod.emissions,
calculatedTotalEmissions:
(reportingPeriod.emissions?.scope1?.total || 0) +
(reportingPeriod.emissions?.scope2
?.calculatedTotalEmissions || 0) +
// if either scope 1 and scope 2 have verification, then we use them for the total.
// Otherwise, we use the combined scope1And2 if it exists
(Boolean(
reportingPeriod.emissions?.scope1?.metadata?.verifiedBy
) ||
Boolean(
reportingPeriod.emissions?.scope2?.metadata?.verifiedBy
)
? (reportingPeriod.emissions?.scope1?.total || 0) +
(reportingPeriod.emissions?.scope2
?.calculatedTotalEmissions || 0)
: reportingPeriod.emissions?.scope1And2?.total || 0) +
(reportingPeriod.emissions?.scope3
?.calculatedTotalEmissions || 0),
},
Expand Down

0 comments on commit 2b84eca

Please sign in to comment.