Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mstrio import microstrategy
- import pandas as pd
- import json
- from datetime import datetime
- from mstrio.api import reports
- from mstrio.utils.parsejson import parsejson
- import concurrent.futures
- import asyncio
- base_url = "http://localhost:8080/MicroStrategyLibrary/api"
- project_id = "2916035E465E16431D6012B3FF137342"
- login = "administrator"
- password = ""
- async def main(): # declaration of asynchronous function
- def loadData(conn, report_id_value, instance_id_value, offset, limit_value):
- response = reports.report_instance_id(connection=conn, report_id = report_id_value, instance_id=instance_id_value, offset=offset, limit=limit_value)
- json_response = response.json()
- value = ""
- if 'result' in json_response:
- value = json_response['result']['data']['root']['children'][0]['element']['formValues']['DESC']
- table_data.append(parsejson(response=json_response))
- else:
- print("One record did not have any result. Offset: " + str(offset))
- conn = microstrategy.Connection(base_url, username=str(login), password=str(password), project_name="MicroStrategy Tutorial")
- conn.connect()
- startTime = datetime.now()
- limit_value = 2000
- report_id_value = "4B99AC5E41A66BB229D3ABAFEF812562" #"4B99AC5E41A66BB229D3ABAFEF812562 B95CAF1B42C8337B74BACA8D5E69C780"
- response = reports.report_instance(connection=conn, report_id = report_id_value, offset=0, limit=limit_value)
- json_response = response.json()
- instance_id_value = json_response['instanceId'] # retrieve instance id
- pagination = json_response['result']['data']['paging']
- table_data = [parsejson(response=json_response)]
- offset = limit_value
- with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: # declare executor and amount of workers
- loop = asyncio.get_event_loop() # create a loop for workers to go through
- futures = [
- loop.run_in_executor(
- executor,
- loadData, conn, report_id_value, instance_id_value, offset, limit_value # apply loadData() function to the executor. It is declared at the begginning of main() function
- )
- for offset in range(limit_value, pagination['total'], limit_value) # loop conditions
- ]
- for response in await asyncio.gather(*futures):
- pass
- instance = pd.concat(table_data)
- res_df = pd.DataFrame(instance)
- print("Rows: " + str(len(res_df)) + " Limit: " + str(limit_value) + " Time: " + str((datetime.now() - startTime)))
- #df = res_df.as_matrix()
- #for x in range(0, len(res_df), limit_value):
- # print("Offset: " + str(x) + " ; value: " + str(df[x][0]))
- loop = asyncio.get_event_loop()
- loop.run_until_complete(main()) # run main() function asynchronously
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement