Skip to content

Commit

Permalink
Fix workspaces for UD (#15784)
Browse files Browse the repository at this point in the history
* fix workspace list

Signed-off-by: Oleksii Orel <oorel@redhat.com>
  • Loading branch information
olexii4 authored Jan 23, 2020
1 parent 28b42e3 commit e8ab082
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class TemplateListController {
this.cheNotification = cheNotification;
this.devfileRegistry = devfileRegistry;
this.createWorkspaceSvc = createWorkspaceSvc;
this.devfileRegistryUrl = cheWorkspace.getWorkspaceSettings().cheWorkspaceDevfileRegistryUrl;

this.createButtonConfig = {
mainAction: {
Expand All @@ -102,10 +101,20 @@ export class TemplateListController {
}]
};

this.init();
cheWorkspace.fetchWorkspaceSettings().then(() => {
const workspaceSettings = cheWorkspace.getWorkspaceSettings();
this.devfileRegistryUrl = workspaceSettings && workspaceSettings.cheWorkspaceDevfileRegistryUrl;
this.init();
});
}

private init(): void {
if (!this.devfileRegistryUrl) {
const message = 'Failed to load the devfile registry URL.';
this.cheNotification.showError(message);
this.$log.error(message);
return;
}
this.isLoading = true;
this.devfileRegistry.fetchDevfiles(this.devfileRegistryUrl).then((devfiles: Array<IDevfileMetaData>) => {
this.devfiles = devfiles.map(devfile => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<!-- Template header -->
<div flex="100" layout="row" layout-align="start center" class="template-header">
<div flex>
<div class="header-title">Select a Template</div>
<div class="header-description">Use a sample template to create your first workspace.</div>
<div class="header-title">Select a Sample</div>
<div class="header-description">Use a sample to create your first workspace.</div>
</div>
<div flex class="template-search">
<div flex="100" layout="row">
Expand Down
11 changes: 5 additions & 6 deletions dashboard/src/app/get-started/template-list/template-list.styl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
.getting-started-toolbar
div.getting-started-toolbar
border-bottom 1px solid $very-light-grey-background-color
user-select none

md-toolbar
height: 110px;
height 110px
max-height 110px

div.che-toolbar-header div:last-child
bottom initial
Expand All @@ -28,15 +29,13 @@
line-height 40px !important

md-content.getting-started
background-color $very-light-grey-background-color
padding-left 10px

& > *
background-color $background-color

.getting-started-list
che-box-shadow()
margin 15px
border 15px solid $very-light-grey-background-color
border-left-width 25px

.template-header
margin 15px
Expand Down
31 changes: 20 additions & 11 deletions dashboard/src/app/navbar/navbar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export class CheNavBarController {
private isPermissionServiceAvailable: boolean;
private isKeycloackPresent: boolean;

private workspacesNumber: number;
private pageFactories: Array<che.IFactory>;

/**
* Default constructor
*/
Expand All @@ -92,6 +95,15 @@ export class CheNavBarController {
this.chePermissions = chePermissions;
this.cheKeycloak = cheKeycloak;
this.cheService = cheService;

const handler = (workspaces: Array<che.IWorkspace>) => {
this.workspacesNumber = workspaces.length;
};
this.cheAPI.getWorkspace().addListener('onChangeWorkspaces', handler);

$scope.$on('$destroy', () => {
this.cheAPI.getWorkspace().removeListener('onChangeWorkspaces', handler);
});
}

$onInit(): void {
Expand All @@ -105,8 +117,13 @@ export class CheNavBarController {
this.$scope.$broadcast('navbar-selected:set', path);
});

this.cheAPI.getWorkspace().fetchWorkspaces();
this.cheAPI.getFactory().fetchFactories();
this.cheAPI.getWorkspace().fetchWorkspaces().then((workspaces: Array<che.IWorkspace>) => {
this.workspacesNumber = workspaces.length;
});

this.cheAPI.getFactory().fetchFactories().then(() => {
this.pageFactories = this.cheAPI.getFactory().getPageFactories();
});

this.isPermissionServiceAvailable = false;
this.resolvePermissionServiceAvailability().then((isAvailable: boolean) => {
Expand Down Expand Up @@ -167,20 +184,12 @@ export class CheNavBarController {
return fullName ? fullName : email;
}

/**
* Returns number of workspaces.
* @return {number}
*/
getWorkspacesNumber(): number {
return this.cheAPI.getWorkspace().getWorkspaces().length;
}

/**
* Returns number of factories.
* @return {number}
*/
getFactoriesNumber(): number {
return this.cheAPI.getFactory().getPageFactories().length;
return this.pageFactories.length;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions dashboard/src/app/navbar/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<div class="navbar-item" layout="row" layout-align="start center" id="workspaces-item">
<md-icon md-font-icon="navbar-icon chefont cheico-workspace"></md-icon>
<span>Workspaces</span>
<span class="navbar-number" ng-show="navbarController.getWorkspacesNumber()">
&nbsp;({{navbarController.getWorkspacesNumber()}})
<span class="navbar-number" ng-show="navbarController.workspacesNumber">
&nbsp;({{navbarController.workspacesNumber}})
</span>
</div>
</md-button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,30 @@ export class NavbarRecentWorkspacesController {
this.dropdownItems = {};
this.dropdownItemTempl = [];

const handler = (workspaces: Array<che.IWorkspace>) => {
this.workspaces = workspaces;
this.updateRecentWorkspaces();
};
this.cheWorkspace.addListener('onChangeWorkspaces', handler);
$scope.$on('$destroy', () => {
this.cheWorkspace.removeListener('onChangeWorkspaces', handler);
});

let cleanup = $rootScope.$on('recent-workspace:set', (event: ng.IAngularEvent, workspaceId: string) => {
this.veryRecentWorkspaceId = workspaceId;
this.updateRecentWorkspaces();
});
$rootScope.$on('$destroy', () => {
cleanup();
});

$scope.$watch(() => {
return this.workspaces;
}, () => {
this.updateRecentWorkspaces();
}, true);
}

$onInit(): void {
this.workspaceCreationLink = this.cheBranding.getWorkspace().creationLink;

// get workspaces
this.workspaces = this.cheWorkspace.getWorkspaces();

// fetch workspaces when initializing
this.cheWorkspace.fetchWorkspaces();
this.cheWorkspace.fetchWorkspaces().then(() => {
this.workspaces = this.cheWorkspace.getWorkspaces();
});

this.updateRecentWorkspaces();
this.fetchWorkspaceSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ export class ListOrganizationWorkspacesController {

/**
* Filter workspaces by namespace.
*
* @returns {Array<che.IWorkspace>}
*/
filterWorkspacesByNamespace(): Array<che.IWorkspace> {
return this.lodash.filter(this.cheWorkspace.getWorkspaces(), (workspace: che.IWorkspace) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class ListWorkspacesCtrl {
// map of workspaces' used resources (consumed GBH):
this.workspaceUsedResources = new Map();

this.getUserWorkspaces();
this.fetchUserWorkspaces();

this.cheNamespaceRegistry.fetchNamespaces().then(() => {
this.namespaceLabels = this.getNamespaceLabelsList();
Expand Down Expand Up @@ -123,69 +123,26 @@ export class ListWorkspacesCtrl {
}

/**
* Fetch current user's workspaces (where he is a member):
* Fetch current user's workspaces.
*/
getUserWorkspaces(): void {
// fetch workspaces when initializing
fetchUserWorkspaces(): void {
const promise = this.cheAPI.getWorkspace().fetchWorkspaces();

promise.then(() => {
return this.updateSharedWorkspaces();
this.userWorkspaces = this.cheAPI.getWorkspace().getWorkspaces();
return this.$q.resolve();
}, (error: any) => {
if (error && error.status === 304) {
// ok
return this.updateSharedWorkspaces();
this.userWorkspaces = this.cheAPI.getWorkspace().getWorkspaces();
return this.$q.resolve();
}
this.state = 'error';
this.isInfoLoading = false;
return this.$q.reject(error);
}).then(() => {
this.cheListHelper.setList(this.userWorkspaces, 'id');
});
}

/**
* Update the info of all user workspaces:
*
* @return {IPromise<any>}
*/
updateSharedWorkspaces(): ng.IPromise<any> {
this.userWorkspaces = [];
let workspaces = this.cheAPI.getWorkspace().getWorkspaces();
if (workspaces.length === 0) {
}).finally(()=> {
this.isInfoLoading = false;
}
const promises: Array<ng.IPromise<any>> = [];
workspaces.forEach((workspace: che.IWorkspace) => {
// first check the list of already received workspace info:
if (!this.workspacesById.get(workspace.id)) {
const promise = this.cheWorkspace.fetchWorkspaceDetails(workspace.id)
.catch((error: any) => {
if (error && error.status === 304) {
return this.$q.when();
}
let message = error.data && error.data.message ? ' Reason: ' + error.data.message : '';
let workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
this.cheNotification.showError('Failed to retrieve workspace ' + workspaceName + ' data.' + message) ;
return this.$q.reject(error);
})
.then(() => {
let userWorkspace = this.cheAPI.getWorkspace().getWorkspaceById(workspace.id);
this.getWorkspaceInfo(userWorkspace);
this.userWorkspaces.push(userWorkspace);
return this.$q.when();
});
promises.push(promise);
} else {
let userWorkspace = this.workspacesById.get(workspace.id);
this.userWorkspaces.push(userWorkspace);
this.isInfoLoading = false;
}
});

this.state = 'loaded';

return this.$q.all(promises);
}

/**
Expand All @@ -199,39 +156,13 @@ export class ListWorkspacesCtrl {
});
}

/**
* Gets all necessary workspace info to be displayed.
*
* @param {che.IWorkspace} workspace
*/
getWorkspaceInfo(workspace: che.IWorkspace): void {
let promises = [];
this.workspacesById.set(workspace.id, workspace);

workspace.isLocked = false;
workspace.usedResources = this.workspaceUsedResources.get(workspace.id);

// no access to runner resources if workspace is locked:
if (!workspace.isLocked) {
let promiseWorkspace = this.cheAPI.getWorkspace().fetchWorkspaceDetails(workspace.id);
promises.push(promiseWorkspace);
}

this.$q.all(promises).finally(() => {
this.isInfoLoading = false;
});
}

/**
* Delete all selected workspaces
*/
deleteSelectedWorkspaces(): void {
const selectedWorkspaces = this.cheListHelper.getSelectedItems(),
selectedWorkspacesIds = selectedWorkspaces.map((workspace: che.IWorkspace) => {
return workspace.id;
});
const selectedWorkspaces = this.cheListHelper.getSelectedItems();

let queueLength = selectedWorkspacesIds.length;
let queueLength = selectedWorkspaces.length;
if (!queueLength) {
this.cheNotification.showError('No such workspace.');
return;
Expand All @@ -244,26 +175,22 @@ export class ListWorkspacesCtrl {
let deleteWorkspacePromises = [];
let workspaceName;

selectedWorkspacesIds.forEach((workspaceId: string) => {
this.cheListHelper.itemsSelectionStatus[workspaceId] = false;
selectedWorkspaces.forEach((workspace: che.IWorkspace) => {
this.cheListHelper.itemsSelectionStatus[workspace.id] = false;

let workspace = this.cheWorkspace.getWorkspaceById(workspaceId);
if (!workspace) {
return;
}
workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
let stoppedStatusPromise = this.cheWorkspace.fetchStatusChange(workspaceId, 'STOPPED');
let stoppedStatusPromise = this.cheWorkspace.fetchStatusChange(workspace.id, 'STOPPED');

// stop workspace if it's status is RUNNING
if (workspace.status === 'RUNNING') {
this.cheWorkspace.stopWorkspace(workspaceId);
this.cheWorkspace.stopWorkspace(workspace.id);
}

// delete stopped workspace
let promise = stoppedStatusPromise.then(() => {
return this.cheWorkspace.deleteWorkspace(workspaceId);
return this.cheWorkspace.deleteWorkspace(workspace.id);
}).then(() => {
this.workspacesById.delete(workspaceId);
this.workspacesById.delete(workspace.id);
queueLength--;
},
(error: any) => {
Expand All @@ -274,7 +201,7 @@ export class ListWorkspacesCtrl {
});

this.$q.all(deleteWorkspacePromises).finally(() => {
this.getUserWorkspaces();
this.fetchUserWorkspaces();

if (isError) {
this.cheNotification.showError('Delete failed.');
Expand Down
Loading

0 comments on commit e8ab082

Please sign in to comment.