Browse Source

Renames Env to Config.

Abhinav Sarkar 4 years ago
parent
commit
f968612f36
No known key found for this signature in database

+ 1
- 0
.gitignore View File

@@ -19,3 +19,4 @@ cabal.sandbox.config
19 19
 *.prof
20 20
 *.aux
21 21
 *.hp
22
+tags

+ 20
- 21
ringo-app/src/Main.hs View File

@@ -27,22 +27,22 @@ main = do
27 27
   case result of
28 28
     Left err                        -> putStrLn err >> exitFailure
29 29
     Right (tables, facts, defaults) ->
30
-      case makeEnv tables facts progSettings defaults of
31
-        Left errors -> mapM_ print errors           >> exitFailure
32
-        Right env   -> writeFiles progOutputDir env >> exitSuccess
30
+      case makeConfig tables facts progSettings defaults of
31
+        Left errors  -> mapM_ print errors              >> exitFailure
32
+        Right config -> writeFiles progOutputDir config >> exitSuccess
33 33
 
34
-writeFiles :: FilePath -> Env -> IO ()
35
-writeFiles outputDir env = do
36
-  let Settings{..} = envSettings env
34
+writeFiles :: FilePath -> Config -> IO ()
35
+writeFiles outputDir config = do
36
+  let Settings{..} = configSettings config
37 37
 
38
-  forM_ (makeSQLs env dimTables factTables) $ \(sqlType, table, sql) -> do
38
+  forM_ (makeSQLs config dimTables factTables) $ \(sqlType, table, sql) -> do
39 39
     let dirName = outputDir </> map toLower (show sqlType)
40 40
     createDirectoryIfMissing True dirName
41 41
     writeFile (dirName </> Text.unpack table <.> "sql") sql
42 42
 
43 43
   BS.writeFile (outputDir </> Text.unpack settingDependenciesJSONFileName)
44 44
     . encode
45
-    . foldl (\acc -> Map.union acc . extractDependencies env) Map.empty
45
+    . foldl (\acc -> Map.union acc . extractDependencies config) Map.empty
46 46
     $ facts
47 47
 
48 48
   BS.writeFile (outputDir </> Text.unpack settingDimensionsJSONFileName) . encode $
@@ -51,31 +51,33 @@ writeFiles outputDir env = do
51 51
   BS.writeFile (outputDir </> Text.unpack settingFactsJSONFileName) . encode $
52 52
     [ tableName table | (_, table) <- factTables ]
53 53
   where
54
-    facts      = envFacts env
55
-    tables     = envTables env
54
+    facts      = configFacts config
55
+    tables     = configTables config
56 56
 
57
-    dimTables  = [ (fact, extractDimensionTables env fact) | fact <- facts ]
58
-    factTables = [ (fact, extractFactTable env fact)       | fact <- facts, factTablePersistent fact ]
57
+    dimTables  = [ (fact, extractDimensionTables config fact) | fact <- facts ]
58
+    factTables = [ (fact, extractFactTable config fact)       | fact <- facts, factTablePersistent fact ]
59
+
60
+makeSQLs :: Config -> [(Fact, [Table])] -> [(Fact, Table)] -> [(SQLType, TableName, String)]
61
+makeSQLs config dimTables factTables = let
62
+    tables = configTables config
59 63
 
60
-makeSQLs :: Env -> [(Fact, [Table])] -> [(Fact, Table)] -> [(SQLType, TableName, String)]
61
-makeSQLs env dimTables factTables = let
62 64
     dimTableDefinitionSQLs =
63
-      [ (Create, tableName table, unlines . map sqlStr $ dimensionTableDefinitionSQL env table)
65
+      [ (Create, tableName table, unlines . map Text.unpack $ dimensionTableDefinitionSQL config table)
64 66
          | (_, tabs) <- dimTables
65 67
          , table     <- tabs
66 68
          , table `notElem` tables ]
67 69
 
68 70
     factTableDefinitionSQLs =
69
-      [ (Create , tableName table, unlines . map sqlStr $ factTableDefinitionSQL env fact table)
71
+      [ (Create , tableName table, unlines . map Text.unpack $ factTableDefinitionSQL config fact table)
70 72
         | (fact, table) <- factTables ]
71 73
 
72 74
     dimTablePopulationSQLs typ gen  =
73
-      [ (typ , tableName table, sqlStr $ gen env fact (tableName table))
75
+      [ (typ , tableName table, Text.unpack $ gen config fact (tableName table))
74 76
         | (fact, tabs) <- dimTables
75 77
         , table        <- tabs
76 78
         , table `notElem` tables ]
77 79
 
78
-    factTablePopulationSQLs typ gen = [ (typ, tableName table, unlines . map sqlStr  $ gen env fact)
80
+    factTablePopulationSQLs typ gen = [ (typ, tableName table, unlines . map Text.unpack $ gen config fact)
79 81
                                         | (fact, table) <- factTables ]
80 82
   in concat [ dimTableDefinitionSQLs
81 83
             , factTableDefinitionSQLs
@@ -84,6 +86,3 @@ makeSQLs env dimTables factTables = let
84 86
             , factTablePopulationSQLs FullRefresh $ factTablePopulationSQL FullPopulation
85 87
             , factTablePopulationSQLs IncRefresh  $ factTablePopulationSQL IncrementalPopulation
86 88
             ]
87
-  where
88
-    tables = envTables env
89
-    sqlStr = Text.unpack

+ 53
- 29
ringo/src/Ringo.hs View File

@@ -5,7 +5,7 @@ module Ringo
5 5
 
6 6
          -- $setup
7 7
          module Ringo.Types
8
-       , makeEnv
8
+       , makeConfig
9 9
        , extractFactTable
10 10
        , extractDimensionTables
11 11
        , extractDependencies
@@ -139,17 +139,17 @@ import qualified Ringo.Validator as V
139 139
 --                                , ("text", "'__UNKNOWN_VAL__'")
140 140
 --                                ]
141 141
 --    settings     = defSettings { settingTableNameSuffixTemplate = "" }
142
+--    config       = case makeConfig tables facts settings typeDefaults of
143
+--                     Left errors  -> error . unlines . map show $ errors
144
+--                     Right config -> config
142 145
 -- :}
143 146
 
144
-makeEnv :: [Table] -> [Fact] -> Settings -> TypeDefaults -> Either [ValidationError] Env
145
-makeEnv = V.validateEnv
147
+makeConfig :: [Table] -> [Fact] -> Settings -> TypeDefaults -> Either [ValidationError] Config
148
+makeConfig = V.validateConfig
146 149
 
147 150
 -- |
148 151
 --
152
+-- >>> print $ extractFactTable config sessionFact
149 153
 -- Table fact_session_by_minute
150 154
 -- Column created_at_minute_id bigint NOT NULL
151 155
 -- Column publisher_id integer NOT NULL
@@ -158,7 +158,7 @@ makeEnv = V.validateEnv
158 158
 -- Column user_agent_id integer NOT NULL
159 159
 -- UniqueKey (created_at_minute_id, publisher_id, geo_id, user_agent_id)
160 160
 -- <BLANKLINE>
161
+-- >>> print $ extractFactTable config pageViewFact
161 162
 -- Table fact_page_view_by_minute
162 163
 -- Column created_at_minute_id bigint NOT NULL
163 164
 -- Column publisher_id integer NOT NULL
@@ -169,12 +169,12 @@ makeEnv = V.validateEnv
169 169
 -- Column user_agent_id integer NOT NULL
170 170
 -- UniqueKey (created_at_minute_id, publisher_id, referrer_id, page_type_id, geo_id, user_agent_id)
171 171
 -- <BLANKLINE>
172
-extractFactTable :: Env -> Fact -> Table
173
-extractFactTable env = flip runReader env . E.extractFactTable
172
+extractFactTable :: Config -> Fact -> Table
173
+extractFactTable config = flip runReader config . E.extractFactTable
174 174
 
175 175
 -- |
176 176
 --
177
+-- >>> mapM_ print $ extractDimensionTables config sessionFact
177 178
 -- Table dim_geo
178 179
 -- Column id serial NOT NULL
179 180
 -- Column country_name character varying(50) NOT NULL
@@ -191,20 +191,20 @@ extractFactTable env = flip runReader env . E.extractFactTable
191 191
 -- PrimaryKey id
192 192
 -- UniqueKey (browser_name, os, name)
193 193
 -- <BLANKLINE>
194
+-- >>> mapM_ print . filter (`notElem` tables) $ extractDimensionTables config pageViewFact
194 195
 -- Table dim_page_type
195 196
 -- Column id serial NOT NULL
196 197
 -- Column page_type character varying(20) NOT NULL
197 198
 -- PrimaryKey id
198 199
 -- UniqueKey (page_type)
199 200
 -- <BLANKLINE>
200
-extractDimensionTables :: Env -> Fact -> [Table]
201
-extractDimensionTables env = flip runReader env . E.extractDimensionTables
201
+extractDimensionTables :: Config -> Fact -> [Table]
202
+extractDimensionTables config = flip runReader config . E.extractDimensionTables
202 203
 
203 204
 -- |
204 205
 --
205 206
 -- >>> let depsToStr = map ((\(k, vs) -> Text.unpack $ k <> ":\n  - " <> Text.intercalate "\n  - " vs)) . Map.toList
207
+-- >>> mapM_ putStrLn . depsToStr $ extractDependencies config sessionFact
206 208
 -- dim_geo:
207 209
 --   - session_events
208 210
 -- dim_user_agent:
@@ -213,7 +213,7 @@ extractDimensionTables env = flip runReader env . E.extractDimensionTables
213 213
 --   - session_events
214 214
 --   - dim_user_agent
215 215
 --   - dim_geo
216
+-- >>> mapM_ putStrLn . depsToStr $ extractDependencies config pageViewFact
216 217
 -- dim_page_type:
217 218
 --   - page_view_events
218 219
 -- fact_page_view_by_minute:
@@ -223,13 +223,13 @@ extractDimensionTables env = flip runReader env . E.extractDimensionTables
223 223
 --   - referrers
224 224
 --   - dim_user_agent
225 225
 --   - dim_geo
226
-extractDependencies :: Env -> Fact -> Dependencies
227
-extractDependencies env = flip runReader env . E.extractDependencies
226
+extractDependencies :: Config -> Fact -> Dependencies
227
+extractDependencies config = flip runReader config . E.extractDependencies
228 228
 
229 229
 -- |
230 230
 --
231
+-- >>> let dimTables = filter (`notElem` tables) . nub . concatMap (extractDimensionTables config) $ facts
232
+-- >>> let sqls = map (dimensionTableDefinitionSQL config) dimTables
231 233
 -- >>> mapM_ (\sqls -> mapM_ (putStr . Text.unpack) sqls >> putStrLn "--------" ) sqls
232 234
 -- create table dim_geo (
233 235
 --   id serial not null,
@@ -282,16 +282,16 @@ extractDependencies env = flip runReader env . E.extractDependencies
282 282
 -- alter table dim_page_type add unique (page_type);
283 283
 -- <BLANKLINE>
284 284
 -- --------
285
-dimensionTableDefinitionSQL :: Env -> Table -> [Text]
286
-dimensionTableDefinitionSQL env = flip runReader env . G.dimensionTableDefinitionSQL
285
+dimensionTableDefinitionSQL :: Config -> Table -> [Text]
286
+dimensionTableDefinitionSQL config = flip runReader config . G.dimensionTableDefinitionSQL
287 287
 
288
-dimensionTableDefinitionStatements :: Env -> Table -> [Statement]
289
-dimensionTableDefinitionStatements env = flip runReader env . G.dimensionTableDefinitionStatements
288
+dimensionTableDefinitionStatements :: Config -> Table -> [Statement]
289
+dimensionTableDefinitionStatements config = flip runReader config . G.dimensionTableDefinitionStatements
290 290
 
291 291
 -- |
292 292
 --
293
+-- >>> let storySessionFactTable = extractFactTable config sessionFact
294
+-- >>> let sqls = factTableDefinitionSQL config sessionFact storySessionFactTable
293 295
 -- >>> mapM_ (putStr . Text.unpack) sqls
294 296
 -- create table fact_session_by_minute (
295 297
 --   created_at_minute_id bigint not null,
@@ -315,8 +315,8 @@ dimensionTableDefinitionStatements env = flip runReader env . G.dimensionTableDe
315 315
 -- ;
316 316
 -- create index  on fact_session_by_minute (user_agent_id)
317 317
 -- ;
318
+-- >>> let pageViewFactTable = extractFactTable config pageViewFact
319
+-- >>> let sqls = factTableDefinitionSQL config pageViewFact pageViewFactTable
318 320
 -- >>> mapM_ (putStr . Text.unpack) sqls
319 321
 -- create table fact_page_view_by_minute (
320 322
 --   created_at_minute_id bigint not null,
@@ -348,16 +348,16 @@ dimensionTableDefinitionStatements env = flip runReader env . G.dimensionTableDe
348 348
 -- ;
349 349
 -- create index  on fact_page_view_by_minute (user_agent_id)
350 350
 -- ;
351
-factTableDefinitionSQL :: Env -> Fact -> Table -> [Text]
352
-factTableDefinitionSQL env fact = flip runReader env . G.factTableDefinitionSQL fact
351
+factTableDefinitionSQL :: Config -> Fact -> Table -> [Text]
352
+factTableDefinitionSQL config fact = flip runReader config . G.factTableDefinitionSQL fact
353 353
 
354
-factTableDefinitionStatements :: Env -> Fact -> Table -> [Statement]
355
-factTableDefinitionStatements env fact = flip runReader env . G.factTableDefinitionStatements fact
354
+factTableDefinitionStatements :: Config -> Fact -> Table -> [Statement]
355
+factTableDefinitionStatements config fact = flip runReader config . G.factTableDefinitionStatements fact
356 356
 
357 357
 -- |
358 358
 --
359
+-- >>> let storySessionDimTableNames = map tableName $ extractDimensionTables config sessionFact
360
+-- >>> let sqls = map (dimensionTablePopulationSQL FullPopulation config sessionFact) storySessionDimTableNames
359 361
 -- >>> mapM_ (putStr . Text.unpack) sqls
360 362
 -- insert into dim_geo (country_name, city_name, continent_name)
361 363
 -- select distinct
@@ -385,7 +385,7 @@ factTableDefinitionStatements env fact = flip runReader env . G.factTableDefinit
385 385
 --     created_at < ?
386 386
 -- ;
387 387
 -- <BLANKLINE>
388
+-- >>> let sqls = map (dimensionTablePopulationSQL IncrementalPopulation config sessionFact) storySessionDimTableNames
388 389
 -- >>> mapM_ (putStr . Text.unpack) sqls
389 390
 -- insert into dim_geo (country_name, city_name, continent_name)
390 391
 -- select
@@ -445,8 +445,8 @@ factTableDefinitionStatements env fact = flip runReader env . G.factTableDefinit
445 445
 --     dim_user_agent.name is null
446 446
 -- ;
447 447
 -- <BLANKLINE>
448
+-- >>> let pageViewDimTableNames = map tableName . filter (`notElem` tables) $ extractDimensionTables config pageViewFact
449
+-- >>> let sqls = map (dimensionTablePopulationSQL FullPopulation config pageViewFact) pageViewDimTableNames
448 450
 -- >>> mapM_ (putStr . Text.unpack) sqls
449 451
 -- insert into dim_page_type (page_type)
450 452
 -- select distinct
@@ -457,7 +457,7 @@ factTableDefinitionStatements env fact = flip runReader env . G.factTableDefinit
457 457
 --     (page_type is not null) and created_at < ?
458 458
 -- ;
459 459
 -- <BLANKLINE>
460
+-- >>> let sqls = map (dimensionTablePopulationSQL IncrementalPopulation config pageViewFact) pageViewDimTableNames
460 461
 -- >>> mapM_ (putStr . Text.unpack) sqls
461 462
 -- insert into dim_page_type (page_type)
462 463
 -- select
@@ -478,17 +478,17 @@ factTableDefinitionStatements env fact = flip runReader env . G.factTableDefinit
478 478
 --     dim_page_type.page_type is null
479 479
 -- ;
480 480
 -- <BLANKLINE>
481
-dimensionTablePopulationSQL :: TablePopulationMode -> Env -> Fact -> TableName -> Text
482
-dimensionTablePopulationSQL popMode env fact =
483
-  flip runReader env . G.dimensionTablePopulationSQL popMode fact
481
+dimensionTablePopulationSQL :: TablePopulationMode -> Config -> Fact -> TableName -> Text
482
+dimensionTablePopulationSQL popMode config fact =
483
+  flip runReader config . G.dimensionTablePopulationSQL popMode fact
484 484
 
485
-dimensionTablePopulationStatement :: TablePopulationMode -> Env -> Fact -> TableName -> Statement
486
-dimensionTablePopulationStatement popMode env fact =
487
-  flip runReader env . G.dimensionTablePopulationStatement popMode fact
485
+dimensionTablePopulationStatement :: TablePopulationMode -> Config -> Fact -> TableName -> Statement
486
+dimensionTablePopulationStatement popMode config fact =
487
+  flip runReader config . G.dimensionTablePopulationStatement popMode fact
488 488
 
489 489
 -- |
490 490
 --
491
+-- >>> let sqls = factTablePopulationSQL FullPopulation config sessionFact
491 492
 -- >>> mapM_ (putStr . Text.unpack) sqls
492 493
 -- insert into fact_session_by_minute (created_at_minute_id,
493 494
 --                                     publisher_id,
@@ -530,7 +530,7 @@ dimensionTablePopulationStatement popMode env fact =
530 530
 --     xxff_user_agent_id
531 531
 -- ;
532 532
 -- <BLANKLINE>
533
+-- >>> let sqls = factTablePopulationSQL IncrementalPopulation config sessionFact
533 534
 -- >>> mapM_ (putStr . Text.unpack) sqls
534 535
 -- insert into fact_session_by_minute (created_at_minute_id,
535 536
 --                                     publisher_id,
@@ -572,7 +572,7 @@ dimensionTablePopulationStatement popMode env fact =
572 572
 --     xxff_user_agent_id
573 573
 -- ;
574 574
 -- <BLANKLINE>
575
+-- >>> let sqls = factTablePopulationSQL FullPopulation config pageViewFact
575 576
 -- >>> mapM_ (putStr . Text.unpack) sqls
576 577
 -- insert into fact_page_view_by_minute (created_at_minute_id,
577 578
 --                                       publisher_id,
@@ -630,10 +630,10 @@ dimensionTablePopulationStatement popMode env fact =
630 630
 --     xxff_user_agent_id
631 631
 -- ;
632 632
 -- <BLANKLINE>
633
-factTablePopulationSQL :: TablePopulationMode -> Env -> Fact -> [Text]
634
-factTablePopulationSQL popMode env =
635
-  flip runReader env . G.factTablePopulationSQL popMode
633
+factTablePopulationSQL :: TablePopulationMode -> Config -> Fact -> [Text]
634
+factTablePopulationSQL popMode config =
635
+  flip runReader config . G.factTablePopulationSQL popMode
636 636
 
637
-factTablePopulationStatements :: TablePopulationMode -> Env -> Fact -> [Statement]
638
-factTablePopulationStatements popMode env =
639
-  flip runReader env . G.factTablePopulationStatements popMode
637
+factTablePopulationStatements :: TablePopulationMode -> Config -> Fact -> [Statement]
638
+factTablePopulationStatements popMode config =
639
+  flip runReader config . G.factTablePopulationStatements popMode

+ 16
- 16
ringo/src/Ringo/Extractor.hs View File

@@ -23,8 +23,8 @@ import Ringo.Extractor.Internal
23 23
 import Ringo.Types.Internal
24 24
 import Ringo.Utils
25 25
 
26
-extractFactTable ::  Fact -> Reader Env Table
27
-extractFactTable fact = mkTable <$> asks envSettings
26
+extractFactTable ::  Fact -> Reader Config Table
27
+extractFactTable fact = mkTable <$> asks configSettings
28 28
                                 <*> extractColumns       fact
29 29
                                 <*> extractFKColumns     fact
30 30
                                 <*> extractUKColumnNames fact
@@ -36,10 +36,10 @@ extractFactTable fact = mkTable <$> asks envSettings
36 36
             , tableConstraints = [ UniqueKey $ ukColNames ++ map columnName fkColumns ]
37 37
             }
38 38
 
39
-extractColumns :: Fact -> Reader Env [Column]
39
+extractColumns :: Fact -> Reader Config [Column]
40 40
 extractColumns fact = do
41
-  Settings {..} <- asks envSettings
42
-  tables        <- asks envTables
41
+  Settings {..} <- asks configSettings
42
+  tables        <- asks configTables
43 43
   let table     =  fromJust . findTable (factTableName fact) $ tables
44 44
 
45 45
   let sourceColumn cName                     = fromJust . findColumn cName . tableColumns $ table
@@ -63,20 +63,20 @@ extractColumns fact = do
63 63
         ]
64 64
       _                      -> []
65 65
 
66
-extractFKColumns :: Fact -> Reader Env [Column]
66
+extractFKColumns :: Fact -> Reader Config [Column]
67 67
 extractFKColumns fact = do
68 68
   allDims       <- extractAllDimensionTables fact
69
-  Settings {..} <- asks envSettings
70
-  tables        <- asks envTables
69
+  Settings {..} <- asks configSettings
70
+  tables        <- asks configTables
71 71
 
72 72
   return $ for allDims $ \(dimFact, dimTable) ->
73 73
     let colName = factDimFKIdColumnName settingDimPrefix settingDimTableIdColumnName dimFact dimTable tables
74 74
         colType = idColTypeToFKIdColType settingDimTableIdColumnType
75 75
     in Column colName colType NotNull
76 76
 
77
-extractUKColumnNames :: Fact -> Reader Env [ColumnName]
77
+extractUKColumnNames :: Fact -> Reader Config [ColumnName]
78 78
 extractUKColumnNames fact = do
79
-  Settings {..} <- asks envSettings
79
+  Settings {..} <- asks configSettings
80 80
   return $ forMaybe (factColumns fact) $ \FactColumn {factColTargetColumn = cName, ..} ->
81 81
       case factColType of
82 82
           DimTime  -> Just $ timeUnitColumnName settingDimTableIdColumnName cName settingTimeUnit
@@ -84,13 +84,13 @@ extractUKColumnNames fact = do
84 84
           TenantId -> Just cName
85 85
           _        -> Nothing
86 86
 
87
-extractDependencies :: Fact -> Reader Env Dependencies
87
+extractDependencies :: Fact -> Reader Config Dependencies
88 88
 extractDependencies fact = Map.union <$> extractFactDeps fact <*> extractDimensionDeps fact
89 89
 
90
-extractFactDeps :: Fact -> Reader Env Dependencies
90
+extractFactDeps :: Fact -> Reader Config Dependencies
91 91
 extractFactDeps fact = do
92
-  Settings{..} <- asks envSettings
93
-  facts        <- asks envFacts
92
+  Settings{..} <- asks configSettings
93
+  facts        <- asks configFacts
94 94
 
95 95
   let extractedTable =
96 96
         extractedFactTableName settingFactPrefix settingFactInfix (factName fact) settingTimeUnit
@@ -112,8 +112,8 @@ extractFactDeps fact = do
112 112
   where
113 113
     parentFacts fct facts = [ fromJust $ findFact pf facts | pf <- factParentNames fct ]
114 114
 
115
-extractDimensionDeps :: Fact -> Reader Env Dependencies
115
+extractDimensionDeps :: Fact -> Reader Config Dependencies
116 116
 extractDimensionDeps fact = do
117
-  Settings{..} <- asks envSettings
117
+  Settings{..} <- asks configSettings
118 118
   return $ Map.fromList [ (settingDimPrefix <> table, [factTableName fact])
119 119
                           | FactColumn {factColType = DimVal table} <- factColumns fact ]

+ 5
- 5
ringo/src/Ringo/Extractor/Internal.hs View File

@@ -68,10 +68,10 @@ idColTypeToFKIdColType typ = case Text.toLower typ of
68 68
   "bigserial"   -> "bigint"
69 69
   _             -> typ
70 70
 
71
-extractDimensionTables :: Fact -> Reader Env [Table]
71
+extractDimensionTables :: Fact -> Reader Config [Table]
72 72
 extractDimensionTables fact = do
73
-  settings  <- asks envSettings
74
-  tables    <- asks envTables
73
+  settings  <- asks configSettings
74
+  tables    <- asks configTables
75 75
   return $ dimTablesFromIds tables fact ++ dimTablesFromVals settings tables fact
76 76
 
77 77
 dimTablesFromIds :: [Table] -> Fact -> [Table]
@@ -112,10 +112,10 @@ dimTablesFromVals Settings {..} tables fact =
112 112
       column <- findColumn factColTargetColumn tableColumns
113 113
       return (factColTargetTable, [column])
114 114
 
115
-extractAllDimensionTables :: Fact -> Reader Env [(Fact, Table)]
115
+extractAllDimensionTables :: Fact -> Reader Config [(Fact, Table)]
116 116
 extractAllDimensionTables fact = do
117 117
   myDims     <- map (fact,) <$> extractDimensionTables fact
118 118
   parentDims <- concat <$> mapM extract (factParentNames fact)
119 119
   return . nubBy ((==) `on` snd) $ myDims ++ parentDims
120 120
   where
121
-    extract fName = asks envFacts >>= extractAllDimensionTables . fromJust . findFact fName
121
+    extract fName = asks configFacts >>= extractAllDimensionTables . fromJust . findFact fName

+ 12
- 12
ringo/src/Ringo/Generator/Create.hs View File

@@ -23,9 +23,9 @@ import Ringo.Generator.Sql
23 23
 import Ringo.Types.Internal
24 24
 import Ringo.Utils
25 25
 
26
-tableDefinitionStatements :: Table -> Reader Env [Statement]
26
+tableDefinitionStatements :: Table -> Reader Config [Statement]
27 27
 tableDefinitionStatements Table {..} = do
28
-  Settings {..} <- asks envSettings
28
+  Settings {..} <- asks configSettings
29 29
   let tabName  = tableName <> settingTableNameSuffixTemplate
30 30
 
31 31
       tableSQL = CreateTable ea (name tabName) (map columnDefnSQL tableColumns) [] Nothing NoReplace
@@ -48,22 +48,22 @@ tableDefinitionStatements Table {..} = do
48 48
 
49 49
   return $ tableSQL : map constraintDefnSQL tableConstraints
50 50
 
51
-tableDefinitionSQL :: Table -> (Table -> Reader Env [Statement]) -> Reader Env [Text]
51
+tableDefinitionSQL :: Table -> (Table -> Reader Config [Statement]) -> Reader Config [Text]
52 52
 tableDefinitionSQL table indexFn = do
53 53
   ds <- map ppStatement <$> tableDefinitionStatements table
54 54
   is <- map (\st -> ppStatement st <> ";\n") <$> indexFn table
55 55
   return $ ds ++ is
56 56
 
57
-dimensionTableDefinitionSQL :: Table -> Reader Env [Text]
57
+dimensionTableDefinitionSQL :: Table -> Reader Config [Text]
58 58
 dimensionTableDefinitionSQL table = tableDefinitionSQL table dimensionTableIndexStatements
59 59
 
60
-dimensionTableDefinitionStatements :: Table -> Reader Env [Statement]
60
+dimensionTableDefinitionStatements :: Table -> Reader Config [Statement]
61 61
 dimensionTableDefinitionStatements table =
62 62
   (++) <$> tableDefinitionStatements table <*> dimensionTableIndexStatements table
63 63
 
64
-dimensionTableIndexStatements :: Table -> Reader Env [Statement]
64
+dimensionTableIndexStatements :: Table -> Reader Config [Statement]
65 65
 dimensionTableIndexStatements Table {..} = do
66
-  Settings {..} <- asks envSettings
66
+  Settings {..} <- asks configSettings
67 67
   let tabName        = tableName <> settingTableNameSuffixTemplate
68 68
       tablePKColName = head [ cName | PrimaryKey cName <- tableConstraints ]
69 69
       nonPKColNames  = [ cName | Column cName _ _ <- tableColumns, cName /= tablePKColName ]
@@ -71,18 +71,18 @@ dimensionTableIndexStatements Table {..} = do
71 71
   return [ CreateIndexTSQL ea (nmc "") (name tabName) [nmc cName]
72 72
            | cName <- nonPKColNames, length nonPKColNames > 1 ]
73 73
 
74
-factTableDefinitionSQL :: Fact -> Table -> Reader Env [Text]
74
+factTableDefinitionSQL :: Fact -> Table -> Reader Config [Text]
75 75
 factTableDefinitionSQL fact table = tableDefinitionSQL table (factTableIndexStatements fact)
76 76
 
77
-factTableDefinitionStatements :: Fact -> Table -> Reader Env [Statement]
77
+factTableDefinitionStatements :: Fact -> Table -> Reader Config [Statement]
78 78
 factTableDefinitionStatements fact table =
79 79
   (++) <$> tableDefinitionStatements table <*> factTableIndexStatements fact table
80 80
 
81
-factTableIndexStatements :: Fact -> Table -> Reader Env [Statement]
81
+factTableIndexStatements :: Fact -> Table -> Reader Config [Statement]
82 82
 factTableIndexStatements fact table = do
83 83
   allDims       <- extractAllDimensionTables fact
84
-  Settings {..} <- asks envSettings
85
-  tables        <- asks envTables
84
+  Settings {..} <- asks configSettings
85
+  tables        <- asks configTables
86 86
 
87 87
   let dimTimeCol           = head [ cName | FactColumn cName DimTime <- factColumns fact ]
88 88
       tenantIdCol          = listToMaybe [ cName | FactColumn cName TenantId <- factColumns fact ]

+ 7
- 7
ringo/src/Ringo/Generator/Populate/Dimension.hs View File

@@ -20,19 +20,19 @@ import Ringo.Generator.Internal
20 20
 import Ringo.Generator.Sql
21 21
 import Ringo.Types.Internal
22 22
 
23
-dimensionTablePopulationSQL :: TablePopulationMode -> Fact -> TableName -> Reader Env Text
23
+dimensionTablePopulationSQL :: TablePopulationMode -> Fact -> TableName -> Reader Config Text
24 24
 dimensionTablePopulationSQL popMode fact dimTableName =
25 25
   ppStatement <$> dimensionTablePopulationStatement popMode fact dimTableName
26 26
 
27
-dimensionTablePopulationStatement :: TablePopulationMode -> Fact -> TableName -> Reader Env Statement
27
+dimensionTablePopulationStatement :: TablePopulationMode -> Fact -> TableName -> Reader Config Statement
28 28
 dimensionTablePopulationStatement popMode fact dimTableName = do
29
-  Settings {..}   <- asks envSettings
29
+  Settings {..}   <- asks configSettings
30 30
   let colMapping  = dimColumnMapping settingDimPrefix fact dimTableName
31 31
   let insertTable = suffixTableName popMode settingTableNameSuffixTemplate dimTableName
32 32
   selectQ         <- makeSelectQuery popMode fact dimTableName colMapping
33 33
   return $ insert insertTable (map fst colMapping) selectQ
34 34
 
35
-makeSelectQuery :: TablePopulationMode -> Fact -> TableName -> [(ColumnName, ColumnName)] -> Reader Env QueryExpr
35
+makeSelectQuery :: TablePopulationMode -> Fact -> TableName -> [(ColumnName, ColumnName)] -> Reader Config QueryExpr
36 36
 makeSelectQuery popMode fact dimTableName colMapping = do
37 37
   selectList <- makeSelectList fact colMapping
38 38
   let selectQ = makeSelect
@@ -46,10 +46,10 @@ makeSelectQuery popMode fact dimTableName colMapping = do
46 46
     FullPopulation        -> selectQ
47 47
     IncrementalPopulation -> makeIncSelectQuery selectQ dimTableName colMapping
48 48
 
49
-makeSelectList :: Fact -> [(ColumnName, ColumnName)] -> Reader Env SelectList
49
+makeSelectList :: Fact -> [(ColumnName, ColumnName)] -> Reader Config SelectList
50 50
 makeSelectList fact colMapping = do
51
-  tables        <- asks envTables
52
-  defaults      <- asks envTypeDefaults
51
+  tables        <- asks configTables
52
+  defaults      <- asks configTypeDefaults
53 53
   let factTable = fromJust $ findTable (factTableName fact) tables
54 54
   return $ sl [ flip sia (nmc cName) $ coalesceColumn defaults (factTableName fact) col
55 55
                 | (_, cName) <- colMapping

+ 15
- 15
ringo/src/Ringo/Generator/Populate/Fact.hs View File

@@ -57,7 +57,7 @@ $$
57 57
 LANGUAGE 'plpgsql' IMMUTABLE;
58 58
 |]
59 59
 
60
-factTablePopulationSQL :: TablePopulationMode -> Fact -> Reader Env [Text]
60
+factTablePopulationSQL :: TablePopulationMode -> Fact -> Reader Config [Text]
61 61
 factTablePopulationSQL popMode fact = do
62 62
   stmts <- factTablePopulationStatements popMode fact
63 63
   return $ case stmts of
@@ -65,9 +65,9 @@ factTablePopulationSQL popMode fact = do
65 65
     [i]  -> [ ppStatement i ]
66 66
     i:us -> [ ppStatement i, ilog2FunctionString ] ++ map ppStatement us
67 67
 
68
-factTablePopulationStatements :: TablePopulationMode -> Fact -> Reader Env [Statement]
68
+factTablePopulationStatements :: TablePopulationMode -> Fact -> Reader Config [Statement]
69 69
 factTablePopulationStatements popMode fact = do
70
-  Settings {..}        <- asks envSettings
70
+  Settings {..}        <- asks configSettings
71 71
   allDims              <- extractAllDimensionTables fact
72 72
   selExprs             <- selectExprs popMode fact allDims groupByColPrefix
73 73
   popQueryExpr         <- populateQueryExpr popMode fact allDims selExprs groupByColPrefix
@@ -85,7 +85,7 @@ selectExprs :: TablePopulationMode
85 85
             -> Fact
86 86
             -> [(Fact, Table)]
87 87
             -> Text
88
-            -> Reader Env [(ColumnName, (ScalarExpr, NameComponent), Bool)]
88
+            -> Reader Config [(ColumnName, (ScalarExpr, NameComponent), Bool)]
89 89
 selectExprs popMode fact allDims groupByColPrefix = do
90 90
   factSelExprs <- factColumnSelectExprs fact
91 91
   dimSelExprs  <- dimColumnSelectExprs popMode allDims
@@ -93,11 +93,11 @@ selectExprs popMode fact allDims groupByColPrefix = do
93 93
   return [ (cName, (expr, nmc $ groupByColPrefix <> cName), addToGroupBy)
94 94
            | (cName, expr, addToGroupBy) <- factSelExprs ++ dimSelExprs ]
95 95
 
96
-factColumnSelectExprs :: Fact -> Reader Env [(ColumnName, ScalarExpr, Bool)]
96
+factColumnSelectExprs :: Fact -> Reader Config [(ColumnName, ScalarExpr, Bool)]
97 97
 factColumnSelectExprs fact = do
98
-  Settings {..}    <- asks envSettings
99
-  tables           <- asks envTables
100
-  typeDefaults     <- asks envTypeDefaults
98
+  Settings {..}    <- asks configSettings
99
+  tables           <- asks configTables
100
+  typeDefaults     <- asks configTypeDefaults
101 101
   let fTableName   = factTableName fact
102 102
       fTable       = fromJust . findTable fTableName $ tables
103 103
       dimIdColName = settingDimTableIdColumnName
@@ -135,11 +135,11 @@ dimIdColumnSelectExpr fTableName fTable typeDefaults cName =
135 135
   let sCol = fromJust . findColumn cName $ tableColumns fTable
136 136
   in (cName, coalesceColumn typeDefaults fTableName sCol, True)
137 137
 
138
-dimColumnSelectExprs :: TablePopulationMode -> [(Fact, Table)] -> Reader Env [(ColumnName, ScalarExpr, Bool)]
138
+dimColumnSelectExprs :: TablePopulationMode -> [(Fact, Table)] -> Reader Config [(ColumnName, ScalarExpr, Bool)]
139 139
 dimColumnSelectExprs popMode allDims = do
140
-  settings@Settings {..} <- asks envSettings
141
-  tables                 <- asks envTables
142
-  typeDefaults           <- asks envTypeDefaults
140
+  settings@Settings {..} <- asks configSettings
141
+  tables                 <- asks configTables
142
+  typeDefaults           <- asks configTypeDefaults
143 143
   let dimIdColName       = settingDimTableIdColumnName
144 144
 
145 145
   return $ for allDims $ \(dimFact, factTable@Table {tableName}) -> let
@@ -172,10 +172,10 @@ populateQueryExpr :: TablePopulationMode
172 172
                   -> [(Fact, Table)]
173 173
                   -> [(ColumnName, (ScalarExpr, NameComponent), Bool)]
174 174
                   -> Text
175
-                  -> Reader Env QueryExpr
175
+                  -> Reader Config QueryExpr
176 176
 populateQueryExpr popMode fact allDims selExprs groupByColPrefix = do
177
-  Settings {..}   <- asks envSettings
178
-  tables          <- asks envTables
177
+  Settings {..}   <- asks configSettings
178
+  tables          <- asks configTables
179 179
   let fTableName  = factTableName fact
180 180
       fTable      = fromJust . findTable fTableName $ tables
181 181
       joinClauses =

+ 7
- 7
ringo/src/Ringo/Generator/Populate/Fact/CountDistinct.hs View File

@@ -23,10 +23,10 @@ import Ringo.Generator.Internal
23 23
 import Ringo.Generator.Sql
24 24
 import Ringo.Types.Internal
25 25
 
26
-factCountDistinctUpdateStatements :: TablePopulationMode -> Fact -> Text -> QueryExpr -> Reader Env [Statement]
26
+factCountDistinctUpdateStatements :: TablePopulationMode -> Fact -> Text -> QueryExpr -> Reader Config [Statement]
27 27
 factCountDistinctUpdateStatements popMode fact groupByColPrefix expr = case expr of
28 28
   select@Select {..} -> do
29
-    Settings {..}         <- asks envSettings
29
+    Settings {..}         <- asks configSettings
30 30
     let extFactTableName  =
31 31
           suffixTableName popMode settingTableNameSuffixTemplate
32 32
             $ extractedFactTableName settingFactPrefix settingFactInfix (factName fact) settingTimeUnit
@@ -56,11 +56,11 @@ factCountDistinctUpdateStatements popMode fact groupByColPrefix expr = case expr
56 56
 
57 57
   _ -> return []
58 58
 
59
-queryExpr :: Fact -> ColumnName -> Maybe ColumnName -> [ColumnName] -> QueryExpr -> Reader Env QueryExpr
59
+queryExpr :: Fact -> ColumnName -> Maybe ColumnName -> [ColumnName] -> QueryExpr -> Reader Config QueryExpr
60 60
 queryExpr fact targetCol sourceCol groupByCols select = case select of
61 61
   Select {selSelectList = SelectList _ origSelectItems, ..} -> do
62
-    Settings {..}      <- asks envSettings
63
-    tables             <- asks envTables
62
+    Settings {..}      <- asks configSettings
63
+    tables             <- asks configTables
64 64
 
65 65
     let fTableName     = factTableName fact
66 66
         fTable         = fromJust . findTable fTableName $ tables
@@ -78,9 +78,9 @@ queryExpr fact targetCol sourceCol groupByCols select = case select of
78 78
 
79 79
   _ -> error "Must be a Select"
80 80
 
81
-bucketSelectItems :: ColumnName -> ScalarExpr -> Reader Env [SelectItem]
81
+bucketSelectItems :: ColumnName -> ScalarExpr -> Reader Config [SelectItem]
82 82
 bucketSelectItems targetCol unqCol = do
83
-  Settings {..} <- asks envSettings
83
+  Settings {..} <- asks configSettings
84 84
 
85 85
   return [ sia (binop "&" (app "hashtext" [ unqCol ])
86 86
                   (num . Text.pack . show $ bucketCount settingFactCountDistinctErrorRate - 1))

+ 5
- 5
ringo/src/Ringo/Types.hs View File

@@ -18,11 +18,11 @@ module Ringo.Types
18 18
   , TimeUnit(..)
19 19
   , timeUnitName
20 20
   , timeUnitToSeconds
21
-  , Env
22
-  , envTables
23
-  , envFacts
24
-  , envSettings
25
-  , envTypeDefaults
21
+  , Config
22
+  , configTables
23
+  , configFacts
24
+  , configSettings
25
+  , configTypeDefaults
26 26
   , Settings(..)
27 27
   , defSettings
28 28
   , TypeDefaults

+ 32
- 26
ringo/src/Ringo/Types/Internal.hs View File

@@ -403,27 +403,27 @@ defSettings = Settings
403 403
               , settingTableNameSuffixTemplate    = "{{suff}}"
404 404
               }
405 405
 
406
+-- | Errors possible while validating the config
406 407
 data ValidationError =
407
-    -- | When referencing a table which is missing from the env
408
+    -- | When referencing a table which is missing from the config
408 409
     MissingTable             !TableName
409
-    -- | When referencing a fact which is missing from the env
410
+    -- | When referencing a fact which is missing from the config
410 411
   | MissingFact              !TableName
411
-    -- | When referencing a column which is missing from the env
412
+    -- | When referencing a column which is missing from the config
412 413
   | MissingColumn            !TableName !ColumnName
413 414
     -- | When a fact has no 'DimTime' columns
414 415
   | MissingTimeColumn        !TableName
415 416
     -- | When a 'DimTime' fact column of a fact is nullable
416 417
   | MissingNotNullConstraint !TableName !ColumnName
417
-    -- | When the default value of a type is missing from the env
418
+    -- | When the default value of a type is missing from the config
418 419
   | MissingTypeDefault       !Text
419
-    -- | When there are multiple tables with the same name in the env
420
+    -- | When there are multiple tables with the same name in the config
420 421
   | DuplicateTable           !TableName
421
-    -- | When there are multiple facts with the same name in the env
422
+    -- | When there are multiple facts with the same name in the config
422 423
   | DuplicateFact            !TableName
423
-    -- | When there are multiple columns with the same name in a table in the env
424
+    -- | When there are multiple columns with the same name in a table in the config
424 425
   | DuplicateColumn          !TableName !ColumnName
425
-    -- | When there are multiple dimensions with the same name in the env
426
+    -- | When there are multiple dimensions with the same name in the config
426 427
   | DuplicateDimension       !TableName
427 428
   deriving (Eq, Show)
428 429
 
@@ -431,29 +431,29 @@ data ValidationError =
431 431
 --   the generated dimension and fact tables
432 432
 type TypeDefaults = Map Text Text
433 433
 
434
-data Env = Env
435
-           { _envTables       :: ![Table]
436
-           , _envFacts        :: ![Fact]
437
-           , _envSettings     :: !Settings
438
-           , _envTypeDefaults :: !TypeDefaults
439
-           } deriving (Show)
440
-
441
-envTables       :: Env -> [Table]
442
-envTables       = _envTables
443
-
444
-envFacts        :: Env -> [Fact]
445
-envFacts        = _envFacts
446
-
447
-envSettings     :: Env -> Settings
448
-envSettings     = _envSettings
449
-
450
-envTypeDefaults :: Env -> TypeDefaults
451
-envTypeDefaults = _envTypeDefaults
434
+-- | The config for the library
435
+data Config = Config
436
+              { _configTables       :: ![Table]
437
+              , _configFacts        :: ![Fact]
438
+              , _configSettings     :: !Settings
439
+              , _configTypeDefaults :: !TypeDefaults
440
+              } deriving (Show)
441
+
442
+-- | Return the list of source tables from the config
443
+configTables       :: Config -> [Table]
444
+configTables       = _configTables
445
+
446
+-- | Return the list of facts to be generated from the config
447
+configFacts        :: Config -> [Fact]
448
+configFacts        = _configFacts
449
+
450
+-- | Return the settings from the config
451
+configSettings     :: Config -> Settings
452
+configSettings     = _configSettings
453
+
454
+-- | Return the defaults for the SQL types from the config
455
+configTypeDefaults :: Config -> TypeDefaults
456
+configTypeDefaults = _configTypeDefaults
452 457
 
453 458
 -- | The mode for population of the generated tables; used to switch the SQL for table population
454 459
 data TablePopulationMode = FullPopulation        -- ^ Populating the tables fully, starting with empty ones

+ 11
- 11
ringo/src/Ringo/Validator.hs View File

@@ -4,7 +4,7 @@
4 4
 {-# LANGUAGE GADTs #-}
5 5
 {-# LANGUAGE PatternSynonyms #-}
6 6
 
7
-module Ringo.Validator (validateEnv) where
7
+module Ringo.Validator (validateConfig) where
8 8
 
9 9
 import qualified Data.Map as Map
10 10
 import qualified Data.Text as Text
@@ -24,9 +24,9 @@ checkTableForCol tab colName =
24 24
   [ MissingColumn (tableName tab) colName |
25 25
       not . any ((colName ==) . columnName) . tableColumns $ tab ]
26 26
 
27
-validateTable :: Table -> Reader Env [ValidationError]
27
+validateTable :: Table -> Reader Config [ValidationError]
28 28
 validateTable table = do
29
-  Env tables _ _ _ <- ask
29
+  Config tables _ _ _ <- ask
30 30
   return . concatMap (checkConstraint tables) . tableConstraints $ table
31 31
   where
32 32
     checkConstraint _ (PrimaryKey colName)    = checkTableForCol table colName
@@ -39,9 +39,9 @@ validateTable table = do
39 39
 
40 40
     checkTableForColRefs tab = concatMap (checkTableForCol tab)
41 41
 
42
-validateFact :: Fact -> Reader Env [ValidationError]
42
+validateFact :: Fact -> Reader Config [ValidationError]
43 43
 validateFact Fact {..} = do
44
-  Env tables _ _ typeDefaults <- ask
44
+  Config tables _ _ typeDefaults <- ask
45 45
   let defaults = Map.keys typeDefaults
46 46
   case findTable factTableName tables of
47 47
     Nothing    -> return [ MissingTable factTableName ]
@@ -70,7 +70,7 @@ validateFact Fact {..} = do
70 70
       return $ tableVs ++ parentVs ++ colVs ++ timeVs ++ notNullVs ++ typeDefaultVs
71 71
   where
72 72
     checkFactParents fName = do
73
-      Env _ facts _ _ <- ask
73
+      Config _ facts _ _ <- ask
74 74
       case findFact fName facts of
75 75
         Nothing    -> return [ MissingFact fName ]
76 76
         Just pFact -> validateFact pFact
@@ -84,10 +84,10 @@ validateFact Fact {..} = do
84 84
       DimId {factColTargetTable = tName} -> maybe [ MissingTable tName ] (const []) $ findTable tName tables
85 85
       _                                  -> []
86 86
 
87
-validateEnv :: [Table] -> [Fact] -> Settings -> TypeDefaults -> Either [ValidationError] Env
88
-validateEnv tables facts settings@Settings {..} typeDefaults = let
89
-    env = Env tables facts settings typeDefaults
90
-  in flip runReader env $ do
87
+validateConfig :: [Table] -> [Fact] -> Settings -> TypeDefaults -> Either [ValidationError] Config
88
+validateConfig tables facts settings@Settings {..} typeDefaults = let
89
+    config = Config tables facts settings typeDefaults
90
+  in flip runReader config $ do
91 91
        tableVs <- concat <$> mapM validateTable tables
92 92
        factVs  <- concat <$> mapM validateFact facts
93 93
        let dupTableVs = [ DuplicateTable table | table <- findDups . map tableName $ tables ]
@@ -103,7 +103,7 @@ validateEnv tables facts settings@Settings {..} typeDefaults = let
103 103
                         >>> map (head >>> fst >>> DuplicateDimension)
104 104
            errors     = nub $ tableVs ++ factVs ++ dupTableVs ++ dupFactVs ++ dupColVs ++ dupDimVs
105 105
        return $ if null errors
106
-         then Right env
106
+         then Right config
107 107
          else Left errors
108 108
   where
109 109
     findDups =

Loading…
Cancel
Save