"""Console script for telemetry."""
import sys
import click
import datetime
import telemetry
import os


@click.group()
def cli():
    pass


@click.command()
@click.option(
    "--tdir",
    default="/test/logs/unprocessed",
    help="Path to directory of unprocessed test logs",
)
@click.option("--server", default=None, help="Address of mongo server")
@click.option("--username", default=None, help="Username for mongo server")
@click.option("--password", default=None, help="Password for mongo server")
@click.option("--dbname", default=None, help="Target collection for mongo server")
@click.option("--board", default=None, help="Name of target board")
def prod_logs_upload(tdir, server, username, password, dbname, board):
    """Upload unprocessed test logs to mongo for synchrona."""
    sync = telemetry.prod.BoardLog(server, username, password, dbname, board)
    sync.default_unprocessed_log_dir = tdir
    sync.default_processed_log_dir = os.path.join(tdir, "processed")
    if not os.path.isdir(sync.default_processed_log_dir):
        os.mkdir(sync.default_processed_log_dir)
    sync() # go go


@click.command()
@click.option("--server", default="picard", help="Address of Elasticsearch server")
@click.option(
    "--filename",
    default="resource_utilization.csv",
    help="Full path to resource utilization csv file generated by HDL builds",
)
def log_hdl_resources_from_csv(server, filename):
    tel = telemetry.ingest(server=server)
    tel.log_hdl_resources_from_csv(filename)


@click.command()
@click.option("--server", default="picard", help="Address of Elasticsearch server")
@click.argument("in_args", nargs=-1)
def log_artifacts(server, in_args):
    entry = {
        "url": "NA",
        "server": "NA",
        "job": "NA",
        "job_no": 0,
        "job_date": None,
        "job_build_parameters": "NA",
        "file_name": "NA",
        "target_board": "NA",
        "artifact_info_type": "NA",
        "payload_raw": "NA",
        "payload_ts": "NA",
        "payload": "NA",
        "payload_param": "NA"
    }
    if len(in_args) == 0:
        click.echo("Must have non-zero arguments for database entry")
        sys.exit(1)
    if int(len(in_args) / 2) != len(in_args) / 2:
        click.echo(
            "ERROR: Number of inputs arguments must be even\n"
            + "       and in the form of: entry1<space>value1<space>entry2<space>value2"
        )
        sys.exit(1)
    for i in range(0, len(in_args), 2):
        if in_args[i] in entry:
            if in_args[i + 1].lower() == "true":
                entry[in_args[i]] = True
            elif in_args[i + 1].lower() == "false":
                entry[in_args[i]] = False
            else:
                entry[in_args[i]] = in_args[i + 1]
        else:
            click.echo("ERROR: " + in_args[i] + " not a valid entry")
            sys.exit(1)
    tel = telemetry.ingest(server=server)
    tel.log_artifacts(**entry)

@click.command()
@click.option("--jenkins-server", required=True, help="Address of Jenkins server")
@click.option("--es-server", required=True, help="Address of Elasticsearch server")
@click.option("--job-name", default="HW_tests/HW_test_multiconfig", help="Name of Jenkins job")
@click.option("--job", multiple=True, help="Job(s)/build(s) to process")
def grab_and_log_artifacts(jenkins_server, es_server, job_name, job):
    if not len(job) > 0:
        click.echo("Atleast 1 Job/Build (--job) is needed.")
        sys.exit(1)
    g = telemetry.gargantua(
        jenkins_server,
        es_server,
        job_name,
        job
    )
    g.log_artifacts()

@click.command()
@click.option("--server", default="picard", help="Address of Elasticsearch server")
@click.argument("in_args", nargs=-1)
def log_boot_logs(server, in_args):
    entry = {
        "boot_folder_name": "NA",
        "hdl_hash": "NA",
        "linux_hash": "NA",
        "boot_partition_hash": "NA",
        "hdl_branch": "NA",
        "linux_branch": "NA",
        "boot_partition_branch": "NA",
        "is_hdl_release": False,
        "is_linux_release": False,
        "is_boot_partition_release" : False,
        "uboot_reached": False,
        "linux_prompt_reached": False,
        "drivers_enumerated": 0,
        "drivers_missing": 0,
        "dmesg_warnings_found": 0,
        "dmesg_errors_found": 0,
        "jenkins_job_date": datetime.datetime.now(),
        "jenkins_build_number": 0,
        "jenkins_project_name": 0,
        "jenkins_agent": "NA",
        "jenkins_trigger": "NA",
        "pytest_errors": 0,
        "pytest_failures": 0,
        "pytest_skipped": 0,
        "pytest_tests": 0,
        "matlab_errors": 0,
        "matlab_failures": 0,
        "matlab_skipped": 0,
        "matlab_tests": 0,
        "last_failing_stage" : "NA",
        "last_failing_stage_failure": "NA"
    }
    if len(in_args) == 0:
        click.echo("Must have non-zero arguments for database entry")
        sys.exit(1)
    if int(len(in_args) / 2) != len(in_args) / 2:
        click.echo(
            "ERROR: Number of inputs arguments must be even\n"
            + "       and in the form of: entry1<space>value1<space>entry2<space>value2"
        )
        sys.exit(1)
    for i in range(0, len(in_args), 2):
        if in_args[i] in entry:
            if in_args[i + 1].lower() == "true":
                entry[in_args[i]] = True
            elif in_args[i + 1].lower() == "false":
                entry[in_args[i]] = False
            else:
                entry[in_args[i]] = in_args[i + 1]
        else:
            click.echo("ERROR: " + in_args[i] + " not a valid entry")
            sys.exit(1)
    tel = telemetry.ingest(server=server)
    tel.log_boot_tests(**entry)


@click.command()
def main(args=None):
    """Console script for telemetry."""
    click.echo("Replace this message by putting your code into " "telemetry.cli.main")
    click.echo("See click documentation at https://click.palletsprojects.com/")
    return 0


cli.add_command(prod_logs_upload)
cli.add_command(log_boot_logs)
cli.add_command(log_hdl_resources_from_csv)
cli.add_command(log_artifacts)
cli.add_command(grab_and_log_artifacts)
cli.add_command(main)

if __name__ == "__main__":
    sys.exit(cli())  # pragma: no cover

"""Console script for telemetry."""
import sys
import click
import datetime
import telemetry
import os


@click.group()
def cli():
    pass


@click.command()
@click.option(
    "--tdir",
    default="/test/logs/unprocessed",
    help="Path to directory of unprocessed test logs",
)
@click.option("--server", default=None, help="Address of mongo server")
@click.option("--username", default=None, help="Username for mongo server")
@click.option("--password", default=None, help="Password for mongo server")
@click.option("--dbname", default=None, help="Target collection for mongo server")
@click.option("--board", default=None, help="Name of target board")
def prod_logs_upload(tdir, server, username, password, dbname, board):
    """Upload unprocessed test logs to mongo for synchrona."""
    sync = telemetry.prod.BoardLog(server, username, password, dbname, board)
    sync.default_unprocessed_log_dir = tdir
    sync.default_processed_log_dir = os.path.join(tdir, "processed")
    if not os.path.isdir(sync.default_processed_log_dir):
        os.mkdir(sync.default_processed_log_dir)
    sync() # go go


@click.command()
@click.option("--server", default="picard", help="Address of Elasticsearch server")
@click.option(
    "--filename",
    default="resource_utilization.csv",
    help="Full path to resource utilization csv file generated by HDL builds",
)
def log_hdl_resources_from_csv(server, filename):
    tel = telemetry.ingest(server=server)
    tel.log_hdl_resources_from_csv(filename)


@click.command()
@click.option("--server", default="picard", help="Address of Elasticsearch server")
@click.argument("in_args", nargs=-1)
def log_artifacts(server, in_args):
    entry = {
        "url": "NA",
        "server": "NA",
        "job": "NA",
        "job_no": 0,
        "job_date": None,
        "job_build_parameters": "NA",
        "file_name": "NA",
        "target_board": "NA",
        "artifact_info_type": "NA",
        "payload_raw": "NA",
        "payload_ts": "NA",
        "payload": "NA",
        "payload_param": "NA"
    }
    if len(in_args) == 0:
        click.echo("Must have non-zero arguments for database entry")
        sys.exit(1)
    if int(len(in_args) / 2) != len(in_args) / 2:
        click.echo(
            "ERROR: Number of inputs arguments must be even\n"
            + "       and in the form of: entry1<space>value1<space>entry2<space>value2"
        )
        sys.exit(1)
    for i in range(0, len(in_args), 2):
        if in_args[i] in entry:
            if in_args[i + 1].lower() == "true":
                entry[in_args[i]] = True
            elif in_args[i + 1].lower() == "false":
                entry[in_args[i]] = False
            else:
                entry[in_args[i]] = in_args[i + 1]
        else:
            click.echo("ERROR: " + in_args[i] + " not a valid entry")
            sys.exit(1)
    tel = telemetry.ingest(server=server)
    tel.log_artifacts(**entry)

@click.command()
@click.option("--jenkins-server", required=True, help="Address of Jenkins server")
@click.option("--es-server", required=True, help="Address of Elasticsearch server")
@click.option("--job-name", default="HW_tests/HW_test_multiconfig", help="Name of Jenkins job")
@click.option("--job", multiple=True, help="Job(s)/build(s) to process")
def grab_and_log_artifacts(jenkins_server, es_server, job_name, job):
    if not len(job) > 0:
        click.echo("Atleast 1 Job/Build (--job) is needed.")
        sys.exit(1)
    g = telemetry.gargantua(
        jenkins_server,
        es_server,
        job_name,
        job
    )
    g.log_artifacts()

@click.command()
@click.option("--server", default="picard", help="Address of Elasticsearch server")
@click.argument("in_args", nargs=-1)
def log_boot_logs(server, in_args):
    entry = {
        "boot_folder_name": "NA",
        "hdl_hash": "NA",
        "linux_hash": "NA",
        "boot_partition_hash": "NA",
        "hdl_branch": "NA",
        "linux_branch": "NA",
        "boot_partition_branch": "NA",
        "is_hdl_release": False,
        "is_linux_release": False,
        "is_boot_partition_release" : False,
        "uboot_reached": False,
        "linux_prompt_reached": False,
        "drivers_enumerated": 0,
        "drivers_missing": 0,
        "dmesg_warnings_found": 0,
        "dmesg_errors_found": 0,
        "jenkins_job_date": datetime.datetime.now(),
        "jenkins_build_number": 0,
        "jenkins_project_name": 0,
        "jenkins_agent": "NA",
        "jenkins_trigger": "NA",
        "pytest_errors": 0,
        "pytest_failures": 0,
        "pytest_skipped": 0,
        "pytest_tests": 0,
        "matlab_errors": 0,
        "matlab_failures": 0,
        "matlab_skipped": 0,
        "matlab_tests": 0,
        "last_failing_stage" : "NA",
        "last_failing_stage_failure": "NA"
    }
    if len(in_args) == 0:
        click.echo("Must have non-zero arguments for database entry")
        sys.exit(1)
    if int(len(in_args) / 2) != len(in_args) / 2:
        click.echo(
            "ERROR: Number of inputs arguments must be even\n"
            + "       and in the form of: entry1<space>value1<space>entry2<space>value2"
        )
        sys.exit(1)
    for i in range(0, len(in_args), 2):
        if in_args[i] in entry:
            if in_args[i + 1].lower() == "true":
                entry[in_args[i]] = True
            elif in_args[i + 1].lower() == "false":
                entry[in_args[i]] = False
            else:
                entry[in_args[i]] = in_args[i + 1]
        else:
            click.echo("ERROR: " + in_args[i] + " not a valid entry")
            sys.exit(1)
    tel = telemetry.ingest(server=server)
    tel.log_boot_tests(**entry)


@click.command()
def main(args=None):
    """Console script for telemetry."""
    click.echo("Replace this message by putting your code into " "telemetry.cli.main")
    click.echo("See click documentation at https://click.palletsprojects.com/")
    return 0


cli.add_command(prod_logs_upload)
cli.add_command(log_boot_logs)
cli.add_command(log_hdl_resources_from_csv)
cli.add_command(log_artifacts)
cli.add_command(grab_and_log_artifacts)
cli.add_command(main)

if __name__ == "__main__":
    sys.exit(cli())  # pragma: no cover

Console script for telemetry. Upload unprocessed test logs to mongo for synchrona. # go go Console script for telemetry. # pragma: no cover