# Copyright 2019 NEC Corporation
#
# Licensed 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.
#

"""
[概要]
Datadog apiを使って現在発生している障害を取得する。

"""
import json
import urllib
import requests
import os
import sys
import hashlib
import pprint
import django
import traceback
import base64

# import検索パス追加
my_path = os.path.dirname(os.path.abspath(__file__))
tmp_path = my_path.split('oase-root')
root_dir_path = tmp_path[0] + 'oase-root'
sys.path.append(root_dir_path)

# OASE モジュール import
# #LOCAL_PATCH#
os.environ['DJANGO_SETTINGS_MODULE'] = 'confs.frameworkconfs.settings'
django.setup()

from django.conf                 import settings
from libs.commonlibs.oase_logger import OaseLogger
from libs.commonlibs.aes_cipher  import AESCipher
from web_app.models.Datadog_monitoring_models import DatadogAdapter

logger = OaseLogger.get_instance() # ロガー初期化

class DatadogApi(object):

    def __init__(self, request_rec):
        """
        Datadog API インスタンスを返す
        [引数]
          request_rec: 対象Datadog監視マスタレコード
        """

        self.request_id      = 1
        self.uri             = request_rec.uri
        self.api_key         = request_rec.api_key
        self.application_key = request_rec.application_key
        self.proxy           = request_rec.proxy

    def _request(self, last_change_since, now):
        """
        Datadog API にリクエストを送信する
        id は現行特に必要ないため単純にインクリメントした数値を代入している。
        [引数]
          params: Datadog API のメソッドのパラメータ
        [戻り値]
          dict型に変換したレスポンス
        """

        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'DD-API-KEY': self.api_key,
            'DD-APPLICATION-KEY': self.application_key,
        }

        # プロクシ設定
        if self.proxy:
            proxies = {
                'http' : self.proxy,
                'https': self.proxy,
            }

        start = str(last_change_since)
        start = start.split('.', 1)[0]
        end   = str(now)
        end   = end.split('.', 1)[0]

        uri = self.uri + '?start=' + start + '&end=' + end

        try:
            if self.proxy:
                response = requests.get(uri, headers=headers, proxies=proxies, verify=False)
            else:
                response = requests.get(uri, headers=headers)

            self.request_id += 1

        except requests.exceptions.ConnectTimeout:
           # "リトライについて検討すべき"
            logger.system_log('LOSM38020', 'Datadog', 'Datadog Timeout error.')
            raise

        except requests.exceptions.RequestException:
            logger.system_log('LOSM38020', 'Datadog', 'RequestException error.')
            raise

        logger.system_log('LOSI38004', response.status_code)

        flg = False
        if response.status_code == 403:
           # 認証に失敗した場合はフラグを立てる
            flg = True

        if response.status_code != 200 and response.status_code != 403:
           # 200と403以外は関知しない 上位で処理
            raise Exception('Failed to get response. sts_code=%s, reason=%s' % (response.status_code, getattr(response, 'text', '')))

        try:
            resp = json.loads(response.text)

        except Exception as e:
            logger.system_log('LOSM38020', 'Datadog', 'JSON decode error. response=%s' % response)
            logger.logic_log('LOSI00005', traceback.format_exc())
            raise

        return resp, flg

    def get_active_triggers(self, last_change_since=None, now=None):
        """
        現在発生している障害を全て取得する。
        発生しているhostidとhost名も要求する。
        [戻り値]
        result: 発生中の障害情報
        """

        try:
            response = self._request(last_change_since, now)
        except Exception as e:
            raise

        if self._has_error(response):
            logger.system_log('LOSM38020', 'Datadog', 'GetResponse error.')
            raise Exception('response error')

        return response

    def logout(self):
        """
        ログアウトを行う。
        成功した場合レスポンスの'result'キーの値がTrueになる。
        [戻り値]
        bool
        """
        try:
            response = self._request('user.logout', [])
        except:
            raise

        return response

    def _has_error(self, response):
        if response != None:
            return True if 'error' in response else False
        else:
            return True


# coding=utf-8

"""
ComicStreamer blacklist manager thread class
"""

import threading
import select
import sys
import logging
import platform
import Queue
import datetime

import utils
import os

from folders import AppFolders
import database
from library import Library

class Blacklist(threading.Thread):
    def __init__(self, dm):
        super(Blacklist, self).__init__()
        self.queue = Queue.Queue(0)
        self.quit = False
        self.dm = dm

    def stop(self):
        self.quit = True
        self.join()

    def setBlacklist(self, comic_id, pagenum):
        # for now, don't defer the blacklist setting, maybe it's not needed
        self.actualSetBlacklist( comic_id, pagenum)
        #self.queue.put((comic_id, pagenum))

    def run(self):
        logging.debug("Blacklist: Started")
        pagenum = 0
        while True:
            try:
                (comic_id, pagenum) = self.queue.get(block=True, timeout=1)
            except:
                comic_id = None

            self.actualSetBlacklist(comic_id, pagenum)

            if self.quit:
                break

        logging.debug("Blacklist: Stopped")

    def actualSetBlacklist(self, comic_id, pagenum):
        if comic_id is not None:
            library = Library(self.dm.Session)
            if pagenum == 'clear':
                library.comicBlacklist(comic_id)
            else:
                library.comicBlacklist(comic_id, pagenum)


from django.shortcuts import render
# from django.http import *
# from django.http import response
# from django.template import RequestContext,loader\
from booktest.models import *
# Create your views here.

def index(request):
    # temp = loader.get_template('booktest/login.html')
    # return HttpResponse(temp.render())
    # book_list = BookInfo
    # book_list = [1, 2, 3]
    bookList=BookInfo.objects.all()
    b = BookInfo()
    b.btitle='123'
    context={'list':bookList}
    return render(request,'booktest/login.html',context)

def show(request, book_id):
    book = BookInfo.objects.get(pk=book_id)
    hero_list = book.heroinfo_set.all()
    context = {'list': hero_list}
    return render(request, 'booktest/show.html',context)


# coding: utf8
import numpy as np
import argparse
import math
import threading
from time import clock, sleep
from solo12_ISAE import Solo12

data = np.load("traj.npz")

t_traj = data['t']
q_traj = data['q']
v_traj = data['q_dot']
kp_traj = data['gains'][:, 0]
kd_traj = data['gains'][:, 1]

Kp = 0*0.1
Kd = 0*0.01

key_pressed = False

def get_input():
    global key_pressed
    keystrk = input('Put the robot on the floor and press Enter \n')
    # thread doesn't continue until key is pressed
    key_pressed = True

def put_on_the_floor(device, q_init):
    """Make the robot go to the default initial position and wait for the user
    to press the Enter key to start the main control loop
    Args:
        device (robot wrapper): a wrapper to communicate with the robot
        q_init (array): the default position of the robot
    """
    global key_pressed
    key_pressed = False
    Kp_pos = .1
    Kd_pos = 0.01
    imax = 3.0
    pos = np.zeros(device.nb_motors)

    for motor in range(device.nb_motors):
        pos[motor] = q_init[device.motorToUrdf[motor]] * device.gearRatioSigned[motor]

    i = threading.Thread(target=get_input)
    i.start()
    while not key_pressed:
        device.UpdateMeasurment()
        for motor in range(device.nb_motors):
            ref = Kp_pos*(pos[motor] - device.hardware.GetMotor(motor).GetPosition() -
                          Kd_pos*device.hardware.GetMotor(motor).GetVelocity())
            ref = min(imax, max(-imax, ref))
            device.hardware.GetMotor(motor).SetCurrentReference(ref)
        device.SendCommand(WaitEndOfCycle=True)

    print("Start the motion.")

def example_script(name_interface):
    device = Solo12(name_interface,dt=0.001)
    nb_motors = device.nb_motors

    device.Init(calibrateEncoders=True, q_init=q_traj[0])
    put_on_the_floor(device, q_traj[0])

    #CONTROL LOOP ***************************************************
    i_traj = 0
    initial_time = clock()

    while ((not device.hardware.IsTimeout()) and (clock() < 200)):
        device.UpdateMeasurment()

        # Get elements from trajectory
        if clock()-initial_time > t_traj[i_traj] and i_traj+1<len(t_traj):
            i_traj += 1

        q_ref = q_traj[i_traj]
        v_ref = v_traj[i_traj]
        ff_ref = 0
        # Kp = kp_traj[i_traj]
        # Kd = kd_traj[i_traj]

        """
        # Compute torque
        q_err = q_ref - device.q_mes
        v_err = v_ref - device.v_mes
        tau = Kp*q_err + Kd*v_err + ff_ref
        device.SetDesiredJointTorque(tau)
        """
        device.SetDesiredJointPDgains([Kp]*12, [Kd]*12)
        device.SetDesiredJointPosition(q_ref)
        device.SetDesiredJointVelocity(v_ref)
        device.SetDesiredJointTorque([ff_ref] * 12)

        device.SendCommand(WaitEndOfCycle=True)

        if ((device.cpt % 100) == 0):
            device.Print()
            for i in range(12):
                print(device.hardware.GetMotor(i).GetPosition())
    #****************************************************************

    # Whatever happened we send 0 torques to the motors.
    device.SetDesiredJointTorque([0]*nb_motors)
    device.SendCommand(WaitEndOfCycle=True)

    if device.hardware.IsTimeout():
        print("Masterboard timeout detected.")
        print("Either the masterboard has been shut down or there has been a connection issue with the cable/wifi.")
    device.hardware.Stop()  # Shut down the interface between the computer and the master board

def main():
    parser = argparse.ArgumentParser(description='Example masterboard use in python.')
    parser.add_argument('-i',
                        '--interface',
                        required=True,
                        help='Name of the interface (use ifconfig in a terminal), for instance "enp1s0"')

    example_script(parser.parse_args().interface)

if __name__ == "__main__":
    main()


# -*- coding: latin1 -*-

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window

#Main app
class BodenGuidenApp(App):
#App build function
    def build(self):
        global CurrentNewsPaper
        global newsPaper
#sets the background color to white
        Window.clearcolor = (1, 1, 1, 1)
#the logo for the start screen
        logo = Image(source = "logga.jpg", size_hint_y = .3, pos_hint = {'x':0,'y':.75})

#creates the buttons and adds their images and their borders
        newsPaperOne = Button(background_normal = "nr1.jpg", background_down = "nr1.jpg")
        newsPaperTwo = Button(background_normal = "nr2.jpg", background_down = "nr2.jpg")
        newsPaperThree = Button(background_normal = "nr3.jpg", background_down = "nr3.jpg")
        newsPaperFour = Button(background_normal = "nr4.jpg")
#Layout for the button display section
        newsPaperLayout = GridLayout(cols = 2, rows = 2, spacing = 30, size_hint_y = .7, size_hint_x = .95,
                                      pos_hint = {'x': .025, 'y':.025})
#Widgets for text, images and a backbutton also added some temporary text
        headline = Label(text = unicode(open("text1.txt").read().split("\n", 1)[0], encoding="latin1", errors='ignore'), markup = True, color = (0,0,0,1), halign = 'center')
        text1 = Label(text = unicode(open("text1.txt").read().split("\n", 1)[1], encoding= "latin1", errors="ignore"),color = (0,0,0,1),markup = True, padding_x = 30)
        image1 = Image(source = "nr1.jpg", allow_stretch = True,)
        backButton = Button(text = "Hem", color = (0,0,0,1), size_hint = (.27,.0595), pos_hint = {"x":0, "y":.9415}, background_normal = "", background_color = (0,0,0,.5))
#Layout for text and images inside the newspaper also adding widgets to said
        def ShowTextLayout(instance):
#Removing unwanted widgets form mainLayout
            mainLayout.remove_widget(logo)
            mainLayout.remove_widget(newsPaperLayout)
#adding new widgets to textLayout and mainLayout
            textLayout.add_widget(logo)
            textLayout.add_widget(headline)
            textLayout.add_widget(text1)
            textLayout.add_widget(image1)
#resizing widgets contaning text to wrap text and updateing them
            headline.text_size =(mainLayout.width, None)
            headline.font_size = text1.font_size + 2
            text1.text_size=(mainLayout.width,None)
            headline.texture_update()
            text1.texture_update()
#resizing textLayout
            textLayout.height = 0
            textLayout.size_hint_y = float(float(headline.texture_size[1] + text1.texture_size[1] + image1.texture_size[1] + logo.texture_size[1])/float(mainLayout.height))
#resizing widgets for textLayout
            logo.height = 0
            text1.height = 0
            headline.height = 0
            image1.height = 0
            image1.size_hint_y = image1.texture_size[1]/float(headline.texture_size[1] + text1.texture_size[1] + image1.texture_size[1] + logo.texture_size[1])
            headline.size_hint_y = headline.texture_size[1]/float(headline.texture_size[1] + text1.texture_size[1] + image1.texture_size[1] + logo.texture_size[1])
            text1.size_hint_y = text1.texture_size[1]/float(headline.texture_size[1] + text1.texture_size[1] + image1.texture_size[1] + logo.texture_size[1])
            logo.size_hint_y = logo.texture_size[1]/float(headline.texture_size[1] + text1.texture_size[1] + image1.texture_size[1] + logo.texture_size[1])
#adding text widget to mainLayout
            mainLayout.add_widget(scrollTextLayout)
            mainLayout.add_widget(backButton)

        scrollTextLayout = ScrollView(size_hint = (1,1))
        textLayout = BoxLayout(orientation = 'vertical')
        scrollTextLayout.add_widget(textLayout)

        def returnNewspaperLayout(instance):
            mainLayout.remove_widget(backButton)
            mainLayout.remove_widget(scrollTextLayout)
            textLayout.clear_widgets()
            logo.size_hint_y = .3
            mainLayout.add_widget(logo)
            mainLayout.add_widget(newsPaperLayout)
        backButton.bind(on_press = returnNewspaperLayout)
#The main layout for the app
        mainLayout = FloatLayout()

#Adding all the newsPaper buttons to their parent widget newsPaperLayout
        newsPaperLayout.add_widget(newsPaperOne)
        newsPaperOne.bind(on_press = ShowTextLayout)
        newsPaperLayout.add_widget(newsPaperTwo)
        newsPaperLayout.add_widget(newsPaperThree)
        newsPaperLayout.add_widget(newsPaperFour)

#adding newsPaperLayout and logo widgets to mainLayout
        mainLayout.add_widget(newsPaperLayout)
        mainLayout.add_widget(logo)
        return mainLayout

#Lauches the app
BodenGuidenApp().run()


from material import Layout, Row, Fieldset
from django import forms
from records.models import Record
from records.data import *
from records.choices import *
from records.form_layouts import *

class GeneralRecordForm(forms.Form):
    """
    This 'general' form is simply a form with entry type and cite key, which is
    common across all records
    """

    entry_type = forms.ChoiceField(choices=ENTRY_TYPE_CHOICES, required=True)
    cite_key = forms.CharField(required=True)
    entry_type.widget.attrs.update({'class': 'form_select'})
    layout = Layout(Row('entry_type','cite_key'))

class SpecificRecordForm(forms.Form):
    """
    This form class generates a record specific form
    The data is stored in data.py
    """
    def __init__(self,*args,**kwargs):
        entry = kwargs.pop('entry')
        super().__init__(*args,**kwargs)
        for fieldname in ENTRY_TYPE_FIELDS[entry][0]:
            if fieldname == 'author':
                self.fields[fieldname] = forms.CharField(
                widget=forms.TextInput(attrs={
                    'placeholder':"          Add multiple authors separated with ' and '",
                    }))
            else:
                self.fields[fieldname] = forms.CharField()
            self.fields[fieldname].required = True

        for fieldname in ENTRY_TYPE_FIELDS[entry][1]:
            self.fields[fieldname] = forms.CharField()
            self.fields[fieldname].required = False

        self.layout = FORM_LAYOUT[entry]

class ShowRecordForm(forms.ModelForm):
    """
    This form class was created to easier be able to show the details of a record.
    """
    class Meta:
        model = Record
        fields = ['entry_type', 'cite_key']

    def __init__(self,*args,**kwargs):
        entry = kwargs.pop('entry')
        super().__init__(*args,**kwargs)
        self.fields['entry_type'].widget.attrs['readonly'] = True
        self.fields['cite_key'].widget.attrs['readonly'] = True
        self.fields['entry_type'].widget.attrs.update({'class': 'form_select', 'id':'entry_type_2'})
        for fieldname in ENTRY_TYPE_FIELDS[entry][0]:
            self.fields[fieldname] = forms.CharField()
            self.fields[fieldname].required = True
            self.fields[fieldname].widget.attrs['readonly'] = True

        for fieldname in ENTRY_TYPE_FIELDS[entry][1]:
            self.fields[fieldname] = forms.CharField()
            self.fields[fieldname].required = False
            self.fields[fieldname].widget.attrs['readonly'] = True

        self.layout = FORM_LAYOUT[entry]


from azure.storage.queue import QueueService
import requests
import json

# variables – needs azure queue name, key, and docker api url
storagacct = "<storage acct>"
queue = "<queue>"
queuekey = "<queue key>"
docker = "http://<docekr host>:2375/containers/"
image = "neilpeterson/domain-search"

while True:
    # set up azure queue
    queue_service = QueueService(account_name=storagacct, account_key=queuekey)

    # get messages from azure queue
    messages = queue_service.get_messages(queue, num_messages=2)

    # delete from queue, create container, start container
    for message in messages:

        # get domain and construct json
        d = message.content
        data = json.loads('{"Image": "' + image + '", "Cmd": ["--domain=' + d + '"]}')

        # delete message from azure queue
        queue_service.delete_message(queue, message.id, message.pop_receipt)

        # create and start docker container
        headers = {'Content-Type': 'application/json'}
        r = requests.post(docker + "create", data=json.dumps(data), headers=headers)
        b = json.loads(r.text)
        x = requests.post(docker + b['Id'] + "/start")


import aiosqlite
from aiohttp import web
from aiohttp_swagger import *
from aiojobs.aiohttp import setup, spawn, get_scheduler_from_app
from core.matcher import MQTTMatcher
from hbmqtt.broker import Broker
from hbmqtt.client import MQTTClient
from hbmqtt.mqtt.constants import QOS_1

from core.websocket import websocket_handler

TEST_DB = "test.db"
c = MQTTClient()
import asyncio

matcher = MQTTMatcher()

config = {
    'listeners': {
        'default': {
            'type': 'tcp',
            'bind': '0.0.0.0:1885',
        },
        'my-ws-1': {
            'bind': '0.0.0.0:8888',
            'type': 'ws'
        }
    },

    'sys_interval': 10,
    'auth': {
        'allow-anonymous': True,
    }
}

broker = Broker(config, plugin_namespace="hbmqtt.test.plugins")

async def test2(name):
    while True:
        print(name)
        await asyncio.sleep(1)

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name

    return web.Response(text=text)

async def test_connection():
    async with aiosqlite.connect(TEST_DB) as db:

        assert isinstance(db, aiosqlite.Connection)

app = web.Application()

async def listen_to_redis(app):
    while True:
        await asyncio.sleep(1)

        #for w in _ws:
        #    pass
            # await w.send_str("HALLO")

            # print(w)

async def myjob(app):
    while True:
        await asyncio.sleep(1)

def ok_msg(msg):
    pass

def ok_msg1(msg):
    pass

def ok_msg2(msg):
    pass

mqtt_methods = {"test": ok_msg, "test/+/ab": ok_msg1, "test/+": ok_msg2}

async def on_message():
    while True:
        message = await c.deliver_message()
        matched = False
        packet = message.publish_packet
        print(message.topic)
        print(message.topic.split('/'))
        data = packet.payload.data.decode("utf-8")

        for callback in matcher.iter_match(message.topic):

            callback(data)
            matched = True

        if matched == False:
            print("NO HANDLER", data)

        #for w in _ws:
        #    await w.send_str(data)

async def start_background_tasks(app):
    app['redis_listener'] = app.loop.create_task(listen_to_redis(app))

async def start_broker(app):

    await broker.start()

    await c.connect('mqtt://localhost:1885')

    for k, v in mqtt_methods.items():
        print(k, v)
        await c.subscribe([(k, QOS_1)])

        matcher[k] = v
    # await c.subscribe([('/test', QOS_1),('/hallo', QOS_1)])

    await get_scheduler_from_app(app).spawn(on_message())

job = None

async def start_task(request):
    global job
    job = await spawn(request, myjob(app))
    await test_connection()
    return web.Response(text="OK")

async def stop_task(request):
    await job.close()
    return web.Response(text="OK")

async def stats(request):
    s = get_scheduler_from_app(app)

    return web.Response(text="%s" % s.active_count)

setup(app)

def start_bg(app, name, method):
    print("HALLO111")

    async def start(app):
        app[name] = app.loop.create_task(method(name))

    app.on_startup.append(start)

# start_bg(app, "test", test2)
# start_bg(app, "test2", test2)

#app.on_startup.append(start_background_tasks)
app.on_startup.append(start_broker)

app.add_routes([web.get('/', handle),
                web.get('/stop', stop_task),
                web.get('/start', start_task),
                web.get('/stats', stats),
                web.get('/ws', websocket_handler),
                web.get('/{name}', handle)

                ])

setup_swagger(app)

web.run_app(app)


from django.db import models

# Create your models here.
import pyodbc
# print([x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')])

#Policy Object
class policy_object():
    def __init__(self,pid, sponsor,policy_num, insurance_ID, company_name, typer, base_am, adj_am,desc):
        self.Policy_ID=pid
        self.Sponsor=sponsor
        self.Policy_Number=policy_num
        self.Insurance_ID = insurance_ID
        self.Company_Name = company_name
        self.Type = typer
        self.Base_Amount = base_am
        self.Adjusted_Amount = adj_am
        self.Description = desc

#User Object
class user_object():
    def __init__(self,uid,first, last, dob, email, address, country, province):
        self.User_ID = uid
        self.First_Name=first
        self.Last_Name=last
        self.DOB = dob
        self.Email=email
        self.Address=address
        self.Country=country
        self.Province=province

#Creates a user
def add_user(first_name, last_name,DOB,email,address,country,province):
    conn = pyodbc.connect(
        r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
        r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
        )
    cursor = conn.cursor()
    cursor.execute("INSERT INTO User (First_Name,Last_Name,DOB,Email,Address,Country,Province) values (?,?,?,?,?,?,?)",
    first_name,last_name,DOB,email,address,country,province)
    conn.commit()

#Creates a policy at a user id
def create_policy(user_id,sponsor, policyNum, insurance_ID , company_name, typers, base_am,adj_am, desc ):
    conn = pyodbc.connect(
        r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
        r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
        )
    cursor = conn.cursor()
    cursor.execute("INSERT INTO Policy (Sponsor, Policy_Number, Insurance_ID, Company_Name, Type, Base_Amount, Adjusted_Amount, Description) values (?,?,?,?,?,?,?,?)",
     sponsor, policyNum, insurance_ID,company_name,typers,base_am,adj_am, desc)
    conn.commit()
    cursor.execute("SELECT @@IDENTITY")
    user_num = cursor.fetchone()[0]
    print(user_num)
    print(user_id)
    cursor.execute("INSERT INTO policy_user (User_ID, Policy_ID) values (?,?)",user_id,user_num)
    conn.commit()

#Delete a particular Policy
def delete_policy(policy_id):
    conn = pyodbc.connect(
        r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
        r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
        )
    cursor = conn.cursor()
    cursor.execute("DELETE FROM Policy where Policy_ID =?",policy_id)
    conn.commit()
    cursor.execute("DELETE FROM policy_user where Policy_ID=?", policy_id)
    conn.commit()

#Gets all policies attached to a user id
def get_all_policy(user_id):
    conn = pyodbc.connect(
            r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
            r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
            )
    cursor = conn.cursor()
    policy_array=[]
    cursor.execute("SELECT Policy_ID FROM policy_user where User_ID=?",user_id)
    for val in cursor.fetchall():
        print(val[0])
        cursor.execute("SELECT * FROM Policy where Policy_ID=?",val)
        grabbed_policy= cursor.fetchone()
        to_return = policy_object(grabbed_policy[0],grabbed_policy[1],grabbed_policy[2],grabbed_policy[3],
        grabbed_policy[4],grabbed_policy[5],grabbed_policy[6],grabbed_policy[7],grabbed_policy[8])
        policy_array.append(to_return)
    return policy_array

#Gets all of a policy info and returns as object
def get_policy(policy_id):
    conn = pyodbc.connect(
            r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
            r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
            )
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM Policy where Policy_ID=?',policy_id)
    grabbed_policy = cursor.fetchone()
    to_return = policy_object(grabbed_policy[0],grabbed_policy[1],grabbed_policy[2],grabbed_policy[3],
    grabbed_policy[4],grabbed_policy[5],grabbed_policy[6],grabbed_policy[7],grabbed_policy[8])
    return to_return

#Gets and returns user object of all user data at specified ID
def get_user_data(user_id):
    conn = pyodbc.connect(
            r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
            r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
            )
    cursor = conn.cursor()

    cursor.execute("SELECT * FROM User where User_ID=?",user_id)
    user_arr = []
    for val in cursor.fetchone():

        user_arr.append(val)

    to_return = user_object(user_arr[0],user_arr[1],user_arr[2],user_arr[3],user_arr[4],user_arr[5],user_arr[6],user_arr[7])
    return to_return

#Adjusts policy up or down
def adjust_policy(policy_id, amount):
    conn = pyodbc.connect(
            r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
            r'DBQ=C:\Users\cybersaint\Desktop\git\tgh2019\Owens Code\IDS.accdb;'
            )
    cursor = conn.cursor()
    cursor.execute("SELECT Adjusted_Amount FROM Policy where Policy_ID=?", policy_id)
    the_val = cursor.fetchone()[0]
    the_val=int(the_val)
    the_val=the_val+amount
    cursor.execute("UPDATE Policy SET Adjusted_Amount=? where Policy_ID=?",the_val,policy_id)
    conn.commit()


import httplib2
import os
import sys
import argparse
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser
from oauth2client import tools

    # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
    # the OAuth 2.0 information for this application, including its client_id and
    # client_secret. You can acquire an OAuth 2.0 client ID and client secret from
    # the Google Cloud Console at
    # https://cloud.google.com/console.
    # Please ensure that you have enabled the YouTube Data API for your project.
    # For more information about using OAuth2 to access the YouTube Data API, see:
    #   https://developers.google.com/youtube/v3/guides/authentication
    # For more information about the client_secrets.json file format, see:
    #   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets

CLIENT_SECRETS_FILE = "API/client_secret.json"

    # This variable defines a message to display if the CLIENT_SECRETS_FILE is
    # missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

%s

with information from the Cloud Console
https://cloud.google.com/console

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                               CLIENT_SECRETS_FILE))

    # This OAuth 2.0 access scope allows for full read/write access to the
    # authenticated user's account.
YOUTUBE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

tools.argparser.add_argument('-v', '--video-id', type=str, required=True, help='The id of the video')
tools.argparser.add_argument('-p', '--playlist-id', type=str, required=True,
                             help='The id of the playlist')
args = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()

def get_authenticated_service():
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

    storage = Storage("API/oauth2.json")
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = tools.run_flow(flow, storage,  tools.argparser.parse_args())

    return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
        http=credentials.authorize(httplib2.Http()))

def add_video_to_playlist(youtube,videoID,playlistID):
    add_video_request=youtube.playlistItems().insert(
    part="snippet",
    body={
        'snippet': {
            'playlistId': playlistID,
            'resourceId': {
                    'kind': 'youtube#video',
                'videoId': videoID
            }
            #'position': 0
        }
}
).execute()

if __name__ == '__main__':
    youtube = get_authenticated_service()
    video = sys.argv[1]
    playlist = sys.argv[2];
    add_video_to_playlist(youtube,args.video_id,args.playlist_id);


import typing as T

from .event.models import BotMessage, Message, Anonymous, ForwardMessages
from .entities import *
from .entities.components import Node
from .network import fetch

class CQHTTP_Protocol:
    baseurl_http: str

    async def sendFriendMessage(self,
                                user_id: int,
                                group_id: int,
                                message: T.Union[str, list],
                                auto_escape: bool = False) -> T.Union[BotMessage, bool]:
        if isinstance(message, list):
            _message = ""
            for chain in message:
                _message += chain.toString()
            message = _message
        result = await fetch.http_post(f"{self.baseurl_http}/send_private_msg", {
            "user_id": user_id,
            "group_id": group_id,
            "message": message,
            "auto_escape": auto_escape
        })
        if result["status"] == "ok":
            return BotMessage.parse_obj(result["data"])
        return False

    async def sendGroupMessage(self,
                               group_id: int,
                               message: T.Union[str, list],
                               auto_escape: bool = False) -> T.Union[BotMessage, bool]:
        if isinstance(message, list):
            _message = ""
            for chain in message:
                _message += chain.toString()
            message = _message
        result = await fetch.http_post(f"{self.baseurl_http}/send_group_msg", {
            "group_id": group_id,
            "message": message,
            "auto_escape": auto_escape
        })
        if result["status"] == "ok":
            return BotMessage.parse_obj(result["data"])
        return False

    async def sendGroupForwardMessage(self,
                                      group_id: int,
                                      messages: T.Union[list]) -> T.Union[BotMessage, bool]:
        for i in range(len(messages)):
            if isinstance(messages[i], Node):
                messages[i] = messages[i].toDict()
        result = await fetch.http_post(f"{self.baseurl_http}/send_group_forward_msg", {
            "group_id": group_id,
            "messages": messages
        })
        if result["status"] == "ok":
            return BotMessage.parse_obj(result["data"])
        return False

    async def recall(self, message_id: int) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/delete_msg", {
            "message_id": message_id
        })
        if result["status"] == "ok":
            return True
        return False

    async def getMessage(self, message_id: int) -> T.Union[Message, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_msg", {
            "message_id": message_id
        })
        if result["status"] == "ok":
            return Message.parse_obj(result["data"])
        return False

    async def getForwardMessage(self, message_id: int) -> T.Union[ForwardMessages, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_forward_msg", {
            "message_id": message_id
        })
        if result["status"] == "ok":
            return ForwardMessages.parse_obj(result["data"])
        return False

    async def getImage(self, file: str) -> T.Union[ImageFile, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_image", {
            "file": file
        })
        if result["status"] == "ok":
            return ImageFile.parse_obj(result["data"])
        return False

    async def kick(self,
                   group_id: int,
                   user_id: int,
                   reject_add_request: bool = False) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_kick", {
            "group_id": group_id,
            "user_id": user_id,
            "reject_add_request": reject_add_request
        })
        if result["status"] == "ok":
            return True
        return False

    async def mute(self,
                   group_id: int,
                   user_id: int,
                   duration: int = 30 * 60) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_ban", {
            "group_id": group_id,
            "user_id": user_id,
            "duration": duration
        })
        if result["status"] == "ok":
            return True
        return False

    async def unmute(self, group_id: int, user_id: int) -> bool:
        return await self.mute(group_id, user_id, 0)

    async def muteAnonymous(self,
                            group_id: int,
                            flag: str,
                            duration: int = 30 * 60,
                            anonymous: Anonymous = None):  # TODO
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_anonymous_ban", {
            "group_id": group_id,
            "flag": flag,
            "duration": duration
        })
        if result["status"] == "ok":
            return True
        return False

    async def muteAll(self,
                      group_id: int,
                      enable: bool = True) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_whole_ban", {
            "group_id": group_id,
            "enable": enable
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupAdmin(self,
                            group_id: int,
                            user_id: int,
                            enable: bool = True) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_admin", {
            "group_id": group_id,
            "user_id": user_id,
            "enable": enable
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupAnonymous(self,
                                group_id: int,
                                enable: bool = True) -> bool:  # TODO go-cqhttp 暂未支持
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_anonymous", {
            "group_id": group_id,
            "enable": enable
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupCard(self,
                           group_id: int,
                           user_id: int,
                           card: str = "") -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_card", {
            "group_id": group_id,
            "user_id": user_id,
            "card": card
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupName(self,
                           group_id: int,
                           group_name: str) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_name", {
            "group_id": group_id,
            "group_name": group_name
        })
        if result["status"] == "ok":
            return True
        return False

    async def leave(self,
                    group_id: int,
                    is_dismiss: bool = False) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_leave", {
            "group_id": group_id,
            "is_dismiss": is_dismiss
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupSpecialTitle(self,
                                   group_id: int,
                                   user_id: int,
                                   special_title: str = "",
                                   duration: int = -1) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_special_title", {
            "group_id": group_id,
            "user_id": user_id,
            "special_title": special_title,
            "duration": duration
        })
        if result["status"] == "ok":
            return True
        return False

    async def setFriendRequest(self,
                               flag: str,
                               approve: bool = True,
                               remark: str = "") -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_friend_add_request", {
            "flag": flag,
            "approve": approve,
            "remark": remark
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupRequest(self,
                              flag: str,
                              sub_type: str,
                              approve: bool = True,
                              reason: str = "") -> bool:
        if sub_type not in ["add", "invite"]:
            return False
        result = await fetch.http_post(f"{self.baseurl_http}/set_group_add_request", {
            "flag": flag,
            "sub_type": sub_type,
            "approve": approve,
            "reason": reason
        })
        if result["status"] == "ok":
            return True
        return False

    async def getLoginInfo(self) -> T.Union[Bot, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_login_info")
        if result["status"] == "ok":
            return Bot.parse_obj(result["data"])
        return False

    async def getQiDianAccountInfo(self) -> T.Union[QiDianAccount, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/qidian_get_account_info")
        if result["status"] == "ok":
            return QiDianAccount.parse_obj(result["data"])
        return False

    async def getStrangerInfo(self,
                              user_id: int,
                              no_cache: bool = False) -> T.Union[Stranger, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_stranger_info", {
            "user_id": user_id,
            "no_cache": no_cache
        })
        if result["status"] == "ok":
            return Stranger.parse_obj(result["data"])
        return False

    async def getFriendList(self) -> T.Union[List[Friend], bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_friend_list")
        if result["status"] == "ok":
            return [Friend.parse_obj(friend_info) for friend_info in result["data"]]
        return False

    async def deleteFriend(self,
                           friend_id: int) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/delete_friend", {
            "friend_id": friend_id
        })
        if result["status"] == "ok":
            return True
        return False

    async def getUnidirectionalFriendList(self) -> T.Union[List[Friend], bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_unidirectional_friend_list")
        if result["status"] == "ok":
            return [Friend.parse_obj(friend_info) for friend_info in result["data"]]
        return False

    async def deleteUnidirectionalFriend(self,
                                         user_id: int) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/delete_unidirectional_friend", {
            "user_id": user_id
        })
        if result["status"] == "ok":
            return True
        return False

    async def getGroupInfo(self,
                           group_id: int,
                           no_cache: bool = False) -> T.Union[Group, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_info", {
            "group_id": group_id,
            "no_cache": no_cache
        })
        if result["status"] == "ok":
            return Group.parse_obj(result["data"])
        return False

    async def getGroupList(self) -> T.Union[List[Group], bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_list")
        if result["status"] == "ok":
            return [Group.parse_obj(group_info) for group_info in result["data"]]
        return False

    async def getGroupMemberInfo(self,
                                 group_id: int,
                                 user_id: int,
                                 no_cache: bool = False) -> T.Union[Member, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_member_info", {
            "group_id": group_id,
            "user_id": user_id,
            "no_cache": no_cache
        })
        if result["status"] == "ok":
            return Member.parse_obj(result["data"])
        return False

    async def getGroupMemberList(self,
                                 group_id: int) -> T.Union[List[Member], bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_member_list", {
            "group_id": group_id
        })
        if result["status"] == "ok":
            return [Member.parse_obj(member_info) for member_info in result["data"]]
        return False

    async def getGroupHonorInfo(self,
                                group_id: int,
                                type: str) -> T.Union[Honor, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_honor_info", {
            "group_id": group_id,
            "type": type
        })
        if result["status"] == "ok":
            return Honor.parse_obj(result["data"])
        return False

    async def canSendImage(self) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/can_send_image")
        if result["status"] == "ok":
            if result["data"]["yes"]:
                return True
        return False

    async def canSendRecord(self) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/can_send_record")
        if result["status"] == "ok":
            if result["data"]["yes"]:
                return True
        return False

    async def getVersionInfo(self) -> T.Union[AppVersion, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_version_info")
        if result["status"] == "ok":
            return AppVersion.parse_obj(result["data"])
        return False

    async def restartAPI(self, delay: int = 0) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_restart", {
            "delay": delay
        })
        if result["status"] == "ok":
            return True
        return False

    async def setGroupPortrait(self,
                               group_id: int,
                               file: str,
                               cache: int) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_restart", {
            "group_id": group_id,
            "file": file,
            "cache": cache
        })
        if result["status"] == "ok":
            return True
        return False

    async def ocrImage(self,
                       image: str) -> T.Union[OCR, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/ocr_image", {
            "image": image
        })
        if result["status"] == "ok":
            return OCR.parse_obj(result["data"])
        return False

    async def getGroupSystemMessage(self) -> T.Union[GroupSystemMessage, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_system_msg")
        if result["status"] == "ok":
            return GroupSystemMessage.parse_obj(result["data"])
        return False

    async def uploadGroupFile(self, group_id: int) -> T.Union[GroupFileSystem, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_file_system_info", {
            "group_id": group_id
        })
        if result["status"] == "ok":
            return GroupFileSystem.parse_obj(result["data"])
        return False

    async def getGroupRootFiles(self, group_id: int) -> T.Union[GroupFileTree, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_root_files", {
            "group_id": group_id
        })
        if result["status"] == "ok":
            return GroupFileTree.parse_obj(result["data"])
        return False

    async def getGroupFilesByFolder(self,
                                    group_id: int,
                                    folder_id: str) -> T.Union[GroupFileTree, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_root_files", {
            "group_id": group_id,
            "folder_id": folder_id
        })
        if result["status"] == "ok":
            return GroupFileTree.parse_obj(result["data"])
        return False

    async def getGroupFileURL(self,
                              group_id: int,
                              file_id: str,
                              busid: int) -> T.Union[str, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_root_files", {
            "group_id": group_id,
            "file_id": file_id,
            "busid": busid
        })
        if result["status"] == "ok":
            return result["data"]["url"]
        return False

    async def getStatus(self) -> T.Union[AppStatus, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_status")
        if result["status"] == "ok":
            return AppStatus.parse_obj(result["data"])
        return False

    async def getGroupAtAllRemain(self, group_id: int) -> T.Union[AtAllRemain, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_at_all_remain", {
            "group_id": group_id
        })
        if result["status"] == "ok":
            return AtAllRemain.parse_obj(result["data"])
        return False

    async def getVipInfo(self, user_id: int) -> T.Union[VipInfo, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/_get_vip_info", {
            "user_id": user_id
        })
        if result["status"] == "ok":
            return VipInfo.parse_obj(result["data"])
        return False

    async def sendGroupNotice(self, group_id: int, content: str):
        result = await fetch.http_post(f"{self.baseurl_http}/_send_group_notice", {
            "group_id": group_id,
            "content": content
        })
        if result["status"] == "ok":
            return True
        return False

    async def reloadEventFilter(self, file: str):
        result = await fetch.http_post(f"{self.baseurl_http}/reload_event_filter", {
            "file": file
        })
        if result["status"] == "ok":
            return True
        return False

    async def downloadFile(self, url: str, headers: str, thread_count=1):
        result = await fetch.http_post(f"{self.baseurl_http}/download_file", {
            "url": url,
            "headers": headers,
            "thread_count": thread_count
        })
        if result["status"] == "ok":
            return True
        return False

    async def getOnlineClients(self, no_cashe: bool) -> T.Union[List[Device], bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_online_clients", {
            "no_cache": no_cashe
        })
        if result["status"] == "ok":
            return [Device.parse_obj(device) for device in result["data"]["clients"]]
        return False

    async def getGroupMessageHistory(self, group_id: int, message_seq: Optional[int] = None) -> T.Union[List[Message],
                                                                                bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_group_msg_history", {
            "message_seq": message_seq,
            "group_id": group_id
        })
        if result["status"] == "ok":
            print(result)
            return [Message.parse_obj(message) for message in result["data"]["messages"]]
        return False

    async def setEssenceMessage(self, message_id: int) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/set_essence_msg", {
            "message_id": message_id
        })  # 草,为什么只有手机看得到
        if result["status"] == "ok":
            return True
        return False

    async def deleteEssenceMessage(self, message_id: int) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/delete_essence_msg", {
            "message_id": message_id
        })  # 这个我没测试过
        if result["status"] == "ok":
            return True
        return False

    async def getEssenceMessageList(self, group_id: int) -> T.Union[EssenceMessage, bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/get_essence_msg_list", {
            "group_id": group_id
        })
        if result["status"] == "ok":
            return EssenceMessage.parse_obj(result["data"])
        return False

    async def checkURLSafety(self, url: str) -> int:
        result = await fetch.http_post(f"{self.baseurl_http}/check_url_safely", {
            "url": url
        })
        if result["status"] == "ok":
            return result["level"]
        return False

    async def getModelShow(self, model: str) -> T.Union[List[ModelShow], bool]:
        result = await fetch.http_post(f"{self.baseurl_http}/_get_model_show", {
            "model": model
        })
        if result["status"] == "ok":
            return [ModelShow.parse_obj(_model) for _model in result["data"]["variants"]]
        return False

    async def setModelShow(self, model: str, model_show: str) -> bool:
        result = await fetch.http_post(f"{self.baseurl_http}/_set_model_show", {
            "model": model,
            "model_show": model_show
        })
        if result["status"] == "ok":
            return True
        return False


import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objects as go

from data import df
from layouts.cards import get_cards

df_cidade = df[(df['place_type'] == 'state') & (df['is_repeated'] == False)]
fig = go.Figure()

for uf in df_cidade['state'].unique():
    df_uf = df_cidade[df_cidade['state'] == uf]
    df_uf.set_index(pd.DatetimeIndex(df_uf.last_available_date), inplace=True)
    df_uf = df_uf.resample('D', origin='start').bfill()
    df_uf.last_available_date = df_uf.index.values
    fig.add_trace(go.Scatter(
        x=df_uf['last_available_date'],
        y=df_uf['last_available_death_rate'] * 100,
        name=uf
    ))

fig.update_xaxes(
    dtick="M1",
    tickformat="%m/%Y"
)
fig.update_layout(
    plot_bgcolor='white',
    xaxis={
        'title': {'text': 'Data'},
        'showgrid': True,
        'gridcolor': 'rgba(100, 100, 100, 0.5)'
    },
    yaxis={
        'title': {'text': 'Taxa de mortalidade (%)'},
        'showgrid': True,
        'gridcolor': 'rgba(100, 100, 100, 0.5)',

    },
)

evolucao_mortalidade = html.Div(get_cards(
    html.Div(
        [
            html.Div(dcc.Graph(id='grafico_evolucao_cidade', figure=fig))
        ],
        id='evolucao_mortalidade_por_estado'
    ), header='Comparativo de mortalidade por dia'
), className='col-6')


import cv2
import numpy as np

DEBUG = True # set to True to show more debug figures

class Settings:
  source = cv2.imread("./pics/blemish.png", cv2.IMREAD_COLOR)
  h,w,c = source.shape
  crop_zone = None
  click_pos = (0, 0)
  orig_shift = [0, 0]
  radius = 15
  block_size = 2*radius

def tile_sub_blocks(sub_blocks ,bsize):
  ''' Debug function
      Show complete sub blocks
  '''
  padding = 1
  sub_blk = sub_blocks.copy()
  for i in range(9):
    bg = np.zeros((bsize+2*padding,bsize+2*padding,3),
      dtype=sub_blk[i].dtype)
    try:
      bg[padding:bsize+padding,padding:bsize+padding] = sub_blk[i]
    except:
      pass
    sub_blk[i] = bg

  blocks_tile = np.vstack((
    np.hstack((sub_blk[0],sub_blk[1],sub_blk[2])),
    np.hstack((sub_blk[3],sub_blk[4],sub_blk[5])),
    np.hstack((sub_blk[6],sub_blk[7],sub_blk[8])),
    ))
  cv2.imshow('debug: tile sub blocks', blocks_tile)

def get_freq():
  ''' TODO
  Maybe use frequency methods as a LPF to make selected patch smoother...
  '''
  pass

def get_gradients(blocks, bsize):
  '''
  Calculate Gxy of (blurred) blocks)
  '''
  gradients = {}
  for i, blk in enumerate(blocks):
    if blk.shape == (bsize, bsize, 3):
      blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)
      gx = np.mean(cv2.Sobel(blk,cv2.CV_32F,1,0,ksize=3))
      gy = np.mean(cv2.Sobel(blk,cv2.CV_32F,0,1,ksize=3))
      gxy = np.sqrt(gx*gx+gy*gy)
      gradients[i] = gxy
    else:
      print(i, 'incomplete block with shape of {}'.format(blk.shape))
  return gradients

def apply_patch(sub_blocks, gradients):
  '''
  Sort the gradients of sub blocks,
  and then choose the one with the least Gxy value as the patch.
  Finally, clone the patch to the source image.
  '''
  sorted_gradients = sorted(gradients, key=gradients.get)
  patch_idx = sorted_gradients[0] if sorted_gradients[0] != 4 else sorted_gradients[1]
  patch = sub_blocks[patch_idx]
  target = Settings.source #sub_blocks[4]
  center = Settings.click_pos #(Settings.radius, Settings.radius)
  if DEBUG:
    cv2.imshow('patch', patch)
  patch_mask = np.zeros(patch.shape, patch.dtype)
  patch_mask = cv2.circle(patch_mask,(Settings.radius, Settings.radius),
                          Settings.radius, (255,255,255), -1)
  Settings.source = cv2.seamlessClone(patch, target, patch_mask, center, cv2.NORMAL_CLONE)

def get_subzones(blurred_roi):
  '''
  According to the click position, divide the cropped ROI into sub blocks.
  If the whole ROI were within the source image, then there'll be 3x3 tiled blocks.
  The center one should contain the user clicked blemish.
  The `tile_sub_blocks()` shows all complete blocks.
  '''
  bsize = Settings.block_size
  shift = Settings.orig_shift
  idx = np.array([[shift[1]+i*bsize,shift[1]+(i+1)*bsize,
                   shift[0]+j*bsize,shift[0]+(j+1)*bsize]
                   for i in range(3) for j in range(3)])
  idx[idx <= 0] = 0 #set negative idx to zero

  blurred_blocks = [blurred_roi[idx[b][0]:idx[b][1], idx[b][2]:idx[b][3]]
                for b in range(9)]
  sub_blocks = [Settings.crop_zone[idx[b][0]:idx[b][1], idx[b][2]:idx[b][3]]
                for b in range(9)]

  # Use blurred blocks to find the usable patch
  if DEBUG:
    tile_sub_blocks(blurred_blocks, bsize)
  gradients = get_gradients(blurred_blocks, bsize)
  # Apply the original (un-blurred) patch to the source image
  apply_patch(sub_blocks, gradients)

def get_roi(action, x, y, flags, userdata):
  radius3 = Settings.radius*3
  if action==cv2.EVENT_LBUTTONDOWN:
    Settings.click_pos = (x, y)

    xmin = x - radius3
    if xmin < 0:
      Settings.orig_shift[0] = xmin
      xmin = 0
    else:
      Settings.orig_shift[0] = 0
    ymin = y - radius3
    if ymin < 0:
      Settings.orig_shift[1] = ymin
      ymin = 0
    else:
      Settings.orig_shift[1] = 0

    xmax = x + radius3
    if xmax > Settings.w:
      xmax = Settings.w
    ymax = y + radius3
    if ymax > Settings.h:
      ymax = Settings.h

    Settings.crop_zone = Settings.source[ymin:ymax,xmin:xmax].copy()
    # Filter out salt-and-pepper noise
    blurred_roi = Settings.crop_zone.copy()
    blurred_roi = cv2.medianBlur(blurred_roi, ksize=3)
    #blurred_roi = cv2.GaussianBlur(blurred_roi, ksize=(3,3), sigmaX=0)
    get_subzones(blurred_roi)

  elif action==cv2.EVENT_RBUTTONDOWN:
    Settings.crop_zone = None # reset
    cv2.destroyWindow("ROI")

  if Settings.crop_zone is not None and DEBUG:
    cv2.imshow("ROI", Settings.crop_zone)

def main():
  cv2.namedWindow("Source Image")
  cv2.setMouseCallback("Source Image", get_roi)

  while True:
    cv2.imshow("Source Image", Settings.source)

    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

  cv2.destroyAllWindows()

if __name__ == "__main__":
  main()


from django.db.models import Q
from django.contrib import admin

from edc_base.modeladmin.admin import BaseModelAdmin

from bhp066.apps.bcpp_household.models import HouseholdStructure

from ..models import HouseholdHeadEligibility, HouseholdMember
from ..forms import HouseholdHeadEligibilityForm
from bhp066.apps.bcpp_household_member.constants import HEAD_OF_HOUSEHOLD

class HouseholdHeadEligibilityAdmin(BaseModelAdmin):

    form = HouseholdHeadEligibilityForm
    fields = (
        "household_structure",
        "household_member",
        "report_datetime",
        "aged_over_18",
        'household_residency',
        "verbal_script")

    radio_fields = {
        "aged_over_18": admin.VERTICAL,
        "household_residency": admin.VERTICAL,
        "verbal_script": admin.VERTICAL}

    list_filter = ('report_datetime', 'user_created', 'user_modified', 'hostname_created',
                   'household_member__household_structure__household__community')

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "household_member":
            kwargs["queryset"] = HouseholdMember.objects.filter(
                Q(household_structure__exact=request.GET.get('household_structure', 0)),
                (Q(is_consented=True) | Q(age_in_years__gte=18) | Q(relation=HEAD_OF_HOUSEHOLD)) &
                (~Q(survival_status='Dead')))

        if db_field.name == "household_structure":
            kwargs["queryset"] = HouseholdStructure.objects.filter(
                id__exact=request.GET.get('household_structure', 0))

        return super(HouseholdHeadEligibilityAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('household_member', 'household_structure', ) + self.readonly_fields
        else:
            return self.readonly_fields

admin.site.register(HouseholdHeadEligibility, HouseholdHeadEligibilityAdmin)


import cv2
import glob
import os
import sys
import numpy as np
from time import sleep, time

normal_cnt = 0
broken_cnt = 0

dataPath = "/home/team/project/GAN/bean/data"
normal_imgs = glob.glob(os.path.join(dataPath+"/normal_data/normal_third_processed_data",'*.jpg'))
normal_imgs = glob.glob(os.path.join(dataPath+"/broken_data/broken_third_processed_data",'*.jpg'))
# normal_imgs = glob.glob(os.path.join(dataPath+"/normal_data/normal_rotated_data",'*.jpg'))

for x in normal_imgs:
    img = cv2.imread(x, cv2.IMREAD_COLOR)
    imgray = cv2.imread(x, cv2.IMREAD_GRAYSCALE)
    bitwise = cv2.bitwise_not(imgray)
    _, thr = cv2.threshold(bitwise, 50, 255, cv2.THRESH_BINARY)
    # 검은 배경 생성
    _, white = cv2.threshold(imgray, 0, 255, cv2.THRESH_BINARY)
    # black을 다시 3 channel로 변경 => 초록 타원을 씌우기 위함
    white = cv2.cvtColor(white, cv2.COLOR_GRAY2BGR)

    # cv2.imshow("img", img)
    # cv2.imshow("imgray", imgray)
    # cv2.imshow("black", black)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    _, contour, _ = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # contour 중에서 최대로 많은 것 탐색
    max_iter = 0
    max_len = 0
    for i in range(len(contour)):
        if len(contour[i]) > max_len:
            max_len = len(contour[i])
            max_iter = i
    # sleep(10)
    con = contour[max_iter]
    (x, y), r = cv2.minEnclosingCircle(con)
    # cv2.circle(img, (int(x), int(y)), int(r), (0, 0, 255), 2)
    ellipse = cv2.fitEllipse(con)
    # black에 ellipse를 그림
    cv2.ellipse(white, ellipse, (0, 255, 0), -1)

    # cv2.imshow("white",white)
    # cv2.imshow("img",img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    start = time()
    check_num = 0
    white_mat = white[0][0]
    is_broken = False

    # normal => 12 정도 limit
    # broken => 들쭉날쭉 ..
    limit_cnt = 0
    for i in range(white.shape[0]):
        for j in range(white.shape[1]):
            r_number = white[i][j][0]
            if r_number == check_num and \
               img[i][j][0] == 255 and \
               img[i][j][1] == 255 and \
               img[i][j][2] == 255:
                is_broken = True
                limit_cnt += 1
                # break
        # if is_broken:
        #     break
    # print("end : " + str( time() - start ))
    # sleep(10)
    if is_broken:
        broken_cnt += 1
    else:
        normal_cnt += 1

    print("broken cnt : %d \nnormal cnt : %d"%(broken_cnt, normal_cnt))
    print("limit_cnt : " + str(limit_cnt))

    # cv2.imshow("black", black)
    # cv2.imshow('img', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    # 녹색으로 다 채운(-1)
    # 녹색 픽셀 획득
    # bgr:  [0, 255, 0]
    # hsv:  [ 60 255 255]
    # 녹색 픽셀 중 배경색(흰색)과 같은 픽셀이 있다 => 브로큰!!


#!/usr/bin/python
# -*- coding: utf-8 -*-
from collections import defaultdict

__author__ = 'kang'

# from kang_dev.NB import Pool
from kang_dev.NB1 import Pool
from parsers import string_parser
import os
import random
import re
Classes = ["clinton",  "lawyer",  "math",  "medical",  "music",  "sex"]
path = 'learn_and_test\learn\\'
p = Pool()

'''the k-folds to test the accuracy'''
result = {}
k_folds_random = {}
for c in Classes:
    result[c] = 0
k = 10
k_folds_obj = {}
for i in range(0, k):
    k_folds_obj[i] = {}

# put all the files together
cate = defaultdict(list)
for i in Classes:
    k_folds_random.update(p.k_folds(path + i, i))
    cate[i].append('')
# split them into k folds
for (key, v) in k_folds_random.items():
    number = random.randint(0, k-1)
    k_folds_obj[number][key] = v

counter = defaultdict()
count = defaultdict()
for cc in Classes:
    count[cc] = 0
    counter[cc] = 0

# the k times loop
for times in range(0, k-1):
    # the 1/k of the whole files used to test
    the_rest = k_folds_obj.pop(times)

    #compute the numbers of the test sample in every category
    for (key, v) in the_rest.items():
        counter[v] += 1
    print (counter)

    # before to learn
    for (dic, value) in k_folds_obj.items():
        for (key, v) in value.items():
            cate[v].append(key)

    # to learn
    for (key2, value2) in cate.items():
        p.learn_2(path + key2, key2, value2)

    # test
    print (the_rest)
    for (key, v) in the_rest.items():
        # name = re.match(r'^[a-zA-Z]+', key)
        res = p.Probability(path + v + "\\" + key)
        name = res[0][0]
        if name == v:
            count[v] += 1

for (key1, value1) in counter.items():
    print ('the ' + str(key1) + ' is {}'.format(float(count[key1])/float(value1)))

'''to learn and test'''
# for i in Classes:
#     p.learn(path + i, i)

# base = 'learn_and_test\\test\\'
# for i in Classes:
#     dir = os.listdir(base + i)
#     count = 0
#     for file in dir:
#         res = p.Probability(base + i + "\\" + file)
#         name = res[0][0]
#
#         if name == i:
#             count += 1
#         print(i + ": " + file + ": " + str(res))
#
#     print(float(count)/float(len(dir)))


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 26 15:58:51 2016

@author: hehu
"""

from sklearn.datasets import load_digits
import matplotlib.pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.feature_selection import RFECV
#from sklearn.linear_model import RandomizedLogisticRegression
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import sys

if __name__ == "__main__":

    digits = load_digits()

    numDigits = 10
    plt.figure(figsize=[4, 12])
    images_and_labels = list(zip(digits.images, digits.target))

    for index, (image, label) in enumerate(images_and_labels[:numDigits]):
        plt.subplot(numDigits/2, 2, index + 1)
        plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
        plt.title('Class %i' % label)

    #plt.tight_layout()
    plt.savefig("../images/digits.pdf", bbox_inches = "tight")

    idx = (digits.target == 8) | (digits.target == 9)
    X = digits.data[idx, :]
    y = digits.target[idx]

    ss = StandardScaler()
    X = ss.fit_transform(X)

    skf = StratifiedKFold(y, 5, shuffle = True, random_state = 1)
    rfecv = RFECV(estimator=LinearDiscriminantAnalysis(), cv = skf)
    rfecv.fit(X, y)

    plt.figure()
    plt.xlabel("Number of features")
    plt.ylabel("Accuracy")
    plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_, linewidth = 2)

    topScore = np.max(rfecv.grid_scores_)
    n_feat = np.sum(rfecv.support_)
    plt.annotate('Top score %.1f %% with %d features' % (100.0*topScore, n_feat),
             xy=(n_feat, topScore),
             xytext=(35, 0.92),
             size=13,
             bbox=dict(boxstyle="round4", fc="w", ec = "g"),
             arrowprops=dict(arrowstyle="simple",
                             connectionstyle="arc3,rad=-0.2",
                             shrinkA = 0,
                             shrinkB = 8,
                             fc = "g",
                             ec = "g"),
             horizontalalignment='center',
             verticalalignment='middle')

    plt.annotate('All features: %.1f %%' % (100.0*rfecv.grid_scores_[-1]),
             xy=(len(rfecv.grid_scores_), rfecv.grid_scores_[-1]),
             xytext=(55, 0.95),
             size=13,
             bbox=dict(boxstyle="round4", fc="w", ec = "g"),
             arrowprops=dict(arrowstyle="simple",
                             connectionstyle="arc3,rad=0.2",
                             shrinkA = 0,
                             shrinkB = 8,
                             fc = "g",
                             ec = "g"),
             horizontalalignment='center',
             verticalalignment='middle')

    plt.savefig("../images/rfe_accuracy.pdf", bbox_inches = "tight")

    mask = rfecv.support_.reshape(8, 8)
    plt.figure()
    plt.imshow(mask.astype(int), interpolation = 'nearest')
    plt.title("RFECV: Selected %d features (bright = selected)" % np.count_nonzero(mask))
    plt.savefig("../images/rfe_mask.pdf", bbox_inches = "tight")

    ############ LR

    lr = LogisticRegression(penalty = "l1")
    C_range = 10.0 ** np.arange(-5,12, 0.5)

    accuracies = []
    nonzeros = []
    bestScore = 0

    skf = StratifiedKFold(y, 5, shuffle = True, random_state = 3)
    np.random.seed(10)

    for C in C_range:
        lr.C = C
        score = cross_val_score(lr, X, y, cv = skf).mean()
        lr.fit(X, y)
        nonzeros.append(np.count_nonzero(lr.coef_))
        accuracies.append(score)

        if score > bestScore:
            bestScore = score
            bestCoef = lr.coef_.ravel()
            bestC = C

    plt.figure()
    plt.xlabel("Number of features")
    plt.ylabel("Accuracy")
    plt.plot(nonzeros, accuracies, linewidth = 2)
    topIdx = np.argmax(accuracies)
    topScore = np.max(accuracies)
    top_nz = nonzeros[topIdx]

    plt.annotate('Top score %.1f %% with %d features' % (100.0*topScore, top_nz),
             xy=(top_nz, topScore),
             xytext=(25, 0.65),
             size=13,
             bbox=dict(boxstyle="round4", fc="w", ec = "g"),
             arrowprops=dict(arrowstyle="simple",
                             connectionstyle="arc3,rad=-0.2",
                             shrinkA = 0,
                             shrinkB = 8,
                             fc = "g",
                             ec = "g"),
             horizontalalignment='center',
             verticalalignment='middle')

    plt.annotate('All features: %.1f %%' % (100.0*accuracies[-1]),
             xy=(nonzeros[-1], accuracies[-1]),
             xytext=(45, 0.75),
             size=13,
             bbox=dict(boxstyle="round4", fc="w", ec = "g"),
             arrowprops=dict(arrowstyle="simple",
                             connectionstyle="arc3,rad=0.2",
                             shrinkA = 0,
                             shrinkB = 8,
                             fc = "g",
                             ec = "g"),
             horizontalalignment='center',
             verticalalignment='middle')

    plt.savefig("../images/L1_digits_accuracy.pdf", bbox_inches = "tight")

    mask = (bestCoef != 0).astype(np.uint8)
    mask = mask.reshape(8, 8)
    plt.figure()
    plt.imshow(mask.astype(int), interpolation = 'nearest')
    plt.title("LOGREG: Selected %d features (bright = selected)" % np.count_nonzero(mask))
    plt.savefig("../images/L1_digits_mask.pdf", bbox_inches = "tight")

    # Stability selection
#
#    model = RandomizedLogisticRegression(C = 1,
#                                         sample_fraction = 0.75,
#                                         n_resampling = 1000,
#                                         selection_threshold=0.25,
#                                         n_jobs = 2,
#                                         random_state = 42)
#
#    model.fit(X, y)
#    mask = model.get_support()
#    mask = mask.reshape(8,8)

  #  plt.figure()
  #  plt.imshow(mask.astype(int), interpolation = 'nearest')
  #  plt.title("RLR: Selected %d features\n(bright = selected)" % np.count_nonzero(mask))
  #  plt.savefig("../images/Stability_selection_digits_mask.pdf", bbox_inches = "tight")

    plt.figure()
    plt.imshow(rfecv.ranking_.max() - rfecv.ranking_.reshape(8,8))
    plt.title("Order of RFECV selection\n(dark = dropped first)")
    plt.savefig("../images/RVECF_ranking.pdf", bbox_inches = "tight")

    plt.figure()
    rf = RandomForestClassifier(n_estimators = 500)
    rf.fit(X, y)
    plt.imshow(rf.feature_importances_.reshape(8,8))
    plt.title("Random forest feature importances\n(bright = important)")
    plt.savefig("../images/RF_ranking.pdf", bbox_inches = "tight")

#    plt.figure()
#    plt.imshow(model.scores_.reshape(8,8))
#    plt.title("Randomized logistic regression scores\n(bright = important)")
#    plt.savefig("../images/RLR_ranking.pdf", bbox_inches = "tight")


#!/usr/bin/env python

from lib.exceptions.check_exception import CheckException
from lib.base.script.integration_test.case_mw import CaseMW

class Test200SmartFullIdevice020(CaseMW):
    """Default configuration
    No.: test_200smart_full_idevice_020
    Preconditions:
        1. Open Micro/WIN V2.5;
        2. In PN wizard, select plc role is "Controller", click "Next";
    Step actions:
        1. View controller configurations;
        2. Click "Previous", change ip or station name, then click "Next" to controller page;
    Expected results:
        1. Ip and station name show correctly in the network topology, there is a  "Catalog" tree with correct
           module in the right.
        2. Ip and station name show correctly in the network topology.
    Priority: H
    Author: Cai, Yong
    ChangeInfo: Cai, Yong 2019-12-13 create
    """

    def prepare(self):
        """the preparation before executing the test steps
        Args:
        Example:
        Return:
        Author: Cai, Yong
        IsInterface: False
        ChangeInfo: Cai, Yong 2019-12-13 create
        """
        super(Test200SmartFullIdevice020, self).prepare()

        self.logger.info('Preconditions:')
        self.logger.info('1. Open Micro/WIN V2.5;')
        self.logger.info('2. In PN wizard, select plc role is "Controller", click "Next";')

    def process(self):
        """execute the test steps
        Args:
        Example:
        Return:
        Author: Cai, Yong
        IsInterface: False
        ChangeInfo: Cai, Yong 2019-12-13 create
        """
        super(Test200SmartFullIdevice020, self).process()

        self.logger.info('Step actions:')
        self.logger.info('1. View controller configurations;')
        self.logger.info('2. Click "Previous", change ip or station name, then click "Next" to controller page;')

        self.logger.info('Expected results:')
        self.logger.info('1. Ip and station name show correctly in the network topology, there is a  "Catalog" '
                         'tree with correct module in the right.')
        self.logger.info('2. Ip and station name show correctly in the network topology.')

    def cleanup(self):
        """clean up after performing the test steps
        Args:
        Example:
        Return:
        Author: Cai, Yong
        IsInterface: False
        ChangeInfo: Cai, Yong 2019-12-13 create
        """
        super(Test200SmartFullIdevice020, self).cleanup()


from django.shortcuts import render
from django.http import JsonResponse
# Create your views here.
from django.conf import settings
import pandas as pd
import os

def getOrder(request):
    """
    R ( int recency )
    F ( int frequency )
    M ( int monetary )
    ID (int ID)

    將 ID 以倒 R 值排序回傳 (大到小) ( function sort_by_recency( ) )
    將 ID 以倒 F 值排序回傳 (大到小) ( function sort_by_frequency( ) )
    將 ID 以 M 值排序回傳 (小到大) ( function sort_by_monetary( ) )
    將 ID 以倒 RFM 總值排序回傳 (大到小) ( function sort_by_RFM( ) )

    rtype : json
    """

    method = request.GET.get('method','R')
    file_dir = os.path.join(settings.STATICFILES_DIRS[0], 'rfm.csv')

    data = pd.read_csv(file_dir)

    if method == 'R':
        result = data.sort_values('R', ascending = False)['ID'].values
        return JsonResponse(result.tolist(), safe=False)
    if method == 'F':
        result = data.sort_values('F', ascending = False)['ID'].values
        return JsonResponse(result.tolist(), safe=False)
    if method == 'M':
        result = data.sort_values('M', ascending = True)['ID'].values
        return JsonResponse(result.tolist(), safe=False)
    if method == 'RFM':
        data.loc[:, "RFM"] = data["R"] + data["F"] + data["M"]
        RFM = data.sort_values('RFM', ascending = False)['ID'].values
        return JsonResponse(RFM.tolist(), safe=False)


from elasticsearch import Elasticsearch

import random
import datetime
import argparse
import sys

def get_index_path(collection_name, namespace, lcp_project_id):
    index_path = "{}_{}_{}".format(
        lcp_project_id, namespace, collection_name
    )

    return index_path.lower()

def get(es, id, index, doc_type):
    document = es.get(
        doc_type=doc_type,
        id=id,
        index=index
    )

    return document.get('_source')

def index(es, body, id, index, doc_type):
    body['id'] = id

    return es.index(body=body, id=id, index=index, doc_type=doc_type)

def search(es, body, index):
    response = es.search(body=body, index=index)

    search_hits = response.get('hits')

    documents = [hit.get('_source') for hit in search_hits.get('hits')]

    return documents, search_hits.get('total')

argument_parser = argparse.ArgumentParser(
    usage='{} recommendation_data_loader '
    '--command <Command> '
    '--elasticsearch-hostname <Elasticsearch Hostname> (Optional) '
    '--model-name <model Name> '
    '--lcp-project-id <LCP Project ID>'.format(sys.argv[0])
)

argument_parser.add_argument('--command', required=True)
argument_parser.add_argument('--elasticsearch-hostname', default='http://localhost:9200')
argument_parser.add_argument('--lcp-project-id', required=True)
argument_parser.add_argument('--model-name', required=True)
argument_parser.add_argument('--output-version-id', required=False)
argument_parser.add_argument('--output-version-status', required=False, default="RUNNING")
argument_parser.add_argument('--output-version-events', required=False, default=random.randrange(1000, 10000))
argument_parser.add_argument('--output-version-items', required=False, default=random.randrange(500, 1000))

args = argument_parser.parse_args()

es = Elasticsearch(args.elasticsearch_hostname)

documents, total = search(
    es,
    {
        "query": {
            "term": {
                "name" : args.model_name
            }
        }
    }, get_index_path('jobs', 'osbasahfaroinfo', args.lcp_project_id)
)

if total == 0:
    sys.exit('No such model with name "{}"'.format(args.model_name))

job = documents[0]

if args.command == 'add_output_version':
    now = datetime.datetime.utcnow()
    now_str = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'

    id = int(now.timestamp())
    job_runs_index = get_index_path('job-runs', 'osbasahfaroinfo', args.lcp_project_id)

    body = {
        'context': {},
        'createdDate': now_str,
        'job': {
            'id': job.get('id') ,
            'type': job.get('type')
        } ,
        'lastUpdatedDate': now_str,
        'status': args.output_version_status,
        'step': 'DATA_PREPARATION',
        'trigger': 'MANUAL'
    }

    if args.output_version_status in ['COMPLETED', 'PUBLISHED']:
        body['completedDate'] = now_str
        body['context']['itemsDatasetCount'] = args.output_version_items
        body['context']['userItemInteractionsDatasetCount'] = args.output_version_events

    index(
        es, body, id, job_runs_index, 'job-runs'
    )

    print("Output Version {}".format(get(es, id, job_runs_index, 'job-runs')))


import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_labs as dl
import dash_bootstrap_components as dbc
import plotly.express as px
import plotly.graph_objects as go

# Make app and template
app = dash.Dash(__name__, plugins=[dl.plugins.FlexibleCallbacks()])
tpl = dl.templates.DbcRow(app, title="Gapminder", left_cols=4, figure_template=True)

# Load and preprocess dataset
df = px.data.gapminder()
years = sorted(df.year.drop_duplicates())
continents = list(df.continent.drop_duplicates())

@app.callback(
    args=dict(
        year=tpl.new_slider(
            years[0], years[-1], step=5, value=years[-1], label="Year"
        ),
        continent=tpl.new_checklist(continents, value=continents, label="Continents"),
        logs=tpl.new_checklist(["log(x)"], value="log(x)", label="Axis Scale"),
    ),
    output=tpl.new_graph(id="scatter_graph"),
    template=tpl,
)
def callback(year, continent, logs):
    # Let parameterize infer output component
    year_df = df[df.year == year]
    if continent:
        year_df = year_df[year_df.continent.isin(continent)]

    if not len(year_df):
        return go.Figure()

    title = f"Life Expectancy ({year})"
    return (
        px.scatter(
            year_df,
            x="gdpPercap",
            y="lifeExp",
            size="pop",
            color="continent",
            hover_name="country",
            log_x="log(x)" in logs,
            size_max=60,
            title=title,
        )
        .update_layout(margin=dict(l=0, r=0, b=0))
        .update_traces(marker_opacity=0.8)
    )

line_chart = dcc.Graph(id="line_chart")
tpl.add_component(line_chart, location="right", after=0)

@app.callback(Output("line_chart", "figure"), Input("scatter_graph", "clickData"))
def display_click_data(clickData):
    country = ""
    fig = px.line(title=f"Country selected: {country}")
    if clickData:
        country = clickData["points"][0]["hovertext"]
        fig = px.line(
            df[df.country == country],
            x="year",
            y="lifeExp",
            title=f"Country selected: {country}",
        )
    return fig

app.layout = dbc.Container(tpl.children, fluid=True)

if __name__ == "__main__":
    app.run_server(debug=True)


# Copyright 2018 Shengyang Sun
#
# Licensed 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.

import tensorflow as tf
from scipy.sparse.linalg import eigsh
import numpy as np

from . import settings

def vectorize(mat):
    return tf.reshape(tf.transpose(mat), [-1, 1])

def devectorize(vec, shape):
    return tf.transpose(tf.reshape(vec, shape[::-1]))

def batch_vectorize(mat):
    return tf.reshape(tf.transpose(mat), [-1, tf.shape(mat)[-1]])

def batch_devectorize(vec, shape):
    return tf.transpose(tf.reshape(vec, shape[::-1]))

class Kronecker(object):

    def __init__(self, vars):
        self.vars = vars
        self.sizes = [tf.shape(v)[0] for v in self.vars]

    def matrix_inverse_root(self, k=10):
        roots = []
        for v in self.vars:
            v = v.numpy() if isinstance(v, tf.Tensor) else v
            eigvals, eigvecs = eigsh(v, k=min(k, v.shape[0]-1))
            eigvals = np.maximum(eigvals, 1e-10)
            eigvals = tf.diag(1. / eigvals ** 0.5)
            root = tf.matmul(eigvecs, tf.matmul(eigvals, eigvecs.transpose()))
            roots.append(root)
        return self.__class__(roots)

    def matrix_root(self, k=10):
        roots = []
        for v in self.vars:
            v = v.numpy() if isinstance(v, tf.Tensor) else v
            eigvals, eigvecs = eigsh(v, k=min(k, v.shape[0]-1))
            eigvals = np.maximum(eigvals, 1e-10)
            eigvals = tf.diag(eigvals ** 0.5)
            root = tf.matmul(eigvecs, tf.matmul(eigvals, eigvecs.transpose()))
            roots.append(root)
        return self.__class__(roots)

    def matmul_vec(self, vec):
        res = vec
        for v in self.vars:
            res = tf.transpose(devectorize(res, [-1, tf.shape(v)[0]]))
            factor = tf.matmul(v, res)
            res = tf.reshape(tf.transpose(factor), [-1, 1])
        res = devectorize(res, [-1, tf.shape(vec)[-1]])
        return res

class testKronecker(tf.test.TestCase):
    def test_2elements(self):
        with self.test_session() as sess:
            var1 = tf.random_normal(shape=[4, 4])
            var2 = tf.random_normal(shape=[3, 3])
            var1 = tf.matmul(var1, var1, transpose_b=True)
            var2 = tf.matmul(var2, var2, transpose_b=True)
            kron = Kronecker([var1, var2])

            v = tf.random_normal(shape=[12, 1])
            v_mat = devectorize(v, [3, 4])

            kv = kron.matmul_vec(v)
            kp = tf.contrib.kfac.utils.kronecker_product
            real_kv = tf.matmul(kp(var1, var2), v)
            # real_kv = vectorize(tf.matmul(tf.matmul(var2, v_mat), var1, transpose_b=True))

            aa, bb = sess.run([kv, real_kv])
            print(aa)
            print(bb)
            self.assertAllClose(aa, bb)

    def test_3elements(self):
        with self.test_session() as sess:
            var1 = tf.random_normal(shape=[3, 3])
            var2 = tf.random_normal(shape=[4, 4])
            var3 = tf.random_normal(shape=[5, 5])
            var1 = tf.matmul(var1, var1, transpose_b=True)
            var2 = tf.matmul(var2, var2, transpose_b=True)
            var3 = tf.matmul(var3, var3, transpose_b=True)
            kron = Kronecker([var1, var2, var3])

            v = tf.random_normal(shape=[12 * 5, 1])
            kv = kron.matmul_vec(v)

            kp = tf.contrib.kfac.utils.kronecker_product
            M = kp(var1, kp(var2, var3))
            real_kv = tf.matmul(M, v)

            aa, bb = sess.run([kv, real_kv])
            print(aa.squeeze())
            print(bb.squeeze())
            self.assertAllClose(aa, bb, rtol=1e-5, atol=1e-4)

    def test_3elements_batch(self):
        with self.test_session() as sess:
            var1 = tf.random_normal(shape=[3, 3])
            var2 = tf.random_normal(shape=[4, 4])
            var3 = tf.random_normal(shape=[5, 5])
            var1 = tf.matmul(var1, var1, transpose_b=True)
            var2 = tf.matmul(var2, var2, transpose_b=True)
            var3 = tf.matmul(var3, var3, transpose_b=True)
            kron = Kronecker([var1, var2, var3])

            v = tf.random_normal(shape=[12 * 5, 5])
            kv = kron.matmul_vec(v)

            kp = tf.contrib.kfac.utils.kronecker_product
            M = kp(var1, kp(var2, var3))
            real_kv = tf.matmul(M, v)

            aa, bb = sess.run([kv, real_kv])
            print(aa.squeeze())
            print('------------------')
            print(bb.squeeze())
            self.assertAllClose(aa, bb, rtol=1e-5, atol=1e-4)


"""
synthetic minority oversampling technique
合成少数类过采样技术
1.对于少数类中的每一个样本x，以欧式距离为标准计算他到少数类样本集中所有其他样本的距离，得到其k近邻
2.根据样本不平衡比例设置一个采样比例，用来确定采样倍率N。
    对于每一个少数类样本x，从其k近邻中随机选择若干个样本xn
3.基于选择的样本xn，分别与原样本x按照公式
    x_new = x + rand(0,1) * (x_n - x) 来生成新样本。
此方法生成的新样本，在空间上的表示为原样本和其某一k近邻之间。
"""

# code:
import random
from sklearn.neighbors import NearestNeighbors
import numpy as np

class Smote:
    def __init__(self, samples, N=1, k=5):
        self.n_samples, self.n_attrs = samples.shape
        self.N = N
        self.k = k
        self.samples = samples
        self.new_index = 0

    def over_sampling(self):
        # N = int(self.N / 100)
        self.synthetic = np.zeros((self.n_samples, self.n_attrs))
        neighbors = NearestNeighbors(n_neighbors=self.k).fit(self.samples)
        print('neighbors', neighbors)
        for i in range(len(self.samples)):
            nnarray = neighbors.kneighbors(self.samples[i].reshape(1, -1), return_distance = False)[0]
            print(nnarray)
            self._populate(N, i, nnarray)
        return self.synthetic
        # for each minority class samples, choose N of the K nearest neighbors and
        # generate N synthetic samples

    def _populate(self, N, i, nnarray):
        for j in range(N):
            nn = random.randint(0, self.k-1)
            dif = self.samples[nnarray[nn]] - self.samples[i]
            gap = random.random()
            self.synthetic[self.new_index] = self.samples[i] + gap * dif
            self.new_index += 1

a = np.array([[1,2,3], [4,5,6], [2,3,1],[2,1,2],[2,3,4],[2,3,4]])
s = Smote(a, N=2)
print(s.over_sampling())


import pygame
from pygame.locals import *
import sys
import random

pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()

# Creates the screen
WIDTH = 1280
HEIGHT = 720
screen = pygame.display.set_mode([WIDTH, HEIGHT])

# Loads the image
bridge = pygame.image.load('images/Bridge.jpg')
# Scales image to fit screen
bridge = pygame.transform.scale(bridge, (WIDTH, HEIGHT))
wreckage = pygame.image.load('images/wreckage.jpg')
wreckage = pygame.transform.scale(wreckage, (WIDTH, HEIGHT))
biodome = pygame.image.load('images/biodome.jpg')
biodome = pygame.transform.scale(biodome, (WIDTH, HEIGHT))

def darker():
    """Makes the image darker by dividing the pixels by 3"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            pxarray[X, Y] = (red/3, green/3, blue/3)

def lighter():
    """Makes the image lighter"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if red < 86 and green < 86 and blue < 86:
                pxarray[X, Y] = (red*3, green*3, blue*3)

def invert():
    """inverts the image colours"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b

            red = 255 - red
            green = 255 - green
            blue = 255 - blue
            pxarray[X, Y] = (red, green, blue)

def greyscale():
    """converts the image to greyscale"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b

            grey = (red + green + blue)/3
            pxarray[X, Y] = (grey, grey, grey)

def bridge_lights():
    """Makes the lights turn off (go dark)"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if red > 150 and green > 150 and blue > 150:
                pxarray[X, Y] = (red/3, green/3, blue/3)

def bridge_lights_red():
    """Makes the lights red"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if red > 150 and green > 150 and blue > 150:
                red = red
                green /= 2
                blue /= 2
                pxarray[X, Y] = (red, green, blue)

def bridge_lights_red_effect():
    """Makes the bridge more"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if 120 > red > 15 and 100 > green > 15 and 100 > blue > 15:
                # - 10 from each rgb value to make it slightly darker

                pxarray[X, Y] = (red + 35, green - 15, blue - 15)

def bridge_lights_effect():
    """Makes the bridge slightly darker"""
    for Y in xrange(HEIGHT):
        for X in xrange(WIDTH):
            red = screen.get_at((X, Y)).r
            green = screen.get_at((X, Y)).g
            blue = screen.get_at((X, Y)).b
            if 100 > red > 15 and 100 > green > 15 and 100 > blue > 15:
                # - 10 from each rgb value to make it slightly darker
                pxarray[X, Y] = (red - 15, green - 15, blue - 15)

def draw_bridge():
    """blits the images to the screen"""
    screen.blit(bridge, [0, 0])
    bridge.set_alpha(alpha)

def draw_wreckage():
    screen.blit(wreckage, [0, 0])
    wreckage.set_alpha(alpha)

def draw_biodome():
    screen.blit(biodome, [0, 0])
    biodome.set_alpha(alpha)

# status of bridge lights (on)
status = 1

# Setting different transparency values
alpha = 255
alphas = [255, 210, 170, 130, 100, 75]

# Setting different background colours
back_colour = (255, 255, 255)
back_colours = [(255, 255, 255), (255, 0, 0), (0, 188, 255), (0, 0, 0), (5, 255, 43), (242, 252, 45), (247, 27, 247)]

# wreckage image functions

updates = 0

def darken_all(n):
    update = n
    for y in xrange(1, HEIGHT - 1):
        for x in xrange(1, WIDTH - 1):
            if update <= 10:
                    darken_pixel(x, y, update)
            else:
                new_updates = (20 - update)
                red_updates = (updates - 10)
                darken_pixel(x, y, new_updates)
                redden_pixel(x, y, red_updates)

def darken_pixel(x, y, n):
    darken_amount = 10 * n
    red = screen.get_at((x, y)).r
    green = screen.get_at((x, y)).g
    blue = screen.get_at((x, y)).b
    if red > darken_amount:
        red -= darken_amount
    if green > darken_amount:
        green -= darken_amount
    if blue > darken_amount:
        blue -= darken_amount
    screen.set_at((x, y), (red, green, blue))

def redden_pixel(x, y, n):
    amount = 10 * n
    limit = 255 - amount
    red = screen.get_at((x, y)).r
    green = screen.get_at((x, y)).g
    blue = screen.get_at((x, y)).b
    if red <= limit:
        red += amount
    screen.set_at((x, y), (red, green, blue))

def alien_torch():
    (mouse_pos_x, mouse_pos_y) = pygame.mouse.get_pos()
    torch_radius = 40
    for y in xrange(1, HEIGHT - 1):
        for x in xrange(1, WIDTH - 1):
            if ((x - mouse_pos_x)**2 + (y - mouse_pos_y)**2) <= (torch_radius**2):
                screen.set_at((x, y), (wreckage.get_at((x, y))))

# Main loop
while True:
    key_pressed = pygame.key.get_pressed()

    # Press 1, 2, and 3 to display each image
    if key_pressed[pygame.K_1]:
        # Resets alpha to 255 (no transparency)
        alpha = 255
        draw_biodome()
    if key_pressed[K_2]:
        alpha = 255
        draw_bridge()
    if key_pressed[K_3]:
        alpha = 255
        draw_wreckage()

    pxarray = pygame.PixelArray(screen)

    # Resets the bridge image
    if key_pressed[K_SPACE]:
        # deletes pxarray to unlock the surface
        del pxarray
        draw_bridge()
        pxarray = pygame.PixelArray(screen)

    # Press/hold down to make image darker
    if key_pressed[K_DOWN]:
        darker()

    # Press/hold down to make image lighter
    if key_pressed[K_UP]:
        lighter()

    # Hold down right to flicker lights on bridge
    if key_pressed[K_RIGHT]:
        if status == 1:
            status = 0
            del pxarray
            draw_bridge()
            pxarray = pygame.PixelArray(screen)
            clock.tick(4)
        elif status == 0:
            status = 1
            bridge_lights()
            bridge_lights_effect()
            clock.tick(25)

    # Hold down left to flicker red lights on bridge
    if key_pressed[K_LEFT]:
        if status == 1:
            status = 0
            del pxarray
            draw_bridge()
            pxarray = pygame.PixelArray(screen)
            clock.tick(4)
        elif status == 0:
            status = 1
            bridge_lights_red()
            bridge_lights_red_effect()
            clock.tick(25)

    # Press i to invert image colours, press i again to undo invert effect
    if key_pressed[pygame.K_i]:
        invert()

    # Press g to convert image to greyscale
    if key_pressed[pygame.K_g]:
        greyscale()

    del pxarray

    # Hold down 4 to see wreckage effect and use alien torch
    if key_pressed[K_4]:
        clock.tick(60)
        screen.blit(wreckage, (0, 0))
        updates += 1
        darken_all(updates)
        alien_torch()

    # Hold down a to see flashing effect on biodome
    if key_pressed[pygame.K_a]:
        # Selects a random background colour and fills the screen with it.
        back_colour = random.choice(back_colours)
        screen.fill(back_colour)
        # Selects a random alpha variable and draws biodome with new alpha value
        alpha = random.choice(alphas)
        draw_biodome()
        # changes how frequently the colours and transparency changes
        clock.tick(5)

    # Hold down a to see flashing effect on bridge
    if key_pressed[pygame.K_s]:
        back_colour = random.choice(back_colours)
        screen.fill(back_colour)
        alpha = random.choice(alphas)
        draw_bridge()
        clock.tick(5)

    # Hold down a to see flashing effect on wreckage
    if key_pressed[pygame.K_d]:
        back_colour = random.choice(back_colours)
        screen.fill(back_colour)
        alpha = random.choice(alphas)
        draw_wreckage()
        clock.tick(5)

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    # Press esc to exit
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            exit()