Skip to content

Commit 53e26b4

Browse files
committed
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 <rohit.yadav@shapeblue.com>
1 parent 0d955ff commit 53e26b4

6 files changed

Lines changed: 35 additions & 13 deletions

File tree

ui/src/components/page/GlobalLayout.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
:collapsed="collapsed"
4545
:collapsible="true"></side-menu>
4646
</template>
47-
<!-- 下次优化这些代码 -->
4847
<template v-else>
4948
<a-drawer
5049
v-if="isMobile()"
@@ -112,7 +111,6 @@ export default {
112111
},
113112
computed: {
114113
...mapState({
115-
// 主路由
116114
mainMenu: state => state.permission.addRouters
117115
}),
118116
contentPaddingLeft () {
@@ -128,6 +126,9 @@ export default {
128126
watch: {
129127
sidebarOpened (val) {
130128
this.collapsed = !val
129+
},
130+
mainMenu (newMenu) {
131+
this.menus = newMenu.find((item) => item.path === '/').children
131132
}
132133
},
133134
created () {

ui/src/config/router.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import config from '@/config/section/config'
3535
import quota from '@/config/section/plugin/quota'
3636
import cloudian from '@/config/section/plugin/cloudian'
3737

38-
export function generateRouterMap (section) {
38+
function generateRouterMap (section) {
3939
var map = {
4040
name: section.name,
4141
path: '/' + section.name,
@@ -167,8 +167,8 @@ export function generateRouterMap (section) {
167167
return map
168168
}
169169

170-
export const asyncRouterMap = [
171-
{
170+
export function asyncRouterMap () {
171+
return [{
172172
path: '/',
173173
name: 'index',
174174
component: BasicLayout,
@@ -255,8 +255,8 @@ export const asyncRouterMap = [
255255
},
256256
{
257257
path: '*', redirect: '/exception/404', hidden: true
258-
}
259-
]
258+
}]
259+
}
260260

261261
export const constantRouterMap = [
262262
{

ui/src/permission.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ router.beforeEach((to, from, next) => {
4343
} else {
4444
if (Object.keys(store.getters.apis).length === 0) {
4545
const cachedApis = Vue.ls.get(APIS, {})
46-
if (Object.keys(cachedApis).length === 0) {
47-
message.loading('Loading...', 4)
46+
if (Object.keys(cachedApis).length > 0) {
47+
message.loading('Loading...', 1.5)
4848
}
4949
store
5050
.dispatch('GetInfo')

ui/src/store/modules/permission.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const permission = {
5757
GenerateRoutes ({ commit }, data) {
5858
return new Promise(resolve => {
5959
const apis = Object.keys(data.apis)
60-
const accessedRouters = filterAsyncRouter(asyncRouterMap, apis)
60+
const accessedRouters = filterAsyncRouter(asyncRouterMap(), apis)
6161
commit('SET_ROUTERS', accessedRouters)
6262
resolve()
6363
})

ui/src/store/modules/user.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import Cookies from 'js-cookie'
1919
import Vue from 'vue'
2020
import md5 from 'md5'
21+
import message from 'ant-design-vue/es/message'
22+
import router from '@/router'
23+
import store from '@/store'
2124
import { login, logout, api } from '@/api'
2225
import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS } from '@/store/mutation-types'
2326

@@ -116,11 +119,25 @@ const user = {
116119
GetInfo ({ commit }) {
117120
return new Promise((resolve, reject) => {
118121
const cachedApis = Vue.ls.get(APIS, {})
119-
if (Object.keys(cachedApis).length > 0) {
122+
const hasAuth = Object.keys(cachedApis).length > 0
123+
if (hasAuth) {
120124
console.log('Login detected, using cached APIs')
121125
commit('SET_APIS', cachedApis)
122126
resolve(cachedApis)
123127
} else {
128+
// This will show the dashboard and some common navigation sections
129+
// to most users/roles, while we complete API autodiscovery
130+
const apis = {}
131+
apis.listVirtualMachinesMetrics = {}
132+
apis.listVolumesMetrics = {}
133+
apis.listNetworks = {}
134+
apis.listTemplates = {}
135+
apis.listUsers = {}
136+
apis.listAccounts = {}
137+
commit('SET_APIS', apis)
138+
resolve(apis)
139+
140+
const hide = message.loading('Discovering features...', 0)
124141
api('listApis').then(response => {
125142
const apis = {}
126143
const apiList = response.listapisresponse.api
@@ -133,7 +150,11 @@ const user = {
133150
}
134151
}
135152
commit('SET_APIS', apis)
136-
resolve(apis)
153+
store.dispatch('GenerateRoutes', { apis }).then(() => {
154+
router.addRoutes(store.getters.addRouters)
155+
})
156+
hide()
157+
message.success('Discovered all available features!')
137158
}).catch(error => {
138159
reject(error)
139160
})

ui/src/views/dashboard/Dashboard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<template>
1919
<div class="page-header-index-wide">
20-
<div v-if="showCapacityDashboard && !project">
20+
<div v-if="$store.getters.userInfo.roletype === 'Admin' && !project">
2121
<capacity-dashboard/>
2222
</div>
2323
<div v-else>

0 commit comments

Comments
 (0)