-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
489826f
commit 002954b
Showing
27 changed files
with
24,198 additions
and
1,110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 117 additions & 24 deletions
141
src/controllers/DisplayResourceDependencyGraphController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,146 @@ | ||
import * as vscode from 'vscode'; | ||
import { IResourceDependencyGraphService } from '../services/ResourcesGraphService'; | ||
import { IErrorHandlerService } from '../services/ErrorHandlerService'; | ||
import { IActiveResourcesRepository } from '../repos/ActiveResourcesRepository'; | ||
import { IDependencyGraphRepository } from '../repos/DependencyGraphRepository'; | ||
import { IDeploymentRepository } from '../repos/DeploymentRepository'; | ||
import { IResourceTypeRepository } from '../repos/ResourceTypeRepository'; | ||
import { IConfigurationRepository } from '../repos/ConfigurationRepository'; | ||
import { IResourceDefinitionRepository } from '../repos/ResourceDefinitionRepository'; | ||
import { ConfigKey } from '../domain/ConfigKey'; | ||
import { IEnvironmentRepository } from '../repos/EnvironmentRepository'; | ||
import { NoDeploymentsInEnvironmentError } from '../errors/NoDeploymentsInEnvironmentError'; | ||
|
||
export class DisplayResourcesGraphController { | ||
private constructor() {} | ||
|
||
static register( | ||
context: vscode.ExtensionContext, | ||
resourcesGraphService: IResourceDependencyGraphService, | ||
activeResourcesRepository: IActiveResourcesRepository, | ||
dependencyGraphRepository: IDependencyGraphRepository, | ||
deploymentRepository: IDeploymentRepository, | ||
ResourceDefinitionRepository: IResourceDefinitionRepository, | ||
resourceTypeRepository: IResourceTypeRepository, | ||
environmentRepository: IEnvironmentRepository, | ||
configs: IConfigurationRepository, | ||
errorHandler: IErrorHandlerService | ||
) { | ||
const disposable = vscode.commands.registerCommand( | ||
'humanitec.display_resources_graph', | ||
async () => { | ||
try { | ||
const graph = await resourcesGraphService.generate(); | ||
const orgId = await configs.get(ConfigKey.HUMANITEC_ORG); | ||
const appId = await configs.get(ConfigKey.HUMANITEC_APP); | ||
const envId = await configs.get(ConfigKey.HUMANITEC_ENV); | ||
|
||
const environment = await environmentRepository.get( | ||
orgId, | ||
appId, | ||
envId | ||
); | ||
|
||
if (!environment.lastDeploymentId) { | ||
throw new NoDeploymentsInEnvironmentError(envId); | ||
} | ||
|
||
const panel = vscode.window.createWebviewPanel( | ||
'humanitec_resources_graph', | ||
'Resources Graph', | ||
'webview', | ||
'Resource Dependency Graph', | ||
vscode.ViewColumn.One, | ||
{ | ||
enableScripts: true, | ||
} | ||
); | ||
panel.webview.html = this.getWebviewContent(graph); | ||
const scriptSrc = panel.webview.asWebviewUri( | ||
vscode.Uri.joinPath( | ||
context.extensionUri, | ||
'webview', | ||
'resource-dependency-graph', | ||
'dist', | ||
'index.js' | ||
) | ||
); | ||
|
||
const cssSrc = panel.webview.asWebviewUri( | ||
vscode.Uri.joinPath( | ||
context.extensionUri, | ||
'webview', | ||
'resource-dependency-graph', | ||
'dist', | ||
'index.css' | ||
) | ||
); | ||
|
||
panel.webview.onDidReceiveMessage(async msg => { | ||
switch (msg.command) { | ||
case 'resource-dependency-graph-send-data': | ||
try { | ||
const activeResources = | ||
await activeResourcesRepository.getRaw(orgId, appId, envId); | ||
|
||
const deployment = await deploymentRepository.getLatestRaw( | ||
orgId, | ||
appId, | ||
envId, | ||
environment.lastDeploymentId! | ||
); | ||
interface DeploymentObject { | ||
dependency_graph_id: string; | ||
} | ||
const dependencyGraph = | ||
await dependencyGraphRepository.getRaw( | ||
orgId, | ||
appId, | ||
envId, | ||
(deployment as DeploymentObject).dependency_graph_id | ||
); | ||
const resourceDefinitions = | ||
await ResourceDefinitionRepository.getAllRaw(orgId); | ||
const resourceTypes = | ||
await resourceTypeRepository.getAvailableRaw(orgId); | ||
panel.webview.postMessage({ | ||
type: 'resource-dependency-graph-data', | ||
value: { | ||
activeResources, | ||
deployment, | ||
dependencyGraph, | ||
resourceDefinitions, | ||
resourceTypes, | ||
}, | ||
}); | ||
} catch (error) { | ||
errorHandler.handle(error); | ||
} | ||
break; | ||
} | ||
}); | ||
|
||
panel.webview.html = `<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<link rel="stylesheet" href="${cssSrc}" /> | ||
</head> | ||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div id="root"></div> | ||
<script> | ||
//declare the vscode variable to be used in the front-end | ||
const vscode = acquireVsCodeApi(); | ||
//send a message to confirm everything is working properly | ||
window.onload = function() { | ||
vscode.postMessage({ command: 'resource-dependency-graph-send-data' }); | ||
console.log('HTML started up.'); | ||
}; | ||
</script> | ||
<script src="${scriptSrc}"></script> | ||
</body> | ||
</html> | ||
`; | ||
} catch (error) { | ||
errorHandler.handle(error); | ||
} | ||
} | ||
); | ||
context.subscriptions.push(disposable); | ||
} | ||
|
||
private static getWebviewContent(graph: string): string { | ||
return `<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script src="https://d3js.org/d3.v5.min.js"></script> | ||
<script src="https://unpkg.com/@hpcc-js/[email protected]/dist/index.min.js"></script> | ||
<script src="https://unpkg.com/[email protected]/build/d3-graphviz.js"></script> | ||
<div id="graph" style="text-align: center;"></div> | ||
<script> | ||
d3.select("#graph").graphviz().renderDot(\`${graph}\`); | ||
</script> | ||
</body> | ||
</html>`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { IHumctlAdapter } from '../adapters/humctl/IHumctlAdapter'; | ||
import { HumctlError } from '../errors/HumctlError'; | ||
|
||
export interface IActiveResourcesRepository { | ||
getRaw( | ||
organizationId: string, | ||
applicationId: string, | ||
environmentId: string | ||
): Promise<unknown>; | ||
} | ||
|
||
export class ActiveResourcesRepository implements IActiveResourcesRepository { | ||
constructor(private humctl: IHumctlAdapter) {} | ||
async getRaw( | ||
organizationId: string, | ||
applicationId: string, | ||
environmentId: string | ||
): Promise<unknown> { | ||
const resourcesUrl = | ||
'/orgs/' + | ||
organizationId + | ||
'/apps/' + | ||
applicationId + | ||
'/envs/' + | ||
environmentId + | ||
'/resources'; | ||
const result = await this.humctl.execute(['api', 'get', resourcesUrl]); | ||
if (result.stderr !== '') { | ||
throw new HumctlError( | ||
'humctl api get ' + resourcesUrl, | ||
result.stderr, | ||
result.exitcode | ||
); | ||
} | ||
return JSON.parse(result.stdout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { IHumctlAdapter } from '../adapters/humctl/IHumctlAdapter'; | ||
import { HumctlError } from '../errors/HumctlError'; | ||
|
||
export interface IDependencyGraphRepository { | ||
getRaw( | ||
organizationId: string, | ||
applicationId: string, | ||
environmentId: string, | ||
dependencyGraphId: string | ||
): Promise<unknown>; | ||
} | ||
|
||
export class DependencyGraphRepository implements IDependencyGraphRepository { | ||
constructor(private humctl: IHumctlAdapter) {} | ||
async getRaw( | ||
organizationId: string, | ||
applicationId: string, | ||
environmentId: string, | ||
dependencyGraphId: string | ||
): Promise<unknown> { | ||
const graphUrl = | ||
'/orgs/' + | ||
organizationId + | ||
'/apps/' + | ||
applicationId + | ||
'/envs/' + | ||
environmentId + | ||
'/resources/graphs/' + | ||
dependencyGraphId; | ||
const result = await this.humctl.execute(['api', 'get', graphUrl]); | ||
if (result.stderr !== '') { | ||
throw new HumctlError( | ||
'humctl api get ' + graphUrl, | ||
result.stderr, | ||
result.exitcode | ||
); | ||
} | ||
return JSON.parse(result.stdout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { IHumctlAdapter } from '../adapters/humctl/IHumctlAdapter'; | ||
import { HumctlError } from '../errors/HumctlError'; | ||
|
||
export interface IDeploymentRepository { | ||
getLatestRaw( | ||
organizationId: string, | ||
applicationId: string, | ||
environmentId: string, | ||
deployId: string | ||
): Promise<unknown>; | ||
} | ||
|
||
export class DeploymentRepository implements IDeploymentRepository { | ||
constructor(private humctl: IHumctlAdapter) {} | ||
|
||
async getLatestRaw( | ||
organizationId: string, | ||
applicationId: string, | ||
environmentId: string, | ||
deployId: string | ||
): Promise<unknown> { | ||
const deploymentUrl = | ||
'/orgs/' + | ||
organizationId + | ||
'/apps/' + | ||
applicationId + | ||
'/envs/' + | ||
environmentId + | ||
'/deploys/' + | ||
deployId; | ||
const result = await this.humctl.execute(['api', 'get', deploymentUrl]); | ||
if (result.stderr !== '') { | ||
throw new HumctlError( | ||
'humctl api get ' + deploymentUrl, | ||
result.stderr, | ||
result.exitcode | ||
); | ||
} | ||
return JSON.parse(result.stdout); | ||
} | ||
} |
Oops, something went wrong.