-
Notifications
You must be signed in to change notification settings - Fork 20
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
[631] WIP: Automatic Data Export to Wikidata/Wikipedia #654
Open
moritzzingg-netlight
wants to merge
5
commits into
Klimatbyran:main
Choose a base branch
from
okis-netlight:feat/automatic-data-export-to--wikidata-wikipedia
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a702a48
bb71157
Added functions and config to interact with wikidata
moritzzingg-netlight fa0b027
Merge pull request #2 from okis-netlight/feat/implement-methods-for-a…
moritzzingg-netlight c29a984
Added worker for wikidata upload
moritzzingg-netlight a2a4624
Merge pull request #3 from okis-netlight/655-create-worker-to-execute…
moritzzingg-netlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
import wikidataConfig from "../src/config/wikidata"; | ||
import { bulkCreateOrEditCarbonFootprintClaim, Claim } from '../src/lib/wikidata'; | ||
|
||
//Currently still in testing the filters filter out only data related to ABB as this company is present in the Sandbox | ||
const entityDownloadId: `Q${number}` = "Q731938"; | ||
const entityUploadId: `Q${number}` = "Q238689"; | ||
|
||
const prisma = new PrismaClient() | ||
|
||
const emissionsScope1 = await prisma.scope1.findMany({ | ||
select: { | ||
total: true, | ||
unit: true, | ||
metadata: { | ||
select: { | ||
verifiedByUserId: true | ||
} | ||
}, | ||
emissions: { | ||
select: { | ||
reportingPeriod: { | ||
select: { | ||
startDate: true, | ||
endDate: true, | ||
reportURL: true, | ||
company: { | ||
select: { | ||
wikidataId: true, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
where: { | ||
metadata: {some: {verifiedByUserId: {not: null}}}, | ||
emissions: {reportingPeriod: {year: "2023"}} | ||
} | ||
}) | ||
let claims: Claim[] = []; | ||
let filtered1 = emissionsScope1.filter((emission) => emission.emissions?.reportingPeriod?.company.wikidataId === entityDownloadId); | ||
|
||
for(const emission of filtered1) { | ||
if(emission.total !== null && emission.unit !== null && emission.emissions !== null | ||
&& emission.emissions!.reportingPeriod !== null && emission.emissions!.reportingPeriod!.startDate !== null | ||
&& emission.emissions!.reportingPeriod!.endDate !== null && emission.emissions!.reportingPeriod!.reportURL !== null | ||
&& emission.emissions!.reportingPeriod!.company !== null && emission.emissions!.reportingPeriod!.company!.wikidataId !== null) { | ||
|
||
claims.push({ | ||
startDate: emission.emissions!.reportingPeriod!.startDate.toISOString(), | ||
endDate: emission.emissions!.reportingPeriod!.endDate.toISOString(), | ||
value: emission.total!.toString(), | ||
referenceUrl: emission.emissions!.reportingPeriod!.reportURL!, | ||
scope: wikidataConfig.entities.SCOPE_1 | ||
}) | ||
} | ||
} | ||
|
||
|
||
const emissionsScope2 = await prisma.scope2.findMany({ | ||
select: { | ||
mb: true, | ||
lb: true, | ||
unknown: true, | ||
unit: true, | ||
metadata: { | ||
select: { | ||
verifiedByUserId: true | ||
} | ||
}, | ||
emissions: { | ||
select: { | ||
reportingPeriod: { | ||
select: { | ||
startDate: true, | ||
endDate: true, | ||
reportURL: true, | ||
company: { | ||
select: { | ||
wikidataId: true, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
where: { | ||
metadata: {some: {verifiedByUserId: {not: null}}}, | ||
emissions: {reportingPeriod: {year: "2023"}} | ||
|
||
} | ||
}) | ||
|
||
let filtered2 = emissionsScope2.filter((emission) => emission.emissions?.reportingPeriod?.company.wikidataId === entityDownloadId); | ||
|
||
for(const emission of filtered2) { | ||
if(emission.unit !== null && emission.emissions !== null | ||
&& emission.emissions!.reportingPeriod !== null && emission.emissions!.reportingPeriod!.startDate !== null | ||
&& emission.emissions!.reportingPeriod!.endDate !== null && emission.emissions!.reportingPeriod!.reportURL !== null | ||
&& emission.emissions!.reportingPeriod!.company !== null && emission.emissions!.reportingPeriod!.company!.wikidataId !== null) { | ||
|
||
if(emission.mb !== null) { | ||
claims.push({ | ||
startDate: emission.emissions!.reportingPeriod!.startDate.toISOString(), | ||
endDate: emission.emissions!.reportingPeriod!.endDate.toISOString(), | ||
value: emission.mb!.toString(), | ||
referenceUrl: emission.emissions!.reportingPeriod!.reportURL!, | ||
scope: wikidataConfig.entities.SCOPE_2_MARKET_BASED | ||
}) | ||
} | ||
|
||
if(emission.lb !== null) { | ||
claims.push({ | ||
startDate: emission.emissions!.reportingPeriod!.startDate.toISOString(), | ||
endDate: emission.emissions!.reportingPeriod!.endDate.toISOString(), | ||
value: emission.lb!.toString(), | ||
referenceUrl: emission.emissions!.reportingPeriod!.reportURL!, | ||
scope: wikidataConfig.entities.SCOPE_2_LOCATION_BASED | ||
}) | ||
} | ||
|
||
if(emission.unknown !== null) { | ||
claims.push({ | ||
startDate: emission.emissions!.reportingPeriod!.startDate.toISOString(), | ||
endDate: emission.emissions!.reportingPeriod!.endDate.toISOString(), | ||
value: emission.unknown!.toString(), | ||
referenceUrl: emission.emissions!.reportingPeriod!.reportURL!, | ||
scope: wikidataConfig.entities.SCOPE_2 | ||
}) | ||
} | ||
} | ||
} | ||
|
||
const emissionsScope3 = await prisma.scope3Category.findMany({ | ||
select: { | ||
total: true, | ||
unit: true, | ||
category: true, | ||
metadata: { | ||
select: { | ||
verifiedByUserId: true | ||
} | ||
}, | ||
scope3: { | ||
select: { | ||
emissions: { | ||
select: { | ||
reportingPeriod: { | ||
select: { | ||
startDate: true, | ||
endDate: true, | ||
reportURL: true, | ||
company: { | ||
select: { | ||
wikidataId: true, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
}, | ||
where: { | ||
metadata: {some: {verifiedByUserId: {not: null}}}, | ||
scope3: {emissions: {reportingPeriod: {year: "2023"}}} | ||
} | ||
}) | ||
|
||
let filtered3 = emissionsScope3.filter((emission) => emission.scope3.emissions?.reportingPeriod?.company.wikidataId === entityDownloadId); | ||
|
||
for(const emission of filtered3) { | ||
if(emission.unit !== null && emission.scope3 !== null && emission.scope3.emissions !== null | ||
&& emission.scope3.emissions!.reportingPeriod !== null && emission.scope3.emissions!.reportingPeriod!.startDate !== null | ||
&& emission.scope3.emissions!.reportingPeriod!.endDate !== null && emission.scope3.emissions!.reportingPeriod!.reportURL !== null | ||
&& emission.scope3.emissions!.reportingPeriod!.company !== null && emission.scope3.emissions!.reportingPeriod!.company!.wikidataId !== null) { | ||
|
||
if(emission.category !== 16) { | ||
claims.push({ | ||
startDate: emission.scope3!.emissions!.reportingPeriod!.startDate.toISOString(), | ||
endDate: emission.scope3!.emissions!.reportingPeriod!.endDate.toISOString(), | ||
value: emission.total!.toString(), | ||
referenceUrl: emission.scope3!.emissions!.reportingPeriod!.reportURL!, | ||
scope: wikidataConfig.entities.SCOPE_3, | ||
category: wikidataConfig.translateIdToCategory(emission.category) | ||
}) | ||
} | ||
} | ||
} | ||
await bulkCreateOrEditCarbonFootprintClaim(entityUploadId, claims); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This script is currently for testing and for the optional task to push existing data into wikidata without the garbo pipepline. I think it make sense to keep it for now, if such tasks arise and also some kind of knowledge on how to optionally transform our database model to the wikidata model.