Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. from mstrio import microstrategy
  2. import pandas as pd
  3. import json
  4. from datetime import datetime
  5. from mstrio.api import reports
  6. from mstrio.utils.parsejson import parsejson
  7. import concurrent.futures
  8. import asyncio
  9.  
  10. base_url = "http://localhost:8080/MicroStrategyLibrary/api"
  11. project_id = "2916035E465E16431D6012B3FF137342"
  12. login = "administrator"
  13. password = ""
  14.  
  15. async def main(): # declaration of asynchronous function
  16. def loadData(conn, report_id_value, instance_id_value, offset, limit_value):
  17. response = reports.report_instance_id(connection=conn, report_id = report_id_value, instance_id=instance_id_value, offset=offset, limit=limit_value)
  18. json_response = response.json()
  19. value = ""
  20.  
  21. if 'result' in json_response:
  22. value = json_response['result']['data']['root']['children'][0]['element']['formValues']['DESC']
  23. table_data.append(parsejson(response=json_response))
  24. else:
  25. print("One record did not have any result. Offset: " + str(offset))
  26.  
  27. conn = microstrategy.Connection(base_url, username=str(login), password=str(password), project_name="MicroStrategy Tutorial")
  28. conn.connect()
  29.  
  30. startTime = datetime.now()
  31. limit_value = 2000
  32. report_id_value = "4B99AC5E41A66BB229D3ABAFEF812562" #"4B99AC5E41A66BB229D3ABAFEF812562 B95CAF1B42C8337B74BACA8D5E69C780"
  33.  
  34. response = reports.report_instance(connection=conn, report_id = report_id_value, offset=0, limit=limit_value)
  35. json_response = response.json()
  36.  
  37. instance_id_value = json_response['instanceId'] # retrieve instance id
  38. pagination = json_response['result']['data']['paging']
  39.  
  40. table_data = [parsejson(response=json_response)]
  41. offset = limit_value
  42. with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: # declare executor and amount of workers
  43. loop = asyncio.get_event_loop() # create a loop for workers to go through
  44. futures = [
  45. loop.run_in_executor(
  46. executor,
  47. 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
  48. )
  49. for offset in range(limit_value, pagination['total'], limit_value) # loop conditions
  50. ]
  51. for response in await asyncio.gather(*futures):
  52. pass
  53.  
  54. instance = pd.concat(table_data)
  55. res_df = pd.DataFrame(instance)
  56. print("Rows: " + str(len(res_df)) + " Limit: " + str(limit_value) + " Time: " + str((datetime.now() - startTime)))
  57. #df = res_df.as_matrix()
  58.  
  59. #for x in range(0, len(res_df), limit_value):
  60. # print("Offset: " + str(x) + " ; value: " + str(df[x][0]))
  61.  
  62. loop = asyncio.get_event_loop()
  63. loop.run_until_complete(main()) # run main() function asynchronously
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement