##
# 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.
# 
#
#
##
import time
import json
from pymongo import MongoClient
import datetime
from dateutil.parser import parse
import os, sys, inspect, importlib
from api import settings,users
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
from backend.ysocket import *
from backend.tcpclient import *
from backend import header
from backend import rabbitmq_config

# ysocket.connectionstatus = 1

connection = ""
channel = ""
mongoClient = ""
user = ""
active_user = ""

def dbdump(mongoClient,active_user, body):
    # body = json.loads(body)
    print(body)
    if "tabname" in body:
        status = False
        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))
                status = True
                break
            else:
                status = False
        if not status:
            if "efficiency" in body["tabname"]:
                db = mongoClient.xregv
                try:
                    db.ddr_efficiency.insert_one(body)
                except Exception as e:
                    print("errror--->>", str(e))
            else:
                db = mongoClient.xregv
                try:
                    db.socketdata.insert_one(body)
                except Exception as e:
                    print("errror--->>", str(e))

