Skip to content

Instantly share code, notes, and snippets.

@akshika47
Created December 25, 2021 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshika47/4856320e9c96c0e29507bded0b9aa1fb to your computer and use it in GitHub Desktop.
Save akshika47/4856320e9c96c0e29507bded0b9aa1fb to your computer and use it in GitHub Desktop.
App Insights Large Data Download Method - Python
import json
import os
import requests
import pandas as pd
end_point = 'api.applicationinsights.io'
def download_data(product_n_data_type):
start_time = "datetime(2021-10-13T00:00:00.001Z)" #Set a start time accoridng to your requirement
download_finished = False
complete_df = {}
end_time = 'now()'
is_first_time_download = True
appId, API_KEY = get_keys()
while not download_finished:
query = get_query(product_n_data_type, start_time, end_time)
params = {"query": query}
headers = {'X-Api-Key': API_KEY, 'Accept': 'application/json'}
url = f'https://api.applicationinsights.io/v1/apps/{appId}/query'
response = requests.get(url, headers=headers, params=params)
logs = json.loads(response.text)
result = {}
for table in logs['tables']:
result[table['name']] = pd.DataFrame.from_records(table['rows'], columns=[col['name'] for col in table['columns']])
df_final = result['PrimaryResult']
if len(df_final) == 500000:
start_time = 'datetime(' + df_final['timestamp'].max() + ')'
if is_first_time_download:
complete_df = df_final
is_first_time_download = False
else:
complete_df = complete_df.append(df_final)
else:
download_finished = True
if is_first_time_download:
complete_df = df_final
is_first_time_download = False
else:
complete_df = complete_df.append(df_final)
return complete_df
def get_query():
return "Add APP Insights Query Here"
def get_keys(product_n_data_type):
# Assign app_Id and API_KEY values to variables or read from the pipeline secrets at run-time
return app_Id, API_KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment