/
GetPowerBIActivityEvent.cs
166 lines (148 loc) · 6.37 KB
/
GetPowerBIActivityEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Net;
using Microsoft.PowerBI.Commands.Profile.Errors;
using Microsoft.PowerBI.Common.Api;
using Microsoft.PowerBI.Common.Api.ActivityEvent;
using Microsoft.PowerBI.Common.Client;
using Microsoft.Rest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.PowerBI.Commands.Admin
{
[Cmdlet(CmdletVerb, CmdletName)]
[Alias("Get-PowerBIActivityEvents")]
[OutputType(typeof(IList<object>))]
public class GetPowerBIActivityEvent : PowerBIClientCmdlet
{
public const string CmdletName = "PowerBIActivityEvent";
public const string CmdletVerb = VerbsCommon.Get;
private const string FeatureNotAvailableError = "FeatureNotAvailableError";
private bool validationError = false;
private string Filter = null;
private string ActivityTypeFilter = null;
private string UserIdFilter = null;
public GetPowerBIActivityEvent() : base() { }
public GetPowerBIActivityEvent(IPowerBIClientCmdletInitFactory init) : base(init) { }
[Parameter(Mandatory = true)]
public string StartDateTime { get; set; }
[Parameter(Mandatory = true)]
public string EndDateTime { get; set; }
[Parameter(Mandatory = false)]
public string ActivityType
{
get => this.ActivityTypeFilter;
set => this.ActivityTypeFilter = !string.IsNullOrEmpty(value) ? $"Activity eq '{value}'" : value;
}
[Parameter(Mandatory = false)]
public string User
{
get => this.UserIdFilter;
set => this.UserIdFilter = !string.IsNullOrEmpty(value) ? $"UserId eq '{value}'" : value;
}
[Parameter(Mandatory = false)]
public OutputType ResultType { get; set; } = OutputType.JsonString;
protected override void BeginProcessing()
{
base.BeginProcessing();
try
{
DateTime.Parse(this.StartDateTime);
}
catch (Exception ex)
{
this.validationError = true;
string errorId = $"{nameof(this.StartDateTime)} is not a valid DateTime.";
var errorRecord = new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this.StartDateTime);
this.Logger.ThrowTerminatingError(errorRecord);
}
try
{
DateTime.Parse(this.EndDateTime);
}
catch (Exception ex)
{
this.validationError = true;
string errorId = $"{nameof(this.EndDateTime)} is not a valid DateTime.";
var errorRecord = new ErrorRecord(ex, errorId, ErrorCategory.InvalidArgument, this.EndDateTime);
this.Logger.ThrowTerminatingError(errorRecord);
}
}
public override void ExecuteCmdlet()
{
if (this.validationError)
{
return;
}
string formattedStartDateTime = $"'{this.StartDateTime}'";
string formattedEndDateTime = $"'{this.EndDateTime}'";
if (!string.IsNullOrEmpty(this.ActivityType))
{
this.Filter = string.IsNullOrEmpty(this.Filter) ? this.ActivityType : $"({this.Filter}) and ({this.ActivityType})";
}
if (!string.IsNullOrEmpty(this.User))
{
this.Filter = string.IsNullOrEmpty(this.Filter) ? this.User : $"({this.Filter}) and ({this.User})";
}
var finalResult = this.ExecuteCmdletHelper(formattedStartDateTime, formattedEndDateTime);
this.LogResult(finalResult);
}
private IList<object> ExecuteCmdletHelper(string formattedStartDateTime, string formattedEndDateTime)
{
var finalResult = new List<object>();
using (var client = this.CreateClient())
{
try
{
ActivityEventResponse response = client.Admin.GetPowerBIActivityEvents(formattedStartDateTime, formattedEndDateTime, null, this.Filter);
while (response.ContinuationToken != null)
{
finalResult.AddRange(response.ActivityEventEntities);
string formattedContinuationToken = $"'{WebUtility.UrlDecode(response.ContinuationToken)}'";
response = client.Admin.GetPowerBIActivityEvents(null, null, formattedContinuationToken, null);
}
finalResult.AddRange(response.ActivityEventEntities);
}
catch (HttpOperationException ex)
{
var deserialized = JsonConvert.DeserializeObject<PowerBIFeatureNotAvailableErrorType>(ex?.Response?.Content);
if (deserialized != null && deserialized.Error != null && deserialized.Error.Code.Equals(FeatureNotAvailableError))
{
string errorId = "Feature is not available on your tenant.";
var errorRecord = new ErrorRecord(ex, errorId, ErrorCategory.NotEnabled, this /*targetObject*/);
var powerBIRestExceptionRecord = new PowerBIRestExceptionRecord(ex, errorRecord);
this.Logger.ThrowTerminatingError(powerBIRestExceptionRecord, ErrorCategory.NotEnabled);
}
else
{
throw;
}
}
}
return finalResult;
}
private void LogResult(IEnumerable<object> result)
{
switch (this.ResultType)
{
case OutputType.JsonString:
{
string jsonRepresentation = JsonConvert.SerializeObject(result);
string indented = JValue.Parse(jsonRepresentation).ToString(Formatting.Indented);
this.Logger.WriteObject(indented, true);
}
break;
case OutputType.JsonObject:
this.Logger.WriteObject(result, true);
break;
default:
break;
}
}
}
}