Skip to content

Instantly share code, notes, and snippets.

@ElliotJH
Created April 11, 2013 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElliotJH/5368093 to your computer and use it in GitHub Desktop.
Save ElliotJH/5368093 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Data.Maybe
import Data.ByteString.Lazy
import Control.Applicative
import Debug.Trace
import Control.Monad
import qualified Data.Aeson.Types as T
import Data.Time
main = do
res <- liftA show (liftA decodeOriginal (Data.ByteString.Lazy.readFile "./a.json"))
Prelude.putStrLn res
interpretResult :: Maybe String -> String
interpretResult Nothing = "Error."
interpretResult x = fromJust x
data TotalLine1 = TotalLine1 {
timestamp :: String,
value :: Integer
} deriving (Eq, Show)
data Original = Original {
totals :: [TotalLine1]
} deriving (Eq, Show)
data Outcome = Outcome {
graph :: Graph
} deriving (Eq, Show)
data Graph = Graph {
title :: String,
datasequences :: [DataSequence]
} deriving (Eq, Show)
data DataSequence = DataSequence {
title :: String,
color :: String,
datapoints :: [DataPoint]
} deriving (Eq, Show)
data DataPoint = DataPoint {
value :: Integer
title :: String
} deriving (Eq, Show)
instance FromJSON Original where
parseJSON (Object v) = traceStack "Original" (Original <$> (parseJSON =<< (v .: "visitors.total")))
parseJSON _ = mzero
instance FromJSON TotalLine1 where
parseJSON (Object v) = TotalLine1 <$>
v .: "timestamp" <*>
v .: "value"
decodeOriginal :: ByteString -> Maybe Original
decodeOriginal b = traceStack "decoding" (do
a <- decode b :: Maybe Original
return a)
convertTimeStamp :: String -> String
convertTimeStamp s = (parseTime "%s" s)
convertTotalLine :: TotalLine1 -> DataPoint
convertTotalLine TotalLine1 timestamp value = DataPoint value (convertTimestamp timestamp)
convertOriginal :: Original -> Outcome
convertOriginal Original totals = map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment