Skip to content

Commit

Permalink
feat: add object explorer pages. (#803)
Browse files Browse the repository at this point in the history
Add explorer pages for browsing available examples and processes.
  • Loading branch information
n1k0 authored Oct 17, 2024
1 parent 91c67a8 commit 0402b55
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 41 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ENCRYPTION_KEY=please-change-this-with-32-chars
EMAIL_HOST_PASSWORD=please-change-this
EMAIL_HOST_USER=please-change-this
ENABLE_FOOD_SECTION=True
ENABLE_OBJECT_SECTION=True
MATOMO_HOST=stats.beta.gouv.fr
MATOMO_SITE_ID=57
MATOMO_TOKEN=xxx
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Les variables d'environnement suivantes doivent être définies :
- `EMAIL_HOST_USER`: l'utilisateur du compte SMTP
- `EMAIL_HOST_PASSWORD` : le mot de passe du compte SMTP pour envoyer les mail liés à l'authentification
- `ENABLE_FOOD_SECTION` : affichage ou non de la section expérimentale dédiée à l'alimentaire (valeur `True` ou `False`, par défault `False`)
- `ENABLE_OBJECT_SECTION` : affichage ou non de la section expérimentale dédiée aux objets génériques (valeur `True` ou `False`, par défault `False`)
- `MATOMO_HOST`: le domaine de l'instance Matomo permettant le suivi d'audience du produit (typiquement `stats.beta.gouv.fr`).
- `MATOMO_SITE_ID`: l'identifiant du site Ecobalyse sur l'instance Matomo permettant le suivi d'audience du produit.
- `MATOMO_TOKEN`: le token Matomo permettant le suivi d'audience du produit.
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const app = Elm.Main.init({
flags: {
clientUrl: location.origin + location.pathname,
enableFoodSection: process.env.ENABLE_FOOD_SECTION === "True",
enableObjectSection: process.env.ENABLE_OBJECT_SECTION === "True",
rawStore: localStorage[storeKey] || "null",
matomo: {
host: process.env.MATOMO_HOST || "",
Expand Down
54 changes: 53 additions & 1 deletion src/Data/Dataset.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Data.Country as Country
import Data.Food.Ingredient as Ingredient
import Data.Food.Process as FoodProcess
import Data.Impact.Definition as Definition
import Data.Object.Process as ObjectProcess
import Data.Scope as Scope exposing (Scope)
import Data.Textile.Material as Material
import Data.Textile.Process as Process
Expand All @@ -33,6 +34,8 @@ type Dataset
| FoodIngredients (Maybe Ingredient.Id)
| FoodProcesses (Maybe FoodProcess.Identifier)
| Impacts (Maybe Definition.Trigram)
| ObjectExamples (Maybe Uuid)
| ObjectProcesses (Maybe ObjectProcess.Id)
| TextileExamples (Maybe Uuid)
| TextileMaterials (Maybe Material.Id)
| TextileProcesses (Maybe Process.Uuid)
Expand All @@ -52,7 +55,8 @@ datasets scope =

Scope.Object ->
[ Impacts Nothing
, Countries Nothing
, ObjectExamples Nothing
, ObjectProcesses Nothing
]

Scope.Textile ->
Expand Down Expand Up @@ -86,6 +90,12 @@ fromSlug string =
"materials" ->
TextileMaterials Nothing

"object-examples" ->
ObjectExamples Nothing

"object-processes" ->
ObjectProcesses Nothing

"processes" ->
TextileProcesses Nothing

Expand Down Expand Up @@ -114,6 +124,12 @@ isDetailed dataset =
Impacts (Just _) ->
True

ObjectExamples (Just _) ->
True

ObjectProcesses (Just _) ->
True

TextileExamples (Just _) ->
True

Expand Down Expand Up @@ -158,6 +174,12 @@ reset dataset =
Impacts _ ->
Impacts Nothing

ObjectExamples _ ->
ObjectExamples Nothing

ObjectProcesses _ ->
ObjectProcesses Nothing

TextileExamples _ ->
TextileExamples Nothing

Expand Down Expand Up @@ -192,6 +214,12 @@ same a b =
( TextileExamples _, TextileExamples _ ) ->
True

( ObjectExamples _, ObjectExamples _ ) ->
True

( ObjectProcesses _, ObjectProcesses _ ) ->
True

( TextileMaterials _, TextileMaterials _ ) ->
True

Expand Down Expand Up @@ -223,6 +251,12 @@ setIdFromString idString dataset =
Impacts _ ->
Impacts (Definition.toTrigram idString |> Result.toMaybe)

ObjectExamples _ ->
ObjectExamples (Uuid.fromString idString)

ObjectProcesses _ ->
ObjectProcesses (ObjectProcess.idFromString idString)

TextileExamples _ ->
TextileExamples (Uuid.fromString idString)

Expand Down Expand Up @@ -259,6 +293,12 @@ strings dataset =
Impacts _ ->
{ label = "Impacts", slug = "impacts" }

ObjectExamples _ ->
{ label = "Exemples", slug = "object-examples" }

ObjectProcesses _ ->
{ label = "Procédés", slug = "object-processes" }

TextileExamples _ ->
{ label = "Exemples", slug = "textile-examples" }

Expand Down Expand Up @@ -305,6 +345,18 @@ toRoutePath dataset =
Impacts Nothing ->
[ slug dataset ]

ObjectExamples (Just id) ->
[ slug dataset, Uuid.toString id ]

ObjectExamples Nothing ->
[ slug dataset ]

ObjectProcesses (Just id) ->
[ slug dataset, ObjectProcess.idToString id ]

ObjectProcesses Nothing ->
[ slug dataset ]

TextileExamples (Just id) ->
[ slug dataset, Uuid.toString id ]

Expand Down
9 changes: 8 additions & 1 deletion src/Data/Object/Process.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Data.Object.Process exposing
( Id
( Id(..)
, Process
, decodeId
, decodeList
, encode
, encodeId
, findById
, idFromString
, idToString
)

import Data.Impact as Impact exposing (Impacts)
Expand Down Expand Up @@ -81,6 +83,11 @@ findById processes id =
|> Result.fromMaybe ("Procédé introuvable par id : " ++ idToString id)


idFromString : String -> Maybe Id
idFromString =
Uuid.fromString >> Maybe.map Id


idToString : Id -> String
idToString (Id uuid) =
Uuid.toString uuid
1 change: 1 addition & 0 deletions src/Data/Session.elm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type alias Session =
, currentVersion : Version
, db : Db
, enableFoodSection : Bool
, enableObjectSection : Bool
, matomo : { host : String, siteId : String }
, navKey : Nav.Key
, notifications : List Notification
Expand Down
2 changes: 2 additions & 0 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Views.Page as Page
type alias Flags =
{ clientUrl : String
, enableFoodSection : Bool
, enableObjectSection : Bool
, matomo : { host : String, siteId : String }
, rawStore : String
}
Expand Down Expand Up @@ -145,6 +146,7 @@ setupSession navKey flags db =
, currentVersion = Request.Version.Unknown
, db = db
, enableFoodSection = flags.enableFoodSection
, enableObjectSection = flags.enableObjectSection
, matomo = flags.matomo
, navKey = navKey
, notifications = []
Expand Down
114 changes: 102 additions & 12 deletions src/Page/Explore.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import Data.Food.Recipe as Recipe
import Data.Impact as Impact
import Data.Impact.Definition as Definition exposing (Definition, Definitions)
import Data.Key as Key
import Data.Object.Process as ObjectProcess
import Data.Object.Query as ObjectQuery
import Data.Object.Simulator as ObjectSimulator
import Data.Scope as Scope exposing (Scope)
import Data.Session exposing (Session)
import Data.Textile.Material as Material exposing (Material)
Expand All @@ -39,6 +42,8 @@ import Page.Explore.FoodExamples as FoodExamples
import Page.Explore.FoodIngredients as FoodIngredients
import Page.Explore.FoodProcesses as FoodProcesses
import Page.Explore.Impacts as ExploreImpacts
import Page.Explore.ObjectExamples as ObjectExamples
import Page.Explore.ObjectProcesses as ObjectProcesses
import Page.Explore.Table as Table
import Page.Explore.TextileExamples as TextileExamples
import Page.Explore.TextileMaterials as TextileMaterials
Expand Down Expand Up @@ -87,6 +92,12 @@ init scope dataset session =
Dataset.Impacts _ ->
"Code"

Dataset.ObjectExamples _ ->
"Coût Environnemental"

Dataset.ObjectProcesses _ ->
"Identifiant"

Dataset.TextileExamples _ ->
"Coût Environnemental"

Expand Down Expand Up @@ -148,9 +159,8 @@ update session msg model =
Scope.Food ->
Dataset.FoodExamples Nothing

-- FIXME: object examples explorer page
Scope.Object ->
Dataset.TextileExamples Nothing
Dataset.ObjectExamples Nothing

Scope.Textile ->
Dataset.TextileExamples Nothing
Expand Down Expand Up @@ -185,10 +195,14 @@ datasetsMenuView { scope, dataset } =


scopesMenuView : Session -> Model -> Html Msg
scopesMenuView { enableFoodSection } model =
[ Scope.Food, Scope.Textile ]
scopesMenuView { enableFoodSection, enableObjectSection } model =
[ ( Scope.Food, enableFoodSection )
, ( Scope.Object, enableObjectSection )
, ( Scope.Textile, True )
]
|> List.filter Tuple.second
|> List.map
(\scope ->
(\( scope, _ ) ->
label []
[ input
[ class "form-check-input ms-1 ms-sm-3 me-1"
Expand All @@ -202,13 +216,7 @@ scopesMenuView { enableFoodSection } model =
]
)
|> (::) (strong [ class "d-block d-sm-inline" ] [ text "Secteur d'activité" ])
|> nav
(if enableFoodSection then
[]

else
[ class "d-none" ]
)
|> nav []


detailsModal : Html Msg -> Html Msg
Expand Down Expand Up @@ -401,6 +409,74 @@ foodProcessesExplorer { food } tableConfig tableState maybeId =
]


objectExamplesExplorer :
Db
-> Table.Config ( Example ObjectQuery.Query, { score : Float } ) Msg
-> SortableTable.State
-> Maybe Uuid
-> List (Html Msg)
objectExamplesExplorer db tableConfig tableState maybeId =
let
scoredExamples =
db.object.examples
|> List.map (\example -> ( example, { score = getObjectScore db example } ))
|> List.sortBy (Tuple.first >> .name)

max =
{ maxScore =
scoredExamples
|> List.map (Tuple.second >> .score)
|> List.maximum
|> Maybe.withDefault 0
}
in
[ scoredExamples
|> List.filter (Tuple.first >> .query >> (/=) ObjectQuery.default)
|> List.sortBy (Tuple.first >> .name)
|> Table.viewList OpenDetail tableConfig tableState Scope.Object (ObjectExamples.table max)
, case maybeId of
Just id ->
detailsModal
(case Example.findByUuid id db.object.examples of
Err error ->
alert error

Ok example ->
( example, { score = getObjectScore db example } )
|> Table.viewDetails Scope.Object (ObjectExamples.table max)
)

Nothing ->
text ""
]


objectProcessesExplorer :
Db
-> Table.Config ObjectProcess.Process Msg
-> SortableTable.State
-> Maybe ObjectProcess.Id
-> List (Html Msg)
objectProcessesExplorer { object } tableConfig tableState maybeId =
[ object.processes
|> Table.viewList OpenDetail tableConfig tableState Scope.Object ObjectProcesses.table
, case maybeId of
Just id ->
detailsModal
(case ObjectProcess.findById object.processes id of
Err error ->
alert error

Ok process ->
process
|> Table.viewDetails Scope.Object ObjectProcesses.table
)

Nothing ->
text ""
]


textileExamplesExplorer :
Db
-> Table.Config ( Example TextileQuery.Query, { score : Float, per100g : Float } ) Msg
Expand Down Expand Up @@ -566,6 +642,14 @@ getFoodScorePer100g db =
>> Result.withDefault 0


getObjectScore : Db -> Example ObjectQuery.Query -> Float
getObjectScore db =
.query
>> ObjectSimulator.compute db
>> Result.map (ObjectSimulator.extractImpacts >> Impact.getImpact Definition.Ecs >> Unit.impactToFloat)
>> Result.withDefault 0


getTextileScore : Db -> Example TextileQuery.Query -> Float
getTextileScore db =
.query
Expand Down Expand Up @@ -619,6 +703,12 @@ explore { db } { scope, dataset, tableState } =
Dataset.Impacts maybeTrigram ->
impactsExplorer db.definitions tableConfig tableState scope maybeTrigram

Dataset.ObjectExamples maybeId ->
objectExamplesExplorer db tableConfig tableState maybeId

Dataset.ObjectProcesses maybeId ->
objectProcessesExplorer db tableConfig tableState maybeId

Dataset.TextileExamples maybeId ->
textileExamplesExplorer db tableConfig tableState maybeId

Expand Down
Loading

0 comments on commit 0402b55

Please sign in to comment.