|
3 | 3 | import Papa from 'papaparse'; |
4 | 4 | import Loader from '$lib/components/Loader.svelte'; |
5 | 5 | import { PUBLIC_VERSION } from '$env/static/public'; |
| 6 | + import getDirectusInstance from '$lib/utils/directus'; |
| 7 | + import { readItems } from '@directus/sdk'; |
| 8 | + import { pivot_multikey } from '@/lib/utils/data'; |
| 9 | +
|
6 | 10 |
|
7 | 11 | let rawData; |
8 | 12 | let unit; |
9 | 13 |
|
10 | | - Papa.parse( |
11 | | - 'https://data.klimadashboard.org/' + |
12 | | - PUBLIC_VERSION + |
13 | | - '/emissions/emissions_consumption_based.csv', |
14 | | - { |
15 | | - download: true, |
16 | | - dynamicTyping: true, |
17 | | - header: true, |
18 | | - skipEmptyLines: true, |
19 | | - complete: function (results) { |
20 | | - if (results) { |
21 | | - rawData = results.data; |
22 | | - unit = rawData[0].unit; |
23 | | - } |
24 | | - } |
| 14 | + const getEmissionsData = async function () { |
| 15 | + try{ |
| 16 | + const directus = getDirectusInstance(fetch); |
| 17 | + let data = await directus.request( |
| 18 | + readItems('emissions_data', { |
| 19 | + filter: { |
| 20 | + _and: [ |
| 21 | + { |
| 22 | + country: { _eq: PUBLIC_VERSION.toUpperCase() }, |
| 23 | + source: { _in: ["OLI 2023 (1990-2022)", "manual_consumption"] }, |
| 24 | + category: { _in: ["0", "consumption_based"] }, |
| 25 | + gas: { _eq: "THG" } |
| 26 | + } |
| 27 | + ] |
| 28 | + }, |
| 29 | + sort: "year", |
| 30 | + fields: ["category","gas.name","gas.unit","id","source","type","value","year"], |
| 31 | + limit: -1 |
| 32 | + }) |
| 33 | + ); |
| 34 | +
|
| 35 | + data = data.map((row, i) => ({ |
| 36 | + value: row.value, |
| 37 | + year: row.year, |
| 38 | + unit: row.gas?.name, |
| 39 | + category: row.category === "0" ? "production_based" : row.category, |
| 40 | + })); |
| 41 | + const pivot_table = pivot_multikey(data, ["year", "unit"], "category") |
| 42 | +
|
| 43 | + rawData = pivot_table |
| 44 | + unit = rawData[0].unit; |
| 45 | +
|
| 46 | + } catch (error) { |
| 47 | + console.error('Error fetching emission data:', error); |
25 | 48 | } |
26 | | - ); |
| 49 | + }; |
| 50 | +
|
| 51 | + $: getEmissionsData(); |
27 | 52 |
|
28 | 53 | $: dataset = rawData?.map((entry, i) => { |
29 | 54 | return { |
|
0 commit comments