StatsLogReader

Class that iterarates through a char stream and extracts the StatsLines

Constructors

this
this(InputStream stream)

Constructor

Members

Functions

last
const(StatsLine) last()

Get the last line from the stats

opApply
int opApply(int delegate(ref const(StatsLine)) dg)
int opApply(int delegate(ref size_t index, ref const(StatsLine)) dg)

Enables 'foreach' iteration over the stat lines.

Examples

Read a list of stats

auto data = new Array(
    "2018-09-12 10:03:07,598 2018-09-12 10:03:07,598 cpu_usage:1 memory:3\n" ~
    "2018-09-12 10:03:07,598 2018-09-12 10:03:07,598 cpu_usage:2 memory:4\n".dup);

auto reader = new StatsLogReader(data);

/// Iteration without index
size_t index;
foreach (line; reader)
{
    if (index == 0)
    {
        test!("==")(line["cpu_usage"], "1");
        test!("==")(line["memory"], "3");
    }
    else
    {
        test!("==")(line["cpu_usage"], "2");
        test!("==")(line["memory"], "4");
    }
    index++;
}

test!("==")(index, 2);

/// Iteration with index
data = new Array(
    "2018-09-12 10:03:07,598 2018-09-12 10:03:07,598 cpu_usage:1 memory:3\n" ~
    "2018-09-12 10:03:07,598 2018-09-12 10:03:07,598 cpu_usage:2 memory:4\n".dup);
reader = new StatsLogReader(data);
index = 0;
foreach (i, line; reader)
{
    if (i == 0)
    {
        test!("==")(line["cpu_usage"], "1");
        test!("==")(line["memory"], "3");
    }
    else
    {
        test!("==")(line["cpu_usage"], "2");
        test!("==")(line["memory"], "4");
    }

    index = i;
}

test!("==")(index, 1);

Get the last line

auto data = new Array(
    "2018-09-12 10:03:07,598 2018-09-12 10:03:07,598 cpu_usage:1 memory:3\n" ~
    "2018-09-12 10:03:07,598 2018-09-12 10:03:07,598 cpu_usage:2 memory:4\n".dup);

/// It should be able to get the last line with a function call
auto reader = new StatsLogReader(data);
auto line = reader.last();

test!("==")(line["cpu_usage"], "2");
test!("==")(line["memory"], "4");

/// It should raise an error for an empty string
data = new Array("".dup);
reader = new StatsLogReader(data);

testThrown!(Exception)(reader.last());

Meta