Skip to content

Commit

Permalink
fixup! fixup! Rework dashboard to work through JSON RPC protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ashumilova committed Aug 16, 2017
1 parent b06b037 commit fa772d2
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {CheAPI} from '../../../components/api/che-api.factory';
import {LoadFactoryService} from './load-factory.service';
import {CheNotification} from '../../../components/notification/che-notification.factory';
import {RouteHistory} from '../../../components/routing/route-history.service';
import {CheJsonRpcApi} from '../../../components/api/json-rpc/che-json-rpc-api.factory';
import {CheJsonRpcMasterApi} from '../../../components/api/json-rpc/che-json-rpc-master-api';

const WS_AGENT_STEP: number = 3

Expand All @@ -37,13 +39,14 @@ export class LoadFactoryController {
private projectsToImport: number;

private factory: che.IFactory;
private jsonRpcMasterApi: CheJsonRpcMasterApi;


/**
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor(cheAPI: CheAPI, $route: ng.route.IRouteService, $timeout: ng.ITimeoutService,
constructor(cheAPI: CheAPI, cheJsonRpcApi: CheJsonRpcApi, $route: ng.route.IRouteService, $timeout: ng.ITimeoutService,
$mdDialog: ng.material.IDialogService, loadFactoryService: LoadFactoryService, lodash: _.LoDashStatic, cheNotification: CheNotification,
$location: ng.ILocationService, routeHistory: RouteHistory, $window: ng.IWindowService) {
this.cheAPI = cheAPI;
Expand All @@ -59,6 +62,7 @@ export class LoadFactoryController {
this.workspaces = [];
this.workspace = {};
this.hideMenuAndFooter();
this.jsonRpcMasterApi = cheJsonRpcApi.getJsonRpcMasterApi(cheAPI.getWorkspace().getJsonRpcApiLocation());

this.loadFactoryService.resetLoadProgress();
this.loadFactoryService.setLoadFactoryInProgress(true);
Expand Down Expand Up @@ -368,7 +372,7 @@ export class LoadFactoryController {
console.log('Status channel of workspaceID', workspaceId, message);
};

this.cheAPI.getWorkspace().getMasterApi().subscribeEnvironmentStatus(workspaceId, environmentStatusHandler);
this.jsonRpcMasterApi.subscribeEnvironmentStatus(workspaceId, environmentStatusHandler);

let environmentOutputHandler = (message: any) => {
// skip displaying machine logs after workspace agent:
Expand All @@ -385,7 +389,7 @@ export class LoadFactoryController {

let machines = this.getMachineNames(data.config);
machines.forEach((machine: string) => {
this.cheAPI.getWorkspace().getMasterApi().subscribeEnvironmentOutput(workspaceId, machine, environmentOutputHandler);
this.jsonRpcMasterApi.subscribeEnvironmentOutput(workspaceId, machine, environmentOutputHandler);
});

let workspaceStatusHandler = (message: any) => {
Expand All @@ -406,7 +410,7 @@ export class LoadFactoryController {
}
};

this.cheAPI.getWorkspace().getMasterApi().subscribeWorkspaceStatus(workspaceId, workspaceStatusHandler);
this.jsonRpcMasterApi.subscribeWorkspaceStatus(workspaceId, workspaceStatusHandler);

let wsAgentHandler = (message: any) => {
if (this.loadFactoryService.getCurrentProgressStep() < WS_AGENT_STEP) {
Expand All @@ -420,7 +424,7 @@ export class LoadFactoryController {
}
};

this.cheAPI.getWorkspace().getMasterApi().subscribeWsAgentOutput(workspaceId, wsAgentHandler);
this.jsonRpcMasterApi.subscribeWsAgentOutput(workspaceId, wsAgentHandler);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {CreateProjectSvc} from './create-project.service';
import {CheNotification} from '../../../components/notification/che-notification.factory';
import {CheEnvironmentRegistry} from '../../../components/api/environment/che-environment-registry.factory';
import {IEnvironmentManagerMachine} from '../../../components/api/environment/environment-manager-machine';
import {CheJsonRpcApi} from '../../../components/api/json-rpc/che-json-rpc-api.factory';
import {CheJsonRpcMasterApi} from '../../../components/api/json-rpc/che-json-rpc-master-api';

/**
* This class is handling the controller for the projects
Expand All @@ -36,6 +38,7 @@ export class CreateProjectController {
cheAPI: CheAPI;
cheStack: CheStack;
cheEnvironmentRegistry: CheEnvironmentRegistry;
jsonRpcMasterApi: CheJsonRpcMasterApi;

stackMachines: any;
importProjectData: che.IImportProject;
Expand Down Expand Up @@ -86,7 +89,7 @@ export class CreateProjectController {
constructor($document: ng.IDocumentService, $filter: ng.IFilterService, $location: ng.ILocationService,
$log: ng.ILogService, $mdDialog: ng.material.IDialogService, $rootScope: che.IRootScopeService,
$routeParams: che.route.IRouteParamsService, $q: ng.IQService, $scope: ng.IScope,
$timeout: ng.ITimeoutService, $window: ng.IWindowService,
$timeout: ng.ITimeoutService, $window: ng.IWindowService, cheJsonRpcApi: CheJsonRpcApi,
lodash: any, cheAPI: CheAPI, cheStack: CheStack, createProjectSvc: CreateProjectSvc,
cheNotification: CheNotification, cheEnvironmentRegistry: CheEnvironmentRegistry) {
this.$log = $log;
Expand All @@ -109,6 +112,7 @@ export class CreateProjectController {
this.resetCreateProgress();

this.importProjectData = this.getDefaultProjectJson();
this.jsonRpcMasterApi = cheJsonRpcApi.getJsonRpcMasterApi(cheAPI.getWorkspace().getJsonRpcApiLocation());

this.enableWizardProject = true;

Expand Down Expand Up @@ -428,7 +432,7 @@ export class CreateProjectController {
}
};

this.cheAPI.getWorkspace().getMasterApi().subscribeWsAgentOutput(workspaceId, this.agentOutputHandler);
this.jsonRpcMasterApi.subscribeWsAgentOutput(workspaceId, this.agentOutputHandler);

this.statusHandler = (message: any) => {
message = this.getDisplayMachineLog(message);
Expand Down Expand Up @@ -463,7 +467,7 @@ export class CreateProjectController {
}
this.$log.log('Status channel of workspaceID', workspaceId, message);
};
this.cheAPI.getWorkspace().getMasterApi().subscribeEnvironmentStatus(workspaceId, this.statusHandler);
this.jsonRpcMasterApi.subscribeEnvironmentStatus(workspaceId, this.statusHandler);

this.environmentOutputHandler = (message: any) => {
message = this.getDisplayMachineLog(message);
Expand All @@ -476,7 +480,7 @@ export class CreateProjectController {

let machineNames = this.getMachineNames(workspace.config);
machineNames.forEach((machine: string) => {
this.cheAPI.getWorkspace().getMasterApi().subscribeEnvironmentOutput(workspaceId, machine, this.environmentOutputHandler);
this.jsonRpcMasterApi.subscribeEnvironmentOutput(workspaceId, machine, this.environmentOutputHandler);
});

let startWorkspacePromise = this.cheAPI.getWorkspace().startWorkspace(workspace.id, workspace.config.defaultEnv);
Expand Down Expand Up @@ -689,11 +693,11 @@ export class CreateProjectController {
*/
cleanupChannels(workspaceId: string, projectName: string): void {
let workspace = this.cheAPI.getWorkspace().getWorkspaceById(workspaceId);
this.cheAPI.getWorkspace().getMasterApi().unSubscribeEnvironmentStatus(workspaceId, this.statusHandler);
this.cheAPI.getWorkspace().getMasterApi().unSubscribeWsAgentOutput(workspaceId, this.agentOutputHandler);
this.jsonRpcMasterApi.unSubscribeEnvironmentStatus(workspaceId, this.statusHandler);
this.jsonRpcMasterApi.unSubscribeWsAgentOutput(workspaceId, this.agentOutputHandler);
let machineNames = this.getMachineNames(workspace.config);
machineNames.forEach((machine: string) => {
this.cheAPI.getWorkspace().getMasterApi().unSubscribeEnvironmentOutput(workspaceId, machine, this.environmentOutputHandler);
this.jsonRpcMasterApi.unSubscribeEnvironmentOutput(workspaceId, machine, this.environmentOutputHandler);
});
this.cheAPI.getWorkspace().getWorkspaceAgent(workspaceId).getWsAgentApi().unSubscribeProjectImport(projectName, this.projectImportHandler);
}
Expand Down
2 changes: 2 additions & 0 deletions dashboard/src/components/api/che-api-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {CheSsh} from './che-ssh.factory';
import {CheNamespaceRegistry} from './namespace/che-namespace-registry.factory';
import {CheUser} from './che-user.factory';
import {ChePageObject} from './paging-resource/page-object.factory';
import {CheJsonRpcApi} from './json-rpc/che-json-rpc-api.factory';

export class ApiConfig {

Expand Down Expand Up @@ -60,5 +61,6 @@ export class ApiConfig {
register.factory('cheNamespaceRegistry', CheNamespaceRegistry);
register.factory('cheUser', CheUser);
register.factory('chePageObject', ChePageObject);
register.factory('cheJsonRpcApi', CheJsonRpcApi);
}
}
2 changes: 1 addition & 1 deletion dashboard/src/components/api/che-workspace-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {CheGit} from './che-git';
import {CheSvn} from './che-svn';
import {CheProjectType} from './che-project-type';
import {CheTypeResolver} from './project/che-type-resolver';
import {CheJsonRpcWsagentApi} from './json-rpc/che-json-rpc-wsagent-api.factory';
import {CheJsonRpcWsagentApi} from './json-rpc/che-json-rpc-wsagent-api';
import {WebsocketClient} from './json-rpc/websocket-client';

interface IWorkspaceAgentData {
Expand Down
29 changes: 11 additions & 18 deletions dashboard/src/components/api/che-workspace.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {ComposeEnvironmentManager} from './environment/compose-environment-manag
import {DockerFileEnvironmentManager} from './environment/docker-file-environment-manager';
import {DockerImageEnvironmentManager} from './environment/docker-image-environment-manager';
import {CheEnvironmentRegistry} from './environment/che-environment-registry.factory';
import {CheWebsocket} from './che-websocket.factory';
import {CheJsonRpcMasterApi} from './json-rpc/che-json-rpc-master-api.factory';
import {WebsocketClient} from './json-rpc/websocket-client';
import {CheJsonRpcMasterApi} from './json-rpc/che-json-rpc-master-api';
import {CheJsonRpcApi} from './json-rpc/che-json-rpc-api.factory';

interface ICHELicenseResource<T> extends ng.resource.IResourceClass<T> {
create: any;
Expand Down Expand Up @@ -54,15 +53,15 @@ export class CheWorkspace {
private workspacesById: Map<string, che.IWorkspace>;
private remoteWorkspaceAPI: ICHELicenseResource<any>;
private lodash: any;
private cheWebsocket: CheWebsocket;
private statusDefers: Object;
private workspaceSettings: any;
private jsonRpcApiLocation: string;

/**
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor($resource: ng.resource.IResourceService, $http: ng.IHttpService, $q: ng.IQService, cheWebsocket: CheWebsocket,
constructor($resource: ng.resource.IResourceService, $http: ng.IHttpService, $q: ng.IQService, cheJsonRpcApi: CheJsonRpcApi,
$websocket: ng.websocket.IWebSocketProvider, $location: ng.ILocationService, proxySettings : string, userDashboardConfig: any,
lodash: any, cheEnvironmentRegistry: CheEnvironmentRegistry, $log: ng.ILogService) {
this.workspaceStatuses = ['RUNNING', 'STOPPED', 'PAUSED', 'STARTING', 'STOPPING', 'ERROR'];
Expand All @@ -74,7 +73,6 @@ export class CheWorkspace {
this.$log = $log;
this.$websocket = $websocket;
this.lodash = lodash;
this.cheWebsocket = cheWebsocket;

// current list of workspaces
this.workspaces = [];
Expand All @@ -94,6 +92,8 @@ export class CheWorkspace {
// list of subscribed to websocket workspace Ids
this.subscribedWorkspacesIds = [];
this.statusDefers = {};
this.jsonRpcApiLocation = this.formJsonRpcApiLocation($location, proxySettings, userDashboardConfig.developmentMode);
this.cheJsonRpcMasterApi = cheJsonRpcApi.getJsonRpcMasterApi(this.jsonRpcApiLocation);

// remote call
this.remoteWorkspaceAPI = <ICHELicenseResource<any>>this.$resource('/api/workspace', {}, {
Expand All @@ -116,9 +116,6 @@ export class CheWorkspace {
cheEnvironmentRegistry.addEnvironmentManager('dockerfile', new DockerFileEnvironmentManager($log));
cheEnvironmentRegistry.addEnvironmentManager('dockerimage', new DockerImageEnvironmentManager($log));

let websocketClient = new WebsocketClient($websocket, $q);
this.cheJsonRpcMasterApi = new CheJsonRpcMasterApi(websocketClient);
this.connectWsMasterApi($location, proxySettings, userDashboardConfig.developmentMode);
this.fetchWorkspaceSettings();
}

Expand Down Expand Up @@ -151,7 +148,7 @@ export class CheWorkspace {
wsAgentWebocketLink = wsAgentWebocketLink.replace('/api/ws', '');
}

let workspaceAgentData = {path: wsAgentLink.href, websocket: wsAgentWebocketLink, clientId: this.getMasterApi().getClientId()};
let workspaceAgentData = {path: wsAgentLink.href, websocket: wsAgentWebocketLink, clientId: this.cheJsonRpcMasterApi.getClientId()};
let wsagent: CheWorkspaceAgent = new CheWorkspaceAgent(this.$resource, this.$q, this.$websocket, workspaceAgentData);
this.workspaceAgents.set(workspaceId, wsagent);
return wsagent;
Expand Down Expand Up @@ -637,8 +634,8 @@ export class CheWorkspace {
return this.workspaceSettings ? this.workspaceSettings['che.workspace.auto_snapshot'] === 'true' : true;
}

public getMasterApi(): CheJsonRpcMasterApi {
return this.cheJsonRpcMasterApi;
getJsonRpcApiLocation(): string {
return this.jsonRpcApiLocation;
}

private updateWorkspacesList(workspace: che.IWorkspace): void {
Expand All @@ -653,7 +650,7 @@ export class CheWorkspace {
this.startUpdateWorkspaceStatus(workspace.id);
}

private connectWsMasterApi($location: ng.ILocationService, proxySettings : string, devmode: boolean): void {
private formJsonRpcApiLocation($location: ng.ILocationService, proxySettings : string, devmode: boolean): string {
let wsUrl;

if (devmode) {
Expand All @@ -664,10 +661,6 @@ export class CheWorkspace {
wsProtocol = 'http' === $location.protocol() ? 'ws' : 'wss';
wsUrl = wsProtocol + '://' + $location.host() + ':' + $location.port();
}
this.cheJsonRpcMasterApi.connect(wsUrl).then(() => {
this.$log.info('Connected to workspace master: ' + wsUrl);
}, (error: any) => {
this.$log.error('Failed to connect to workspace master: ' + wsUrl, error);
});
return wsUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* Copyright (c) 2015-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
* Red Hat, Inc. - initial API and implementation
*/
'use strict';
import {ICommunicationClient, JsonRpcClient} from './json-rpc-client';
Expand Down
48 changes: 48 additions & 0 deletions dashboard/src/components/api/json-rpc/che-json-rpc-api.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2015-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';

import {CheJsonRpcMasterApi} from './che-json-rpc-master-api';
import {WebsocketClient} from './websocket-client';

/**
* This class manages the api connection through JSON RPC.
*
* @author Ann Shumilova
*/
export class CheJsonRpcApi {
private $q: ng.IQService;
private $websocket: ng.websocket.IWebSocketProvider;
private $log: ng.ILogService;
private jsonRpcApiConnection: Map<string, CheJsonRpcMasterApi>;

/**
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor($q: ng.IQService, $websocket: ng.websocket.IWebSocketProvider, $log: ng.ILogService) {
this.$q = $q;
this.$websocket = $websocket;
this.$log = $log;
this.jsonRpcApiConnection = new Map<string, CheJsonRpcMasterApi>();
}

getJsonRpcMasterApi(entrypoint: string): CheJsonRpcMasterApi {
if (this.jsonRpcApiConnection.has(entrypoint)) {
return this.jsonRpcApiConnection.get(entrypoint);
} else {
let websocketClient = new WebsocketClient(this.$websocket, this.$q);
let cheJsonRpcMasterApi: CheJsonRpcMasterApi = new CheJsonRpcMasterApi(websocketClient, entrypoint);
this.jsonRpcApiConnection.set(entrypoint, cheJsonRpcMasterApi);
return cheJsonRpcMasterApi;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* Copyright (c) 2015-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
* Red Hat, Inc. - initial API and implementation
*/
'use strict';
import {CheJsonRpcApiClient, IChannel} from './che-json-rpc-api-service';
Expand All @@ -28,7 +28,7 @@ export class CheJsonRpcMasterApi {
private channels: Map<MasterChannels, IChannel>;
private clientId: string;

constructor (client: ICommunicationClient) {
constructor (client: ICommunicationClient, entrypoint: string) {
this.cheJsonRpcApi = new CheJsonRpcApiClient(client);

this.channels = new Map<MasterChannels, IChannel>();
Expand All @@ -55,6 +55,8 @@ export class CheJsonRpcMasterApi {
unsubscription: 'event:workspace-status:un-subscribe',
notification: 'event:workspace-status:changed'
});

this.connect(entrypoint);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* Copyright (c) 2015-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
* Red Hat, Inc. - initial API and implementation
*/
'use strict';
import {WebsocketClient} from './websocket-client';
import {CheJsonRpcApiClient, IChannel} from './che-json-rpc-api-service';
import {ICommunicationClient} from './json-rpc-client';

Expand Down
4 changes: 2 additions & 2 deletions dashboard/src/components/api/json-rpc/json-rpc-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* Copyright (c) 2015-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
* Red Hat, Inc. - initial API and implementation
*/
'use strict';

Expand Down
Loading

0 comments on commit fa772d2

Please sign in to comment.