#!/usr/bin/env python3

import sys
import re
import argparse
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D


class Point:
    "CC event"

    def __init__(self, x, y):
        self.x = x
        self.y = y


def listx(points):
    return list(map(lambda pt: pt.x, points))


def listy(points):
    return list(map(lambda pt: pt.y, points))


def plot_data(d):
    plt.figure(1)

    cwndx = listx(d["cwnd"])
    cwndy = listy(d["cwnd"])
    congx = listx(d["congestion"])
    congy = listy(d["congestion"])
    rcvrdx = listx(d["recovered"])
    rcvrdy = listy(d["recovered"])
    rxttx = listx(d["rxtTimeout"])
    rxtty = listy(d["rxtTimeout"])

    # cwnd/ssthresh/cc events
    plt.subplot(311)
    plt.title("cwnd/ssthresh")
    pcwnd = plt.plot(cwndx, cwndy, "r")
    psst = plt.plot(cwndx, d["ssthresh"], "y-")
    pcong = plt.plot(congx, congy, "yo")
    precov = plt.plot(rcvrdx, rcvrdy, "co")
    prxtt = plt.plot(rxttx, rxtty, "mo")

    marker1 = Line2D(range(1), range(1), color="r")
    marker2 = Line2D(range(1), range(1), color="y")
    marker3 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="y")
    marker4 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="c")
    marker5 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="m")
    plt.legend(
        (marker1, marker2, marker3, marker4, marker5),
        ("cwnd", "ssthresh", "congestion", "recovered", "rxt-timeout"),
        loc=4,
    )
    axes = plt.gca()
    axes.set_ylim([-20e4, max(cwndy) + 20e4])

    # snd variables
    plt.subplot(312)
    plt.title("cc variables")
    plt.plot(cwndx, d["space"], "g-", markersize=1)
    plt.plot(cwndx, d["flight"], "b-", markersize=1)
    plt.plot(cwndx, d["sacked"], "m:", markersize=1)
    plt.plot(cwndx, d["lost"], "y:", markersize=1)
    plt.plot(cwndx, d["cc-space"], "k:", markersize=1)
    plt.plot(cwndx, cwndy, "ro", markersize=2)

    plt.plot(congx, congy, "y^", markersize=10, markerfacecolor="y")
    plt.plot(rcvrdx, rcvrdy, "c^", markersize=10, markerfacecolor="c")
    plt.plot(rxttx, rxtty, "m^", markersize=10, markerfacecolor="m")

    # plt.plot(cwndx, d["snd_wnd"], 'ko', markersize=1)
    plt.legend(
        (
            "snd-space",
            "flight",
            "sacked",
            "lost",
            "cc-space",
            "cwnd",
            "congestion",
            "recovered",
            "rxt-timeout",
        ),
        loc=1,
    )

    # rto/srrt/rttvar
    plt.subplot(313)
    plt.title("rtt")
    plt.plot(cwndx, d["srtt"], "g-")
    plt.plot(cwndx, [x / 1000 for x in d["mrtt-us"]], "r-")
    plt.plot(cwndx, d["rttvar"], "b-")
    plt.legend(["srtt", "mrtt-us", "rttvar"])
    axes = plt.gca()
    # plt.plot(cwndx, rto, 'r-')
    # axes.set_ylim([0, int(max(rto[2:len(rto)])) + 50])

    # show
    plt.show()


def find_pattern(file_path, session_idx):
    is_active_open = 1
    listener_pattern = "l\[\d\]"
    if is_active_open:
        initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s+open:\s"
    else:
        initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s"
    idx = 0
    f = open(file_path, "r")
    for line in f:
        # skip listener lines (server)
        if re.search(listener_pattern, line) != None:
            continue
        match = re.search(initial_pattern, line)
        if match == None:
            continue
        if idx < session_idx:
            idx += 1
            continue
        filter_pattern = str(match.group(1)) + "\s+(.+)"
        print("pattern is %s" % filter_pattern)
        f.close()
        return filter_pattern
    raise Exception("Could not find initial pattern")


def compute_time(min, sec, msec):
    return int(min) * 60 + int(sec) + int(msec) / 1000.0


def run(file_path, session_idx):
    filter_sessions = 1
    filter_pattern = ""

    patterns = {
        "time": "^\d+:(\d+):(\d+):(\d+):\d+",
        "listener": "l\[\d\]",
        "cc": "cwnd (\d+) flight (\d+) space (\d+) ssthresh (\d+) snd_wnd (\d+)",
        "cc-snd": "cc_space (\d+) sacked (\d+) lost (\d+)",
        "rtt": "rto (\d+) srtt (\d+) mrtt-us (\d+) rttvar (\d+)",
        "rxtt": "rxt-timeout",
        "congestion": "congestion",
        "recovered": "recovered",
    }
    d = {
        "cwnd": [],
        "space": [],
        "flight": [],
        "ssthresh": [],
        "snd_wnd": [],
        "cc-space": [],
        "lost": [],
        "sacked": [],
        "rto": [],
        "srtt": [],
        "mrtt-us": [],
        "rttvar": [],
        "rxtTimeout": [],
        "congestion": [],
        "recovered": [],
    }

    if filter_sessions:
        filter_pattern = find_pattern(file_path, session_idx)
    f = open(file_path, "r")

    stats_index = 0
    start_time = 0

    for line in f:
        # skip listener lines (server)
        if re.search(patterns["listener"], line) != None:
            continue
        # filter sessions
        if filter_sessions:
            match = re.search(filter_pattern, line)
            if match == None:
                continue

        original_line = line
        line = match.group(1)
        match = re.search(patterns["time"], original_line)
        if match == None:
            print("something went wrong! no time!")
            continue
        time = compute_time(match.group(1), match.group(2), match.group(3))
        if start_time == 0:
            start_time = time

        time = time - start_time
        match = re.search(patterns["cc"], line)
        if match != None:
            d["cwnd"].append(Point(time, int(match.group(1))))
            d["flight"].append(int(match.group(2)))
            d["space"].append(int(match.group(3)))
            d["ssthresh"].append(int(match.group(4)))
            d["snd_wnd"].append(int(match.group(5)))
            stats_index += 1
            continue
        match = re.search(patterns["cc-snd"], line)
        if match != None:
            d["cc-space"].append(int(match.group(1)))
            d["sacked"].append(int(match.group(2)))
            d["lost"].append(int(match.group(3)))
        match = re.search(patterns["rtt"], line)
        if match != None:
            d["rto"].append(int(match.group(1)))
            d["srtt"].append(int(match.group(2)))
            d["mrtt-us"].append(int(match.group(3)))
            d["rttvar"].append(int(match.group(4)))
        if stats_index == 0:
            continue
        match = re.search(patterns["rxtt"], line)
        if match != None:
            d["rxtTimeout"].append(Point(time, d["cwnd"][stats_index - 1].y + 1e4))
            continue
        match = re.search(patterns["congestion"], line)
        if match != None:
            d["congestion"].append(Point(time, d["cwnd"][stats_index - 1].y - 1e4))
            continue
        match = re.search(patterns["recovered"], line)
        if match != None:
            d["recovered"].append(Point(time, d["cwnd"][stats_index - 1].y))
            continue

    plot_data(d)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Plot tcp cc logs")
    parser.add_argument(
        "-f", action="store", dest="file", required=True, help="elog file in txt format"
    )
    parser.add_argument(
        "-s",
        action="store",
        dest="session_index",
        default=0,
        help="session index for which to plot cc logs",
    )
    results = parser.parse_args()
    run(results.file, int(results.session_index))
