]> www.average.org Git - pulsecounter.git/blobdiff - web/query.cgi
fix time interval selection
[pulsecounter.git] / web / query.cgi
index f45c844ede55f9fbd540f6c6347f073e30c0f67b..839f7885faf7ea19016e7b344762655314feb50c 100755 (executable)
@@ -14,6 +14,7 @@ module Main where
 import Control.Monad
 import Data.Maybe
 import Data.List
+import Data.Ratio
 import System.Locale
 import System.Time
 import Network.CGI
@@ -29,6 +30,7 @@ cgiMain = do
                                               , connectPassword = pass conf
                                               , connectDatabase = dbnm conf
                                               }
+  _ <- liftIO $ execute_ conn "set time_zone = '+00:00';";
   today <- liftIO getClockTime
   let tomorrow = addToClockTime (noTimeDiff {tdDay = 1}) today
       daystart x = (toUTCTime x) { ctHour = 0, ctMin = 0
@@ -42,16 +44,16 @@ cgiMain = do
   --                      ++ " dhi=" ++ show dhi ++ " ihi=" ++ show ihi
   let slo = fromMaybe dlo ilo :: String
       shi = fromMaybe dhi ihi :: String
-  [(olo, ohi)] <- liftIO $ query conn "select to_seconds(?), to_seconds(?);"
+  [(olo, ohi)] <- liftIO $ query conn "select unix_timestamp(?), unix_timestamp(?);"
                          [slo, shi]
   cold <- liftIO $ query conn
-    "select to_seconds(timestamp) as time, value+adj as value from \
+    "select unix_timestamp(timestamp) as time, value+adj as value from \
        \(select c.timestamp timestamp, c.value value, \
          \(select sum(value) from coldadj a where a.timestamp <= c.timestamp \
          \) adj from coldcnt c where timestamp between ? and ? \
        \) t order by timestamp;" (slo, shi)
   hot <- liftIO $ query conn
-    "select to_seconds(timestamp) as time, value+adj as value from \
+    "select unix_timestamp(timestamp) as time, value+adj as value from \
        \(select c.timestamp timestamp, c.value value, \
          \(select sum(value) from hotadj a where a.timestamp <= c.timestamp \
          \) adj from hotcnt c where timestamp between ? and ? \
@@ -65,14 +67,14 @@ cgiMain = do
   _ <- liftIO $ close conn
 
   setHeader "Content-type" "application/json"
-  output $ "{\"range\": {\"lo\": " ++ show (olo :: Int)
-          ++ ", \"hi\": " ++ show (ohi :: Int)
-          ++ "}, \"current\": {\"cold\": " ++ show (floor (ccold :: Double))
-          ++ ", \"hot\": " ++ show (floor (chot :: Double))
+  output $ "{\"range\": {\"lo\": " ++ show (floor (olo :: (Ratio Integer)))
+          ++ ", \"hi\": " ++ show (floor (ohi :: (Ratio Integer)))
+          ++ "}, \"current\": {\"cold\": " ++ show (floor (ccold :: (Ratio Integer)))
+          ++ ", \"hot\": " ++ show (floor (chot :: (Ratio Integer)))
           ++ "}, \"cold\": [" ++ showjson cold
           ++ "], \"hot\": [" ++ showjson hot ++ "]}\n"
 
-showjson :: [(Int, Double)] -> String
+showjson :: [(Int, (Ratio Integer))] -> String
 showjson l = intercalate "," $ map (\(t, c) -> "[" ++ show t ++ "," ++ show (floor c) ++ "]") l
 
 data Conf = Conf { host :: String