{-# LANGUAGE OverloadedStrings #-}
module Eventlog.Data (generateJson, generateJsonValidate, generateJsonData ) where

import Prelude hiding (readFile)
import Data.Aeson (Value(..), (.=), object)
import qualified Data.Map as Map

import Eventlog.Args (Args(..))
import Eventlog.Bands (bands)
import qualified Eventlog.Events as E
import qualified Eventlog.HeapProf as H
import Eventlog.Prune
import Eventlog.Vega
import Eventlog.Types (Header(..), ProfData(..), HeapProfBreakdown(..))
import Data.List
import Data.Ord
import Eventlog.Trie
import Eventlog.Detailed
import Text.Blaze.Html

generateJsonData :: Args -> ProfData -> IO (Header, Value, Maybe Value, Maybe Html)
generateJsonData :: Args -> ProfData -> IO (Header, Value, Maybe Value, Maybe Html)
generateJsonData Args
a (ProfData Header
h Map Bucket BucketInfo
binfo Map Word32 CostCentre
ccMap [Frame]
fs [Trace]
traces HeapInfo
heap_info Map InfoTablePtr InfoTableLoc
ipes) = do
  let keeps :: Map Bucket (Int, BucketInfo)
keeps = Args -> Map Bucket BucketInfo -> Map Bucket (Int, BucketInfo)
pruneBands Args
a Map Bucket BucketInfo
binfo
      bs :: (UArray Int Double, UArray (Int, Int) Double)
bs = Header
-> Map Bucket Int
-> [Frame]
-> (UArray Int Double, UArray (Int, Int) Double)
bands Header
h (((Int, BucketInfo) -> Int)
-> Map Bucket (Int, BucketInfo) -> Map Bucket Int
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (Int, BucketInfo) -> Int
forall a b. (a, b) -> a
fst Map Bucket (Int, BucketInfo)
keeps) [Frame]
fs
      combinedJson :: Value
combinedJson = [Pair] -> Value
object [
          Text
"samples" Text -> [VegaEntry] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Map Bucket (Int, BucketInfo)
-> (UArray Int Double, UArray (Int, Int) Double) -> [VegaEntry]
bandsToVega Map Bucket (Int, BucketInfo)
keeps (UArray Int Double, UArray (Int, Int) Double)
bs
        , Text
"traces"  Text -> [VegaTrace] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= [Trace] -> [VegaTrace]
tracesToVega [Trace]
traces
        , Text
"heap"    Text -> [VegaHeap] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= HeapInfo -> [VegaHeap]
heapToVega HeapInfo
heap_info
        ]
      mdescs :: [(Bucket, (Int, BucketInfo))]
mdescs =
        ((Bucket, (Int, BucketInfo))
 -> (Bucket, (Int, BucketInfo)) -> Ordering)
-> [(Bucket, (Int, BucketInfo))] -> [(Bucket, (Int, BucketInfo))]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (((Bucket, (Int, BucketInfo))
 -> (Bucket, (Int, BucketInfo)) -> Ordering)
-> (Bucket, (Int, BucketInfo))
-> (Bucket, (Int, BucketInfo))
-> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((Bucket, (Int, BucketInfo)) -> Int)
-> (Bucket, (Int, BucketInfo))
-> (Bucket, (Int, BucketInfo))
-> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing ((Int, BucketInfo) -> Int
forall a b. (a, b) -> a
fst ((Int, BucketInfo) -> Int)
-> ((Bucket, (Int, BucketInfo)) -> (Int, BucketInfo))
-> (Bucket, (Int, BucketInfo))
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bucket, (Int, BucketInfo)) -> (Int, BucketInfo)
forall a b. (a, b) -> b
snd))) ([(Bucket, (Int, BucketInfo))] -> [(Bucket, (Int, BucketInfo))])
-> [(Bucket, (Int, BucketInfo))] -> [(Bucket, (Int, BucketInfo))]
forall a b. (a -> b) -> a -> b
$ Map Bucket (Int, BucketInfo) -> [(Bucket, (Int, BucketInfo))]
forall k a. Map k a -> [(k, a)]
Map.toList Map Bucket (Int, BucketInfo)
keeps
      -- Only supply the cost centre view in cost centre profiling mode.
      cc_descs :: Maybe Value
cc_descs = case Header -> Maybe HeapProfBreakdown
hHeapProfileType Header
h of
                Just HeapProfBreakdown
HeapProfBreakdownCostCentre -> Value -> Maybe Value
forall a. a -> Maybe a
Just (Map Word32 CostCentre -> [(Bucket, (Int, BucketInfo))] -> Value
outputTree Map Word32 CostCentre
ccMap [(Bucket, (Int, BucketInfo))]
mdescs)
                Maybe HeapProfBreakdown
_ -> Maybe Value
forall a. Maybe a
Nothing

  let use_ipes :: Maybe (Map InfoTablePtr InfoTableLoc)
use_ipes = case Header -> Maybe HeapProfBreakdown
hHeapProfileType Header
h of
                   Just HeapProfBreakdown
HeapProfBreakdownInfoTable -> Map InfoTablePtr InfoTableLoc
-> Maybe (Map InfoTablePtr InfoTableLoc)
forall a. a -> Maybe a
Just Map InfoTablePtr InfoTableLoc
ipes
                   Maybe HeapProfBreakdown
_ -> Maybe (Map InfoTablePtr InfoTableLoc)
forall a. Maybe a
Nothing
      desc_buckets :: Map Bucket (Int, BucketInfo)
desc_buckets = Args -> Map Bucket BucketInfo -> Map Bucket (Int, BucketInfo)
pruneDetailed Args
a Map Bucket BucketInfo
binfo
      bs' :: (UArray Int Double, UArray (Int, Int) Double)
bs' = Header
-> Map Bucket Int
-> [Frame]
-> (UArray Int Double, UArray (Int, Int) Double)
bands Header
h (((Int, BucketInfo) -> Int)
-> Map Bucket (Int, BucketInfo) -> Map Bucket Int
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (Int, BucketInfo) -> Int
forall a b. (a, b) -> a
fst Map Bucket (Int, BucketInfo)
desc_buckets) [Frame]
fs
      closure_table :: Maybe Html
closure_table =
        case Args -> Maybe Int
detailedLimit Args
a of
          Just Int
0 ->  Maybe Html
forall a. Maybe a
Nothing
          Maybe Int
_ -> Html -> Maybe Html
forall a. a -> Maybe a
Just ((UArray Int Double, UArray (Int, Int) Double)
-> Maybe (Map InfoTablePtr InfoTableLoc)
-> Map Bucket (Int, BucketInfo)
-> Html
renderClosureInfo (UArray Int Double, UArray (Int, Int) Double)
bs' Maybe (Map InfoTablePtr InfoTableLoc)
use_ipes Map Bucket (Int, BucketInfo)
desc_buckets)
  (Header, Value, Maybe Value, Maybe Html)
-> IO (Header, Value, Maybe Value, Maybe Html)
forall (m :: * -> *) a. Monad m => a -> m a
return (Header
h, Value
combinedJson, Maybe Value
cc_descs, Maybe Html
closure_table)

generateJson :: FilePath -> Args -> IO (Header, Value, Maybe Value, Maybe Html)
generateJson :: FilePath -> Args -> IO (Header, Value, Maybe Value, Maybe Html)
generateJson = (ProfData -> IO ())
-> FilePath -> Args -> IO (Header, Value, Maybe Value, Maybe Html)
generateJsonValidate (IO () -> ProfData -> IO ()
forall a b. a -> b -> a
const (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()))

generateJsonValidate :: (ProfData -> IO ()) -> FilePath
                     -> Args -> IO (Header, Value, Maybe Value, Maybe Html)
generateJsonValidate :: (ProfData -> IO ())
-> FilePath -> Args -> IO (Header, Value, Maybe Value, Maybe Html)
generateJsonValidate ProfData -> IO ()
validate FilePath
file Args
a = do
  let chunk :: FilePath -> IO ProfData
chunk = if Args -> Bool
heapProfile Args
a then FilePath -> IO ProfData
H.chunk else Args -> FilePath -> IO ProfData
E.chunk Args
a
  ProfData
dat <- FilePath -> IO ProfData
chunk FilePath
file
  ProfData -> IO ()
validate ProfData
dat
  Args -> ProfData -> IO (Header, Value, Maybe Value, Maybe Html)
generateJsonData Args
a ProfData
dat