-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathindex.vue
More file actions
165 lines (144 loc) · 4.47 KB
/
index.vue
File metadata and controls
165 lines (144 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<script lang="ts">
import Card from '@shell/components/Resource/Detail/Card/index.vue';
import Scaler from '@shell/components/Resource/Detail/Card/Scaler.vue';
import VerticalGap from '@shell/components/Resource/Detail/Card/VerticalGap.vue';
import StatusBar from '@shell/components/Resource/Detail/StatusBar.vue';
import StatusRow from '@shell/components/Resource/Detail/StatusRow.vue';
import { useI18n } from '@shell/composables/useI18n';
import { StateColor } from '@shell/utils/style';
import { computed } from 'vue';
import { useStore } from 'vuex';
import type { RouteLocationRaw } from 'vue-router';
export interface Props {
title: string;
resources?: any[];
showScaling?: boolean;
showPercent?: boolean;
noResourcesMessage?: string;
to?: RouteLocationRaw;
rowTo?: RouteLocationRaw | string;
}
</script>
<script setup lang="ts">
const store = useStore();
const i18n = useI18n(store);
const props = withDefaults(defineProps<Props>(), {
resources: undefined,
showScaling: false,
showPercent: true,
noResourcesMessage: undefined,
to: undefined,
rowTo: undefined,
});
const emit = defineEmits(['decrease', 'increase']);
const segmentAccumulator = computed(() => {
interface Value {
count: number;
}
const accumulator: {[key in StateColor]?: Value} = {};
props.resources?.forEach((resource: any) => {
const color: StateColor = resource.stateSimpleColor;
accumulator[color] = accumulator[color] || { count: 0 };
accumulator[color].count += resource.count || 1;
});
return accumulator;
});
const rowAccumulator = computed(() => {
interface Value {
count: number;
color: StateColor;
stateId: string;
}
const accumulator: {[key in string]: Value} = {};
props.resources?.forEach((resource: any) => {
accumulator[resource.stateDisplay] = accumulator[resource.stateDisplay] || { count: 0, stateId: resource.stateId || resource.stateDisplay };
accumulator[resource.stateDisplay].count += resource.count || 1;
accumulator[resource.stateDisplay].color = resource.stateSimpleColor.replace('text-', '') as StateColor;
});
return accumulator;
});
const percent = (count: number, total: number) => {
return count / total * 100;
};
const count = computed(() => {
if (!props.resources?.length) {
return 0;
}
return props.resources.reduce((sum: number, r: any) => sum + (r.count || 1), 0);
});
const segmentColors = computed(() => Object.keys(segmentAccumulator.value) as StateColor[]);
const segments = computed(() => segmentColors.value.map((color: StateColor) => ({
color,
percent: percent(segmentAccumulator.value[color]?.count || 0, count.value)
})));
const rowStates = computed(() => {
return Object.keys(rowAccumulator.value);
});
const rows = computed(() => {
return rowStates.value.map((state) => ({
color: rowAccumulator.value[state].color,
label: state,
stateId: rowAccumulator.value[state].stateId,
count: rowAccumulator.value[state].count,
percent: percent(rowAccumulator.value[state].count, count.value)
}));
});
function rowRoute(stateId: string): RouteLocationRaw | undefined {
if (!props.rowTo || typeof props.rowTo === 'string') {
return undefined;
}
return { ...props.rowTo, query: { q: `"metadata.state.name":"${ stateId }"` } };
}
</script>
<template>
<Card
:title="title"
data-testid="resource-detail-status-card"
:to="props.to"
>
<template
v-if="props.showScaling"
#heading-action
>
<Scaler
:ariaResourceName="i18n.t('component.resource.detail.card.podsCard.ariaResourceName')"
:value="count"
:min="0"
@increase="(newValue) => emit('increase', newValue)"
@decrease="(newValue) => emit('decrease', newValue)"
/>
</template>
<StatusBar
v-if="rows.length > 0"
:segments="segments"
/>
<VerticalGap v-if="rows.length > 0" />
<div
v-if="rows.length > 0"
class="pod-distribution"
>
<StatusRow
v-for="(row, i) in rows"
:key="i"
:color="row.color"
:label="row.label"
:count="row.count"
:percent="row.percent"
:showPercent="props.showPercent"
:to="rowRoute(row.stateId)"
/>
</div>
<div
v-else-if="props.noResourcesMessage"
class="text-deemphasized"
>
{{ props.noResourcesMessage }}
</div>
</Card>
</template>
<style lang="scss" scoped>
.pod-distribution {
display: flex;
flex-direction: column;
}
</style>