Metrics.purs 4.69 KB
Newer Older
1
module Gargantext.Components.Nodes.Corpus.Chart.Metrics where
2

3
import Data.Generic.Rep (class Generic)
4
import Data.Either (Either)
5
import Data.Eq.Generic (genericEq)
6 7
import Data.Map as Map
import Data.Map (Map)
8
import Data.Maybe (Maybe(..))
9
import Data.Newtype (class Newtype)
10
import Data.Tuple (Tuple(..))
11
import Data.Tuple.Nested ((/\))
12
import Effect.Aff (Aff)
13
import Reactix as R
14
import Reactix.DOM.HTML as H
15
import Simple.JSON as JSON
16
import Toestand as T
17

18 19
import Gargantext.Prelude (class Eq, bind, negate, pure, ($), (<$>), (<>))

20 21 22 23 24 25
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, yAxis')
import Gargantext.Components.Charts.Options.Type (xAxis)
import Gargantext.Components.Charts.Options.Series (Series, seriesScatterD2)
import Gargantext.Components.Charts.Options.Color (green, grey, red)
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Data (dataSerie)
26
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
27
import Gargantext.Components.Nodes.Corpus.Chart.Types
28
  (MetricsProps, Path, Props, ReloadPath)
29
import Gargantext.Config.REST (RESTError)
30
import Gargantext.Hooks.Loader (HashedResponse(..))
31
import Gargantext.Routes (SessionRoute(..))
32
import Gargantext.Sessions (Session, get)
33
import Gargantext.Types (TermList(..))
34
import Gargantext.Utils.CacheAPI as GUC
35
import Gargantext.Utils.Reactix as R2
36
import Gargantext.Utils.Toestand as T2
37

38
here :: R2.Here
39
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Metrics"
40 41 42 43 44 45 46

newtype Metric = Metric
  { label :: String
  , x     :: Number
  , y     :: Number
  , cat   :: TermList
  }
47
derive instance Generic Metric _
48 49 50 51
derive instance Newtype Metric _
instance Eq Metric where eq = genericEq
derive newtype instance JSON.ReadForeign Metric
derive newtype instance JSON.WriteForeign Metric
52

53 54
newtype Metrics = Metrics {
     "data" :: Array Metric
55
  }
56 57 58
derive instance Generic Metrics _
derive instance Newtype Metrics _
derive newtype instance JSON.ReadForeign Metrics
59 60 61

type Loaded  = Array Metric

62 63
scatterOptions :: Record MetricsProps -> Array Metric -> Options
scatterOptions { onClick, onInit } metrics' = Options
64
  { mainTitle : "Ngrams Selection Metrics"
65
  , subTitle  : "Local metrics (Inc/Exc, Spe/Gen), Global metrics (TFICF maillage)"
66 67
  , xAxis     : xAxis { min: -1 }
  , yAxis     : yAxis' { position : "", show: true, min : -2}
68
  , series    : map2series $ metric2map metrics'
69
  , addZoom   : false
70
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
71 72
  , onClick
  , onInit
73 74
  }
  where
75 76
    metric2map :: Array Metric -> Map TermList (Array Metric)
    metric2map ds = Map.fromFoldableWith (<>) $ (\(Metric m) -> Tuple m.cat [Metric m]) <$> ds
77 78

    --{-
79
    map2series :: Map TermList (Array Metric) -> Array Series
80
    map2series ms = toSeries <$> Map.toUnfoldable ms
81
      where
82
        -- TODO colors are not respected yet
83 84
        toSeries (Tuple k ms') =
            seriesScatterD2 {symbolSize: 5.0} (toSerie color <$> ms')
85 86 87 88
          where
            color =
              case k of
                StopTerm -> red
89
                MapTerm -> green
90
                CandidateTerm -> grey
91 92
            toSerie color' (Metric {label,x,y}) =
              dataSerie { name: label, itemStyle: itemStyle {color: color'}
93 94 95
                     -- , label: {show: true}
                        , value: [x,y]
                        }
96
    --}
97

98
getMetricsHash :: Session -> ReloadPath -> Aff (Either RESTError String)
99 100
getMetricsHash session (_ /\ { corpusId, listId, tabType }) =
  get session $ CorpusMetricsHash { listId, tabType } (Just corpusId)
101

102 103 104 105 106 107 108
chartUrl :: Record Path -> SessionRoute
chartUrl { corpusId, limit, listId, tabType } = CorpusMetrics { limit, listId, tabType } (Just corpusId)

handleResponse :: HashedResponse Metrics -> Loaded
handleResponse (HashedResponse { value: Metrics ms }) = ms."data"

mkRequest :: Session -> ReloadPath -> GUC.Request
109
mkRequest session (_ /\ path) = GUC.makeGetRequest session $ chartUrl path
110

111
metrics :: Record Props -> R.Element
112
metrics props = R.createElement metricsCpt props []
113
metricsCpt :: R.Component Props
114
metricsCpt = here.component "etrics" cpt
115
  where
116
    cpt { boxes, onClick, onInit, path, session } _ = do
117 118
      reload <- T.useBox T2.newReload

119
      pure $ metricsWithCacheLoadView {
120
          boxes
121
        , getMetricsHash
122 123 124 125 126 127
        , handleResponse
        , loaded
        , mkRequest: mkRequest session
        , path
        , reload
        , session
128 129
        , onClick
        , onInit
130 131 132 133
        }


loaded :: Record MetricsProps -> Loaded -> R.Element
134
loaded p loaded' =
135
  H.div {} [
136
  {-  U.reloadButton reload
137
  , U.chartUpdateButton { chartType: Scatter, path, reload, session }
138
  , -} chart $ scatterOptions p loaded'
139
  ]