#!/usr/bin/python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import with_statement

import argparse
import json

from plano import *

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("results_dir", metavar="RESULTS-DIR")

    args = parser.parse_args()

    for name in list_dir(args.results_dir):
        server, client = name.split("--")
        result_file = join(args.results_dir, name, "result.txt")
        data_dir = join(args.results_dir, name, "data")
        summary_file = join(data_dir, "receiver-summary.json")

        if not exists(summary_file):
            continue

        with open(summary_file, "rb") as f:
            data = json.load(f)

        message_rate = data["results"]["message_rate"]
        latency_average = data["results"]["latency_average"]
        latency_nines = data["results"]["latency_nines"]

        record = [
            server,
            client,
            message_rate,
            latency_average,
            latency_nines[0],
            latency_nines[1],
            latency_nines[2],
            latency_nines[3],
        ]

        record = map(str, record)
        record = ",".join(record)
        
        print(record)

try:
    main()
except KeyboardInterrupt:
    pass
