##
# Copyright (c) 2021 Xilinx, Inc.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
# associated documentation files (the "Software"), to deal in the Software without restriction, including 
# without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to 
# the following conditions: 
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial 
# portions of the Software. 
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
#
#
##
#!/usr/bin/env python
import os,sys,inspect,importlib,json,datetime
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
apidir = os.path.join(parentdir,"api")
sys.path.insert(0,parentdir)
from backend import ysocket
from backend import tcpclient
# from backend import ysocket
from backend import header
from backend import rabbitmq_config
from time import sleep
import string
import logging
import threading
from multiprocessing import Process
threadarr = []

# reduce log level

connection=""
channel = ""
def myconverter(o):
    if isinstance(o, datetime.datetime):
        return o.__str__()

def set_bit(v, index, x):
  """Set the index:th bit of v to 1 if x is truthy, else to 0, and return the new value."""
  v = int(v, 16)
  mask = 1 << index   # Compute mask, an integer with just bit 'index' set.
  v &= ~mask          # Clear the bit indicated by the mask (if x is False)
  if x:
    v |= mask         # If x was True, set the bit indicated by the mask.
  p = hex(v)[2:]
  while len(p) <8:
      p = str(0)+p
  return p

def getperf(exchange,log,active_user,client):
    perfArr = {"tabname":exchange["name"]+"_"+active_user, "data":[]}
    for i in range(len(exchange["config"]["instances"])):
        filename = os.path.join(apidir,str(exchange["config"]["file_name"] + str(exchange["config"]["instances"][i]) + ".tcl"))
        mrdNoc = open(filename, "r")
        timenow = datetime.datetime.now()
        overflow = []
        for line in mrdNoc:
            line = line.replace("\n", "")
            keys = line.split(",")
            perfmonData = {}
            perfmonData["data"] = []
            newobj = {}
            newobj["accumulatedlatency"] = (
                tcpclient.getcommandresponse(("mrd 0x%s\n" % str("%x" % (int(keys[5], 16) + int("0", 16)))), log,client))
            newobj["burstcount"] = (
                tcpclient.getcommandresponse(("mrd 0x%s\n" % str("%x" % (int(keys[5], 16) + int("4", 16)))), log,client))
            newobj["transactioncount"] = (
                tcpclient.getcommandresponse(("mrd 0x%s\n" % str("%x" % (int(keys[5], 16) + int("8", 16)))), log,client))
            newobj["time"] = timenow
            perfmonData["module"] = keys[0]
            perfmonData["submodule"] = keys[1]
            perfmonData["monitor"] = keys[2]
            perfmonData["key"] = perfmonData["module"]+"_"+perfmonData["submodule"]+"_"+perfmonData["monitor"]
            if all(c in 'xX' + string.hexdigits for c in newobj["accumulatedlatency"]) and all(c in 'xX' + string.hexdigits for c in newobj["burstcount"]) and all(c in 'xX' + string.hexdigits for c in newobj["transactioncount"]):
                if (int(newobj["accumulatedlatency"],  16) & (1<<31)):
                    overflow.append("accumulatedlatency")
                else:
                    val = "accumulatedlatency"
                    overflow = [value for value in overflow if value != val]
                if (int(newobj["burstcount"],  16) & (1<<31)):
                    overflow.append("burstcount")
                else:
                    val = "burstcount"
                    overflow = [value for value in overflow if value != val]
                if (int(newobj["transactioncount"],  16) & (1<<31)):
                    overflow.append("transactioncount")
                else:
                    val = "transactioncount"
                    overflow = [value for value in overflow if value != val]
                newobj["accumulatedlatency"] = str("%x" % (int(newobj["accumulatedlatency"],  16) & int("0x7FFFFFFF", 16)))
                newobj["burstcount"] = str("%x" % (int(newobj["burstcount"], 16) & int("0x7FFFFFFF", 16)))
                newobj["transactioncount"] = str("%x" % (int(newobj["transactioncount"], 16) & int("0x7FFFFFFF", 16)))
                newobj["overflow"] = overflow
                perfmonData["data"].append(newobj)
                perfArr["data"].append(perfmonData)
            else:
                perfArr["error"] = "Error on reading data from board"

    mrdNoc.close()
    return perfArr


def getlpdcounters(exchange,log,active_user,client):
    filename = os.path.join(apidir,str(header.READ_LPD_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"tabname":"lpd_"+active_user,"data":[]}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now()
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(
                        ("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                        {"time": timenow, str(keys[0]): str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr
def getpmccounters(exchange,log,active_user,client):
    filename = os.path.join(apidir,str(header.READ_PMC_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"tabname":"pmc_"+active_user,"data":[]}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now()
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(
                        ("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] ="Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                    {"time": timenow, str(keys[0]): str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getfpdcounters(exchange,log,active_user,client):
    filename = os.path.join(apidir,str(header.READ_FPD_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"tabname":"fpd_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now()
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time":timenow, str(keys[0]): str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getcpmcounters(exchange,log,active_user,client):
    filename = os.path.join(apidir,str(header.READ_CPM_COUNTERS))
    readcounters = open(filename, "r")
    counterArr = {"tabname":"cpm_"+active_user,"data":[]}
    counterData = {}
    newobj = {}
    counterData["data"] = []
    timenow = datetime.datetime.now()
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[2]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[0])] = str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16)))
                        counterArr["data"][i]["data"][0]["overflow"] = []
                    else:
                        counterArr["error"] ="Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[2])
                obj["key"] = str(keys[2])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append(
                    {"time": timenow, str(keys[0]): str("%x" % (int(tempvar,  16) & int("0x0000FFFF", 16))), "overflow" : []})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getdcperformance(exchange,log,active_user,client):
    filename = os.path.join(apidir, str(header.PERF_DDR_DC))
    readcounters = open(filename, "r")
    counterArr = {"tabname":"dc_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now()
    status = False
    overflow = []
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[0]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        if (int(tempvar,  16) & (1<<31)):
                            counterArr["data"][i]["data"][0]["overflow"].append(str(keys[2]))
                        else:
                            val = str(keys[2])
                            counterArr["data"][i]["data"][0]["overflow"] = [value for value in (counterArr["data"][i]["data"][0]["overflow"]) if value != val]
                        counterArr["data"][i]["data"][0][str(keys[2])] = str("%x" % (int(tempvar,  16) & int("0x7FFFFFFF", 16)))
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[0])
                obj["key"] = str(keys[0])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[1])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    if (int(tempvar,  16) & (1<<31)):
                        overflow.append(str(keys[2]))
                    else:
                        val = str(keys[2])
                        overflow = [value for value in overflow if value != val]
                    obj["data"].append({"time":timenow, str(keys[2]): str("%x" % (int(tempvar,  16) & int("0x7FFFFFFF", 16))), "overflow" : overflow})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getapmperformance(exchange,log,active_user,client):
    filename = os.path.join(apidir,str(header.READ_APM))
    readcounters = open(filename, "r")
    counterArr = {"tabname":"pl_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    timenow = datetime.datetime.now()
    status = False
    for line in readcounters:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if ((len(keys) >= 3) and (line[0] != '#')):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[1]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[0])),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[2])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[1])
                obj["key"] = str(keys[1])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[0])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time": timenow, str(keys[2]): tempvar})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
    readcounters.close()
    return counterArr

def getnocperformance(exchange,log,active_user,client):
    counterArr = {"tabname":"noc_"+active_user,"data":[]}
    timenow = datetime.datetime.now()
    status = False
    dataobj = {}
    filename = os.path.join(apidir, str(header.READ_NMU_REG))
    readnmu = open(filename, "r")
    for line in readnmu:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if (len(keys) >= 3) and (line[0] != "#"):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[1]) and counterArr["data"][i]["key"] == "nmu_"+str(keys[1]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[0])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[2])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False
            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[1])
                obj["key"] = "nmu_"+str(keys[1])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[0])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time":timenow, str(keys[2]):tempvar, "Accumulated_latency":""})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
            for i in range(len(counterArr["data"])):
                if (counterArr["data"][i]["data"][0]) and ("Accumulated_latency_upper" in counterArr["data"][i]["data"][0]) and ("Accumulated_latency_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["Accumulated_latency"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["Accumulated_latency_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["Accumulated_latency_lower"])
                if (counterArr["data"][i]["data"][0]) and ("byte_count_upper" in counterArr["data"][i]["data"][0]) and ("byte_count_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["byte_count"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["byte_count_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["byte_count_lower"])
    readnmu.close()
    status = False
    filename = os.path.join(apidir,str(header.READ_NSU_REG))
    readnsu = open(filename, "r")
    for line in readnsu:
        line = line.replace("\n", "")
        keys = line.split(" ")
        if (len(keys) >= 3) and (line[0] != "#"):
            for i in range(len(counterArr["data"])):
                if counterArr["data"][i]["displayname"] == str(keys[1]) and counterArr["data"][i]["key"] == "nsu_"+str(keys[1]):
                    status = True
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[0])), log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        counterArr["data"][i]["data"][0][str(keys[2])] = tempvar
                    else:
                        counterArr["error"] = "Error on reading data from board"
                    break
                else:
                    status = False

            if not status:
                obj = {}
                obj["data"] = []
                obj["displayname"] = str(keys[1])
                obj["key"] = "nsu_"+str(keys[1])
                tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[0])), log,client)
                if all(c in 'xX' + string.hexdigits for c in tempvar):
                    obj["data"].append({"time":timenow, str(keys[2]):tempvar})
                    counterArr["data"].append(obj)
                else:
                    counterArr["error"] = "Error on reading data from board"
            for i in range(len(counterArr["data"])):
                if (counterArr["data"][i]["data"][0]) and ("Accumulated_latency_upper" in counterArr["data"][i]["data"][0]) and ("Accumulated_latency_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["Accumulated_latency"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["Accumulated_latency_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["Accumulated_latency_lower"])
                if (counterArr["data"][i]["data"][0]) and ("byte_count_upper" in counterArr["data"][i]["data"][0]) and ("byte_count_lower" in counterArr["data"][i]["data"][0]):
                    counterArr["data"][i]["data"][0]["byte_count"] = str(str("%x" % (int(str(counterArr["data"][i]["data"][0]["byte_count_upper"]), 16) & int("0x000000FF", 16))) +
                     counterArr["data"][i]["data"][0]["byte_count_lower"])
    readnsu.close()

    npsdataarr = []
    for j in range(0, ysocket.numberofnps):
        status = False
        filename = os.path.join(apidir, str("%s"%header.READ_NPS_REG+str(j)+".tcl"))
        readnps = open(filename, "r")
        for line in readnps:
            line = line.replace("\n", "")
            keys = line.split(" ")
            if (len(keys) >= 3) and (line[0] != "#"):
                for i in range(len(counterArr["data"])):
                    if  counterArr["data"][i]["key"] == "nps_"+str(j):
                        status = True
                        if (str(keys[2]) == "counter_1"):
                            prevvalue = counterArr["data"][i]["data"][0][str(keys[1])]
                            tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[0])),log,client)
                            if all(c in 'xX' + string.hexdigits for c in tempvar):
                                counterArr["data"][i]["data"][0][str(keys[1])] = str(set_bit(tempvar, 31, 0))
                                counterArr["data"][i]["data"][0][str(keys[1])] = counterArr["data"][i]["data"][0][
                                                                                     str(keys[1])] + "" + prevvalue
                            else:
                                counterArr["error"] = "Error on reading data from board"
                        else:
                            tempvar = tcpclient.getcommandresponse(("mrd %s\n" % str(keys[0])),log,client)
                            if all(c in 'xX' + string.hexdigits for c in tempvar):
                                counterArr["data"][i]["data"][0][str(keys[1])] = tempvar
                            else:
                                counterArr["error"] = "Error on reading data from board"
                        break
                    else:
                        status = False

                if not status:
                    obj = {}
                    obj["data"] = []
                    obj["displayname"] = str(keys[2])
                    obj["key"] = "nps_"+str(j)
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n"% str(keys[0])),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        obj["data"].append({"time":timenow, str(keys[1]):tempvar})
                        counterArr["data"].append(obj)
                    else:
                        counterArr["error"] = "Error on reading data from board"
        readnps.close()
    return counterArr

def getmeperf(exchange,log,active_user,client):
    counterArr = {"tabname":"aiengine_"+active_user, "data":[]}
    counterData = {}
    counterData["data"] = []
    status = False

    for j in range(0,len(client.mebaseaddress)):
        timenow = datetime.datetime.now()
        filename = os.path.join(apidir, str(header.READ_ME))
        readcounters = open(filename, "r")
        for line in readcounters:
            line = line.replace("\n", "")
            keys = line.split(" ")
            if ((len(keys) >= 3) and (line[0] != '#')):
                for i in range(len(counterArr["data"])):
                    if counterArr["data"][i]["displayname"] == str(keys[1])+str(j):
                        status = True
                        address = hex(int(client.mebaseaddress[j], 16) + int(str(keys[0]), 16))
                        tempvar = tcpclient.getcommandresponse(("mrd %s\n" %address),log,client)
                        if all(c in 'xX' + string.hexdigits for c in tempvar):
                            counterArr["data"][i]["data"][0][str(keys[2])] = tempvar
                        else:
                            counterArr["error"] = "Error on reading data from board"
                        break
                    else:
                        status = False

                if not status:
                    obj = {}
                    obj["data"] = []
                    obj["displayname"] = str(keys[1]) + str(j)
                    obj["key"] = str(keys[1])+str(j)
                    address = hex(int(client.mebaseaddress[j], 16) + int(str(keys[0]), 16))
                    tempvar = tcpclient.getcommandresponse(("mrd %s\n" %address),log,client)
                    if all(c in 'xX' + string.hexdigits for c in tempvar):
                        obj["data"].append({"time": timenow, str(keys[2]): tempvar})
                        counterArr["data"].append(obj)
                    else:
                        counterArr["error"] = "Error on reading data from board"
        readcounters.close()
    return counterArr

def dumpperformancedata(active_user='',client="",constatus = 0):
    tcpclient.configuremonitors(client)
    tcpclient.configuredcmonitors(client)
    tcpclient.configurelpdCounters(client)
    tcpclient.configurefpdCounters(client)
    tcpclient.configureapmmonitors(client)
    tcpclient.configurecpmcounters(client)
    tcpclient.configurepmccounters(client)

    while client.connectionstatus:
        for i in range(len(rabbitmq_config.queues)):
            if rabbitmq_config.queues[i]['config']['method'] == 'getlpdcounters':
                message = getlpdcounters(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getfpdcounters':
                message = getfpdcounters(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getcpmcounters':
                message = getcpmcounters(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getpmccounters':
                message = getpmccounters(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getperf':
                message = getperf(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getdcperformance':
                message = getdcperformance(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getapmperformance':
                message = getapmperformance(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            elif rabbitmq_config.queues[i]['config']['method'] == 'getmeperf':
                message = getmeperf(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)
            if ysocket.numberofnps and rabbitmq_config.queues[i]['config']['method'] == 'getnocperformance':
                message = getnocperformance(rabbitmq_config.queues[i],rabbitmq_config.queues[i]['config']['logfile'],active_user,client)

            body = message
            print(body)
            if "tabname" in body:
                for i in range(len(rabbitmq_config.queues)):
                    if body["tabname"] == rabbitmq_config.queues[i]["name"]+"_"+active_user and "efficiency" not in body["tabname"]:
                        db = mongoClient.xregv
                        try:
                            db[rabbitmq_config.queues[i]["config"]["dbname"]].insert_one(body)
                        except Exception as e:
                            print("errror--->>", str(e))
                if "efficiency" in body["tabname"]:
                    db = mongoClient.xregv
                    try:
                        db.ddr_efficiency.insert_one(body)
                    except Exception as e:
                        print("errror--->>", str(e))

if __name__ == '__main__':
    dumpperformancedata()