Aller au contenu

La version 0.1.0 est une première version, à l'essai. Voir ce qui n'est pas prouvé →

bogolang
EN

← Exemples

report_chart

Transforme une journée de mesures d'une liaison montante en un tableau, un fichier Excel, un graphique et un rapport HTML.

Ce programme prend une journée de relevés horaires d'une liaison montante, les affiche sous forme de tableau et trouve les heures chargées. Il écrit les relevés dans un fichier Excel, dessine un graphique en barres du trafic de pointe, et remplit un rapport HTML avec les chiffres, le graphique et le tableau. Les relevés sont inventés. Aucun équipement n'est contacté et aucun courriel n'est envoyé.

Exécutez-le depuis ce dossier :

bogo run report_chart.bogo
bogo test report_chart.bogo

Les fichiers vont dans le dossier out : readings.xlsx, peak.png et report.html.

Pour l'utiliser sur votre propre réseau, remplissez la liste readings à partir de vos propres données, par exemple d'un fichier CSV ou de snmp_get, et réglez link, capacity et limit. La mise en page du rapport est dans template.html. Un emplacement s'écrit {name}. Une vraie accolade, comme dans la feuille de style, s'écrit deux fois : {{ et }}. Un rapport Word fonctionne de la même façon à partir d'un modèle .docx.

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"