Skip to content

First two days logs are 0KB #4

Description

@SNathan78

Hi,

I am facing a trivial issue where first two days are always generated as 0KB files if I run for 30 days. Not sure why? If I set 28 days, it works just fine.

My code is as below

client_id = "XXXXXX"
client_secret = "XXXXXXX"

authority_url= "https://login.microsoftonline.com/MyTeanantID

scope = ["https://analysis.windows.net/powerbi/api/.default"]

#Set Power BI REST API to get Activities for today

Function <----->

def CreateLogs(startDate):

#Set CSV path

url = "https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime='" + startDate + "T00:00:00'&endDateTime='" + startDate + "T23:59:59'"

In[ ]:

#Use MSAL to grab token
app = msal.ConfidentialClientApplication(client_id, authority=authority_url, client_credential=client_secret)
result = app.acquire_token_for_client(scopes=scope)

#Get latest Power BI Activities

if 'access_token' in result:

    access_token = result['access_token']

    #print(access_token)
    header = {'Content-Type':'application/json', 'Authorization':f'Bearer {access_token}'}
    
    api_call = requests.get(url=url, headers=header)

    #Specify empty Dataframe with all columns
    column_names = ['Id', 'RecordType', 'CreationTime', 'Operation', 'OrganizationId', 'UserType', 'UserKey', 'Workload', 'UserId', 'ClientIP', 'UserAgent', 'Activity', 'IsSuccess', 'RequestId', 'ActivityId', 'ItemName', 'WorkSpaceName', 'DatasetName', 'ReportName', 'WorkspaceId', 'ObjectId', 'DatasetId', 'ReportId', 'ReportType', 'DistributionMethod', 'ConsumptionMethod']
    df = pd.DataFrame(columns=column_names)

    #Set continuation URL
    contUrl = api_call.json()['continuationUri']
    
    #Get all Activities for first hour, save to dataframe (df1) and append to empty created df
    result = api_call.json()['activityEventEntities']
    df1 = pd.DataFrame(result)
    pd.concat([df, df1])

    #Call Continuation URL as long as results get one back to get all activities through the day
    while contUrl is not None:        
        api_call_cont = requests.get(url=contUrl, headers=header)
        contUrl = api_call_cont.json()['continuationUri']
        result = api_call_cont.json()['activityEventEntities']
        df2 = pd.DataFrame(result)
        df = pd.concat([df, df2])
    
    #Set ID as Index of df
    df = df.set_index('Id')

    #Save df as CSV
    path='C:\\out\\'
    df.to_csv(path + startDate + '.csv')
    print("Logs Created Successfully for the date "+ startDate)

<<<<-----Function Ends--------->>>

endDate = date.today() - timedelta(days=1)
startDate= date.today() - timedelta(days=30)

while startDate <= endDate:
# print(current_date)
# Increment the current date by one day
strDate=startDate.strftime("%Y-%m-%d")
CreateLogs(strDate)
startDate += timedelta(days=1)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions