#!/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))
