Skip to content

Commit 2c46f2a

Browse files
committed
dfrows
1 parent 5e60692 commit 2c46f2a

File tree

8 files changed

+484
-439
lines changed

8 files changed

+484
-439
lines changed

src/sempy_labs/_capacities.py

Lines changed: 114 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ def list_vcores() -> pd.DataFrame:
233233
"Total Purchased Cores": response_json.get("totalPurchasedCores"),
234234
"Available Cores": response_json.get("availableCores"),
235235
}
236-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
237236

237+
df = pd.DataFrame([new_data], columns=columns.keys())
238238
_update_dataframe_datatypes(dataframe=df, column_map=columns)
239239

240240
return df
@@ -249,7 +249,7 @@ def get_capacity_resource_governance(capacity_name: str):
249249

250250
response = _base_api(request=f"capacities/{capacity_id}/resourceGovernance")
251251

252-
return response.json()["workloadSettings"]
252+
return response.json().get("workloadSettings", {})
253253

254254

255255
@log
@@ -588,15 +588,19 @@ def list_skus_for_capacity(
588588
url = f"https://management.azure.com/subscriptions/{azure_subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Fabric/capacities/{capacity}/skus?api-version=2023-11-01"
589589
response = _base_api(request=url, client="azure")
590590

591+
rows = []
591592
for v in response.json().get("value", []):
592593
sku = v.get("sku", {})
593-
new_data = {
594-
"Resource Type": v.get("resourceType"),
595-
"Sku": sku.get("name"),
596-
"Sku Tier": sku.get("tier"),
597-
}
594+
rows.append(
595+
{
596+
"Resource Type": v.get("resourceType"),
597+
"Sku": sku.get("name"),
598+
"Sku Tier": sku.get("tier"),
599+
}
600+
)
598601

599-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
602+
if rows:
603+
df = pd.DataFrame(rows, columns=columns.keys())
600604

601605
return df
602606

@@ -632,13 +636,17 @@ def list_skus(
632636
url = f"https://management.azure.com/subscriptions/{azure_subscription_id}/providers/Microsoft.Fabric/skus?api-version=2023-11-01"
633637
response = _base_api(request=url, client="azure")
634638

639+
rows = []
635640
for v in response.json().get("value", []):
636-
new_data = {
637-
"Sku": v.get("name"),
638-
"Locations": v.get("locations", []),
639-
}
641+
rows.append(
642+
{
643+
"Sku": v.get("name"),
644+
"Locations": v.get("locations", []),
645+
}
646+
)
640647

641-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
648+
if rows:
649+
df = pd.DataFrame(rows, columns=columns.keys())
642650

643651
return df
644652

@@ -675,23 +683,27 @@ def list_subscriptions() -> pd.DataFrame:
675683
url = "https://management.azure.com/subscriptions?api-version=2022-12-01"
676684
response = _base_api(request=url, client="azure")
677685

686+
rows = []
678687
for v in response.json().get("value", []):
679688
policy = v.get("subscriptionPolicies", {})
680689
tenants = v.get("managedByTenants")
681-
new_data = {
682-
"Subscription Id": v.get("subscriptionId"),
683-
"Subscription Name": v.get("displayName"),
684-
"Tenant Id": v.get("tenantId"),
685-
"State": v.get("state"),
686-
"Location Placement Id": policy.get("locationPlacementId"),
687-
"Quota Id": policy.get("quotaId"),
688-
"Spending Limit": policy.get("spendingLimit"),
689-
"Authorization Source": v.get("authorizationSource"),
690-
"Managed by Tenants": tenants if tenants is not None else [],
691-
"Tags": v.get("tags", {}),
692-
}
693-
694-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
690+
rows.append(
691+
{
692+
"Subscription Id": v.get("subscriptionId"),
693+
"Subscription Name": v.get("displayName"),
694+
"Tenant Id": v.get("tenantId"),
695+
"State": v.get("state"),
696+
"Location Placement Id": policy.get("locationPlacementId"),
697+
"Quota Id": policy.get("quotaId"),
698+
"Spending Limit": policy.get("spendingLimit"),
699+
"Authorization Source": v.get("authorizationSource"),
700+
"Managed by Tenants": tenants if tenants is not None else [],
701+
"Tags": v.get("tags", {}),
702+
}
703+
)
704+
705+
if rows:
706+
df = pd.DataFrame(rows, columns=columns.keys())
695707

696708
return df
697709

@@ -749,7 +761,7 @@ def get_subscription(azure_subscription_id: str) -> pd.DataFrame:
749761
"Tags": v.get("tags", {}),
750762
}
751763

752-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
764+
df = pd.DataFrame([new_data], columns=columns.keys())
753765

754766
return df
755767

@@ -806,20 +818,24 @@ def list_tenants() -> pd.DataFrame:
806818

807819
response = _base_api(request=url, client="azure")
808820

821+
rows = []
809822
for v in response.json().get("value", []):
810823
d = v.get("domains")
811-
new_data = {
812-
"Tenant Id": v.get("tenantId"),
813-
"Tenant Name": v.get("displayName"),
814-
"Country Code": v.get("countryCode"),
815-
"Domains": d if d is not None else "",
816-
"Tenant Category": v.get("tenantCategory"),
817-
"Default Domain": v.get("defaultDomain"),
818-
"Tenant Type": v.get("tenantType"),
819-
"Tenant Branding Logo Url": v.get("tenantBrandingLogoUrl"),
820-
}
821-
822-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
824+
rows.append(
825+
{
826+
"Tenant Id": v.get("tenantId"),
827+
"Tenant Name": v.get("displayName"),
828+
"Country Code": v.get("countryCode"),
829+
"Domains": d if d is not None else "",
830+
"Tenant Category": v.get("tenantCategory"),
831+
"Default Domain": v.get("defaultDomain"),
832+
"Tenant Type": v.get("tenantType"),
833+
"Tenant Branding Logo Url": v.get("tenantBrandingLogoUrl"),
834+
}
835+
)
836+
837+
if rows:
838+
df = pd.DataFrame(rows, columns=columns.keys())
823839

824840
return df
825841

@@ -966,35 +982,38 @@ def list_storage_accounts(
966982

967983
response = _base_api(request=url, client="azure")
968984

985+
rows = []
969986
for v in response.json().get("value", []):
970987
p = v.get("properties", {})
971-
new_data = {
972-
"Storage Account Id": v.get("id"),
973-
"Storage Account Name": v.get("name"),
974-
"Kind": v.get("kind"),
975-
"Location": v.get("location"),
976-
"Sku Name": v.get("sku", {}).get("name"),
977-
"Sku Tier": v.get("sku", {}).get("tier"),
978-
"Is HNS Enabled": p.get("isHnsEnabled"),
979-
"Creation Time": p.get("creationTime"),
980-
"Web Endpoint": p.get("primaryEndpoints", {}).get("web"),
981-
"DFS Endpoint": p.get("primaryEndpoints", {}).get("dfs"),
982-
"Blob Endpoint": p.get("primaryEndpoints", {}).get("blob"),
983-
"File Endpoint": p.get("primaryEndpoints", {}).get("file"),
984-
"Queue Endpoint": p.get("primaryEndpoints", {}).get("queue"),
985-
"Table Endpoint": p.get("primaryEndpoints", {}).get("table"),
986-
"Primary Location": p.get("primaryLocation"),
987-
"Provisioning State": p.get("provisioningState"),
988-
"Secondary Location": p.get("secondaryLocation"),
989-
"Status of Primary": p.get("statusOfPrimary"),
990-
"Status of Secondary": p.get("statusOfSecondary"),
991-
"Supports HTTPS Traffic Only": p.get("supportsHttpsTrafficOnly"),
992-
"Tags": v.get("tags"),
993-
}
994-
995-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
988+
rows.append(
989+
{
990+
"Storage Account Id": v.get("id"),
991+
"Storage Account Name": v.get("name"),
992+
"Kind": v.get("kind"),
993+
"Location": v.get("location"),
994+
"Sku Name": v.get("sku", {}).get("name"),
995+
"Sku Tier": v.get("sku", {}).get("tier"),
996+
"Is HNS Enabled": p.get("isHnsEnabled"),
997+
"Creation Time": p.get("creationTime"),
998+
"Web Endpoint": p.get("primaryEndpoints", {}).get("web"),
999+
"DFS Endpoint": p.get("primaryEndpoints", {}).get("dfs"),
1000+
"Blob Endpoint": p.get("primaryEndpoints", {}).get("blob"),
1001+
"File Endpoint": p.get("primaryEndpoints", {}).get("file"),
1002+
"Queue Endpoint": p.get("primaryEndpoints", {}).get("queue"),
1003+
"Table Endpoint": p.get("primaryEndpoints", {}).get("table"),
1004+
"Primary Location": p.get("primaryLocation"),
1005+
"Provisioning State": p.get("provisioningState"),
1006+
"Secondary Location": p.get("secondaryLocation"),
1007+
"Status of Primary": p.get("statusOfPrimary"),
1008+
"Status of Secondary": p.get("statusOfSecondary"),
1009+
"Supports HTTPS Traffic Only": p.get("supportsHttpsTrafficOnly"),
1010+
"Tags": v.get("tags"),
1011+
}
1012+
)
9961013

997-
_update_dataframe_datatypes(dataframe=df, column_map=columns)
1014+
if rows:
1015+
df = pd.DataFrame(rows, columns=columns.keys())
1016+
_update_dataframe_datatypes(dataframe=df, column_map=columns)
9981017

9991018
return df
10001019

@@ -1079,18 +1098,22 @@ def list_resource_groups(
10791098

10801099
response = _base_api(request=url, client="azure")
10811100

1101+
rows = []
10821102
for v in response.json().get("value", []):
1083-
new_data = {
1084-
"Resource Group Id": v.get("id"),
1085-
"Resource Group Name": v.get("name"),
1086-
"Location": v.get("location"),
1087-
"Managed By": v.get("managedBy"),
1088-
"Tags": v.get("tags"),
1089-
"Type": v.get("type"),
1090-
"Provisioning State": v.get("properties", {}).get("provisioningState"),
1091-
}
1092-
1093-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
1103+
rows.append(
1104+
{
1105+
"Resource Group Id": v.get("id"),
1106+
"Resource Group Name": v.get("name"),
1107+
"Location": v.get("location"),
1108+
"Managed By": v.get("managedBy"),
1109+
"Tags": v.get("tags"),
1110+
"Type": v.get("type"),
1111+
"Provisioning State": v.get("properties", {}).get("provisioningState"),
1112+
}
1113+
)
1114+
1115+
if rows:
1116+
df = pd.DataFrame(rows, columns=columns.keys())
10941117

10951118
return df
10961119

@@ -1156,15 +1179,20 @@ def list_capacities() -> pd.DataFrame:
11561179

11571180
response = _base_api(request="/v1.0/myorg/capacities", client="fabric_sp")
11581181

1182+
rows = []
11591183
for i in response.json().get("value", []):
1160-
new_data = {
1161-
"Id": i.get("id").lower(),
1162-
"Display Name": i.get("displayName"),
1163-
"Sku": i.get("sku"),
1164-
"Region": i.get("region"),
1165-
"State": i.get("state"),
1166-
"Admins": [i.get("admins", [])],
1167-
}
1168-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
1184+
rows.append(
1185+
{
1186+
"Id": i.get("id").lower(),
1187+
"Display Name": i.get("displayName"),
1188+
"Sku": i.get("sku"),
1189+
"Region": i.get("region"),
1190+
"State": i.get("state"),
1191+
"Admins": [i.get("admins", [])],
1192+
}
1193+
)
1194+
1195+
if rows:
1196+
df = pd.DataFrame(rows, columns=columns.keys())
11691197

11701198
return df

src/sempy_labs/_clear_cache.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
resolve_dataset_name_and_id,
77
_update_dataframe_datatypes,
88
_base_api,
9+
_create_dataframe,
910
)
1011
from typing import Optional
1112
import sempy_labs._icons as icons
@@ -316,37 +317,35 @@ def list_storage_account_files(
316317
A pandas dataframe showing a list of files contained within an ADLS Gen2 storage account.
317318
"""
318319

319-
df = pd.DataFrame(
320-
columns=[
321-
"File Path",
322-
"File Size",
323-
"Creation Time",
324-
"Last Modified",
325-
"Expiry Time",
326-
"Encryption Scope",
327-
]
328-
)
320+
columns = {
321+
"File Path": "str",
322+
"File Size": "int",
323+
"Creation Time": "datetime",
324+
"Last Modified": "datetime",
325+
"Expiry Time": "datetime",
326+
"Encryption Scope": "str",
327+
}
329328

329+
df = _create_dataframe(columns=columns)
330330
client = _get_adls_client(storage_account)
331331
fs = client.get_file_system_client(container)
332332

333+
rows = []
333334
for x in list(fs.get_paths()):
334335
if not x.is_directory:
335-
new_data = {
336-
"File Path": x.name,
337-
"File Size": x.content_length,
338-
"Creation Time": x.creation_time,
339-
"Last Modified": x.last_modified,
340-
"Expiry Time": x.expiry_time,
341-
"Encryption Scope": x.encryption_scope,
342-
}
343-
344-
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
345-
346-
column_map = {
347-
"File Size": "int",
348-
}
349-
350-
_update_dataframe_datatypes(dataframe=df, column_map=column_map)
336+
rows.append(
337+
{
338+
"File Path": x.name,
339+
"File Size": x.content_length,
340+
"Creation Time": x.creation_time,
341+
"Last Modified": x.last_modified,
342+
"Expiry Time": x.expiry_time,
343+
"Encryption Scope": x.encryption_scope,
344+
}
345+
)
346+
347+
if rows:
348+
df = pd.DataFrame(rows)
349+
_update_dataframe_datatypes(dataframe=df, column_map=columns)
351350

352351
return df

0 commit comments

Comments
 (0)