-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
213 lines (170 loc) · 7.71 KB
/
app.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
###############################################################################
# @@@ FLASK FRAMEWORK : Fuzzy Units Auto Mapper @@@
# version: v2.2
# app.py
###############################################################################
# Importing the Libraries:-
from flask import Flask, jsonify, request
from flask_restful import Resource, Api
from Connections.connections import CreateConnection
from fuzzyUnitMapper.fuzzyUnitMapper import FuzzyAutoMapper
from UnitsDataDecoupler.UnitsDataDecoupler import CoupleDecouple
import json
# Initialize the Flask App:-
app = Flask(__name__)
app.secret_key = 'mash'
api = Api(app)
class FuzzyUnitMapper(Resource):
def __init__(self):
# APP Resource variable:-
self.resource = \
{
"data": {
"domain": "",
"local_db_name": "",
"source_sql": "",
"left": "",
"right": "",
"migration_phase": ""
},
"url": "",
"Status": "",
"message": "",
"ok": ""
}
print('Resource Initialized')
def post(self):
request_data = request.get_json()
try:
self.resource["data"]["domain"] = request_data["domain"]
self.resource["data"]["local_db_name"] = request_data["local_db_name"]
self.resource["data"]["source_sql"] = request_data["source_sql"]
self.resource["data"]["left"] = request_data["left"]
self.resource["data"]["right"] = request_data["right"]
self.resource["data"]["migration_phase"] = request_data["migration_phase"]
self.resource["Status"] = 200
self.resource["ok"] = True
self.resource["message"] = "Units API Mapper Resource Config Updated Succesfully!"
self.resource["url"] = "http://127.0.0.1:7000/track/api/v1/units-api-mapper"
# Update configuration --> settings.json file
with open("settings.json", "w") as outfile:
json.dump(self.resource["data"], outfile)
return jsonify(self.resource)
except KeyError:
self.resource["Status"] = 422
self.resource["ok"] = False
self.resource["message"] = "Un-processable Entity: Check the config payload key data"
self.resource["url"] = "http://127.0.0.1:7000/track/api/v1/units-api-mapper"
return jsonify(self.resource)
def get(self):
# Read Configuration settings.json:-
with open("settings.json") as json_file:
config_data = json.load(json_file)
# Initialize the Config Parameters:-
domain = config_data['domain']
local_db_name = config_data['local_db_name']
scsql = config_data['source_sql']
migration_phase = config_data['migration_phase']
left = [config_data['left'].split(",") if type(config_data['left']) == str else config_data['left']][0]
right = [config_data['right'].split(",") if type(config_data['right']) == str else config_data['right']][0]
# Instantiate the Connection class Object:-
con = CreateConnection(domain, local_db_name)
# Initialize the Database Connections:-
local = con.local_engine_connection()
stage = con.stage_engine_connection()
prod = con.prod_engine_connection()
# Instantiate the Fuzzy Unit Mapper Class Object:-
fm = FuzzyAutoMapper(migration_phase, local, stage, prod, scsql, left, right)
# Start Fuzzy Mapping:-
df_matcher = fm.fuzzyUnitResolver()
fm.fuzzyUnitProcessor(df_matcher)
# Delete configuration --> settings.json file
with open("settings.json", "w") as outfile:
json.dump({"data": "Empty JSON"}, outfile)
print("Configuration: Reset")
return jsonify({"message": "Units API Mapper execution: Success",
"url": "http://127.0.0.1:7000/track/api/v1/units-api-mapper",
"Status": 200,
"ok": True})
class UnitDecoupler(Resource):
def __init__(self):
# APP Resource variable:-
self.resource = \
{
"data": {
"domain": "",
"local_db_name": "",
"GSHEET_ID": "",
"migration_phase": "",
"process_flow": ""
},
"url": "",
"Status": "",
"message": "",
"ok": ""
}
print('Resource Initialized')
def post(self):
request_data = request.get_json()
try:
self.resource["data"]["domain"] = request_data["domain"]
self.resource["data"]["local_db_name"] = request_data["local_db_name"]
self.resource["data"]["GSHEET_ID"] = request_data["GSHEET_ID"]
self.resource["data"]["migration_phase"] = request_data["migration_phase"]
self.resource["data"]["process_flow"] = request_data["process_flow"]
self.resource["Status"] = 200
self.resource["ok"] = True
self.resource["message"] = "Units Decoupler API Resource Config Updated Succesfully!"
self.resource["url"] = "http://127.0.0.1:7000/track/api/v1/units-decoupler"
# Update configuration --> settings.json file
with open("settings.json", "w") as outfile:
json.dump(self.resource["data"], outfile)
return jsonify(self.resource)
except KeyError:
self.resource["Status"] = 422
self.resource["ok"] = False
self.resource["message"] = "Un-processable Entity: Check the config payload key data"
self.resource["url"] = "http://127.0.0.1:7000/track/api/v1/units-decoupler"
return jsonify(self.resource)
def get(self):
# Read Configuration settings.json:-
with open("settings.json") as json_file:
config_data = json.load(json_file)
# Input Config Parameters:-
domain = config_data['domain']
local_DB = config_data['local_db_name']
GSHEET_ID = config_data['GSHEET_ID']
process_flow = config_data['process_flow']
migration_phase = config_data['migration_phase']
# Main loop:-
print('Start of Main Loop')
con = CreateConnection(domain, local_DB)
stage = con.stage_engine_connection()
local = con.local_engine_connection()
# Instantiate the Gsheet Writer Class Object:-
cd = CoupleDecouple(domain, GSHEET_ID, process_flow, migration_phase, stage, local)
cd.unit_distinct_duplicator()
cd.write2gsheet()
cd.readgsheet()
cd.data_cleaner()
cd.folio_resolver()
print('End of Main Loop')
# Delete configuration --> settings.json file
with open("settings.json", "w") as outfile:
json.dump({"data": "Empty JSON"}, outfile)
print("Configuration: Reset")
return jsonify({"message": "Units API Couple-Decoupler execution: Success",
"url": "http://127.0.0.1:7000/track/api/v1/units-decoupler",
"Status": 200,
"ok": True})
# Main Loop:-
if __name__ == '__main__':
# Add resource endpoints to the class:-
api.add_resource(FuzzyUnitMapper, '/track/api/v1/units-api-mapper')
api.add_resource(UnitDecoupler, '/track/api/v1/units-decoupler')
# Run the Flask App in the designated Port:-
app.run(port=7000)
with open("settings.json", "w") as outfile:
json.dump({"data": "Empty JSON"}, outfile)
print("Configuration: Reset")
print("End of App")