-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebapp.py
More file actions
187 lines (173 loc) · 6.53 KB
/
webapp.py
File metadata and controls
187 lines (173 loc) · 6.53 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import numpy as np
import pandas as pd
import spark_df_profiling
from flask import Flask
from flask import render_template
from flask import request
from pyspark.sql.types import StructField,IntegerType, StructType,StringType
from pyspark import SparkContext, SparkConf
from flask import send_file
from flask import send_from_directory
from pyspark.sql import SparkSession
import glob
import os
import re
spark = SparkSession \
.builder \
.appName("Data Quality Analyzer") \
.getOrCreate()
print("------------------------------")
print("[Start] Loaded Spark")
path ='./Dirty-Data/'
csvs = glob.glob(path + "/*.csv")
df = []
p=[]
print("[Start] Loading and profiling Spark data frames...")
dfS=[StructField('CODLINHA',StringType(),True),
StructField('NOMELINHA',StringType(),True),
StructField('CODVEICULO',StringType(),True),
StructField('NUMEROCARTAO',StringType(),True),
StructField('DATAUTILIZACAO',StringType(),True),
StructField('COMPLETENESS',IntegerType(),True),
StructField('CONSISTENCY',IntegerType(),True),
StructField('CONFORMITY',IntegerType(),True)
]
dfStruct=StructType(fields=dfS)
for file_ in csvs:
s_df = spark.read.csv(file_,header = True,schema=dfStruct)
df.append(s_df)
p.append(spark_df_profiling.ProfileReport(s_df).rendered_html())
print("[Start] Loaded and profiled Spark data frames")
print("[Start] Starting Flask...")
app = Flask(__name__)
print("[Start] Started Flask")
print("------------------------------")
def qualityAttrs(query):
suggested = []
if re.search("(WHERE)*COMPLETENESS[ *]*>?[ *]*=[ *]*", query):
suggested.append("COMPLETENESS")
if re.search("(WHERE)*CONSISTENCY[ *]*>?[ *]*=[ *]*", query):
suggested.append("CONSISTENCY")
if re.search("(WHERE)*CONFORMITY[ *]*>?[ *]*=[ *]*", query):
suggested.append("CONFORMITY")
return suggested
def leaveQuality(query, dimension):
dims = ["COMPLETENESS", "CONSISTENCY", "CONFORMITY"]
dims.remove(dimension)
query = re.sub("(AND[ *]*)?("+dims[0]+"|"+dims[1]+")[ *]*>[ *]*=[ *]*[0-9]+([ *]*(AND))?", "", query)
return query
@app.route('/compare', methods=['GET', 'POST'])
def compare():
if request.method == 'POST':
c=[]
for idx, dfc in enumerate(df):
if request.form.get('d'+str(idx)) is not None:
c.append(idx)
if len(c)!=2:
return "ERROR: You must select 2 days to compare"
reports = {"profile1": p[c[0]], "profile2": p[c[1]]}
return render_template('compare.html', reports=reports, dfs=df)
else:
return render_template('compare.html', dfs=df)
@app.route('/', methods=['GET', 'POST'])
@app.route('/query', methods=['GET', 'POST'])
def query():
if request.method == 'POST':
c=[]
suggested=[]
for idx, dfc in enumerate(df):
if request.form.get('d') == str(idx):
c.append(idx)
if len(c)!=1:
return "ERROR: You must select 1 day to query"
df[c[0]].createOrReplaceTempView("PEOPLE")
if request.form.get('query') is not "":
sqlDF = spark.sql(request.form.get('query'))
pdf = sqlDF.toPandas().head(20).to_html(classes='table')
suggested = qualityAttrs(request.form.get('query'))
if len(suggested) > 1:
return render_template('query.html', dfs=df, attrs=df[0].columns, pdf=pdf, query=request.form.get('query'), day=c[0], suggested=suggested)
else:
return render_template('query.html', dfs=df, attrs=df[0].columns, pdf=pdf, query=request.form.get('query'), day=c[0])
else:
query = "SELECT "
for attr in df[0].columns:
if request.form.get('s'+attr) is not None:
query = query + attr + ','
query = query[:-1] + " FROM PEOPLE WHERE "
for attr in df[0].columns:
if request.form.get('w'+attr) is not "":
char = '='
if attr in ['COMPLETENESS', 'CONSISTENCY', 'CONFORMITY']:
char = '>='
suggested.append(attr)
query = query + attr + char + request.form.get('w'+attr) + " AND "
if query[-6:] == "WHERE ":
query = query[:-6]
else:
query = query[:-4]
query = query + "GROUP BY "
for attr in df[0].columns:
if request.form.get('g'+attr) is not None:
query = query + attr + ','
if query[-9:] == "GROUP BY ":
query = query[:-9]
else:
query = query[:-1] + " HAVING count("
for attr in df[0].columns:
if request.form.get('h'+attr) is not None:
query = query + attr + ','
if query[-6:] == 'count(':
query = query[:-14]
else:
query = query[:-1]+')'
if request.form.get('op') is not None:
query = query + request.form.get('op')
else:
print("Error on query: " + query)
return "ERROR building the query: max min or eq must be non null when attrs to count are selected. <br/> Query:" + query
if request.form.get('count') is not None:
query = query + request.form.get('count')
else:
print("Error on query: " + query)
return "ERROR building the query: " + query
print(query)
sqlDF = spark.sql(query)
pdf = sqlDF.toPandas().head(20).to_html(classes='table')
if len(suggested) > 1:
return render_template('query.html', dfs=df, attrs=df[0].columns, pdf=pdf, query=query, day=c[0], suggested=suggested)
else:
return render_template('query.html', dfs=df, attrs=df[0].columns, pdf=pdf, query=query, day=c[0])
else:
return render_template('query.html', dfs=df, attrs=df[0].columns)
@app.route('/download', methods=['POST'])
def download():
df[int(request.form.get('day'))].createOrReplaceTempView("PEOPLE")
sqlDF = spark.sql(request.form.get('query'))
sqlDF.toPandas().to_csv('/tmp/out.csv', encoding='utf-8')
return send_file('/tmp/out.csv', as_attachment=True, attachment_filename='Dataset.csv')
@app.route('/profileQuery', methods=['POST'])
def profileQuery():
df[int(request.form.get('day'))].createOrReplaceTempView("PEOPLE")
sqlDF = spark.sql(request.form.get('query'))
if len(sqlDF.head(1)) > 0:
profile = spark_df_profiling.ProfileReport(sqlDF).rendered_html(False)
return profile
else:
return "Can't profile empty dataset"
@app.route('/trySuggestion', methods=['POST'])
def trySuggestion():
df[int(request.form.get('day'))].createOrReplaceTempView("PEOPLE")
queryN = leaveQuality(request.form.get('query'),request.form.get('dimension'))
sqlDF = spark.sql(queryN)
if len(sqlDF.head(1)) > 0:
profile = spark_df_profiling.ProfileReport(sqlDF).rendered_html(False)
return render_template('suggested.html', queryN=queryN, profile=profile)
else:
return "The query did not return any rows."
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'templates'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == '__main__':
app.run()