-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
109 lines (93 loc) · 3.7 KB
/
Main.hs
File metadata and controls
109 lines (93 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- TODO make fork of EasyPlot more obviously a fork
import Graphics.EasyPlot
import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.Format
import System.Locale
import Database.HDBC
import Database.HDBC.Sqlite3
import System.Environment
main = do
args <- getArgs
let Just command = (lookup (head args) commands)
command (tail args)
-- Takes the arguments and does an action block based on it
commands :: [(String, [String] -> IO () )]
commands = [("add", addToTable),
("show", showTable),
("create", createTable),
("init", initDatabase),
("list", listTables)
]
initDatabase :: [String] -> IO ()
initDatabase _ = do
-- TODO make sure this works even if the file doesn't exist yet
db <- connectSqlite3 "tracking.sqlite3"
run db "create table tracking_master (name STRING, x_intercept DATE, per_day FLOAT);" []
commit db
return ()
listTables :: [String] -> IO ()
listTables _ = do
db <- connectSqlite3 "tracking.sqlite3"
r <- quickQuery' db ("select * from tracking_master") []
putStrLn "The following items are being tracked in the database:"
mapM putStrLn (getNames r)
return ()
where getNames rowList = map getName rowList
getName (name:_) = (fromSql name) :: String
createTable :: [String] -> IO ()
createTable [tableName] = do
-- TODO: use tracking_master table to also add intercept line
db <- connectSqlite3 "tracking.sqlite3"
run db ("create table " ++ tableName ++ " (id INTEGER PRIMARY KEY, date DATE, amt FLOAT, to_date FLOAT);") []
-- TODO make x intercept and per-day customizable
t <- today
run db "INSERT into tracking_master VALUES (?,?,?)" [toSql tableName, toSql t, toSql (1.0 :: Double)]
commit db
-- TODO: Add confirmation or error message
return ()
showTable :: [String] -> IO ()
showTable args = do
-- TODO: use tracking_master table to show desired line
-- (and margins of error?)
db <- connectSqlite3 "tracking.sqlite3"
r <- quickQuery' db ("select * from " ++ (head args)) []
t <- today
plot' [Debug] X11 $ [TimeData2D [Title "Reviews", XTime True] [] (getDateAmountTuples t r) ]
return ()
showReviews :: [String] -> IO ()
showReviews _ = showTable ["reviews"]
-- Takes the rest of the args
-- and adds a review based on them.
addToTable :: [String] -> IO ()
addToTable [tableName, dateString, amtString] = do
db <- connectSqlite3 "tracking.sqlite3"
t <- today
let date = if dateString == "t"
then t
else addDays (-1) t
addItem db tableName date (read amtString :: Double)
-- Note: doesn't work b/c of id column
addItem :: Connection -> String -> Day -> Double -> IO ()
addItem c n d a = do
-- TODO make to_date actually work
run c ("INSERT INTO " ++ n ++ " VALUES (NULL,?,?,?)") [toSql d, toSql a, toSql a]
commit c
today :: IO Day
today = getCurrentTime >>= return . utctDay
-- Want a function to zip from a list of rows to
-- a list of plot tuples.
getDateAmountTuples :: Day -> [[SqlValue]] -> [(Day,Double)]
getDateAmountTuples defaultDay list = map (getDateAmountTuple defaultDay) list
getDateAmountTuple :: Day -> [SqlValue] -> (Day, Double)
-- List should be ID, date, amt, to_datet
getDateAmountTuple defaultDay [a,b,c,d] = ( (fromSql b) :: Day, (fromSql c) :: Double)
getDateAmountTuple defaultDay _ = (defaultDay, 0.0 )
toGnuplotDateString :: Day -> String
toGnuplotDateString d = formatTime defaultTimeLocale "%Y%m%d" d
where locale = defaultTimeLocale
getDateNumAmountTuples :: Day -> [[SqlValue]] -> [(Double, Double)]
getDateNumAmountTuples defaultDay =
map $ getDateNumAmountTuple . (getDateAmountTuple defaultDay)
getDateNumAmountTuple :: (Day, Double) -> (Double, Double)
getDateNumAmountTuple (d,a) = (read (toGnuplotDateString d) :: Double, a)