X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=blobdiff_plain;f=web%2Fquery.cgi;h=967127650427e5fda18d04e8a2b27d2bec937f7b;hp=872023716d1cd12ecd43af9e6bcca7e76b2eed8f;hb=dc967d814f0eb1146f3823fabc12a252559e75af;hpb=57ab1d5776f832c3df5c8b2514f34e282b39fbd9 diff --git a/web/query.cgi b/web/query.cgi index 8720237..9671276 100755 --- a/web/query.cgi +++ b/web/query.cgi @@ -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 @@ -42,22 +43,20 @@ 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 ? order by timestamp \ - \) t;" (slo, shi) + \) 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 ? order by timestamp \ - \) t;" (slo, shi) + \) adj from hotcnt c where timestamp between ? and ? \ + \) t order by timestamp;" (slo, shi) [(ccold, chot)] <- liftIO $ query_ conn "select lcold+acold as cold, lhot+ahot as hot from \ \(select value as lcold from coldcnt order by timestamp desc limit 1) cc, \ @@ -67,14 +66,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)) - ++ "}}, \"cold\": [" ++ showjson cold + 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