Skip to content
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 53 additions & 36 deletions visualization_tool/src/components/ControlMenu/Controller.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
import {OutputFile, Resource} from '../../types/resources';

const getResourceData = (
resource: Resource,
projectId: string,
fileName: string
) => {
return {
projectId,
file: fileName,
id: resource.id,
name: resource.name,
creationTimestamp: resource.creationTimestamp,
status: resource?.status || 'READY',
};
import {
OutputFile,
Resource,
ResourceType,
availableResourceTypes,
} from '../../types/resources';

const titleCase = (str: string) => {
return str
.replace('_', ' ')
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};

const parseData = (data: OutputFile, fileName: string) => {
const resources = [];
for (const [projectId, projectData] of Object.entries(data.projects)) {
const computeInstances = projectData.compute_instances?.map(
computeInstance => {
return {
...getResourceData(computeInstance, projectId, fileName),
type: 'Compute Instance' as const,
zone: computeInstance.zone.split('/').at(-1) as string,
machineType: computeInstance.machineType.split('/').at(-1) as string,
};
for (const [resourceType, resourceList] of Object.entries(projectData)) {
let type = titleCase(resourceType.slice(0, -1));
if (type === 'Dns Policie') type = 'Dns Policy';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, sorry. Forgot to flag this one. Dns Policies or Dns Policy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get the type of the resource, I use the name of it in the json file, then remove the last character to make it singular
This doesn't work in the naming of dns_policies so I handle it differently using this if statement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to remove the last character?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I used the singular names of resources in the code
I can change that to use plural ones

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good. merging it.

if (
resourceList instanceof Array &&
availableResourceTypes.includes(type as ResourceType)
) {
const currentResources = [];
for (const resource of resourceList) {
const resourceData: Record<string, string | number> = {};
resourceData['file'] = fileName;
resourceData['projectId'] = projectId;

for (const [key, value] of Object.entries(resource)) {
switch (typeof value) {
case 'string':
// if this attribute is a link, get the last part of the link
if (value.split('/').length > 1) {
resourceData[key] = value.split('/').at(-1) || 'unknown';
} else {
resourceData[key] = value;
}
break;
case 'number':
resourceData[key] = value;
break;
default:
break;
}
}

resourceData['name'] = resourceData['name'] || 'unknown';
resourceData['status'] = resourceData['status'] || 'READY';
resourceData['type'] = type;

currentResources.push(resourceData as Resource);
}

resources.push(...currentResources);
}
);
resources.push(...computeInstances);

const computeDisks = projectData.compute_disks.map(computeDisk => {
return {
...getResourceData(computeDisk, projectId, fileName),
type: 'Compute Disk' as const,
storageType: computeDisk.type.split('/').at(-1) as string,
sizeGb: computeDisk.sizeGb,
};
});

resources.push(...computeDisks);
}
}

return resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
font-family: Inter;
font-size: 18px;
margin-bottom: 5px;

display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
}

.resource-type {
Expand Down
15 changes: 14 additions & 1 deletion visualization_tool/src/components/ResourcesList/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import {Resource} from '../../types/resources';
import ComputeInstance from '../../assets/resources/Compute Instance.png';
import ComputeDisk from '../../assets/resources/Compute Disk.png';
import ManagedZone from '../../assets/resources/Managed Zone.png';
import SQLInstance from '../../assets/resources/SQL Instance.png';
import CloudFunction from '../../assets/resources/Cloud Function.png';
import defaultLogo from '../../assets/resources/resource.png';

const typeToImage = (resource: Resource) => {
switch (resource.type) {
case 'Compute Instance':
case 'Compute Image':
case 'Machine Image':
case 'Compute Snapshot':
return ComputeInstance;
case 'Compute Disk':
return ComputeDisk;
case 'Managed Zone':
return ManagedZone;
case 'SQL Instance':
return SQLInstance;
case 'Cloud Function':
return CloudFunction;
default:
return '';
return defaultLogo;
}
};

Expand Down
59 changes: 30 additions & 29 deletions visualization_tool/src/types/resources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
type ResourceType = 'Compute Instance' | 'Compute Disk';
type ResourceStatus = 'READY' | 'RUNNING' | 'STOPPED' | 'DELETED';
type ResourceType =
| 'Compute Instance'
| 'Compute Disk'
| 'Compute Image'
| 'Machine Image'
| 'Compute Snapshot'
| 'Managed Zone'
| 'SQL Instance'
| 'Cloud Function'
| 'Dns Policy'
| 'Pubsub Sub';

const availableResourceTypes: ResourceType[] = [
'Compute Instance',
'Compute Disk',
'Compute Image',
'Machine Image',
'Compute Snapshot',
'Managed Zone',
'SQL Instance',
'Cloud Function',
'Dns Policy',
'Pubsub Sub',
];

type ResourceStatus = 'READY' | 'ACTIVE' | 'RUNNING' | 'STOPPED' | 'DELETED';

type Resource = {
projectId: string;
Expand All @@ -11,25 +35,8 @@ type Resource = {
status: ResourceStatus;
};

type ComputeInstance = Resource & {
type: 'Compute Instance';
zone: string;
machineType: string;
};

type ComputeDisk = Resource & {
type: 'Compute Disk';
storageType: string;
sizeGb: number;
};

type Project = {
project_info: {
projectNumber: string;
projectId: string;
};
compute_instances: ComputeInstance[];
compute_disks: ComputeDisk[];
[key: string]: Resource[];
};

type OutputFile = {
Expand All @@ -38,12 +45,6 @@ type OutputFile = {
};
};

export type {
ResourceType,
ResourceStatus,
Resource,
ComputeInstance,
ComputeDisk,
Project,
OutputFile,
};
export type {ResourceType, ResourceStatus, Resource, Project, OutputFile};

export {availableResourceTypes};