status_feed.py

Script · code/module-04/status_feed.py

Download status_feed.py

"""Status feed: writes charger status changes, keyed by charger id, at a steady rate.

It plays the changes that follow the first --after changes of the scenario (default 3,000), so a
notebook can write those first and then let this feed continue. The changes are always the same
for a given seed, so a notebook can rebuild what the feed sent from the count in its last line.
Run it from the code/ folder and stop it with Ctrl+C:

    python module-04/status_feed.py --topic m04l03-charger-status --rate 2
"""
import argparse
import signal
import sys
import time
from datetime import datetime
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from confluent_kafka import Producer  # noqa: E402
from common import settings  # noqa: E402
from common.scenario import Brightlane, to_json  # noqa: E402


def feed_changes(after=3000, count=5000):
    """The status changes this feed sends, in order."""
    return Brightlane().status_changes(after + count)[after:]


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--topic", default="m04l03-charger-status")
    ap.add_argument("--rate", type=float, default=2, help="changes per second (default 2)")
    ap.add_argument("--after", type=int, default=3000, help="skip the scenario's first N changes (default 3000)")
    args = ap.parse_args()

    stop = False

    def on_signal(*_):
        nonlocal stop
        stop = True

    signal.signal(signal.SIGINT, on_signal)
    signal.signal(signal.SIGTERM, on_signal)

    producer = Producer(settings.PRODUCER_DEFAULTS)
    changes = feed_changes(args.after)
    print(f"{datetime.now():%H:%M:%S} status feed: {args.rate:g} changes/s -> {args.topic}", flush=True)
    start = time.monotonic()
    sent = 0
    for change in changes:
        if stop:
            break
        time.sleep(max(0.0, start + sent / args.rate - time.monotonic()))
        producer.produce(args.topic, key=change["charger_id"], value=to_json(change))
        producer.poll(0)
        sent += 1
    left = producer.flush(15)
    print(f"{datetime.now():%H:%M:%S} status feed stopped: sent={sent} not delivered={left}", flush=True)


if __name__ == "__main__":
    main()