-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
481 lines (445 loc) · 17.6 KB
/
script.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*By David wyatt, https://www.linkedin.com/in/wyattdave/ , under license: https://github.com/wyattdave/my-power-platform/blob/main/LICENSE*/
let sApiUrlFlow = 'https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/';
let sApiUrlApp="https://<tenantID>.tenant.api.powerplatform.com/powerapps/environments?api-version=1";
let dDate = new Date(new Date().getFullYear()+"-01-01");
let sDate= dDate.getFullYear()+"-01-01";
let dDateTo = new Date(new Date().getFullYear()+"-12-31");
let sDateTo= dDateTo.getFullYear()+"-12-31";
let sFilter="";
let bFirst=true;
let oDataAPI;
let aAllData=[];
let iAPICount=0;
let aEnvironmentsMaster=[];
let aEnvironmentData=[];
let iNonGeo=0;
let oUser;
let sCurrentGeo="";
const eData=document.getElementById("data");
const eDate=document.getElementById("input-date");
const eDateTo=document.getElementById("input-dateTo");
const eFilter=document.getElementById("input-filter");
const eUpdate=document.getElementById("button-update");
const eLoad=document.getElementById("sideTitle");
const eTotal=document.getElementById("span-total");
const eCount=document.getElementById("span-count");
const eDiv=document.getElementById("div-count");
const eDataDownload=document.getElementById("button-data");
const eEnvirDownload=document.getElementById("button-envir");
const eCsvDownload=document.getElementById("button-csv");
eUpdate.addEventListener("click", setDate);
eDataDownload.addEventListener("click", downloadData);
eEnvirDownload.addEventListener("click", downloadEnvironment);
eCsvDownload.addEventListener("click", downloadCsv);
eDate.value=sDate;
eDateTo.value=sDateTo;
document.addEventListener("DOMContentLoaded", () => {
if(localStorage.getItem("solutionFilter")){
eFilter.value=sFilter=localStorage.getItem("solutionFilter");
}
chrome.runtime.sendMessage({ type: "REQUEST_DATA" }, (response) => {
console.log(response)
if (response) {
if(response.dataverse!="" && response.flow!="" && response.user!=""){
oDataAPI=response;
eLoad.innerHTML="Data Loading..";
load();
} else if(localStorage.getItem("data")){
aAllData=JSON.parse(localStorage.getItem("data"));
if( localStorage.getItem("geo")){sCurrentGeo=localStorage.getItem("geo")}
aEnvironmentsMaster=JSON.parse(localStorage.getItem("environments"));
loadCharts(aAllData,aEnvironmentsMaster,false);
sDate = localStorage.getItem("date");
sDateTo = localStorage.getItem("dateTo");
eDate.value=sDate;
eDateTo.value=sDateTo;
eDiv.style.display="none";
eLoad.innerHTML="Key Data Reloaded";
alert("Access expired, reloaded previous data, if new update required please got to https:/make.powerautomate.com or refresh the page");
}else{
alert("Access expired, please go to https://make.powerautomate.com or refresh the page");
}
} else {
console.error("Failed to receive data");
}
});
});
function downloadData(){
downloadJSON(aAllData,"MyPowerPlatform ("+sDate+" to "+sDateTo+").json");
}
function downloadEnvironment(){
downloadJSON(aEnvironmentData,"MyEnvironments ("+sDate+" to "+sDateTo+").json");
}
function downloadCsv(){
let aCsv=[];
aAllData.forEach(item =>{
let sDataverseId="Not solution aware";
if(item.dataverseId){sDataverseId= item.dataverseId};
aCsv.push({
displayName:item.displayName,
id:item.id,
dataverseId: sDataverseId,
created:item.createdTime,
type:item.type,
environment:item.environment.displayName,
environmentId:item.environment.id,
environmentsUrl:item.environment.url
})
})
const aHeaders=["Display_Name","Id","Dataverse_Id","Created","Type","Environment","Environment_Id","Environments_Url"];
exportCSVFile(aHeaders,aCsv,"MyPowerPlatform ("+sDate+" to "+sDateTo+")");
}
function setDate(){
sDate= eDate.value;
dDate= new Date(sDate);
sDateTo= eDateTo.value;
dDateTo= new Date(sDateTo);
eData.innerHTML="";
eLoad.innerHTML="Data Loading..";
eDiv.style.display=null;
aAllData.length=0;
iAPICount=0;
eCount.innerText=0;
console.log(aAllData);
aEnvironmentsMaster.forEach(envir =>{
getData(envir,true)
})
}
async function load(){
eDiv.style.display=null;
const aEnvironmentsAllGeos=await getEnvironments(oDataAPI.envirs,oDataAPI.url,"")
const sThisEnvironment=oDataAPI.user.split("/api/data")[0];
sCurrentGeo=aEnvironmentsAllGeos.find(envir => envir.url==sThisEnvironment).location;
aEnvironmentsMaster=aEnvironmentsAllGeos.filter(envir =>{return envir.location==sCurrentGeo})
iNonGeo=aEnvironmentsAllGeos.length-aEnvironmentsMaster.length;
eTotal.innerText=aEnvironmentsMaster.length;
oUser =await fetchAPIData(oDataAPI.user,oDataAPI.dataverse,"");
eData.innerHTML="Hello "+oUser.fullname+", all environments in "+sCurrentGeo+" selected<br>";
aEnvironmentsMaster.forEach(envir =>{
getData(envir,false)
})
}
async function getData(oEnvir,bDestroy){
let oWhoAmI;
localStorage.setItem("solutionFilter",eFilter.value);
sFilter=eFilter.value;
try{
const aWhoAmI=await fetchAPIData(oEnvir.url+"/api/data/v9.2/systemusers?$filter=internalemailaddress eq '"+oUser.internalemailaddress+"'",oDataAPI.dataverse,oEnvir.displayName);
if(aWhoAmI){
oWhoAmI=aWhoAmI.value[0].systemuserid
}else{
oWhoAmI=null;
}
if(!oWhoAmI){
iAPICount++;
eData.innerHTML+="<i class='fa-solid fa-globe' style='color:red' aria-hidden='true'></i> "+oEnvir.displayName+" failed, id: cant find<br>";
}else{
eData.innerHTML+="<i class='fa-solid fa-globe' aria-hidden='true'></i> "+oEnvir.displayName+" identified, id:"+oWhoAmI+"<br>";
////flows
const aFlows=await fetchAPIData(sApiUrlFlow+oEnvir.id+"/flows?api-version=2016-11-01",oDataAPI.flow,oEnvir.displayName )
if(aFlows){
aFlows.value.forEach(flow =>{
const dCreated=new Date(flow.properties.createdTime);
if(dCreated>=dDate && dCreated<=dDateTo){
aAllData.push(
{
type:"flow",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:flow.name,
dataverseId:flow.properties.workflowEntityId,
displayName:flow.properties.displayName,
createdTime:flow.properties.createdTime,
isManaged:flow.properties.isManaged,
trigger:flow.properties.definitionSummary.triggers[0].type,
month:dCreated.getMonth()+1
}
)
}
})
eData.innerHTML+="<i class='fa-solid fa-puzzle-piece'></i> "+oEnvir.displayName+" flows found "+aFlows.value.length+"<br>";
}
////apps
const aApps =await fetchAPIData(oEnvir.url+"/api/data/v9.2/canvasapps?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdtime ge "+sDate+" and createdtime le "+sDateTo,oDataAPI.dataverse,oEnvir.displayName);
if(aApps){
aApps.value.forEach(app =>{
aAllData.push(
{
type:"app",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:app.uniquecanvasappid,
dataverseId:app.canvasappid,
displayName:app.displayname,
createdTime:app.createdtime,
isManaged:app.ismanaged,
appUrl:app.appopenuri,
month:new Date(app.createdtime).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-laptop'></i> "+oEnvir.displayName+" apps found "+aApps.value.length+"<br>";
}
////agents
const aBots =await fetchAPIData(oEnvir.url+"/api/data/v9.2/bots?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,oDataAPI.dataverse,oEnvir.displayName);
if(aBots){
aBots.value.forEach(bot =>{
aAllData.push(
{
type:"agent",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:bot.schemaname,
dataverseId:bot.botid,
displayName:bot.name,
createdTime:bot.createdon,
isManaged:bot.ismanaged,
triggger:bot.authenticationtrigger,
month:new Date(bot.createdon).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-robot'></i> "+oEnvir.displayName+" copilot agents found "+aBots.value.length+"<br>";
}
///solutions
const aSolutions =await fetchAPIData(oEnvir.url+"/api/data/v9.2/solutions?$filter=_createdby_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,oDataAPI.dataverse,oEnvir.displayName);
if(aSolutions){
aSolutions.value.forEach(sol =>{
if(!sol.uniquename.includes("msdyn")){
getComponents(oEnvir,sol,oWhoAmI)
}
})
eData.innerHTML+="<i class='fa-solid fa-box-open'></i> "+oEnvir.displayName+" solutions found "+aSolutions.value.length+"<br>";
}
////connection refs
const aConnections =await fetchAPIData(oEnvir.url+"/api/data/v9.2/connectionreferences?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,oDataAPI.dataverse,oEnvir.displayName);
if(aConnections){
aConnections.value.forEach(con =>{
aAllData.push(
{
type:"connection reference",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:con.connectionreferencelogicalname,
dataverseId:con.connectionreferenceid,
displayName:con.connectionreferencedisplayname,
createdTime:con.createdon,
isManaged:con.ismanaged,
connector:con.connectorid.replace("/providers/Microsoft.PowerApps/apis/",""),
month:new Date(con.createdon).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-plug'></i> "+oEnvir.displayName+" connection references found "+aConnections.value.length+"<br>";
}
////environment variables
const aVaraiables =await fetchAPIData(oEnvir.url+"/api/data/v9.2/environmentvariabledefinitions?$filter=_ownerid_value eq '"+oWhoAmI+"' and createdon gt "+sDate+" and createdon le "+sDateTo,oDataAPI.dataverse,oEnvir.displayName);
if(aVaraiables){
aVaraiables.value.forEach(eva =>{
aAllData.push(
{
type:"environment variable",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:eva.schemaname,
dataverseId:eva.environmentvariabledefinitionid,
displayName:eva.displayname,
createdTime:eva.createdon,
isManaged:eva.ismanaged,
variableType:variableType(eva.type),
month:new Date(eva.createdon).getMonth()+1
}
)
})
eData.innerHTML+="<i class='fa-solid fa-database'></i> "+oEnvir.displayName+" environment variables found "+aVaraiables.value.length+"<br>";
}
iAPICount++;
eCount.innerText=iAPICount;
}
}catch(error){
eData.innerHTML+="<i class='fa-solid fa-laptop'></i> "+oEnvir.displayName+"error loading data <br>";
iAPICount++
}
if(iAPICount==aEnvironmentsMaster.length){
console.log(aAllData);
eLoad.innerHTML="Key Data";
eDiv.style.display="none";
aEnvironmentData.length=0;
aEnvironmentsMaster.forEach(envir =>{
const aThisEnvironment=aAllData.filter(item => {return item.environment.id==envir.id});
aEnvironmentData.push({
displayName:envir.displayName,
id:envir.id,
flows: aThisEnvironment.filter(item =>{return item.type=="flow"}).length,
apps: aThisEnvironment.filter(item =>{return item.type=="app"}).length,
agents: aThisEnvironment.filter(item =>{return item.type=="agent"}).length,
solutions: aThisEnvironment.filter(item =>{return item.type=="solution"}).length,
connectionReferences: aThisEnvironment.filter(item =>{return item.type=="connectionReferences"}).length,
environmentVariables: aThisEnvironment.filter(item =>{return item.type=="environmentVariable"}).length,
components:aThisEnvironment.length
})
})
loadCharts(aAllData,aEnvironmentData,bDestroy,sCurrentGeo);
localStorage.setItem("geo",sCurrentGeo);
localStorage.setItem("data",JSON.stringify(aAllData));
localStorage.setItem("environments",JSON.stringify(aEnvironmentsMaster));
localStorage.setItem("date",sDate);
localStorage.setItem("dateTo",sDateTo);
}
}
async function getComponents(oEnvir,sol,sUser){
const aComponents =await fetchAPIData(oEnvir.url+"/api/data/v9.2/solutioncomponents?$filter=_createdby_value eq '"+sUser+"' and _solutionid_value eq '"+sol.solutionid+"'",oDataAPI.dataverse,oEnvir.displayName);
if(aComponents.value.length>0){
aAllData.push(
{
type:"solution",
environment:{
displayName:oEnvir.displayName,
id:oEnvir.id,
url:oEnvir.dyn
},
id:sol.uniquename,
dataverseId:sol.solutionid,
displayName:sol.friendlyname,
createdTime:sol.createdon,
isManaged:sol.ismanaged,
contents:{
flows:aComponents.value.filter(item =>{return item.componenttype == 29}).length,
apps:aComponents.value.filter(item =>{return item.componenttype == 300}).length,
var:aComponents.value.filter(item =>{return item.componenttype == 381}).length,
components:aComponents.value.length
},
month:new Date(sol.createdon).getMonth()+1
}
)
}
}
function downloadJSON(data,sFileName) {
const jsonString = JSON.stringify(data, null, 2);
const blob = new Blob([jsonString], { type: "application/json" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = sFileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
async function getEnvironments(sEnvirToken, sEnvirURL) {
try {
const data = await fetchAPIData(sEnvirURL, sEnvirToken,"");
console.log(data.value)
const aEnvironments = data.value
.sort((a, b) => {
const titleA = a.properties.displayName.toUpperCase();
const titleB = b.properties.displayName.toUpperCase();
return titleA.localeCompare(titleB);
})
.map(item => {
let sUrl = "";
if (item.properties.hasOwnProperty("linkedEnvironmentMetadata")) {
sUrl = item.properties.linkedEnvironmentMetadata.instanceUrl;
sUrlAPI= item.properties.linkedEnvironmentMetadata.instanceApiUrl;
}
return {
displayName: item.properties.displayName,
id: item.name,
url: sUrlAPI,
dyn: sUrl,
location: item.location
};
});
return aEnvironments;
} catch (error) {
console.log("Error Get Environments:", error);
return [];
}
}
function variableType(iVar){
if(iVar==100000000){
return "String"
}
if(iVar==100000001){
return "Number"
}
if(iVar==100000002){
return "Boolean"
}
if(iVar==100000003){
return "JSON"
}
if(iVar==100000004){
return "Data Source"
}
if(iVar==100000005){
return "Secret"
}
}
async function fetchAPIData(url, token, environment) {
try {
const options = {
headers: {
Authorization: token
},
};
const response = await fetch(url, options);
if (!response.ok) {
console.log("Error fetching API data:", response.status+", "+response.statusText);
if (response.status == 401 && !url.includes("WhoAmI")) {
eData.innerHTML+="<br><b style='color:red;'>"+environment+" Invalid token, refresh https://make.powerautomate.com page to update</b><br>";
return null;
}
if (!response.status == 401 && url.includes("WhoAmI")){
eData.innerHTML+="<br><b style='color:red;'>"+environment+" WhoAmI failed</b><br>";
return null;
}
}
const data = await response.json();
return data;
} catch (error) {
console.log("Error fetching API data:", error);
return null
}
}
//// not in use - need to add "https://make.powerapps.com/*" to manifest
async function test(token){
const oRefresh=await fetchAPIDataPost("https://make.powerapps.com/api/exchangeToken", token,"https://eu-uk-dev.api.crm11.dynamics.com");
console.log(oRefresh);
}
async function fetchAPIDataPost(url, token, environment) {
try {
const body={"audience":environment.replace(".api",""),"tenantId":"common","isCaeEnabled":false,"aadClientId":"a8f7a65c-f5ba-4859-b2d6-df772c264e9d"};
const options = {
method:"POST",
body:JSON.stringify(body),
headers: {
Authorization: token
},
};
const response = await fetch(url, options);
console.log(options,body,response)
if (!response.ok) {
console.log("Error fetching API data:", response.status+", "+response.statusText);
return null
}
const data = await response.json();
return data;
} catch (error) {
console.log("Error fetching API data:", error);
return null
}
}