{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd\n",
    "import threading\n",
    "from tqdm import tqdm\n",
    "from sqlalchemy import create_engine\n",
    "con_tuomin_xyqb = create_engine('mysql+pymysql://pengcheng.zhang_r:0p951ekx8XWz3df@172.30.5.27:8066/xyqb?charset=utf8'\n",
    "                                ,echo=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyThread(threading.Thread):\n",
    "    def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None):\n",
    "        threading.Thread.__init__(self, group, target, name, args, kwargs, daemon=daemon)\n",
    "        self._return = None\n",
    "\n",
    "    def run(self):\n",
    "        if self._target is not None:\n",
    "            self._return = self._target(*self._args, **self._kwargs)\n",
    "\n",
    "    def get_return(self):\n",
    "        try:\n",
    "            return self._return\n",
    "        except Exception:\n",
    "            return None\n",
    "\n",
    "    def join(self):\n",
    "        threading.Thread.join(self)\n",
    "        return self._return\n",
    "    \n",
    "from collections import defaultdict\n",
    "import requests\n",
    "def get_json_feature(n,host,url,params = None,method='GET',semlock=None):\n",
    "    \"\"\" 访问特征,返回json \"\"\"\n",
    "    _dict = defaultdict(str)  \n",
    "    headers = {\n",
    "        'rc_auth_key':'rc_developer',\n",
    "        'rc_auth_secret':'rc_secret'\n",
    "    }\n",
    "    try:\n",
    "        _url = host+url\n",
    "        if method =='GET':\n",
    "            _request = requests.get(_url,params=params,headers = headers)\n",
    "        elif method == 'POST':\n",
    "            _request = requests.post(_url,data=params,headers = headers,timeout=5)\n",
    "            \n",
    "        if _request.status_code == 200:\n",
    "            _response = _request.json()\n",
    "            if _response['code'] == 200:\n",
    "                for key ,value in _response['data']['subInfo'].items():\n",
    "                    _dict[key] = value\n",
    "\n",
    "                for key,value in _response['data']['features'].items():\n",
    "                \n",
    "                    _dict[key] = _response['data']['features'][key]['value']\n",
    "#                 if 'loanId' not in _dict.keys():\n",
    "#                     _dict['loanId'] = params['loan_id']\n",
    "#                 if 'term_no'not in _dict.keys():\n",
    "#                     _dict['term_no'] = params['term_no']\n",
    "            else:\n",
    "                print(\"接口返回错误 ：%s  , -- , %s\"%(_response,params))\n",
    "        else:\n",
    "            print('Response 返回错误：%s'%(_request))\n",
    "    except Exception as e:\n",
    "        print(params)\n",
    "        print(e.args,'错误')\n",
    "    finally:\n",
    "        if semlock:\n",
    "            semlock.release()\n",
    "        if  dict(_dict):\n",
    "            return dict(_dict)\n",
    "    pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql_user = \"\"\"\n",
    "select plan.user_id,u.uuid as user_uuid,\n",
    "IFNULL(lae.order_no,'') as order_id,plan.loan_application_history_id as loan_id,\n",
    "plan.id as plan_id , plan.term_no,loan.business_type,\n",
    "DATE_ADD(plan.deadline, INTERVAL 1 second) as deadline,\n",
    "repaid_at,\n",
    "DATEDIFF(CURDATE(),DATE_ADD(plan.deadline, interval 1 second)) as passdue_day\n",
    "from repay as plan\n",
    "left join user as u on u.id = plan.user_id\n",
    "left join loan_application_history as loan on loan.id = plan.loan_application_history_id\n",
    "left join loan_account_ext as lae on lae.loan_id = loan.id\n",
    "where \n",
    "DATEDIFF(plan.repaid_at,DATE_ADD(plan.deadline, interval 1 second)) >1\n",
    "group by plan.loan_application_history_id\n",
    "order by plan.loan_application_history_id desc\n",
    "limit 10000\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df_user_msg = pd.read_sql(sql_user,con=con_tuomin_xyqb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "df_user_msg = df_user_msg[['user_uuid']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_user_msg['codes'] = 'model_exec_data_source#reloan_v4_raw'\n",
    "df_user_msg['business_type'] = 0\n",
    "df_user_msg['url'] = 'rc-model-calc-empty.qg-rc.net'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# _df = pd.read_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# df_user_msg = pd.read_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_msg = df_user_msg.to_dict(orient='recods')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'business_type': 0,\n",
       " 'codes': 'model_exec_data_source#reloan_v4_raw',\n",
       " 'url': 'rc-model-calc-empty.qg-rc.net',\n",
       " 'user_uuid': 'a16179f7-d3d9-442a-88ef-308c88f96499'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_msg[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "host = 'http://172.20.1.184:23010'\n",
    "url = '/calc/features_with_url'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'allowFromCache': True,\n",
       " 'async': False,\n",
       " 'batchUuid': 'fa7e5b3a-3787-4528-b3ab-f4fd6e19ccbc',\n",
       " 'businessType': None,\n",
       " 'currentCode': 'model_exec_data_source',\n",
       " 'forbidAsync': False,\n",
       " 'ignoreSinkDB': False,\n",
       " 'model_exec_data_source#reloan_v4_raw': 0.055556945502758026,\n",
       " 'orderId': None,\n",
       " 'snapshotTime': 1576034499116,\n",
       " 'testUrl': 'rc-model-calc-empty.qg-rc.net',\n",
       " 'userUuid': 'a16179f7-d3d9-442a-88ef-308c88f96499'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_json_feature(1,host,url,params=user_msg[0],method='POST')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "接口执行：: 100%|██████████| 5000/5000 [01:20<00:00, 62.49it/s]\n"
     ]
    }
   ],
   "source": [
    "host = 'http://172.20.1.184:23010'\n",
    "url = '/calc/features_with_url'\n",
    "maxconnections = 15 #最大并发数\n",
    "semlock = threading.BoundedSemaphore(maxconnections)\n",
    "list_Thread = []\n",
    "result = []\n",
    "params = user_msg[0:5000]\n",
    "pbar = tqdm(range(len(params)),\"接口执行：\")\n",
    "for i in pbar:\n",
    "    semlock.acquire()\n",
    "    t = MyThread(target=get_json_feature, args=(i,host,url,params[i],'POST',semlock,))\n",
    "    list_Thread.append(t)\n",
    "    t.start()\n",
    "pbar.close()\n",
    "for i in range(len(list_Thread)):\n",
    "    list_Thread[i].join()\n",
    "    result.append(list_Thread[i].get_return())\n",
    "_df = pd.DataFrame(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>allowFromCache</th>\n",
       "      <th>async</th>\n",
       "      <th>batchUuid</th>\n",
       "      <th>businessType</th>\n",
       "      <th>currentCode</th>\n",
       "      <th>forbidAsync</th>\n",
       "      <th>ignoreSinkDB</th>\n",
       "      <th>model_exec_data_source#reloan_v4_raw</th>\n",
       "      <th>orderId</th>\n",
       "      <th>snapshotTime</th>\n",
       "      <th>testUrl</th>\n",
       "      <th>userUuid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [allowFromCache, async, batchUuid, businessType, currentCode, forbidAsync, ignoreSinkDB, model_exec_data_source#reloan_v4_raw, orderId, snapshotTime, testUrl, userUuid]\n",
       "Index: []"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_df.loc[_df['model_exec_data_source#reloan_v4_raw'].isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "_df.to_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
