-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEPDUtils.ts
338 lines (291 loc) · 8.53 KB
/
EPDUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import axios from 'axios'
import type { AxiosInstance } from 'axios'
import { delay } from '@/utils/math'
import { useSettingsStore } from '@/stores/settings'
import { EPDSource } from '@/models/settings'
import type { RevaluData } from '@/models/revaluDataSource'
import { type Product, type Emission, type LifeCycleStageEmission, type Assembly, Source} from '@/models/material'
//import { convertIlcd } from 'epdx'
const MAX_EPD_COUNT = 15
// Ilcd reference to GWP total
const GWP_REF_OBJECT_ID = '6a37f984-a4b3-458a-a20a-64418c145fa2'
interface EPDService {
createApiClient(): AxiosInstance
createListUrl(): string
createEPDUrl(epd: any): string
createListParams(): any
createEPDParams(): any
updatePageIndex(params: any): any
extractEPDData(data: any): Product | null
extractEPDList(data: any): any[]
}
/**
* Class for fetching data from EcoPortal
* TODO: This implementation is not done yet, EPD Information is flaky at best
*/
class EcoPortalService implements EPDService {
createApiClient() {
const settingsStore = useSettingsStore()
return axios.create({
headers: {
Authorization: `Bearer ${settingsStore.keySettings.materialKeys.ecoPortal}`,
},
})
}
createListUrl() {
return '/api/eco/resource/processes'
}
createEPDUrl(epd: any) {
return `/${epd.nodeid}${epd.uuid}`
}
createListParams() {
return {
search: 'true',
distributed: 'true',
virtual: 'true',
metaDataOnly: 'false',
startIndex: '0',
pageSize: MAX_EPD_COUNT,
format: 'json',
}
}
createEPDParams() {
return {
format: 'json',
view: 'extended',
}
}
updatePageIndex(params: any) {
const startIndex = parseInt(params.startIndex) + parseInt(params.pageSize)
return { ...params, startIndex: startIndex.toString() }
}
extractEPDData(data: any) {
return extractILCDData(data)
}
extractEPDList(data: any): any[] {
return data.data;
}
}
/**
* Class for fetching data from Revalu
*/
class RevaluService implements EPDService {
createApiClient() {
const settingsStore = useSettingsStore()
return axios.create({
headers: {
'x-api-key': settingsStore.keySettings.materialKeys.revalu,
},
})
}
// Create list URL, we check if dev or not and switch the URL
createListUrl() {
const apiListUrl = import.meta.env.MODE === 'development'
? '/SpeckleLCA/api/revalu/epds/search'
: 'https://api.revalu.io/epds/search'
return apiListUrl
}
// Create EPD URL, we check if dev or not and switch the URL
createEPDUrl(epd: any) {
const apiEPDUrl = import.meta.env.MODE === 'development'
? `/SpeckleLCA/api/revalu/epds/${epd.id}`
: `https://api.revalu.io/epds/${epd.id}`
return apiEPDUrl
}
createListParams() {
return {
search_term: '',
page_no: 0,
page_size: MAX_EPD_COUNT,
}
}
createEPDParams() {
return {}
}
updatePageIndex(params: any) {
return { ...params, page_no: params.page_no + 1 }
}
extractEPDData(data: any) {
return extractRevaluData(data)
}
extractEPDList(data: any): any[] {
return data.body.search_results
}
}
/**
* Extract ILCD data from the response
* TODO: Implement lcaX conversion already made
* @param data
* @returns
*/
const extractILCDData = (data: any) => {
// Extract metaData
const metaData: Record<string, string> = {}
const classifications =
data?.processInformation?.dataSetInformation?.classificationInformation
?.classification || []
classifications.forEach((classification: any) => {
classification.class.forEach((cls: any) => {
metaData[cls.classId] = cls.value
})
})
// Extract GWP emissions
const lciaResults = data?.LCIAResults?.LCIAResult || []
const gwpResult = lciaResults.find(
(result: any) =>
result.referenceToLCIAMethodDataSet.refObjectId === GWP_REF_OBJECT_ID
)
const emission = {} as Emission
let totalA1A3 = 0
if (gwpResult?.other?.anies) {
gwpResult.other.anies.forEach((entry: any) => {
const module = entry.module?.toLowerCase().replace(/-/g, '') as string
const amount = parseFloat(entry.value)
if (['a1', 'a2', 'a3'].includes(module)) {
totalA1A3 += amount
} else {
emission.gwp = emission.gwp || {} as LifeCycleStageEmission
emission.gwp[module] = { amount }
}
})
if (totalA1A3 > 0) {
emission.gwp = emission.gwp || {} as LifeCycleStageEmission
emission.gwp['a1a3'] = totalA1A3
}
}
// Extract unit and quantity
const exchange = data?.exchanges?.exchange?.[0]
const flowProperties = exchange?.flowProperties || []
const referenceProperty = flowProperties.find(
(prop: any) => prop.referenceUnit
)
const unit = referenceProperty?.referenceUnit || ''
const quantity = parseFloat(referenceProperty?.meanValue || '0')
// Build the product object
const product: Product = {
id: data.processInformation.dataSetInformation.UUID,
name:
data.processInformation.dataSetInformation.name.baseName[0]?.value ||
'',
description:
data.processInformation.technology
.technologyDescriptionAndIncludedProcesses[0]?.value || '',
referenceServiceLife: 50,
impactData: null,
quantity,
unit,
transport: null,
results: null,
metaData,
emission,
source: Source.ECOPortal,
}
return product
}
/**
* Extracts data from Revalu and their different data structure
* @param data
* @returns
*/
const extractRevaluData = (response: { body: RevaluData }) => {
const data = response.body
const emission: Emission = {
gwp: data.gwp,
gwp_fossil: data.gwp_fossil,
//gwp_biogenic: data.gwp_biogenic,
//gwp_luluc: data.gwp_luluc,
fw: data.fw,
pert: data.pert,
penrt: data.penrt,
//energy_mix_percentage: data.energy_mix_percentage,
}
const product: Product = {
id: data.id,
name: data.name,
description: data.manufacturer,
referenceServiceLife: 50,
impactData: null,
quantity: 1,
unit: data.declared_unit,
transport: null,
results: null,
metaData: {},
emission,
source: Source.Revalu,
}
return product
}
/**
* Checks projectstore and gets the relevant EPD service
* @returns
*/
function getEPDService(): EPDService {
const settingsStore = useSettingsStore()
switch (settingsStore.materialSettings.epdSource) {
case EPDSource.EcoPortal:
return new EcoPortalService()
case EPDSource.Revalu:
return new RevaluService()
default:
throw new Error('Unsupported EPD source')
}
}
/**
* Get all EPDs from the ECO Portal
* Acording to parameters, check API documentation for more information
* @param parameters Key value pairs for the API call
* @returns List of products with emission data
*/
export async function getEPDList(parameters: { [key: string]: string | string[] } = {}): Promise<Product[]> {
const epdService = getEPDService()
const EPDList: Product[] = []
const apiClient = epdService.createApiClient()
const baseUrl = epdService.createListUrl()
let params = epdService.createListParams()
params = { ...params, ...parameters }
await delay(1000)
while (EPDList.length < MAX_EPD_COUNT) {
try {
const response = await apiClient.get(baseUrl, { params })
const data = response.data
const epdListData = epdService.extractEPDList(data)
if (!epdListData || epdListData.length === 0) {
console.log('No more data to fetch')
break
}
for (const epd of epdListData) {
const product = await getSpecificEPD(epd)
if (product) {
EPDList.push(product)
}
}
params = epdService.updatePageIndex(params)
} catch (error) {
console.error('Error fetching EPD list:', error)
break
}
}
return EPDList
}
/**
* Fetches a specific EPD
* @param epd The EPD data containing uuid and nodeid for ecoportal.
* @returns A Product with EPD data or null if an error occurs.
*/
export async function getSpecificEPD(epd: any): Promise<Product | null> {
const epdService = getEPDService()
const apiClient = epdService.createApiClient()
const url = epdService.createEPDUrl(epd)
const params = epdService.createEPDParams()
try {
const response = await apiClient.get(url, { params })
const data = response.data
return epdService.extractEPDData(data)
} catch (error) {
console.error(`Error fetching EPD ${epd.uuid}:`, error)
return null
}
}
export function isAssembly(val: any): val is Assembly {
return val && typeof val === 'object' && 'products' in val && 'id' in val;
}