{-# 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
$cshowsPrec :: Int -> VegaEntry -> ShowS
showsPrec :: Int -> VegaEntry -> ShowS
$cshow :: VegaEntry -> String
show :: VegaEntry -> String
$cshowList :: [VegaEntry] -> ShowS
showList :: [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
$ctoJSON :: VegaEntry -> Value
toJSON :: VegaEntry -> Value
$ctoEncoding :: VegaEntry -> Encoding
toEncoding :: VegaEntry -> Encoding
$ctoJSONList :: [VegaEntry] -> Value
toJSONList :: [VegaEntry] -> Value
$ctoEncodingList :: [VegaEntry] -> Encoding
toEncodingList :: [VegaEntry] -> Encoding
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
$cfrom :: forall x. VegaEntry -> Rep VegaEntry x
from :: forall x. VegaEntry -> Rep VegaEntry x
$cto :: forall x. Rep VegaEntry x -> VegaEntry
to :: forall x. Rep VegaEntry x -> VegaEntry
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 i. Ix i => UArray i Double -> (i, i)
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
$cshowsPrec :: Int -> VegaTrace -> ShowS
showsPrec :: Int -> VegaTrace -> ShowS
$cshow :: VegaTrace -> String
show :: VegaTrace -> String
$cshowList :: [VegaTrace] -> ShowS
showList :: [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
$ctoJSON :: VegaTrace -> Value
toJSON :: VegaTrace -> Value
$ctoEncoding :: VegaTrace -> Encoding
toEncoding :: VegaTrace -> Encoding
$ctoJSONList :: [VegaTrace] -> Value
toJSONList :: [VegaTrace] -> Value
$ctoEncodingList :: [VegaTrace] -> Encoding
toEncodingList :: [VegaTrace] -> Encoding
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
$cfrom :: forall x. VegaTrace -> Rep VegaTrace x
from :: forall x. VegaTrace -> Rep VegaTrace x
$cto :: forall x. Rep VegaTrace x -> VegaTrace
to :: forall x. Rep VegaTrace x -> VegaTrace
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
$cshowsPrec :: Int -> VegaHeap -> ShowS
showsPrec :: Int -> VegaHeap -> ShowS
$cshow :: VegaHeap -> String
show :: VegaHeap -> String
$cshowList :: [VegaHeap] -> ShowS
showList :: [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
$ctoJSON :: VegaHeap -> Value
toJSON :: VegaHeap -> Value
$ctoEncoding :: VegaHeap -> Encoding
toEncoding :: VegaHeap -> Encoding
$ctoJSONList :: [VegaHeap] -> Value
toJSONList :: [VegaHeap] -> Value
$ctoEncodingList :: [VegaHeap] -> Encoding
toEncodingList :: [VegaHeap] -> Encoding
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
$cfrom :: forall x. VegaHeap -> Rep VegaHeap x
from :: forall x. VegaHeap -> Rep VegaHeap x
$cto :: forall x. Rep VegaHeap x -> VegaHeap
to :: forall x. Rep VegaHeap x -> VegaHeap
Generic)

heapToVega :: HeapInfo -> [VegaHeap]
heapToVega :: HeapInfo -> [VegaHeap]
heapToVega HeapInfo{[HeapSample]
heapSizeSamples :: [HeapSample]
blocksSizeSamples :: [HeapSample]
liveBytesSamples :: [HeapSample]
heapSizeSamples :: HeapInfo -> [HeapSample]
blocksSizeSamples :: HeapInfo -> [HeapSample]
liveBytesSamples :: HeapInfo -> [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]