Created
March 11, 2019 06:57
-
-
Save rajajawahar/48d0275c357f57a3aa72ed918de432e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import json | |
import datetime | |
import xlsxwriter | |
def flatten_user_json(userJson, userId): | |
print(userJson) | |
prefix = "user_property_" | |
flat_json = {} | |
for individual_json in userJson: | |
key = prefix + individual_json['key'] | |
property_json = (individual_json['value']) | |
if 'string_value' in property_json: | |
flat_json[key] = property_json['string_value'] | |
if 'int_value' in property_json: | |
flat_json[key] = property_json['int_value'] | |
if str(individual_json['key']) == 'user_id': | |
flat_json[key] = userId | |
return json.dumps(flat_json) | |
def flatten_app_info(app_info_Json): | |
print("\n") | |
print("App JSON") | |
print("\n") | |
print(app_info_Json) | |
prefix = "app_info_" | |
flat_json = {} | |
required_key_set = ["version"] | |
for key in required_key_set: | |
if key in app_info_Json: | |
flat_json[prefix + key] = app_info_Json[key] | |
return json.dumps(flat_json) | |
def flatten_device_info(device_json): | |
print("\n") | |
print("Device JSON") | |
print("\n") | |
print(device_json) | |
prefix = "device_info_" | |
flat_json = {} | |
required_key_set = ["mobile_brand_name", "mobile_model_name", "mobile_marketing_name", "platform_version", | |
"device_model"] | |
for key in required_key_set: | |
if key in device_json: | |
flat_json[prefix + key] = device_json[key] | |
return json.dumps(flat_json) | |
def flatten_geo_info(geo_json): | |
print("\n") | |
print("GEO JSON") | |
print("\n") | |
print(geo_json) | |
prefix = "geo_info_" | |
flat_json = {} | |
required_key_set = ["country", "region", "city"] | |
for key in required_key_set: | |
if key in geo_json: | |
flat_json[prefix + key] = geo_json[key] | |
return json.dumps(flat_json) | |
headers_map = { "user_property_user_id": "User ID", | |
"app_info_version": "App Version", | |
"device_info_mobile_brand_name": "Device Name", | |
"device_info_platform_version": "Android Version", | |
"event_name": "Event", | |
"event_param_date": "Event Date", | |
"event_param_time": "Event Time", | |
"geo_info_city": "City", | |
"geo_info_region": "Region", | |
"geo_info_country": "Country"} | |
def convertFromJSON(rows, file_path): | |
eventsDicArray = [] | |
field_names = [field.name for field in rows.schema] | |
for row in rows: | |
print(row) | |
event_name = row[field_names.index("event_name")] | |
print(event_name) | |
user_id_index = field_names.index("user_id") | |
print(user_id_index) | |
userid = row[user_id_index] | |
print(userid) | |
user_properties = row[field_names.index("user_properties")] | |
print(user_properties) | |
device_info = row[field_names.index("device")] | |
print(device_info) | |
geo_info = row[field_names.index("geo")] | |
print(geo_info) | |
app_info = row[field_names.index("app_info")] | |
print(app_info) | |
# //Separting UserJSON from the list | |
user_json = flatten_user_json(user_properties, userid) | |
# // Separting DeviceType from the list | |
device_json = flatten_device_info(device_info) | |
# // Separting DeviceType from the list | |
geo_json = flatten_geo_info(geo_info) | |
# // Separting AppInfo from the list | |
app_info_json = flatten_app_info(app_info) | |
print(user_json) | |
print("\n") | |
print(device_json) | |
print("\n") | |
print(geo_json) | |
print("\n") | |
print(app_info_json) | |
print("\n") | |
single_entry = json.loads(user_json) | |
print(single_entry) | |
single_entry.update(json.loads(device_json)) | |
print(single_entry) | |
single_entry.update(json.loads(geo_json)) | |
single_entry.update(json.loads(app_info_json)) | |
print(single_entry) | |
eventsDicArray.append(single_entry) | |
print(eventsDicArray) | |
write_dict_array_to_excel_sendMail(eventsDicArray, file_path + '.xls') | |
def write_dict_array_to_excel_sendMail(array_of_dict, outputPath): | |
# //Creating a WorkBook | |
workbook = xlsxwriter.Workbook(outputPath) | |
worksheet = workbook.add_worksheet() | |
dict_to_write = {} | |
for each_dict in array_of_dict: | |
for key in each_dict.keys(): | |
if key not in dict_to_write: | |
dict_to_write[key] = [] | |
dict_to_write[key].append(each_dict[key]) | |
print(dict_to_write) | |
dict_key_list = list(set(dict_to_write)) | |
col = 0 | |
for key in dict_key_list: | |
if key in headers_map: | |
key = headers_map[key] | |
worksheet.write(0, col, key) | |
col += 1 | |
row = 0 | |
for single_dict in array_of_dict: | |
col = 0 | |
for key in dict_key_list: | |
if key in single_dict: | |
worksheet.write(row + 1, col, single_dict[key]) | |
col += 1 | |
row += 1 | |
workbook.close() | |
if workbookSize > 0: | |
print ("Ready to send email") | |
fromAddr = "raja@tarkalabs.com" | |
toAddr = "rajajawahar77@gmail.com" | |
subject = "Mobile User Activity - " + \ | |
datetime.datetime.strftime( | |
datetime.datetime.now() - datetime.timedelta(1), '%Y-%m-%d') | |
bodyMessage = "Hi, \n Greetings of the day!!. \n Attached sheet contains the details of the mobile user activity for your app " | |
#print "Message :" + bodyMessage | |
sendMail.send_mail(fromAddr, toAddr, subject, bodyMessage, attachmentPath, server, serverPort, username, | |
password, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment