-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
458 lines (400 loc) · 18.6 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
'''
Goal of LSTM microservice:
1. LSTM microservice will accept the GitHub data from Flask microservice and will forecast the data for next 1 year based on past 30 days
2. It will also plot three different graph (i.e. "Model Loss", "LSTM Generated Data", "All Issues Data") using matplot lib
3. This graph will be stored as image in Google Cloud Storage.
4. The image URL are then returned back to Flask microservice.
'''
# Import all the required packages
from flask import Flask, jsonify, request, make_response
import os
from dateutil import *
from datetime import timedelta
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import time
from flask_cors import CORS
# Tensorflow (Keras & LSTM) related packages
import tensorflow as tf
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Input, Dense, LSTM, Dropout
from tensorflow.python.keras.callbacks import EarlyStopping
from sklearn.preprocessing import MinMaxScaler
# Import required storage package from Google Cloud Storage
from google.cloud import storage
# Initilize flask app
app = Flask(__name__)
# Handles CORS (cross-origin resource sharing)
CORS(app)
# Initlize Google cloud storage client
client = storage.Client()
# Add response headers to accept all types of requests
def build_preflight_response():
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type")
response.headers.add("Access-Control-Allow-Methods",
"PUT, GET, POST, DELETE, OPTIONS")
return response
# Modify response headers when returning to the origin
def build_actual_response(response):
response.headers.set("Access-Control-Allow-Origin", "*")
response.headers.set("Access-Control-Allow-Methods",
"PUT, GET, POST, DELETE, OPTIONS")
return response
@app.route('/api/pulls', methods=['POST'])
def pulls():
body = request.get_json()
pull_req_response = body["pull"]
repo_name = body["repo"]
type = body["type"]
print("type",type)
data_frame = pd.DataFrame(pull_req_response)
df1 = data_frame.groupby(["created_at"], as_index=False).count()
df = df1[["created_at", 'pull_req_number']]
df.columns = ['ds', 'y']
print(df)
df['ds'] = df['ds'].astype('datetime64[ns]')
array = df.to_numpy()
x = np.array([time.mktime(i[0].timetuple()) for i in array])
y = np.array([i[1] for i in array])
lzip = lambda *x: list(zip(*x))
days = df.groupby('ds')['ds'].value_counts()
Y = df['y'].values
X = lzip(*days.index.values)[0]
firstDay = min(X)
Ys = [0, ]*((max(X) - firstDay).days + 1)
days = pd.Series([firstDay + timedelta(days=i)
for i in range(len(Ys))])
for x, y in zip(X, Y):
Ys[(x - firstDay).days] = y
# Modify the data that is suitable for LSTM
Ys = np.array(Ys)
Ys = Ys.astype('float32')
Ys = np.reshape(Ys, (-1, 1))
# Apply min max scaler to transform the data
scaler = MinMaxScaler(feature_range=(0, 1))
Ys = scaler.fit_transform(Ys)
# Divide training - test data with 80-20 split
train_size = int(len(Ys) * 0.80)
test_size = len(Ys) - train_size
train, test = Ys[0:train_size, :], Ys[train_size:len(Ys), :]
print('train size:', len(train), ", test size:", len(test))
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
X.append(a)
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
'''
Look back decides how many days of data the model looks at for prediction
Here LSTM looks at approximately one month data
'''
look_back = 7
X_train, Y_train = create_dataset(train, look_back)
X_test, Y_test = create_dataset(test, look_back)
# Reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Verifying the shapes
X_train.shape, X_test.shape, Y_train.shape, Y_test.shape
# Model to forecast
model = Sequential()
model.add(LSTM(100, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
# Fit the model with training data and set appropriate hyper parameters
history = model.fit(X_train, Y_train, epochs=20, batch_size=70, validation_data=(X_test, Y_test),
callbacks=[EarlyStopping(monitor='val_loss', patience=10)], verbose=1, shuffle=False)
BASE_IMAGE_PATH = os.environ.get(
'BASE_IMAGE_PATH', 'Your_Base_Image_path')
# DO NOT DELETE "static/images" FOLDER as it is used to store figures/images generated by matplotlib
LOCAL_IMAGE_PATH = "static/images/"
MODEL_LOSS_IMAGE_NAME = "model_loss_" + type +"_"+ repo_name + ".png"
MODEL_LOSS_URL = BASE_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME
LSTM_GENERATED_IMAGE_NAME = "lstm_generated_data_" + type +"_" + repo_name + ".png"
LSTM_GENERATED_URL = BASE_IMAGE_PATH + LSTM_GENERATED_IMAGE_NAME
ALL_ISSUES_DATA_IMAGE_NAME = "all_issues_data_" + type + "_"+ repo_name + ".png"
ALL_ISSUES_DATA_URL = BASE_IMAGE_PATH + ALL_ISSUES_DATA_IMAGE_NAME
# Add your unique Bucket Name if you want to run it local
BUCKET_NAME = os.environ.get(
'BUCKET_NAME', 'Your_BUCKET_NAME')
# Plot the model loss image
plt.figure(figsize=(8, 4))
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('Model Loss For ' + "Pull Request")
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend(loc='upper right')
# Save the figure in /static/images folder
#plt.savefig(LOCAL_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME)
plt.savefig(LOCAL_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME)
# Predict issues for test data
y_pred = model.predict(X_test)
# Plot the LSTM Generated image
fig, axs = plt.subplots(1, 1, figsize=(10, 4))
X = mdates.date2num(days)
axs.plot(np.arange(0, len(Y_train)), Y_train, 'g', label="history")
axs.plot(np.arange(len(Y_train), len(Y_train) + len(Y_test)),
Y_test, marker='.', label="true")
axs.plot(np.arange(len(Y_train), len(Y_train) + len(Y_test)),
y_pred, 'r', label="prediction")
axs.legend()
axs.set_title('LSTM Generated Data For ' + "Pull request")
axs.set_xlabel('Time Steps')
axs.set_ylabel('Pull Request')
# Save the figure in /static/images folder
plt.savefig(LOCAL_IMAGE_PATH + LSTM_GENERATED_IMAGE_NAME)
# Plot the All Pull request data images
fig, axs = plt.subplots(1, 1, figsize=(10, 4))
X = mdates.date2num(days)
axs.plot(X, Ys, 'purple', marker='.')
locator = mdates.AutoDateLocator()
axs.xaxis.set_major_locator(locator)
axs.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
axs.legend()
axs.set_title('All Pull Request Data')
axs.set_xlabel('Date')
axs.set_ylabel('Pull Request')
# Save the figure in /static/images folder
plt.savefig(LOCAL_IMAGE_PATH + ALL_ISSUES_DATA_IMAGE_NAME)
# Uploads an images into the google cloud storage bucket
bucket = client.get_bucket(BUCKET_NAME)
new_blob = bucket.blob(MODEL_LOSS_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME)
new_blob = bucket.blob(ALL_ISSUES_DATA_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + ALL_ISSUES_DATA_IMAGE_NAME)
new_blob = bucket.blob(LSTM_GENERATED_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + LSTM_GENERATED_IMAGE_NAME)
# Construct the response
json_response = {
"model_loss_image_url": MODEL_LOSS_URL,
"lstm_generated_image_url": LSTM_GENERATED_URL,
"all_issues_data_image": ALL_ISSUES_DATA_URL
}
# Returns image url back to flask microservice
return jsonify(json_response)
'''
API route path is "/api/forecast"
This API will accept only POST request
'''
@app.route('/api/forecast', methods=['POST'])
def forecast():
body = request.get_json()
issues = body["issues"]
type = body["type"]
repo_name = body["repo"]
data_frame = pd.DataFrame(issues)
df1 = data_frame.groupby([type], as_index=False).count()
df = df1[[type, 'issue_number']]
df.columns = ['ds', 'y']
df['ds'] = df['ds'].astype('datetime64[ns]')
array = df.to_numpy()
x = np.array([time.mktime(i[0].timetuple()) for i in array])
y = np.array([i[1] for i in array])
lzip = lambda *x: list(zip(*x))
days = df.groupby('ds')['ds'].value_counts()
Y = df['y'].values
X = lzip(*days.index.values)[0]
firstDay = min(X)
'''
To achieve data consistancy with both actual data and predicted values,
add zeros to dates that do not have orders
[firstDay + timedelta(days=day) for day in range((max(X) - firstDay).days + 1)]
'''
Ys = [0, ]*((max(X) - firstDay).days + 1)
days = pd.Series([firstDay + timedelta(days=i)
for i in range(len(Ys))])
for x, y in zip(X, Y):
Ys[(x - firstDay).days] = y
# Modify the data that is suitable for LSTM
Ys = np.array(Ys)
Ys = Ys.astype('float32')
Ys = np.reshape(Ys, (-1, 1))
# Apply min max scaler to transform the data
scaler = MinMaxScaler(feature_range=(0, 1))
Ys = scaler.fit_transform(Ys)
# Divide training - test data with 80-20 split
train_size = int(len(Ys) * 0.80)
test_size = len(Ys) - train_size
train, test = Ys[0:train_size, :], Ys[train_size:len(Ys), :]
print('train size:', len(train), ", test size:", len(test))
# Create the training and test dataset
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
X.append(a)
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
'''
Look back decides how many days of data the model looks at for prediction
Here LSTM looks at approximately one month data
'''
look_back = 7
X_train, Y_train = create_dataset(train, look_back)
X_test, Y_test = create_dataset(test, look_back)
# Reshape input to be [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Verifying the shapes
X_train.shape, X_test.shape, Y_train.shape, Y_test.shape
# Model to forecast
model = Sequential()
model.add(LSTM(100, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
# Fit the model with training data and set appropriate hyper parameters
history = model.fit(X_train, Y_train, epochs=20, batch_size=70, validation_data=(X_test, Y_test),
callbacks=[EarlyStopping(monitor='val_loss', patience=10)], verbose=1, shuffle=False)
'''
Creating image URL
BASE_IMAGE_PATH refers to Google Cloud Storage Bucket URL.Add your Base Image Path in line 145
if you want to run the application local
LOCAL_IMAGE_PATH refers local directory where the figures generated by matplotlib are stored
These locally stored images will then be uploaded to Google Cloud Storage
'''
BASE_IMAGE_PATH = os.environ.get(
'BASE_IMAGE_PATH', 'Your_Base_Image_path')
# DO NOT DELETE "static/images" FOLDER as it is used to store figures/images generated by matplotlib
LOCAL_IMAGE_PATH = "static/images/"
# Creating the image path for model loss, LSTM generated image and all issues data image
MODEL_LOSS_IMAGE_NAME = "model_loss_" + type +"_"+ repo_name + ".png"
MODEL_LOSS_URL = BASE_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME
LSTM_GENERATED_IMAGE_NAME = "lstm_generated_data_" + type +"_" + repo_name + ".png"
LSTM_GENERATED_URL = BASE_IMAGE_PATH + LSTM_GENERATED_IMAGE_NAME
ALL_ISSUES_DATA_IMAGE_NAME = "all_issues_data_" + type + "_"+ repo_name + ".png"
ALL_ISSUES_DATA_URL = BASE_IMAGE_PATH + ALL_ISSUES_DATA_IMAGE_NAME
DAY_MAX_ISSUE_CREATED_IMAGE_NAME = "day_max_issues_created_data_" + type + "_"+ repo_name + ".png"
DAY_MAX_ISSUE_CREATED_DATA_URL = BASE_IMAGE_PATH + DAY_MAX_ISSUE_CREATED_IMAGE_NAME
DAY_MAX_ISSUE_CLOSED_IMAGE_NAME = "day_max_issues_closed_data_" + type + "_"+ repo_name + ".png"
DAY_MAX_ISSUE_CLOSED_DATA_URL = BASE_IMAGE_PATH + DAY_MAX_ISSUE_CLOSED_IMAGE_NAME
MONTH_MAX_ISSUE_CLOSED_IMAGE_NAME = "month_max_issues_closed_data_" + type + "_"+ repo_name + ".png"
MONTH_MAX_ISSUE_CLOSED_DATA_URL = BASE_IMAGE_PATH + MONTH_MAX_ISSUE_CLOSED_IMAGE_NAME
# Add your unique Bucket Name if you want to run it local
BUCKET_NAME = os.environ.get(
'BUCKET_NAME', 'Your_BUCKET_NAME')
# Model summary()
# Plot the model loss image
plt.figure(figsize=(8, 4))
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('Model Loss For ' + type)
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend(loc='upper right')
# Save the figure in /static/images folder
plt.savefig(LOCAL_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME)
# Predict issues for test data
y_pred = model.predict(X_test)
# Plot the LSTM Generated image
fig, axs = plt.subplots(1, 1, figsize=(10, 4))
X = mdates.date2num(days)
axs.plot(np.arange(0, len(Y_train)), Y_train, 'g', label="history")
axs.plot(np.arange(len(Y_train), len(Y_train) + len(Y_test)),
Y_test, marker='.', label="true")
axs.plot(np.arange(len(Y_train), len(Y_train) + len(Y_test)),
y_pred, 'r', label="prediction")
axs.legend()
axs.set_title('LSTM Generated Data For ' + type)
axs.set_xlabel('Time Steps')
axs.set_ylabel('Issues')
# Save the figure in /static/images folder
plt.savefig(LOCAL_IMAGE_PATH + LSTM_GENERATED_IMAGE_NAME)
# Plot the All Issues data images
fig, axs = plt.subplots(1, 1, figsize=(10, 4))
X = mdates.date2num(days)
axs.plot(X, Ys, 'purple', marker='.')
locator = mdates.AutoDateLocator()
axs.xaxis.set_major_locator(locator)
axs.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
axs.legend()
axs.set_title('All Issues Data')
axs.set_xlabel('Date')
axs.set_ylabel('Issues')
# Save the figure in /static/images folder
plt.savefig(LOCAL_IMAGE_PATH + ALL_ISSUES_DATA_IMAGE_NAME)
#requirement 1
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
data_frame = pd.DataFrame(issues)
data_frame['created_at'] = pd.to_datetime(data_frame['created_at'], errors='coerce')
week_df = data_frame.groupby(data_frame['created_at'].dt.day_name()).size()
week_df = pd.DataFrame({'Created_On':week_df.index, 'Count':week_df.values})
week_df = week_df.groupby(['Created_On']).sum().reindex(x)
max_issue_count = week_df.max()
max_issue_day = week_df['Count'].idxmax()
plt.figure(figsize=(12, 7))
plt.plot(week_df['Count'], label='Issues')
plt.title('Number of Issues Created for particular Week Days.')
plt.ylabel('Number of Issues')
plt.xlabel('Week Days')
plt.savefig(LOCAL_IMAGE_PATH + DAY_MAX_ISSUE_CREATED_IMAGE_NAME)
data_frame['closed_at'] = pd.to_datetime(data_frame['closed_at'], errors='coerce')
week_df = data_frame.groupby(data_frame['closed_at'].dt.day_name()).size()
week_df = pd.DataFrame({'Closed_On':week_df.index, 'Count':week_df.values})
week_df = week_df.groupby(['Closed_On']).sum().reindex(x)
max_issue_count_closed = week_df.max()
max_issue_day_closed = week_df['Count'].idxmax()
plt.figure(figsize=(12, 7))
plt.plot(week_df['Count'], label='Issues')
plt.title('Number of Issues Closed for particular Week Days.')
plt.ylabel('Number of Issues')
plt.xlabel('Week Days')
plt.savefig(LOCAL_IMAGE_PATH + DAY_MAX_ISSUE_CLOSED_IMAGE_NAME)
data_frame = pd.DataFrame(issues)
x = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
data_frame['closed_at'] = pd.to_datetime(data_frame['closed_at'], errors='coerce')
month_df = data_frame.groupby(data_frame['closed_at'].dt.month_name()).size()
month_df = pd.DataFrame({'Closed_On':month_df.index, 'Count':month_df.values})
month_df = month_df.groupby(['Closed_On']).sum().reindex(x)
max_issue_count_closed_month = month_df.max()
max_issue_closed_month = month_df['Count'].idxmax()
plt.figure(figsize=(12, 7))
plt.plot(month_df['Count'], label='Issues')
plt.title('Number of Issues Closed for particular Month.')
plt.ylabel('Number of Issues')
plt.xlabel('Month Names')
plt.savefig(LOCAL_IMAGE_PATH + MONTH_MAX_ISSUE_CLOSED_IMAGE_NAME)
# Uploads an images into the google cloud storage bucket
bucket = client.get_bucket(BUCKET_NAME)
new_blob = bucket.blob(MODEL_LOSS_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + MODEL_LOSS_IMAGE_NAME)
new_blob = bucket.blob(ALL_ISSUES_DATA_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + ALL_ISSUES_DATA_IMAGE_NAME)
new_blob = bucket.blob(LSTM_GENERATED_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + LSTM_GENERATED_IMAGE_NAME)
new_blob = bucket.blob(DAY_MAX_ISSUE_CREATED_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + DAY_MAX_ISSUE_CREATED_IMAGE_NAME)
new_blob = bucket.blob(DAY_MAX_ISSUE_CLOSED_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + DAY_MAX_ISSUE_CLOSED_IMAGE_NAME)
new_blob = bucket.blob(MONTH_MAX_ISSUE_CLOSED_IMAGE_NAME)
new_blob.upload_from_filename(
filename=LOCAL_IMAGE_PATH + MONTH_MAX_ISSUE_CLOSED_IMAGE_NAME)
# Construct the response
json_response = {
"model_loss_image_url": MODEL_LOSS_URL,
"lstm_generated_image_url": LSTM_GENERATED_URL,
"day_max_issue_created": DAY_MAX_ISSUE_CREATED_DATA_URL,
"all_issues_data_image": ALL_ISSUES_DATA_URL,
"day_max_issue_closed": DAY_MAX_ISSUE_CLOSED_DATA_URL,
"month_max_issues_closed": MONTH_MAX_ISSUE_CLOSED_DATA_URL
}
# Returns image url back to flask microservice
return jsonify(json_response)
# Run LSTM app server on port 8080
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)