You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2020/05/23 18:37:46 UTC

[cloudstack-primate] branch master updated: primate: enable dynamic routes on API discovery

This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-primate.git


The following commit(s) were added to refs/heads/master by this push:
     new b194ad2  primate: enable dynamic routes on API discovery
b194ad2 is described below

commit b194ad205b558803b7a1c0c8cc177e5b14e80835
Author: Rohit Yadav <ro...@shapeblue.com>
AuthorDate: Sun May 24 00:05:15 2020 +0530

    primate: enable dynamic routes on API discovery
    
    This would load Primate before API discovery finishes with some most
    commonly available routes and make it seem Primate loads faster. After
    login APIs are cached and further refreshes or opening views in another
    tab would be super quick and won't require API discovery until session
    expiry.
    
    Fixes #332
    
    Signed-off-by: Rohit Yadav <ro...@shapeblue.com>
---
 src/components/page/GlobalLayout.vue |  5 +++--
 src/config/router.js                 | 10 +++++-----
 src/permission.js                    |  4 ++--
 src/store/modules/permission.js      |  2 +-
 src/store/modules/user.js            | 25 +++++++++++++++++++++++--
 src/views/dashboard/Dashboard.vue    |  2 +-
 6 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/components/page/GlobalLayout.vue b/src/components/page/GlobalLayout.vue
index a37e0a5..0ddf934 100644
--- a/src/components/page/GlobalLayout.vue
+++ b/src/components/page/GlobalLayout.vue
@@ -44,7 +44,6 @@
         :collapsed="collapsed"
         :collapsible="true"></side-menu>
     </template>
-    <!-- 下次优化这些代码 -->
     <template v-else>
       <a-drawer
         v-if="isMobile()"
@@ -112,7 +111,6 @@ export default {
   },
   computed: {
     ...mapState({
-      // 主路由
       mainMenu: state => state.permission.addRouters
     }),
     contentPaddingLeft () {
@@ -128,6 +126,9 @@ export default {
   watch: {
     sidebarOpened (val) {
       this.collapsed = !val
+    },
+    mainMenu (newMenu) {
+      this.menus = newMenu.find((item) => item.path === '/').children
     }
   },
   created () {
diff --git a/src/config/router.js b/src/config/router.js
index 58bc558..3ca17e4 100644
--- a/src/config/router.js
+++ b/src/config/router.js
@@ -35,7 +35,7 @@ import config from '@/config/section/config'
 import quota from '@/config/section/plugin/quota'
 import cloudian from '@/config/section/plugin/cloudian'
 
-export function generateRouterMap (section) {
+function generateRouterMap (section) {
   var map = {
     name: section.name,
     path: '/' + section.name,
@@ -167,8 +167,8 @@ export function generateRouterMap (section) {
   return map
 }
 
-export const asyncRouterMap = [
-  {
+export function asyncRouterMap () {
+  return [{
     path: '/',
     name: 'index',
     component: BasicLayout,
@@ -255,8 +255,8 @@ export const asyncRouterMap = [
   },
   {
     path: '*', redirect: '/exception/404', hidden: true
-  }
-]
+  }]
+}
 
 export const constantRouterMap = [
   {
diff --git a/src/permission.js b/src/permission.js
index 22dab80..97aff53 100644
--- a/src/permission.js
+++ b/src/permission.js
@@ -43,8 +43,8 @@ router.beforeEach((to, from, next) => {
     } else {
       if (Object.keys(store.getters.apis).length === 0) {
         const cachedApis = Vue.ls.get(APIS, {})
-        if (Object.keys(cachedApis).length === 0) {
-          message.loading('Loading...', 4)
+        if (Object.keys(cachedApis).length > 0) {
+          message.loading('Loading...', 1.5)
         }
         store
           .dispatch('GetInfo')
diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js
index 1104a9f..3b080be 100644
--- a/src/store/modules/permission.js
+++ b/src/store/modules/permission.js
@@ -57,7 +57,7 @@ const permission = {
     GenerateRoutes ({ commit }, data) {
       return new Promise(resolve => {
         const apis = Object.keys(data.apis)
-        const accessedRouters = filterAsyncRouter(asyncRouterMap, apis)
+        const accessedRouters = filterAsyncRouter(asyncRouterMap(), apis)
         commit('SET_ROUTERS', accessedRouters)
         resolve()
       })
diff --git a/src/store/modules/user.js b/src/store/modules/user.js
index ad2c253..ad7497c 100644
--- a/src/store/modules/user.js
+++ b/src/store/modules/user.js
@@ -18,6 +18,9 @@
 import Cookies from 'js-cookie'
 import Vue from 'vue'
 import md5 from 'md5'
+import message from 'ant-design-vue/es/message'
+import router from '@/router'
+import store from '@/store'
 import { login, logout, api } from '@/api'
 import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS } from '@/store/mutation-types'
 
@@ -116,11 +119,25 @@ const user = {
     GetInfo ({ commit }) {
       return new Promise((resolve, reject) => {
         const cachedApis = Vue.ls.get(APIS, {})
-        if (Object.keys(cachedApis).length > 0) {
+        const hasAuth = Object.keys(cachedApis).length > 0
+        if (hasAuth) {
           console.log('Login detected, using cached APIs')
           commit('SET_APIS', cachedApis)
           resolve(cachedApis)
         } else {
+          // This will show the dashboard and some common navigation sections
+          // to most users/roles, while we complete API autodiscovery
+          const apis = {}
+          apis.listVirtualMachinesMetrics = {}
+          apis.listVolumesMetrics = {}
+          apis.listNetworks = {}
+          apis.listTemplates = {}
+          apis.listUsers = {}
+          apis.listAccounts = {}
+          commit('SET_APIS', apis)
+          resolve(apis)
+
+          const hide = message.loading('Discovering features...', 0)
           api('listApis').then(response => {
             const apis = {}
             const apiList = response.listapisresponse.api
@@ -133,7 +150,11 @@ const user = {
               }
             }
             commit('SET_APIS', apis)
-            resolve(apis)
+            store.dispatch('GenerateRoutes', { apis }).then(() => {
+              router.addRoutes(store.getters.addRouters)
+            })
+            hide()
+            message.success('Discovered all available features!')
           }).catch(error => {
             reject(error)
           })
diff --git a/src/views/dashboard/Dashboard.vue b/src/views/dashboard/Dashboard.vue
index c46ba82..0147b4f 100644
--- a/src/views/dashboard/Dashboard.vue
+++ b/src/views/dashboard/Dashboard.vue
@@ -17,7 +17,7 @@
 
 <template>
   <div class="page-header-index-wide">
-    <div v-if="showCapacityDashboard && !project">
+    <div v-if="$store.getters.userInfo.roletype === 'Admin' && !project">
       <capacity-dashboard/>
     </div>
     <div v-else>