Skip to content

Commit

Permalink
refactor: 集群首页重构 (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
HubuHito authored Oct 15, 2021
1 parent fc881f1 commit 6b1e622
Show file tree
Hide file tree
Showing 7 changed files with 649 additions and 1,074 deletions.
5 changes: 4 additions & 1 deletion bcs-app/frontend/src/api/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const fetchBizTopo = request('get', '/api/projects/$projectId/cc/topology
export const fetchBizHosts = request('post', '/api/projects/$projectId/cc/hosts/')
export const logLinks = request('post', '/api/datalog/projects/$projectId/log_links/')

export const fetchClusterList = request('get', '/api/projects/$projectId/clusters/')

export default {
stdLogs,
stdLogsDownload,
Expand Down Expand Up @@ -78,5 +80,6 @@ export default {
fetchBizTopo,
fetchBizHosts,
reschedulePod,
logLinks
logLinks,
fetchClusterList
}
15 changes: 0 additions & 15 deletions bcs-app/frontend/src/store/modules/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,6 @@ export default {
context.commit('updateCacheRes', res)
return res
},
async getClusterListNew (context, { projectId, cache = true }, config = {}) {
if (context.state.clusterList.length && Object.keys(context.state.cacheRes) && cache) {
delete context.state.cacheRes.request_id
return JSON.parse(JSON.stringify(context.state.cacheRes))
}
// return http.get('/app/cluster?invoke=getClusterList', {}, config)
const res = await http.get(
`${DEVOPS_BCS_API_URL}/api/projects/${projectId}/clusters?limit=1000`,
{},
Object.assign(config, { urlId: 'getClusterList' })
)
context.commit('forceUpdateClusterList', res?.data?.results || [])
context.commit('updateCacheRes', res)
return res
},

/**
* 根据项目 id 获取项目下有权限的集群
Expand Down
27 changes: 15 additions & 12 deletions bcs-app/frontend/src/views/cluster/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
.cluster-btns {
display: flex;
align-items: center;
padding: 20px 30px 20px;
padding: 20px 20px 0 20px;
}

.biz-cluster-list {
padding: 30px 0 30px 30px;
font-size: 0;
padding: 20px;
}

.biz-status-box {
margin-top: 10px;
margin-top: 20px;
}

.biz-cluster {
Expand Down Expand Up @@ -56,7 +55,12 @@
font-size: 16px;
font-weight: normal;
margin: 0 0 4px 0;

&.clickable {
cursor: pointer;
&:hover {
color: #3a84ff;
}
}
a {
color: $fontColor;

Expand All @@ -70,6 +74,8 @@
margin: 0;
font-size: 12px;
color: $fnMinorColor;
display: flex;
align-items: center;

.cluster-id {
@mixin ellipsis 200px;
Expand Down Expand Up @@ -226,16 +232,17 @@
}
}

.add-node-btn {
width: 100%;
}

.biz-progress-box,
.add-node-btn-wrapper {
margin-bottom: 28px;
}

.add-node-btn-wrapper {
padding: 0 24px;
.add-node-btn {
width: 100%;
}
}

.biz-log-box {
Expand Down Expand Up @@ -379,10 +386,6 @@
.biz-cluster-guide {
width: 320px;
}

.biz-cluster-list {
padding: 20px;
}
}

@media screen and (min-width: $mediaWidth) {
Expand Down
1,496 changes: 451 additions & 1,045 deletions bcs-app/frontend/src/views/cluster/index.vue

Large diffs are not rendered by default.

173 changes: 173 additions & 0 deletions bcs-app/frontend/src/views/cluster/use-cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* eslint-disable camelcase */
import { SetupContext, computed, ref, watch, Ref, set } from '@vue/composition-api'
import { fetchClusterList } from '@/api/base'
import useInterval from '@/views/dashboard/common/use-interval'

/**
* 获取集群列表
* @param ctx
* @returns
*/
export function useClusterList (ctx: SetupContext) {
const { $store } = ctx.root

const clusterList = ref<any[]>([])
const permissions = ref({})
const curProjectId = computed(() => {
return $store.state.curProjectId
})
// 获取集群列表
const getClusterList = async () => {
const res = await fetchClusterList({
$projectId: curProjectId.value
}, { needRes: true }).catch(() => ({ data: { results: [] } }))
clusterList.value = res.data.results
permissions.value = res.permissions
// 更新全局集群列表信息
$store.commit('cluster/forceUpdateClusterList', clusterList.value)
return res.data
}
// 开启轮询
const { start, stop } = useInterval(getClusterList, 5000)
const runningClusterIds = computed(() => {
return clusterList.value.filter(item => [
'initial_checking',
'initializing',
'removing',
'so_initializing',
'scheduling',
'upgrading',
'bke_installing'].includes(item.status)).map(item => item.cluster_id)
})
watch(runningClusterIds, (newValue, oldValue) => {
if (!newValue.length) {
stop()
} else if (newValue.sort().join() !== oldValue.sort().join()) {
start()
}
})

return {
clusterList,
permissions,
curProjectId,
getClusterList
}
}
export interface IOverviewMap {
[key: string]: {
cpu_usage: any;
disk_usage: any;
memory_usage: any;
};
}
/**
* 获取集群指标数据
* @param ctx
* @param clusterList
* @returns
*/
export function useClusterOverview (ctx: SetupContext, clusterList: Ref<any[]>) {
const { $store } = ctx.root

const clusterOverviewMap = ref<IOverviewMap>({})
// 获取当前集群的指标信息
const getClusterOverview = (clusterId) => {
if (!clusterOverviewMap.value[clusterId]) return null

return clusterOverviewMap.value[clusterId]
}
// 集群指标信息
const fetchClusterOverview = async (cluster) => {
const res = await $store.dispatch('cluster/clusterOverview', {
projectId: cluster.project_id,
clusterId: cluster.cluster_id
}).catch(() => ({ data: {} }))
set(clusterOverviewMap.value, cluster.cluster_id, res.data)
return res.data
}
const overviewCluster = computed(() => {
return clusterList.value.filter(item => item.status === 'normal'
&& !clusterOverviewMap.value?.[item.cluster_id])
})

watch(overviewCluster, () => {
overviewCluster.value.forEach(item => {
fetchClusterOverview(item)
})
})

return {
clusterOverviewMap,
getClusterOverview
}
}
/**
* 集群操作
* @param ctx
* @returns
*/
export function useClusterOperate (ctx: SetupContext) {
const { $store, $bkMessage, $i18n } = ctx.root
// 集群删除
const deleteCluster = async (cluster): Promise<boolean> => {
const result = await $store.dispatch('cluster/deleteCluster', {
projectId: cluster.project_id,
clusterId: cluster.cluster_id
}).catch(() => false)
result && $bkMessage({
theme: 'success',
message: $i18n.t('删除成功')
})
return result
}
// 任务下发成功提示
const successTips = () => {
$bkMessage({
theme: 'success',
message: $i18n.t('任务下发成功')
})
}
// 集群升级
const upgradeCluster = async (cluster, version: string): Promise<boolean> => {
const result = await $store.dispatch('cluster/upgradeCluster', {
projectId: cluster.project_id,
clusterId: cluster.cluster_id,
data: {
version,
operation: 'upgrade'
}
}).catch(() => false)
result && successTips()
return result
}
// 重新升级
const reUpgradeCluster = async (cluster): Promise<boolean> => {
const result = await $store.dispatch('cluster/upgradeCluster', {
projectId: cluster.project_id,
clusterId: cluster.cluster_id,
data: {
version: '',
operation: 'reupgrade'
}
}).catch(() => false)
result && successTips()
return result
}
// 重新初始化
const reInitializationCluster = async (cluster): Promise<boolean> => {
const result = await $store.dispatch('cluster/reInitializationCluster', {
projectId: cluster.project_id,
clusterId: cluster.cluster_id
}).catch(() => false)
result && successTips()
return result
}

return {
deleteCluster,
upgradeCluster,
reUpgradeCluster,
reInitializationCluster
}
}
7 changes: 6 additions & 1 deletion bcs-app/frontend/src/views/dashboard/common/use-interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function useIntervalFn (
immediate = false
): ITimeoutFnResult {
const isPending = ref(false)
const flag = ref(false)

const timer = ref<number | null>(null)

Expand All @@ -33,13 +34,15 @@ export default function useIntervalFn (

function stop () {
isPending.value = false
flag.value = false
clear()
}

function start (...args: unknown[]) {
clear()
if (!interval) return

flag.value = true
async function timerFn () {
// 上一个接口未执行完,不执行本次轮询
if (isPending.value) return
Expand All @@ -48,7 +51,9 @@ export default function useIntervalFn (
// eslint-disable-next-line standard/no-callback-literal
await cb(...args)
isPending.value = false
timer.value = setTimeout(timerFn, interval) as unknown as number
if (flag.value) {
timer.value = setTimeout(timerFn, interval) as unknown as number
}
}
timerFn()
}
Expand Down
Empty file.

0 comments on commit 6b1e622

Please sign in to comment.