{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE RecordWildCards #-}
module Eventlog.Vega (bandsToVega, tracesToVega, heapToVega) where

import Data.Array.Base ((!), bounds)
import Data.Array.Unboxed (UArray)
import Data.Map (Map,  foldr)
import Prelude hiding (lookup, lines, words, length)
import Data.Text (Text)
import Eventlog.Types
import Data.Aeson hiding (Series)
import GHC.Generics
import Data.Word

data VegaEntry = VegaEntry { VegaEntry -> Double
x :: Double, VegaEntry -> Double
y :: Double, VegaEntry -> Int
k :: Int, VegaEntry -> Text
c :: Text }
  deriving (Int -> VegaEntry -> ShowS
[VegaEntry] -> ShowS
VegaEntry -> String
(Int -> VegaEntry -> ShowS)
-> (VegaEntry -> String)
-> ([VegaEntry] -> ShowS)
-> Show VegaEntry
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VegaEntry] -> ShowS
$cshowList :: [VegaEntry] -> ShowS
show :: VegaEntry -> String
$cshow :: VegaEntry -> String
showsPrec :: Int -> VegaEntry -> ShowS
$cshowsPrec :: Int -> VegaEntry -> ShowS
Show, [VegaEntry] -> Encoding
[VegaEntry] -> Value
VegaEntry -> Encoding
VegaEntry -> Value
(VegaEntry -> Value)
-> (VegaEntry -> Encoding)
-> ([VegaEntry] -> Value)
-> ([VegaEntry] -> Encoding)
-> ToJSON VegaEntry
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [VegaEntry] -> Encoding
$ctoEncodingList :: [VegaEntry] -> Encoding
toJSONList :: [VegaEntry] -> Value
$ctoJSONList :: [VegaEntry] -> Value
toEncoding :: VegaEntry -> Encoding
$ctoEncoding :: VegaEntry -> Encoding
toJSON :: VegaEntry -> Value
$ctoJSON :: VegaEntry -> Value
ToJSON, (forall x. VegaEntry -> Rep VegaEntry x)
-> (forall x. Rep VegaEntry x -> VegaEntry) -> Generic VegaEntry
forall x. Rep VegaEntry x -> VegaEntry
forall x. VegaEntry -> Rep VegaEntry x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep VegaEntry x -> VegaEntry
$cfrom :: forall x. VegaEntry -> Rep VegaEntry x
Generic)

bandsToVega :: Map Bucket (Int, BucketInfo)
            -> (UArray Int Double, UArray (Int, Int) Double)
            -> [VegaEntry]
bandsToVega :: Map Bucket (Int, BucketInfo)
-> (UArray Int Double, UArray (Int, Int) Double) -> [VegaEntry]
bandsToVega Map Bucket (Int, BucketInfo)
ks (UArray Int Double
ts, UArray (Int, Int) Double
vs) =
  let (Int
t1, Int
tn) = UArray Int Double -> (Int, Int)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds UArray Int Double
ts
      go :: (Int, BucketInfo) -> [VegaEntry] -> [VegaEntry]
go (Int
i, BucketInfo
binfo) [VegaEntry]
rs = [VegaEntry]
go_1 [VegaEntry] -> [VegaEntry] -> [VegaEntry]
forall a. [a] -> [a] -> [a]
++ [VegaEntry]
rs
        where
          txt :: Text
txt = BucketInfo -> Text
shortDescription BucketInfo
binfo

          go_1 :: [VegaEntry]
          go_1 :: [VegaEntry]
go_1 = ((Int -> VegaEntry) -> [Int] -> [VegaEntry])
-> [Int] -> (Int -> VegaEntry) -> [VegaEntry]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Int -> VegaEntry) -> [Int] -> [VegaEntry]
forall a b. (a -> b) -> [a] -> [b]
map [Int
t1 .. Int
tn] ((Int -> VegaEntry) -> [VegaEntry])
-> (Int -> VegaEntry) -> [VegaEntry]
forall a b. (a -> b) -> a -> b
$ \Int
t -> Double -> Double -> Int -> Text -> VegaEntry
VegaEntry (UArray Int Double
ts UArray Int Double -> Int -> Double
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
t) (UArray (Int, Int) Double
vs UArray (Int, Int) Double -> (Int, Int) -> Double
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! (Int
i, Int
t)) Int
i Text
txt

      other_binfo :: BucketInfo
other_binfo = Text
-> Maybe [Word32]
-> Double
-> Double
-> Maybe (Double, Double, Double)
-> BucketInfo
BucketInfo Text
"OTHER" Maybe [Word32]
forall a. Maybe a
Nothing
                               -- Last two fields currently unused
                               Double
0 Double
0 Maybe (Double, Double, Double)
forall a. Maybe a
Nothing
  in ((Int, BucketInfo) -> [VegaEntry] -> [VegaEntry])
-> [VegaEntry] -> Map Bucket (Int, BucketInfo) -> [VegaEntry]
forall a b k. (a -> b -> b) -> b -> Map k a -> b
Data.Map.foldr (Int, BucketInfo) -> [VegaEntry] -> [VegaEntry]
go ((Int, BucketInfo) -> [VegaEntry] -> [VegaEntry]
go (Int
0, BucketInfo
other_binfo) []) Map Bucket (Int, BucketInfo)
ks

data VegaTrace = VegaTrace { VegaTrace -> Double
tx :: Double, VegaTrace -> Text
desc :: Text }
  deriving (Int -> VegaTrace -> ShowS
[VegaTrace] -> ShowS
VegaTrace -> String
(Int -> VegaTrace -> ShowS)
-> (VegaTrace -> String)
-> ([VegaTrace] -> ShowS)
-> Show VegaTrace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VegaTrace] -> ShowS
$cshowList :: [VegaTrace] -> ShowS
show :: VegaTrace -> String
$cshow :: VegaTrace -> String
showsPrec :: Int -> VegaTrace -> ShowS
$cshowsPrec :: Int -> VegaTrace -> ShowS
Show, [VegaTrace] -> Encoding
[VegaTrace] -> Value
VegaTrace -> Encoding
VegaTrace -> Value
(VegaTrace -> Value)
-> (VegaTrace -> Encoding)
-> ([VegaTrace] -> Value)
-> ([VegaTrace] -> Encoding)
-> ToJSON VegaTrace
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [VegaTrace] -> Encoding
$ctoEncodingList :: [VegaTrace] -> Encoding
toJSONList :: [VegaTrace] -> Value
$ctoJSONList :: [VegaTrace] -> Value
toEncoding :: VegaTrace -> Encoding
$ctoEncoding :: VegaTrace -> Encoding
toJSON :: VegaTrace -> Value
$ctoJSON :: VegaTrace -> Value
ToJSON, (forall x. VegaTrace -> Rep VegaTrace x)
-> (forall x. Rep VegaTrace x -> VegaTrace) -> Generic VegaTrace
forall x. Rep VegaTrace x -> VegaTrace
forall x. VegaTrace -> Rep VegaTrace x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep VegaTrace x -> VegaTrace
$cfrom :: forall x. VegaTrace -> Rep VegaTrace x
Generic)

tracesToVega :: [Trace] -> [VegaTrace]
tracesToVega :: [Trace] -> [VegaTrace]
tracesToVega = (Trace -> VegaTrace) -> [Trace] -> [VegaTrace]
forall a b. (a -> b) -> [a] -> [b]
map (\(Trace Double
t Text
d) -> Double -> Text -> VegaTrace
VegaTrace Double
t Text
d)

data VegaHeap = VegaHeap { VegaHeap -> Double
x :: Double, VegaHeap -> Word64
y :: Word64, VegaHeap -> Text
c :: Text, VegaHeap -> Int
k :: Int}
  deriving (Int -> VegaHeap -> ShowS
[VegaHeap] -> ShowS
VegaHeap -> String
(Int -> VegaHeap -> ShowS)
-> (VegaHeap -> String) -> ([VegaHeap] -> ShowS) -> Show VegaHeap
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VegaHeap] -> ShowS
$cshowList :: [VegaHeap] -> ShowS
show :: VegaHeap -> String
$cshow :: VegaHeap -> String
showsPrec :: Int -> VegaHeap -> ShowS
$cshowsPrec :: Int -> VegaHeap -> ShowS
Show, [VegaHeap] -> Encoding
[VegaHeap] -> Value
VegaHeap -> Encoding
VegaHeap -> Value
(VegaHeap -> Value)
-> (VegaHeap -> Encoding)
-> ([VegaHeap] -> Value)
-> ([VegaHeap] -> Encoding)
-> ToJSON VegaHeap
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [VegaHeap] -> Encoding
$ctoEncodingList :: [VegaHeap] -> Encoding
toJSONList :: [VegaHeap] -> Value
$ctoJSONList :: [VegaHeap] -> Value
toEncoding :: VegaHeap -> Encoding
$ctoEncoding :: VegaHeap -> Encoding
toJSON :: VegaHeap -> Value
$ctoJSON :: VegaHeap -> Value
ToJSON, (forall x. VegaHeap -> Rep VegaHeap x)
-> (forall x. Rep VegaHeap x -> VegaHeap) -> Generic VegaHeap
forall x. Rep VegaHeap x -> VegaHeap
forall x. VegaHeap -> Rep VegaHeap x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep VegaHeap x -> VegaHeap
$cfrom :: forall x. VegaHeap -> Rep VegaHeap x
Generic)

heapToVega :: HeapInfo -> [VegaHeap]
heapToVega :: HeapInfo -> [VegaHeap]
heapToVega HeapInfo{[HeapSample]
liveBytesSamples :: HeapInfo -> [HeapSample]
blocksSizeSamples :: HeapInfo -> [HeapSample]
heapSizeSamples :: HeapInfo -> [HeapSample]
liveBytesSamples :: [HeapSample]
blocksSizeSamples :: [HeapSample]
heapSizeSamples :: [HeapSample]
..} = Int -> Text -> [HeapSample] -> [VegaHeap]
mk Int
2 Text
"Heap Size" [HeapSample]
heapSizeSamples [VegaHeap] -> [VegaHeap] -> [VegaHeap]
forall a. [a] -> [a] -> [a]
++ Int -> Text -> [HeapSample] -> [VegaHeap]
mk Int
0 Text
"Live Bytes" [HeapSample]
liveBytesSamples [VegaHeap] -> [VegaHeap] -> [VegaHeap]
forall a. [a] -> [a] -> [a]
++ Int -> Text -> [HeapSample] -> [VegaHeap]
mk Int
1 Text
"Blocks Size" [HeapSample]
blocksSizeSamples

  where
    mk :: Int -> Text -> [HeapSample] -> [VegaHeap]
mk Int
k Text
l [HeapSample]
xs = [Double -> Word64 -> Text -> Int -> VegaHeap
VegaHeap Double
t Word64
v Text
l Int
k | HeapSample Double
t Word64
v <- [HeapSample]
xs]