forked from kskk02/Fraud_Detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFraudWebApp.py
140 lines (117 loc) · 5.3 KB
/
FraudWebApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import d3py
import networkx as nx
import pandas as pd
import logging
import json
from flask import Flask
from flask import render_template,request
from sqlalchemy import create_engine
#import Fraud_Detection
from pandas.io import sql
import pdb
app = Flask(__name__)
'''
Need the below global variable to do graph computation as a one time analysis and keeping track of the user state without using sessions
since Flask does not provide session support.
'''
global max_fraudsters_in_DB
max_fraudsters_in_DB=1
#Fraud_Detection.RunComplete()
TotalGraph = nx.read_edgelist("edgelist.csv",delimiter=',')
print "initial max_fraudsters_in_DB is " ,max_fraudsters_in_DB
@app.route('/',methods=['GET','POST'])
def Main_Page():
"""
Description: This function inserts the possible possible_fraudsters identified from the graph analysis into the database.
INPUT:
df - type: DataFrame - the list of fraudster ID's
table - type: string - the table to be inserted into
OUTPUT: No output. Just stores the data in the database
"""
print "initial max_fraudsters_in_DB is " ,max_fraudsters_in_DB
possible_fraudsters_index=0
print "general"
if request.method == 'POST':
if request.form['submit'] == 'Confirm Fraudster Anomaly':
possible_fraudsters_index = int(request.form['fraud_index']) + 1
if possible_fraudsters_index >= max_fraudsters_in_DB:
possible_fraudsters_index = 0
elif request.form['submit'] == "Not Anomaly and Skip to Next":
possible_fraudsters_index = int(request.form['fraud_index']) + 1
if possible_fraudsters_index >= max_fraudsters_in_DB:
print "possible_fraudsters_index is ", possible_fraudsters_index
print "max_fraudsters_in_DB is ", max_fraudsters_in_DB
possible_fraudsters_index = 0
print "index is ", possible_fraudsters_index
pass
elif request.form['submit'] == "Confirm Fraudster Anomaly":
possible_fraudsters_index = int(request.form['fraud_index']) + 1
if possible_fraudsters_index >= max_fraudsters_in_DB:
print "possible_fraudsters_index is ", possible_fraudsters_index
print "max_fraudsters_in_DB is ", max_fraudsters_in_DB
possible_fraudsters_index = 0
pass
elif request.form['submit'] == 'Run Complete Detection':
# Fraud_Detection.RunComplete()
possible_fraudsters_index =0
else:
pass # unknown
def Generate_Graph (anomaly):
# this function builds the subgraph for the anomalies set. TBD : Build the subgraph using a longer time window
Anomalies_Subgraph = nx.ego_graph(TotalGraph,anomaly,radius=1)
print "length of subgraph for " ,anomaly , " is " ,len(Anomalies_Subgraph.nodes())
myjson = d3py.json_graph.node_link_data(Anomalies_Subgraph)
return myjson
def _decode_list(data):
rv = []
for item in data:
if isinstance(item, unicode):
item = item.encode('utf-8')
elif isinstance(item, list):
item = _decode_list(item)
elif isinstance(item, dict):
item = _decode_dict(item)
rv.append(item)
return rv
def _decode_dict(data):
rv = {}
for key, value in data.iteritems():
if isinstance(key, unicode):
key = key.encode('utf-8')
if isinstance(value, unicode):
value = value.encode('utf-8')
elif isinstance(value, list):
value = _decode_list(value)
elif isinstance(value, dict):
value = _decode_dict(value)
rv[key] = value
return rv
def Find_Next_Fraudster (table,possible_fraudsters_index):
global max_fraudsters_in_DB
engine = create_engine('postgresql://skanajan:abcdef@localhost:5432/fraud_detector')
cnx = engine.raw_connection()
possible_fraudsters_df = sql.read_sql("SELECT * FROM " + table , cnx)
print "max_fraudsters_in_DB before" , max_fraudsters_in_DB
max_fraudsters_in_DB = sql.read_sql("SELECT count(*) FROM " + table , cnx)['count'][0]
print "max_fraudsters_in_DB after" , max_fraudsters_in_DB
cnx.close()
print "fraudster index is " , possible_fraudsters_index
fraudster_id = possible_fraudsters_df.iloc[possible_fraudsters_index,1].encode('utf-8')
return fraudster_id
Fraudster_ID = Find_Next_Fraudster ('possible_fraudsters',possible_fraudsters_index)
print " New Fraudster_ID is ", Fraudster_ID
json_data = Generate_Graph(Fraudster_ID)
# with open("network_data.json") as json_file:
# json_data = json.load(json_file,object_hook=_decode_dict)
json_data = _decode_dict(json_data)
# myjson = d3py.json_graph.node_link_data(G)
myjson = json_data
newdict = dict()
newdict['links']= myjson['links']
newdict['nodes']= myjson['nodes']
# newdict = myjson
# with open('figure.json', 'w') as outfile:
# json.dump(newdict, outfile)
return render_template('force.html', data=newdict, Fraudster_index = possible_fraudsters_index, Fraudster_ID = Fraudster_ID )
if __name__ == '__main__':
app.run(host='0.0.0.0', port=6969, debug=True)