Refactors ringo extractor module to simplify code.

master
Abhinav Sarkar 2016-07-06 21:43:15 +05:30
parent d106a19d22
commit 1bf931d697
No known key found for this signature in database
GPG Key ID: 7C9166A6F5465AD5
1 changed files with 39 additions and 29 deletions

View File

@ -18,6 +18,7 @@ import Data.List (nub, nubBy, find)
import Data.Text (Text) import Data.Text (Text)
import Ringo.Types.Internal import Ringo.Types.Internal
import Ringo.Utils
findTable :: TableName -> [Table] -> Maybe Table findTable :: TableName -> [Table] -> Maybe Table
findTable tName = find ((== tName) . tableName) findTable tName = find ((== tName) . tableName)
@ -71,36 +72,45 @@ extractDimensionTables :: Fact -> Reader Env [Table]
extractDimensionTables fact = do extractDimensionTables fact = do
settings <- asks envSettings settings <- asks envSettings
tables <- asks envTables tables <- asks envTables
let table = fromJust . findTable (factTableName fact) $ tables return $ dimTablesFromIds tables fact ++ dimTablesFromVals settings tables fact
return $ dimsFromIds tables ++ dimsFromVals settings (tableColumns table)
where dimTablesFromIds :: [Table] -> Fact -> [Table]
dimsFromIds tables = dimTablesFromIds tables fact =
catMaybes [ findTable factColTargetTable tables catMaybes [ findTable factColTargetTable tables
| FactColumn { factColType = DimId {..} } <- factColumns fact ] | FactColumn { factColType = DimId {..} } <- factColumns fact ]
dimsFromVals Settings {..} tableColumns = dimTablesFromVals :: Settings -> [Table] -> Fact -> [Table]
map (\(dim, cols) -> dimTablesFromVals Settings {..} tables fact =
fact
>>- factColumns
>>> mapMaybe (findDimValColumn . Just)
>>> Map.fromListWith (flip (++))
>>> Map.mapWithKey makeDimColumns
>>> Map.toList
>>> map (uncurry makeDimTable)
where
Table {..} = fromJust . findTable (factTableName fact) $ tables
makeDimTable dim cols =
Table { tableName = settingDimPrefix <> dim Table { tableName = settingDimPrefix <> dim
, tableColumns = , tableColumns =
Column settingDimTableIdColumnName settingDimTableIdColumnType NotNull : cols Column settingDimTableIdColumnName settingDimTableIdColumnType NotNull : cols
, tableConstraints = [ PrimaryKey settingDimTableIdColumnName , tableConstraints = [ PrimaryKey settingDimTableIdColumnName
, UniqueKey (map columnName cols) , UniqueKey (map columnName cols)
] ]
}) }
. Map.toList
. Map.mapWithKey makeDimColumns dim cols = [ col { columnName = dimColumnName dim (columnName col)
(\dim -> map (\col -> col { columnName = dimColumnName dim (columnName col)
, columnNullable = NotNull , columnNullable = NotNull
}) }
. nub) | col <- nub cols
. Map.fromListWith (flip (++)) ]
. mapMaybe (\fcol -> do
findDimValColumn :: Maybe FactColumn -> Maybe (TableName, [Column])
findDimValColumn fcol = do
FactColumn { factColType = DimVal {..}, .. } <- fcol FactColumn { factColType = DimVal {..}, .. } <- fcol
column <- findColumn factColTargetColumn tableColumns column <- findColumn factColTargetColumn tableColumns
return (factColTargetTable, [ column ])) return (factColTargetTable, [column])
. map Just
. factColumns
$ fact
extractAllDimensionTables :: Fact -> Reader Env [(Fact, Table)] extractAllDimensionTables :: Fact -> Reader Env [(Fact, Table)]
extractAllDimensionTables fact = do extractAllDimensionTables fact = do