Skip to content

Version 0.1.0 is a first version, for trial. See what is not proven →

bogolang
FR

← Examples

report_chart

Turns a day of uplink readings into a table, an Excel file, a chart and an HTML report.

This program takes a day of hourly readings of one uplink, prints them as a table and finds the busy hours. It writes the readings to an Excel file, draws a bar chart of the peak traffic, and fills an HTML report with the figures, the chart and the table. The readings are invented. No device is contacted and no mail is sent.

Run it from this folder:

bogo run report_chart.bogo
bogo test report_chart.bogo

The files go to the folder out: readings.xlsx, peak.png and report.html.

To use it on your own network, fill the list readings from your own data, for example from a CSV file or from snmp_get, and set link, capacity and limit. The report layout is in template.html. A hole is written {name}. A real brace, as in the style sheet, is written twice: {{ and }}. A Word report works the same way from a .docx template.

report_chart.bogo
edition 2027
"""Hourly uplink readings as a table, an Excel file and an HTML report with a chart."""

from pathlib import Path
from typing import NamedTuple


class Reading(NamedTuple):
    """One hourly reading of an uplink."""

    hour: str
    peak: Rate
    loss: Percent
    latency: Duration


class ReportChart:
    """Makes the day report of one uplink. The readings are invented for the example."""

    link = "edge-rt2 Gi0/0/1"
    capacity = 100Mbps
    limit = 0.8  # an hour above 80 percent of the capacity is busy
    readings: list[Reading] = [
        Reading("06:00", 12Mbps, 0pct, 21ms),
        Reading("08:00", 48Mbps, 0pct, 23ms),
        Reading("10:00", 86Mbps, 0.4pct, 41ms),
        Reading("12:00", 71Mbps, 0.1pct, 30ms),
        Reading("14:00", 92Mbps, 1.2pct, 58ms),
        Reading("16:00", 64Mbps, 0pct, 26ms),
        Reading("18:00", 35Mbps, 0pct, 22ms),
        Reading("20:00", 18Mbps, 0pct, 21ms),
    ]

    @staticmethod
    def busy(readings: list[Reading], capacity: Rate, limit: float) -> list[Reading]:
        return [r for r in readings if r.peak / capacity > limit]

    @staticmethod
    def busiest(readings: list[Reading]) -> Reading:
        top = readings[0]
        for r in readings:
            if r.peak > top.peak:
                top = r
        return top

    @staticmethod
    def main(args: list[str]) -> None:
        c = ReportChart
        out = Path("out")
        out.mkdir(exist_ok=True)
        table(c.readings)
        busy = c.busy(c.readings, c.capacity, c.limit)
        top = c.busiest(c.readings)
        print(f"Busy hours: {', '.join(r.hour for r in busy) or 'none'}")

        export(c.readings, str(out / "readings.xlsx"))
        drawn = chart(
            c.readings,
            x="hour",
            y="peak",
            path=str(out / "peak.png"),
            kind="bar",
            title=f"Peak traffic on {c.link}",
        )
        report(
            "template.html",
            str(out / "report.html"),
            link=c.link,
            capacity=c.capacity,
            busiest=top.hour,
            peak=top.peak,
            limit=f"{c.limit:.0%}",
            busy_hours=len(busy),
            chart=drawn,
            readings=c.readings,
        )
        print(f"Written: {out / 'readings.xlsx'}, {out / 'peak.png'}, {out / 'report.html'}")


class TestReportChart:
    def test_busy_hours_are_above_the_limit(self) -> None:
        hours = [r.hour for r in ReportChart.busy(ReportChart.readings, 100Mbps, 0.8)]
        assert hours == ["10:00", "14:00"]

    def test_a_bigger_link_has_no_busy_hour(self) -> None:
        assert ReportChart.busy(ReportChart.readings, 1Gbps, 0.8) == []

    def test_busiest_hour(self) -> None:
        assert ReportChart.busiest(ReportChart.readings).hour == "14:00"