blob: 36b68ff866c7cbed101db7c0a8fdbea15cca819c [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Get Suspicious Requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import urllib\n",
"import os\n",
"import datetime\n",
"import csv \n",
"\n",
"# getting date from the parent path. \n",
"path = os.getcwd().split(\"/\") \n",
"date = path[len(path)-1] \n",
"dsource = path[len(path)-2] \n",
"score_values = []"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"try:\n",
" import ipywidgets as widgets # For jupyter/ipython >= 1.4\n",
"except ImportError:\n",
" from IPython.html import widgets\n",
"from IPython.display import display, HTML, clear_output, Javascript \n",
"\n",
"def fill_list(list_control,source):\n",
" options_list = ['- Select -'] \n",
" options_list.extend([s for s in source])\n",
" list_control.options = options_list\n",
"\n",
"# URI panel\n",
"uri_header = widgets.HTML(value=\"URI\", height='10%', width='100%')\n",
"uri_select = widgets.Select(height='100%', width='99%')\n",
"\n",
"uri_select._css = (\n",
" (None, 'height', '90%'),\n",
" (None, 'width', '95%'),\n",
" ('select', 'overflow-x', 'auto'),\n",
" ('select', 'width', '100%'),\n",
" ('select', 'margin', 0)\n",
")\n",
"\n",
"uri_box = widgets.Box(width='70%', height='100%')\n",
"uri_box.children = [uri_header, uri_select]\n",
"\n",
"# Actions Panel\n",
"actions_header = widgets.HTML(value=\" \", width='100%', height='10%')\n",
"quick_text = widgets.Text(value='', width='100%', placeholder='Quick scoring')\n",
"quick_text._css = (\n",
" (None, 'width', '100%'),\n",
")\n",
"rating_btn = widgets.RadioButtons(description='Rating:', options=['1', '2', '3'], width='100%')\n",
"assign_btn = widgets.Button(description='Score', width='45%')\n",
"assign_btn.button_style = 'primary'\n",
"save_btn = widgets.Button(description='Save', width='45%')\n",
"save_btn.button_style = 'primary'\n",
"save_btn._css = (\n",
" (None, 'margin-left', '10%'),\n",
")\n",
"\n",
"actions_box = widgets.Box(width='30%', height='100%')\n",
"actions_box.children = [actions_header,quick_text,rating_btn, assign_btn,save_btn]\n",
"\n",
"scoring_form = widgets.HBox(width='90%', height=250)\n",
"scoring_form.children = [uri_box, actions_box]\n",
"\n",
"rest_msg_box = widgets.HTML()\n",
"\n",
"def data_loader(): \n",
" us_uris = []\n",
" global score_values\n",
" \n",
" response = GraphQLClient.request(\n",
" query=\"\"\"query($date:SpotDateType!) {\n",
" proxy{\n",
" suspicious(date:$date){\n",
" uri\n",
" }\n",
" }\n",
" }\"\"\",\n",
" variables={\n",
" 'date': datetime.datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d')\n",
" }\n",
" )\n",
" \n",
" scored = []\n",
" for item in score_values:\n",
" scored.append(urllib.quote_plus(item[0]))\n",
" \n",
" if not 'errors' in response: \n",
" for row in response['data']['proxy']['suspicious']:\n",
" if not row['uri'] in scored:\n",
" us_uris.append(row['uri'])\n",
" else:\n",
" print 'An error occured : '+ response['errors'][0]['message']\n",
" \n",
" fill_list(uri_select,us_uris)\n",
" uri_select.value = \"- Select -\" \n",
"\n",
"display(Javascript(\"$('.widget-area > .widget-subarea > *').remove();\"))\n",
"data_loader()\n",
"display(scoring_form)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Update Suspicious Requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import csv\n",
"import datetime\n",
"import subprocess \n",
"\n",
"def assign_score(b): \n",
" clear_output()\n",
" uri = quick_text.value or uri_select.value\n",
" uri_sev = int(rating_btn.selected_label) \n",
" \n",
" global score_values\n",
" \n",
" score_values.append((uri, uri_sev))\n",
" \n",
" if uri_select.value != \"- Select -\":\n",
" display(Javascript(\"$(\\\"option[data-value='\" + urllib.quote_plus(uri_select.value) +\"']\\\").remove();\"))\n",
" else:\n",
" display(Javascript(\"$(\\\"option[data-value$='\" + uri +\"']\\\").remove();\"))\n",
" \n",
" clear_output() \n",
" rest_msg_box.visible = False\n",
" data_loader()\n",
" uri_select.value = \"- Select -\"\n",
" quick_text.value = \"\"\n",
" \n",
" print \"Click the 'Save' button when you're finished scoring\" \n",
"\n",
"\n",
" \n",
"def reset_scoring():\n",
" response = GraphQLClient.request(\n",
" query=\"\"\"mutation($date:SpotDateType!) {\n",
" proxy{\n",
" resetScoredConnections(date:$date){\n",
" success\n",
" }\n",
" }\n",
" }\"\"\",\n",
" variables={\n",
" 'date': datetime.datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d')\n",
" }\n",
" ) \n",
" \n",
" if not 'errors' in response:\n",
" rest_msg_box.value=\"Suspicious connects successfully reset\"\n",
" data_loader()\n",
" rest_msg_box.visible = True\n",
" display(rest_msg_box)\n",
" else:\n",
" print \"An error ocurred: \" + response['errors'][0]['message']\n",
" \n",
" \n",
" \n",
"def save(b): \n",
" variables=[]\n",
" global score_values\n",
" mutation=\"\"\"mutation($input:[ProxyScoreInputType!]!)\n",
" {\n",
" proxy{\n",
" score(input:$input)\n",
" {success}\n",
" }\n",
" }\"\"\" \n",
" \n",
" for row in score_values:\n",
" variables.append({\n",
" 'date': datetime.datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d'),\n",
" 'uri': row[0] if row[0] != \"\" else None,\n",
" 'score': row[1] if row[1] != \"\" else None \n",
" })\n",
"\n",
" var = {'input':variables}\n",
" response = GraphQLClient.request(mutation,var)\n",
" \n",
" score_values = []\n",
" if not 'errors' in response:\n",
" clear_output() \n",
" display(Javascript(\"$('.widget-area > .widget-subarea > *').remove();\"))\n",
" data_loader() \n",
" display(scoring_form)\n",
" display(Javascript('reloadParentData();')) \n",
" print \"Suspicious connects successfully updated\"\n",
" else:\n",
" print \"An error ocurred: \" + response['errors'][0]['message']\n",
"\n",
" \n",
"assign_btn.on_click(assign_score)\n",
"save_btn.on_click(save)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# reset_scoring()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}