Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vis: Add double line chart, refactor chart types #17

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/server/lib/EasyBI/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Data.String (IsString (..))
import EasyBI.Server.API (API)
import EasyBI.Server.Cube (Cube)
import EasyBI.Server.Eval (DbConnectionPool, applyFieldModifiers,
evalQueryDebug)
evalQuery)
import EasyBI.Server.State (ServerState (..))
import EasyBI.Server.State qualified as State
import EasyBI.Server.Visualisation (FieldInMode, InOut (..), Visualisation)
Expand Down Expand Up @@ -68,7 +68,7 @@ lkp :: (MonadError ServerError m) => ServerState -> NiceHash TypedQueryExpr -> m
lkp state = lookupFromMaybe (State.findQuery state)

eval :: (MonadFail m, MonadIO m, MonadError ServerError m) => DbConnectionPool -> ServerState -> NiceHash TypedQueryExpr -> [FieldInMode In] -> m [WrappedObject]
eval pool state hsh fields = lkp state hsh >>= failOnError . applyFieldModifiers fields . teQuery >>= liftIO . evalQueryDebug putStrLn pool
eval pool state hsh fields = lkp state hsh >>= failOnError . applyFieldModifiers fields . teQuery >>= liftIO . evalQuery pool

lookupFromMaybe :: (MonadError ServerError m, Show k) => (k -> Maybe v) -> k -> m v
lookupFromMaybe f k = case f k of
Expand Down
28 changes: 17 additions & 11 deletions src/server/lib/EasyBI/Server/Visualisation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module EasyBI.Server.Visualisation
) where

import Codec.Serialise (Serialise (..))
import Control.Lens (Lens', lens, over, view)
import Control.Lens (Lens', lens, over)
import Data.Aeson (FromJSON (..), ToJSON (..), object,
withText, (.=))
import Data.Aeson qualified as JSON
Expand All @@ -47,7 +47,7 @@ import Data.Functor (($>))
import Data.List (sortOn)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Maybe (mapMaybe)
import Data.Ord (Down (..))
import Data.String (IsString (..))
import Data.Text qualified as Text
Expand All @@ -57,15 +57,19 @@ import EasyBI.Util.JSON (SerialiseViaJSON (..), WrappedObject (..),
_WrappedObject, customJsonOptions,
fromValue)
import EasyBI.Util.NiceHash (HasNiceHash (..), NiceHash)
import EasyBI.Vis.Charts (Chart)
import EasyBI.Vis.HVega qualified as HVega
import EasyBI.Vis.Rules (makeChart)
import EasyBI.Vis.Types (Archetype (Misc), Encoding,
Measurement (..), Relation (..),
Score (..), Selections, archetype,
initialSelections, runRule, score)
import EasyBI.Vis.Types (Archetype, Measurement (..),
Relation (..), Selections, archetype,
initialSelections, runRule)
import GHC.Generics (Generic)
import Language.SQL.SimpleSQL.Syntax qualified as Syntax

newtype Score = Score Int
deriving stock (Generic, Show)
deriving newtype (ToJSON, FromJSON, Serialise, Ord, Eq)

{-| Visualisation to be shown on the client
-}
data Visualisation a =
Expand All @@ -80,7 +84,7 @@ data Visualisation a =
-- ^ Archetype of the visualisation
, visFields :: [FieldInMode In]
-- ^ The fields used by this visualisation
, visEncoding :: Encoding (FieldInMode In)
, visEncoding :: Chart (FieldInMode In)
, visQuery :: a
}
deriving stock (Generic, Show)
Expand All @@ -91,8 +95,10 @@ instance HasNiceHash (Visualisation (NiceHash TypedQueryExpr)) where

visualisations :: a -> Selections [] (FieldInMode In) -> [Visualisation a]
visualisations hsh selections =
let addScore x = traverse score (x, x)
mkSel = take 10 . mapMaybe (uncurry (enc hsh)) . sortOn (Down . snd) . mapMaybe addScore . nubOrd . runRule 50 makeChart
let usesAllFields Visualisation{visFields} = length visFields == length selections
addScore x = traverse score (x, x)
score _ = pure (Score 10)
mkSel = take 10 . filter usesAllFields . mapMaybe (uncurry (enc hsh)) . sortOn (Down . snd) . mapMaybe addScore . nubOrd . runRule 50 makeChart
in mconcat (mkSel <$> initialSelections selections)

{-| The fields of a record
Expand Down Expand Up @@ -150,7 +156,7 @@ fields mp = mapMaybe (fmap (uncurry mkField . first getName) . traverse getMeasu
, sortOrder = Ascending
}

enc :: a -> Encoding (FieldInMode In) -> Score -> Maybe (Visualisation a)
enc :: a -> Chart (FieldInMode In) -> Score -> Maybe (Visualisation a)
enc hsh e score_ =
let setData = KM.insert "data" (object ["name" .= s "table"])
. KM.insert "width" (toJSON (s "container"))
Expand All @@ -159,7 +165,7 @@ enc hsh e score_ =
<$> fmap (over _WrappedObject setData) (fromValue (HVega.toJSON e))
<*> pure "FIXME: enc.visDescription"
<*> pure score_
<*> pure (fromMaybe Misc (view archetype e))
<*> pure (archetype e)
<*> pure (toList e)
<*> pure e
<*> pure hsh
Expand Down
1 change: 1 addition & 0 deletions src/server/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ checkVisualisations step = do
{ _WildCards = [category, price]
, _XAxis = []
, _YAxis = []
, _YAxis2 = []
, _Color = []
, _selectedMark = []
, _selectedArchetype = []
Expand Down
Loading