zrhoffman commented on code in PR #7615: URL: https://github.com/apache/trafficcontrol/pull/7615#discussion_r1273678356
########## experimental/traffic-portal/src/app/api/topology.service.ts: ########## @@ -0,0 +1,196 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import { HttpClient } from "@angular/common/http"; +import { Injectable } from "@angular/core"; +import type { + RequestTopology, + ResponseTopology, + ResponseTopologyNode, +} from "trafficops-types"; + +import { APIService } from "./base-api.service"; + +/** + * TopTreeNode is used to represent a topology in a format usable as a material + * nested tree data source. + */ +export interface TopTreeNode { + name: string; + cachegroup: string; + children: Array<TopTreeNode>; + parents: Array<this>; +} + +/** + * TopologyService exposes API functionality relating to Topologies. + */ +@Injectable() +export class TopologyService extends APIService { + + constructor(http: HttpClient) { + super(http); + } + + /** + * Gets a specific Topology from Traffic Ops + * + * @param name The name of the Topology to be returned. + * @returns The Topology with the given name. + */ + public async getTopologies(name: string): Promise<ResponseTopology>; + /** + * Gets all Topologies from Traffic Ops + * + * @returns An Array of all Topologies configured in Traffic Ops. + */ + public async getTopologies(): Promise<Array<ResponseTopology>>; + /** + * Gets one or all Topologies from Traffic Ops + * + * @param name The name of a single Topology to be returned. + * @returns Either an Array of Topology objects, or a single Topology, depending on + * whether `name` was passed. + */ + public async getTopologies(name?: string): Promise<Array<ResponseTopology> | ResponseTopology> { + const path = "topologies"; + if (name) { + const topology = await this.get<[ResponseTopology]>(path, undefined, {name}).toPromise(); + if (topology.length !== 1) { + throw new Error(`${topology.length} Topologies found by name ${name}`); + } + return topology[0]; + } + return this.get<Array<ResponseTopology>>(path).toPromise(); + } + + /** + * Deletes a Topology. + * + * @param topology The Topology to be deleted, or just its name. + */ + public async deleteTopology(topology: ResponseTopology | string): Promise<void> { + const name = typeof topology === "string" ? topology : topology.name; + return this.delete(`topologies?name=${name}`).toPromise(); + } + + /** + * Creates a new Topology. + * + * @param topology The Topology to create. + */ + public async createTopology(topology: RequestTopology): Promise<ResponseTopology> { + return this.post<ResponseTopology>("topologies", topology).toPromise(); + } + + /** + * Replaces an existing Topology with the provided new definition of a + * Topology. + * + * @param topology The full new definition of the Topology being updated + */ + public async updateTopology(topology: ResponseTopology): Promise<ResponseTopology> { + return this.put<ResponseTopology>(`topologies?name=${topology.name}`, topology).toPromise(); + } + + /** + * Generates a material tree from a topology. + * + * @param topology The topology to generate a material tree from. + * @returns a material tree. + */ + public topologyToTree(topology: ResponseTopology): Array<TopTreeNode> { + const treeNodes: Array<TopTreeNode> = []; + const topLevel: Array<TopTreeNode> = []; + for (const node of topology.nodes) { + const name = node.cachegroup; + const cachegroup = node.cachegroup; + const children: Array<TopTreeNode> = []; + const parents: Array<TopTreeNode> = []; + treeNodes.push({ + cachegroup, + children, + name, + parents, + }); + } + for (let index = 0; index < topology.nodes.length; index++) { + const node = topology.nodes[index]; + const treeNode = treeNodes[index]; + if (!(node.parents instanceof Array) || node.parents.length < 1) { + topLevel.push(treeNode); + continue; + } + for (const parent of node.parents) { + treeNodes[parent].children.push(treeNode); + treeNode.parents.push(treeNodes[parent]); + } + } + return topLevel; + } + + /** + * Generates a topology from a material tree. + * + * @param name The topology name + * @param description The topology description + * @param treeNodes The data for a material tree + * @returns a material tree. + */ + public treeToTopology(name: string, description: string, treeNodes: Array<TopTreeNode>): ResponseTopology { + const topologyNodeIndicesByCacheGroup: Map<string, number> = new Map(); + const nodes: Array<ResponseTopologyNode> = new Array<ResponseTopologyNode>(); + this.treeToTopologyInner(topologyNodeIndicesByCacheGroup, nodes, undefined, treeNodes); + const topology: ResponseTopology = { + description, + lastUpdated: new Date(), + name, + nodes, + }; + return topology; + } + + /** + * Inner recursive function for generating a Topology from a material tree. + * + * @param topologyNodeIndicesByCacheGroup A map of Topology node indices + * using cache group names as the key + * @param topologyNodes The mutable array of Topology nodes + * @param parent The parent, if it exists + * @param treeNodes The data for a material tree + */ + protected treeToTopologyInner(topologyNodeIndicesByCacheGroup: Map<string, number>, + topologyNodes: Array<ResponseTopologyNode>, parent: ResponseTopologyNode | undefined, treeNodes: Array<TopTreeNode>): void { Review Comment: Moved to one argument per line ########## experimental/traffic-portal/src/app/api/topology.service.ts: ########## @@ -0,0 +1,196 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import { HttpClient } from "@angular/common/http"; +import { Injectable } from "@angular/core"; +import type { + RequestTopology, + ResponseTopology, + ResponseTopologyNode, +} from "trafficops-types"; + +import { APIService } from "./base-api.service"; + +/** + * TopTreeNode is used to represent a topology in a format usable as a material + * nested tree data source. + */ +export interface TopTreeNode { + name: string; + cachegroup: string; + children: Array<TopTreeNode>; + parents: Array<this>; +} + +/** + * TopologyService exposes API functionality relating to Topologies. + */ +@Injectable() +export class TopologyService extends APIService { + + constructor(http: HttpClient) { + super(http); + } + + /** + * Gets a specific Topology from Traffic Ops + * + * @param name The name of the Topology to be returned. + * @returns The Topology with the given name. + */ + public async getTopologies(name: string): Promise<ResponseTopology>; + /** + * Gets all Topologies from Traffic Ops + * + * @returns An Array of all Topologies configured in Traffic Ops. + */ + public async getTopologies(): Promise<Array<ResponseTopology>>; + /** + * Gets one or all Topologies from Traffic Ops + * + * @param name The name of a single Topology to be returned. + * @returns Either an Array of Topology objects, or a single Topology, depending on + * whether `name` was passed. + */ + public async getTopologies(name?: string): Promise<Array<ResponseTopology> | ResponseTopology> { + const path = "topologies"; + if (name) { + const topology = await this.get<[ResponseTopology]>(path, undefined, {name}).toPromise(); + if (topology.length !== 1) { + throw new Error(`${topology.length} Topologies found by name ${name}`); + } + return topology[0]; + } + return this.get<Array<ResponseTopology>>(path).toPromise(); + } + + /** + * Deletes a Topology. + * + * @param topology The Topology to be deleted, or just its name. + */ + public async deleteTopology(topology: ResponseTopology | string): Promise<void> { + const name = typeof topology === "string" ? topology : topology.name; + return this.delete(`topologies?name=${name}`).toPromise(); + } + + /** + * Creates a new Topology. + * + * @param topology The Topology to create. + */ + public async createTopology(topology: RequestTopology): Promise<ResponseTopology> { + return this.post<ResponseTopology>("topologies", topology).toPromise(); + } + + /** + * Replaces an existing Topology with the provided new definition of a + * Topology. + * + * @param topology The full new definition of the Topology being updated + */ + public async updateTopology(topology: ResponseTopology): Promise<ResponseTopology> { + return this.put<ResponseTopology>(`topologies?name=${topology.name}`, topology).toPromise(); + } + + /** + * Generates a material tree from a topology. + * + * @param topology The topology to generate a material tree from. + * @returns a material tree. + */ + public topologyToTree(topology: ResponseTopology): Array<TopTreeNode> { + const treeNodes: Array<TopTreeNode> = []; + const topLevel: Array<TopTreeNode> = []; + for (const node of topology.nodes) { + const name = node.cachegroup; + const cachegroup = node.cachegroup; + const children: Array<TopTreeNode> = []; + const parents: Array<TopTreeNode> = []; + treeNodes.push({ + cachegroup, + children, + name, + parents, + }); + } + for (let index = 0; index < topology.nodes.length; index++) { + const node = topology.nodes[index]; + const treeNode = treeNodes[index]; + if (!(node.parents instanceof Array) || node.parents.length < 1) { + topLevel.push(treeNode); + continue; + } + for (const parent of node.parents) { + treeNodes[parent].children.push(treeNode); + treeNode.parents.push(treeNodes[parent]); + } + } + return topLevel; + } + + /** + * Generates a topology from a material tree. + * + * @param name The topology name + * @param description The topology description + * @param treeNodes The data for a material tree + * @returns a material tree. + */ + public treeToTopology(name: string, description: string, treeNodes: Array<TopTreeNode>): ResponseTopology { + const topologyNodeIndicesByCacheGroup: Map<string, number> = new Map(); + const nodes: Array<ResponseTopologyNode> = new Array<ResponseTopologyNode>(); + this.treeToTopologyInner(topologyNodeIndicesByCacheGroup, nodes, undefined, treeNodes); + const topology: ResponseTopology = { + description, + lastUpdated: new Date(), + name, + nodes, + }; + return topology; + } + + /** + * Inner recursive function for generating a Topology from a material tree. + * + * @param topologyNodeIndicesByCacheGroup A map of Topology node indices + * using cache group names as the key + * @param topologyNodes The mutable array of Topology nodes + * @param parent The parent, if it exists + * @param treeNodes The data for a material tree + */ + protected treeToTopologyInner(topologyNodeIndicesByCacheGroup: Map<string, number>, + topologyNodes: Array<ResponseTopologyNode>, parent: ResponseTopologyNode | undefined, treeNodes: Array<TopTreeNode>): void { + + for (const treeNode of treeNodes) { + const cachegroup = treeNode.cachegroup; + const parents: number[] = []; + if (parent instanceof Object) { Review Comment: > If you want to know if something is an object primitive type, use typeof(x) === "object" `typeof object` is bad for the reason you mentioned, that `typeof null` returns `"object"`. > if you want to know if it's an instance of some specific class, you can use `x instanceof Class` How? If I change that line to ```typescript if (parent instanceof ResponseTopologyNode) { ``` I get ``` TS2693: ResponseTopologyNode only refers to a type, but is being used as a value here. ``` But since all I'm trying to rule out is non-instances, `parent instanceof Object` seems like the safest way. ########## experimental/traffic-portal/src/app/api/topology.service.ts: ########## @@ -0,0 +1,196 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import { HttpClient } from "@angular/common/http"; +import { Injectable } from "@angular/core"; +import type { + RequestTopology, + ResponseTopology, + ResponseTopologyNode, +} from "trafficops-types"; + +import { APIService } from "./base-api.service"; + +/** + * TopTreeNode is used to represent a topology in a format usable as a material + * nested tree data source. + */ +export interface TopTreeNode { + name: string; + cachegroup: string; + children: Array<TopTreeNode>; + parents: Array<this>; +} + +/** + * TopologyService exposes API functionality relating to Topologies. + */ +@Injectable() +export class TopologyService extends APIService { + + constructor(http: HttpClient) { + super(http); + } + + /** + * Gets a specific Topology from Traffic Ops + * + * @param name The name of the Topology to be returned. + * @returns The Topology with the given name. + */ + public async getTopologies(name: string): Promise<ResponseTopology>; + /** + * Gets all Topologies from Traffic Ops + * + * @returns An Array of all Topologies configured in Traffic Ops. + */ + public async getTopologies(): Promise<Array<ResponseTopology>>; + /** + * Gets one or all Topologies from Traffic Ops + * + * @param name The name of a single Topology to be returned. + * @returns Either an Array of Topology objects, or a single Topology, depending on + * whether `name` was passed. + */ + public async getTopologies(name?: string): Promise<Array<ResponseTopology> | ResponseTopology> { + const path = "topologies"; + if (name) { + const topology = await this.get<[ResponseTopology]>(path, undefined, {name}).toPromise(); + if (topology.length !== 1) { + throw new Error(`${topology.length} Topologies found by name ${name}`); + } + return topology[0]; + } + return this.get<Array<ResponseTopology>>(path).toPromise(); + } + + /** + * Deletes a Topology. + * + * @param topology The Topology to be deleted, or just its name. + */ + public async deleteTopology(topology: ResponseTopology | string): Promise<void> { + const name = typeof topology === "string" ? topology : topology.name; + return this.delete(`topologies?name=${name}`).toPromise(); + } + + /** + * Creates a new Topology. + * + * @param topology The Topology to create. + */ + public async createTopology(topology: RequestTopology): Promise<ResponseTopology> { + return this.post<ResponseTopology>("topologies", topology).toPromise(); + } + + /** + * Replaces an existing Topology with the provided new definition of a + * Topology. + * + * @param topology The full new definition of the Topology being updated + */ + public async updateTopology(topology: ResponseTopology): Promise<ResponseTopology> { + return this.put<ResponseTopology>(`topologies?name=${topology.name}`, topology).toPromise(); + } + + /** + * Generates a material tree from a topology. + * + * @param topology The topology to generate a material tree from. + * @returns a material tree. + */ + public topologyToTree(topology: ResponseTopology): Array<TopTreeNode> { + const treeNodes: Array<TopTreeNode> = []; + const topLevel: Array<TopTreeNode> = []; + for (const node of topology.nodes) { + const name = node.cachegroup; + const cachegroup = node.cachegroup; + const children: Array<TopTreeNode> = []; + const parents: Array<TopTreeNode> = []; + treeNodes.push({ + cachegroup, + children, + name, + parents, + }); + } + for (let index = 0; index < topology.nodes.length; index++) { + const node = topology.nodes[index]; + const treeNode = treeNodes[index]; + if (!(node.parents instanceof Array) || node.parents.length < 1) { + topLevel.push(treeNode); + continue; + } + for (const parent of node.parents) { + treeNodes[parent].children.push(treeNode); + treeNode.parents.push(treeNodes[parent]); + } + } + return topLevel; + } + + /** + * Generates a topology from a material tree. + * + * @param name The topology name + * @param description The topology description + * @param treeNodes The data for a material tree + * @returns a material tree. + */ + public treeToTopology(name: string, description: string, treeNodes: Array<TopTreeNode>): ResponseTopology { + const topologyNodeIndicesByCacheGroup: Map<string, number> = new Map(); + const nodes: Array<ResponseTopologyNode> = new Array<ResponseTopologyNode>(); + this.treeToTopologyInner(topologyNodeIndicesByCacheGroup, nodes, undefined, treeNodes); + const topology: ResponseTopology = { + description, + lastUpdated: new Date(), + name, + nodes, + }; + return topology; + } + + /** + * Inner recursive function for generating a Topology from a material tree. + * + * @param topologyNodeIndicesByCacheGroup A map of Topology node indices + * using cache group names as the key + * @param topologyNodes The mutable array of Topology nodes + * @param parent The parent, if it exists + * @param treeNodes The data for a material tree + */ + protected treeToTopologyInner(topologyNodeIndicesByCacheGroup: Map<string, number>, + topologyNodes: Array<ResponseTopologyNode>, parent: ResponseTopologyNode | undefined, treeNodes: Array<TopTreeNode>): void { + + for (const treeNode of treeNodes) { + const cachegroup = treeNode.cachegroup; + const parents: number[] = []; + if (parent instanceof Object) { Review Comment: > If you want to know if something is an object primitive type, use typeof(x) === "object" `typeof object` is bad for the reason you mentioned, that `typeof null` returns `"object"`. > if you want to know if it's an instance of some specific class, you can use `x instanceof Class` How? If I change that line to ```typescript if (parent instanceof ResponseTopologyNode) { ``` I get ``` TS2693: ResponseTopologyNode only refers to a type, but is being used as a value here. ``` But since all I'm trying to rule out is non-instances, `parent instanceof Object` seems like the safest way. ########## experimental/traffic-portal/src/app/core/topologies/topology-details/topology-details.component.ts: ########## @@ -0,0 +1,152 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import { NestedTreeControl } from "@angular/cdk/tree"; +import { Location } from "@angular/common"; +import { Component, OnInit } from "@angular/core"; +import { MatDialog } from "@angular/material/dialog"; +import { MatTreeNestedDataSource } from "@angular/material/tree"; +import { ActivatedRoute } from "@angular/router"; +import { ResponseTopology } from "trafficops-types"; + +import { TopologyService, TopTreeNode } from "src/app/api"; +import { + DecisionDialogComponent, + DecisionDialogData, +} from "src/app/shared/dialogs/decision-dialog/decision-dialog.component"; +import { + NavigationService +} from "src/app/shared/navigation/navigation.service"; + +/** + * TopologyDetailComponent is the controller for a Topology's "detail" page. + */ +@Component({ + selector: "tp-topology-details", + styleUrls: ["./topology-details.component.scss"], + templateUrl: "./topology-details.component.html", +}) +export class TopologyDetailsComponent implements OnInit { + public new = false; + public topology: ResponseTopology = { + description: "", + lastUpdated: new Date(), + name: "", + nodes: [], + }; + public showErrors = false; + public topologies: Array<ResponseTopology> = []; + public topologySource = new MatTreeNestedDataSource<TopTreeNode>(); + public topologyControl = new NestedTreeControl<TopTreeNode>(node => node.children); + + constructor( + private readonly route: ActivatedRoute, + private readonly api: TopologyService, + private readonly location: Location, + private readonly dialog: MatDialog, + private readonly navSvc: NavigationService, + ) { + } + + /** + * Angular lifecycle hook where data is initialized. + */ + public async ngOnInit(): Promise<void> { + const name = this.route.snapshot.paramMap.get("name"); + if (name === null) { + console.error("missing required route parameter 'name'"); + return; + } + + const topologiesPromise = this.api.getTopologies().then(topologies => this.topologies = topologies); + if (name === "new") { + this.new = true; + this.setTitle(); + await topologiesPromise; + return; + } + await topologiesPromise; + const index = this.topologies.findIndex(c => c.name === name); + if (index < 0) { + console.error(`no such Topology: #${name}`); Review Comment: Removed `#` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
