/
tables.jl
48 lines (39 loc) · 1.31 KB
/
tables.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
TableStorage
LogWindow-compatible aggregator which stores logs in a Tables.jl-compatible
sink.
Using a `TableStorage` is reasonably simple:
```julia
ml = TimespanLogging.MultiEventLog()
... # Add some events
lw = TimespanLogging.LogWindow(5*10^9, :core)
# Create a DataFrame with one Any[] for each event
df = DataFrame([key=>[] for key in keys(ml.consumers)]...)
# Create the TableStorage and register its creation handler
ts = DaggerWebDash.TableStorage(df)
push!(lw.creation_handlers, ts)
ml.aggregators[:lw] = lw
# Logs will now be saved into `df` automatically, and packages like
# DaggerWebDash.jl will automatically use it to retrieve subsets of the logs.
```
"""
struct TableStorage{T}
sink::T
function TableStorage(sink::T) where T
@assert Tables.istable(sink)
new{T}(sink)
end
end
TimespanLogging.init_similar(ts::TableStorage) = TableStorage(similar(ts.sink, 0))
function TimespanLogging.Events.creation_hook(ts::TableStorage, log)
try
push!(ts.sink, NamedTuple(log))
catch err
rethrow(err)
end
end
function Base.getindex(ts::TableStorage, ts_range::UnitRange)
ts_low, ts_high = ts_range.start, ts_range.stop
return filter(row->ts_low <= row.core.timestamp <= ts_high,
Tables.rows(ts.sink)) |> Tables.materializer(ts.sink)
end