Skip to content

Commit

Permalink
feat: render component transforms (#907)
Browse files Browse the repository at this point in the history
## 🔧 Problem

render the details of applied transform processes to each
component element material
  • Loading branch information
n1k0 authored Jan 23, 2025
1 parent d1bc984 commit fd7108d
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 156 deletions.
22 changes: 22 additions & 0 deletions public/data/object/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,27 @@
],
"id": "190276e9-5b90-42d6-8fbd-bc7ddfd4c960",
"name": "Cadre plastique"
},
{
"elements": [
{
"amount": 1.05761,
"material": "62a4d6fb-3276-4ba5-93a3-889ecd3bff84",
"transforms": [
"9c478d79-ff6b-45e1-9396-c3bd897faa1d",
"da9d1c32-a166-41ab-bac6-f67aff0cf44a"
]
},
{
"amount": 1.05761,
"material": "9dba0e95-0c35-4f8b-9267-62ddf47d4984",
"transforms": [
"9c478d79-ff6b-45e1-9396-c3bd897faa1d",
"ae9cbbad-7982-4f3c-9220-edf27946d347"
]
}
],
"id": "8ca2ca05-8aec-4121-acaa-7cdcc03150a9",
"name": "Tissu pour canapé"
}
]
105 changes: 70 additions & 35 deletions src/Data/Component.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Data.Component exposing
, emptyResults
, encodeId
, encodeItem
, encodeResults
, expandElements
, expandItems
, extractImpacts
Expand All @@ -33,9 +34,11 @@ module Data.Component exposing
)

import Data.Impact as Impact exposing (Impacts)
import Data.Impact.Definition exposing (Trigram)
import Data.Process as Process exposing (Process)
import Data.Scope as Scope exposing (Scope)
import Data.Split as Split
import Data.Unit as Unit
import Data.Uuid as Uuid exposing (Uuid)
import Energy
import Json.Decode as Decode exposing (Decoder)
Expand Down Expand Up @@ -141,7 +144,7 @@ specifying specific country mixes in the future.
-}
applyTransforms : List Process -> List Process -> Results -> Result String Results
applyTransforms allProcesses transforms (Results materialResults) =
applyTransforms allProcesses transforms materialResults =
loadDefaultEnergyMixes allProcesses
|> Result.map
(\{ elec, heat } ->
Expand All @@ -157,23 +160,31 @@ applyTransforms allProcesses transforms (Results materialResults) =

-- Note: impacts are always computed from input mass
transformImpacts =
Impact.sumImpacts
[ transform.impacts |> Impact.multiplyBy (Mass.inKilograms mass)
, elec.impacts
|> Impact.multiplyBy (Energy.inKilowattHours transform.elec)
|> Impact.multiplyBy (Mass.inKilograms mass)
, heat.impacts
|> Impact.multiplyBy (Energy.inKilowattHours transform.heat)
|> Impact.multiplyBy (Mass.inKilograms mass)
]
[ transform.impacts
, elec.impacts
|> Impact.multiplyBy (Energy.inKilowattHours transform.elec)
, heat.impacts
|> Impact.multiplyBy (Energy.inMegajoules transform.heat)
]
|> Impact.sumImpacts
|> Impact.multiplyBy (Mass.inKilograms mass)
in
Results
-- global result
{ impacts = Impact.sumImpacts [ transformImpacts, impacts ]
, items = Results { impacts = transformImpacts, items = [], mass = Quantity.negate wastedMass } :: items
, items =
items
++ [ -- transform result
Results
{ impacts = transformImpacts
, items = []
, mass = outputMass
}
]
, mass = outputMass
}
)
(Results materialResults)
materialResults
)


Expand Down Expand Up @@ -205,34 +216,12 @@ computeElementResults processes =
)


computeMaterialResults : Amount -> Process -> Results
computeMaterialResults amount process =
let
( impacts, mass ) =
( process.impacts
|> Impact.mapImpacts (\_ -> Quantity.multiplyBy (amountToFloat amount))
, Mass.kilograms <|
if process.unit == "kg" then
amountToFloat amount

else
-- apply density
amountToFloat amount * process.density
)
in
Results
{ impacts = impacts
, items = [ Results { impacts = impacts, items = [], mass = mass } ]
, mass = mass
}


computeImpacts : List Process -> Component -> Result String Results
computeImpacts processes =
.elements
>> List.map (computeElementResults processes)
>> RE.combine
>> Result.map (List.foldl addResults emptyResults)
>> Result.map (List.foldr addResults emptyResults)


computeItemResults : DataContainer db -> Item -> Result String Results
Expand All @@ -257,6 +246,33 @@ computeItemResults { components, processes } { id, quantity } =
)


computeMaterialResults : Amount -> Process -> Results
computeMaterialResults amount process =
let
impacts =
process.impacts
|> Impact.multiplyBy (amountToFloat amount)

mass =
Mass.kilograms <|
if process.unit == "kg" then
amountToFloat amount

else
-- apply density
amountToFloat amount * process.density
in
-- global result
Results
{ impacts = impacts
, items =
[ -- material result
Results { impacts = impacts, items = [], mass = mass }
]
, mass = mass
}


decode : List Scope -> Decoder Component
decode scopes =
Decode.succeed Component
Expand Down Expand Up @@ -355,6 +371,25 @@ encodeId =
idToString >> Encode.string


encodeResults : Maybe Trigram -> Results -> Encode.Value
encodeResults maybeTrigram (Results results) =
Encode.object
[ ( "impacts"
, case maybeTrigram of
Just trigram ->
results.impacts
|> Impact.getImpact trigram
|> Unit.impactToFloat
|> Encode.float

Nothing ->
Impact.encode results.impacts
)
, ( "items", Encode.list (encodeResults maybeTrigram) results.items )
, ( "mass", results.mass |> Mass.inKilograms |> Encode.float )
]


{-| Lookup a Component from a provided Id
-}
findById : Id -> List Component -> Result String Component
Expand Down
15 changes: 7 additions & 8 deletions src/Page/Explore/Components.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ table db { detailed, scope } =
scopedProcesses =
db.processes
|> Scope.anyOf [ scope ]

expandElements =
Component.expandElements scopedProcesses
in
{ filename = "components"
, toId = .id >> Component.idToString
Expand All @@ -50,7 +47,7 @@ table db { detailed, scope } =
, toValue =
Table.StringValue <|
\{ elements } ->
case expandElements elements of
case Component.expandElements db.processes elements of
Err _ ->
""

Expand All @@ -66,7 +63,7 @@ table db { detailed, scope } =
|> String.join ", "
, toCell =
\{ elements } ->
case expandElements elements of
case Component.expandElements db.processes elements of
Err err ->
Alert.simple
{ close = Nothing
Expand All @@ -78,19 +75,21 @@ table db { detailed, scope } =
Ok list ->
list
|> List.map
(\{ amount, material } ->
(\{ amount, material, transforms } ->
li []
[ Format.amount material amount
, text <| " de " ++ Process.getDisplayName material
, transforms
|> List.map (\transform -> li [] [ text <| Process.getDisplayName transform ])
|> ul []
]
)
|> List.intersperse (text ", ")
|> ul [ class "m-0 px-2" ]
}
, { label = "Coût environnemental"
, toValue = Table.FloatValue <| getComponentEcoscore scopedProcesses >> Result.withDefault 0
, toCell =
getComponentEcoscore scopedProcesses
getComponentEcoscore db.processes
>> Result.map (Format.formatImpactFloat { decimals = 2, unit = "Pts par composant" })
>> Result.withDefault (text "N/A")
}
Expand Down
Loading

0 comments on commit fd7108d

Please sign in to comment.