#!/usr/bin/env pmpython
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name
""" Generate a test archive for testing ranking, predicates, and limit filtering """

from pcp import pmapi, pmi

# Our metrics for testing
metrics = ["disk.dev.read", "disk.dev.write",
           "kernel.pernode.cpu.user", "kernel.pernode.cpu.sys",
           "mem.util.used"]
# Instances / disk
insts_d = ["sda", "sdb", "sdc", "sdd", "sde"]
# Instance / kernel
insts_k = ["cpu0", "cpu1", "cpu2", "cpu3", "cpu4", "cpu5", "cpu6", "cpu7"]

# Get a context
pmfg = pmapi.fetchgroup()
ctx = pmfg.get_context()

# Initialize the archive
arch = pmi.pmiLogImport("rank-pred")
arch.pmiSetHostname("localhost")
arch.pmiSetTimezone("UTC")
for metric in metrics:
    pmids = ctx.pmLookupName(metric)
    descs = ctx.pmLookupDescs(pmids)
    arch.pmiAddMetric(metric,
                      pmids[0],
                      descs[0].contents.type,
                      descs[0].contents.indom,
                      descs[0].contents.sem,
                      descs[0].contents.units)
inst = -1
for name in insts_d:
    inst += 1
    pmids = ctx.pmLookupName("disk.dev.read")
    descs = ctx.pmLookupDescs(pmids)
    indom = descs[0].contents.indom
    arch.pmiAddInstance(indom, name, inst)
inst = -1
for name in insts_k:
    inst += 1
    pmids = ctx.pmLookupName("kernel.pernode.cpu.user")
    descs = ctx.pmLookupDescs(pmids)
    indom = descs[0].contents.indom
    arch.pmiAddInstance(indom, name, inst)

# Timestamp
start = 0
batch = -1

# Start values
for _ in range(2):
    batch += 1
    value = batch
    for name in insts_d:
        arch.pmiPutValue("disk.dev.read", name, "%d" % value)
        arch.pmiPutValue("disk.dev.write", name, "%d" % value)
    for name in insts_k:
        arch.pmiPutValue("kernel.pernode.cpu.user", name, "%d" % value)
        arch.pmiPutValue("kernel.pernode.cpu.sys", name, "%d" % value)
    arch.pmiPutValue("mem.util.used", None, "%d" % value)
    arch.pmiWrite(start + batch, 0)

# Values for ranking tests
for _ in range(5):
    batch += 1
    for name in insts_d:
        value = batch
        if name == insts_d[1]:
            value *= 2
        if name == insts_d[3]:
            value *= 3
        arch.pmiPutValue("disk.dev.read", name, "%d" % value)
        arch.pmiPutValue("disk.dev.write", name, "%d" % value)
    for name in insts_k:
        value = batch
        if name == insts_k[1]:
            value *= 2
        if name == insts_k[3]:
            value *= 3
        arch.pmiPutValue("kernel.pernode.cpu.user", name, "%d" % value)
        arch.pmiPutValue("kernel.pernode.cpu.sys", name, "%d" % value)
    arch.pmiPutValue("mem.util.used", None, "%d" % batch)
    arch.pmiWrite(start + batch, 0)

# Values for predicate tests
base = 100
for _ in range(5):
    batch += 1

    value = base + batch
    arch.pmiPutValue("disk.dev.read", "sdc", "%d" % value)
    arch.pmiPutValue("kernel.pernode.cpu.sys", "cpu2", "%d" % value)

    for name in insts_d:
        value = batch
        if name == "sdd":
            arch.pmiPutValue("disk.dev.write", name, "%d" % (value + 100))
        else:
            arch.pmiPutValue("disk.dev.write", name, "%d" % value)
        if name == "sdc":
            continue
        arch.pmiPutValue("disk.dev.read", name, "%d" % value)

    for name in insts_k:
        value = batch
        arch.pmiPutValue("kernel.pernode.cpu.user", name, "%d" % value)
        if name == "cpu2":
            continue
        arch.pmiPutValue("kernel.pernode.cpu.sys", name, "%d" % value)

    arch.pmiPutValue("mem.util.used", None, "%d" % batch)
    arch.pmiWrite(start + batch, 0)

# End values before limit tests
for _ in range(2):
    batch += 1
    for name in insts_d:
        arch.pmiPutValue("disk.dev.read", name, "%d" % 1000)
        arch.pmiPutValue("disk.dev.write", name, "%d" % 1000)
    for name in insts_k:
        arch.pmiPutValue("kernel.pernode.cpu.user", name, "%d" % 1000)
        arch.pmiPutValue("kernel.pernode.cpu.sys", name, "%d" % 1000)
    arch.pmiPutValue("mem.util.used", None, "%d" % 1000)
    arch.pmiWrite(start + batch, 0)

# Continue with even values
for _ in range(2):
    batch += 1
    for name in insts_d:
        arch.pmiPutValue("disk.dev.read", name, "%d" % 1000)
        arch.pmiPutValue("disk.dev.write", name, "%d" % 1000)
    for name in insts_k:
        arch.pmiPutValue("kernel.pernode.cpu.user", name, "%d" % 1000)
        arch.pmiPutValue("kernel.pernode.cpu.sys", name, "%d" % 1000)
    arch.pmiPutValue("mem.util.used", None, "%d" % 1000)
    arch.pmiWrite(start + batch, 0)

# End with predicate instances low, others high, all varying
for i in range(2):
    batch += 1
    for j, name in enumerate(insts_d):
        arch.pmiPutValue("disk.dev.read", name, "%d" % (10 + j))
        arch.pmiPutValue("disk.dev.write", name, "%d" % (100 - j))
    for j, name in enumerate(insts_k):
        arch.pmiPutValue("kernel.pernode.cpu.user", name, "%d" % (10 + j))
        arch.pmiPutValue("kernel.pernode.cpu.sys", name, "%d" % (100 - j))
    arch.pmiPutValue("mem.util.used", None, "%d" % batch)
    arch.pmiWrite(start + batch, 0)

# Done
arch.pmiEnd()
