@mixd-id/web-scaffold 0.2.250801012 → 0.2.250801014
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/components/AIDashboard.md +472 -0
- package/docs/components/WebPageBuilder.md +125 -0
- package/package.json +8 -30
- package/src/components/AIChat.vue +479 -0
- package/src/components/AIChatItem.vue +168 -0
- package/src/components/AIChatTextbox.vue +56 -0
- package/src/components/AIDashboard.vue +1113 -0
- package/src/components/AccountIcon.vue +19 -0
- package/src/components/Ahref.vue +200 -0
- package/src/components/Alert.vue +257 -0
- package/src/components/Article.vue +387 -0
- package/src/components/BarChart.vue +87 -0
- package/src/components/Block.vue +31 -0
- package/src/components/Button.vue +258 -0
- package/src/components/Card.vue +255 -0
- package/src/components/Carousel.vue +404 -0
- package/src/components/Cart.vue +192 -0
- package/src/components/CartIcon.vue +89 -0
- package/src/components/Chart.vue +486 -0
- package/src/components/ChatTyping.vue +69 -0
- package/src/components/Checkbox.vue +200 -0
- package/src/components/CodeEditor.vue +158 -0
- package/src/components/CollapsiblePanel.vue +70 -0
- package/src/components/ColorPicker.vue +653 -0
- package/src/components/Confirm.vue +180 -0
- package/src/components/ContextMenu.vue +265 -0
- package/src/components/ContextMenuItem.vue +53 -0
- package/src/components/CopyToClipboard.vue +34 -0
- package/src/components/Countdown.vue +264 -0
- package/src/components/Dashboard.vue +1151 -0
- package/src/components/Datepicker.vue +412 -0
- package/src/components/Datetimepicker.vue +355 -0
- package/src/components/DoughnutChart.vue +68 -0
- package/src/components/Dropdown.vue +314 -0
- package/src/components/FilterCard.vue +177 -0
- package/src/components/Flex.vue +44 -0
- package/src/components/Gmaps.vue +251 -0
- package/src/components/Grid.vue +35 -0
- package/src/components/HTMLEditor.vue +786 -0
- package/src/components/IconMenu2.vue +89 -0
- package/src/components/Image.vue +311 -0
- package/src/components/ImagePreview.vue +93 -0
- package/src/components/ImageUploader.vue +111 -0
- package/src/components/LineChart.vue +104 -0
- package/src/components/Link.vue +95 -0
- package/src/components/List.vue +1549 -0
- package/src/components/ListItem.vue +160 -0
- package/src/components/MarkdownEdit.vue +346 -0
- package/src/components/MarkdownPreview.vue +102 -0
- package/src/components/MenuItem1.vue +36 -0
- package/src/components/Metric.vue +105 -0
- package/src/components/MultiDropdown.vue +124 -0
- package/src/components/OTPField.vue +139 -0
- package/src/components/Paragraph.vue +40 -0
- package/src/components/PieChart.vue +67 -0
- package/src/components/PresetSelectorFilterItem.vue +204 -0
- package/src/components/Radio.vue +109 -0
- package/src/components/SearchModal.vue +153 -0
- package/src/components/Slider.vue +407 -0
- package/src/components/Svg.vue +33 -0
- package/src/components/SvgEditor.vue +173 -0
- package/src/components/Switch.vue +101 -0
- package/src/components/Table.vue +56 -0
- package/src/components/Tabs.vue +131 -0
- package/src/components/Testimonial.vue +86 -0
- package/src/components/Text.vue +34 -0
- package/src/components/TextBlock.vue +50 -0
- package/src/components/TextWithTag.vue +298 -0
- package/src/components/Textarea.vue +195 -0
- package/src/components/Textbox.vue +216 -0
- package/src/components/Thumbnail.vue +27 -0
- package/src/components/Timepicker.vue +223 -0
- package/src/components/Toast.vue +117 -0
- package/src/components/TreeMenu.vue +122 -0
- package/src/components/TreeView.vue +107 -0
- package/src/components/TreeViewItem.vue +452 -0
- package/src/components/Uploader.vue +45 -0
- package/src/components/Video.vue +119 -0
- package/src/components/VirtualGrid.vue +296 -0
- package/src/components/VirtualTable.vue +1047 -0
- package/src/components/YoutubeVideo.vue +196 -0
- package/src/configs/web-page-builder.js +118 -0
- package/src/directives/intersect.js +26 -0
- package/src/hooks/device.js +14 -0
- package/src/index.css +3 -0
- package/src/index.js +635 -0
- package/src/instructions/dashboard.js +203 -0
- package/src/mixin/component.js +306 -0
- package/src/themes/default/index.js +303 -0
- package/src/utils/ai-chat-answer.mjs +240 -0
- package/src/utils/ai-chat.js +167 -0
- package/src/utils/ai-dashboard-chat.mjs +371 -0
- package/src/utils/ai-dashboard.js +321 -0
- package/src/utils/ai-list-chat.mjs +407 -0
- package/src/utils/dashboard.js +317 -0
- package/src/utils/helpers.js +635 -0
- package/src/utils/helpers.mjs +722 -0
- package/src/utils/list.mjs +1564 -0
- package/src/utils/preset-selector.mjs +390 -0
- package/src/utils/selection.js +43 -0
- package/src/utils/wss.js +311 -0
- package/src/utils/wss.mjs +286 -0
- package/src/widgets/AhrefSetting.vue +97 -0
- package/src/widgets/ArticleSetting.vue +457 -0
- package/src/widgets/BackgroundColorSetting.vue +149 -0
- package/src/widgets/BlockSetting.vue +49 -0
- package/src/widgets/BorderColorSetting.vue +53 -0
- package/src/widgets/ButtonSetting.vue +113 -0
- package/src/widgets/CarouselSetting.vue +181 -0
- package/src/widgets/CartSetting.vue +46 -0
- package/src/widgets/CollapsiblePanelSetting.vue +46 -0
- package/src/widgets/ColumnSelector.vue +93 -0
- package/src/widgets/ComponentSetting.vue +2220 -0
- package/src/widgets/ComponentSetting2.vue +927 -0
- package/src/widgets/ContactForm.vue +193 -0
- package/src/widgets/ContactFormSetting.vue +395 -0
- package/src/widgets/CountdownSetting.vue +76 -0
- package/src/widgets/DataListSetting.vue +102 -0
- package/src/widgets/EmbeddedVideo.vue +60 -0
- package/src/widgets/EmbeddedVideoSetting.vue +50 -0
- package/src/widgets/FAQ.vue +98 -0
- package/src/widgets/FAQSetting.vue +94 -0
- package/src/widgets/FeatureList.vue +131 -0
- package/src/widgets/FeatureListSetting.vue +233 -0
- package/src/widgets/FlexSetting.vue +174 -0
- package/src/widgets/GridSetting.vue +165 -0
- package/src/widgets/Header2.vue +65 -0
- package/src/widgets/Header2Setting.vue +193 -0
- package/src/widgets/IconList.vue +51 -0
- package/src/widgets/IconListSetting.vue +188 -0
- package/src/widgets/ImageSetting.vue +119 -0
- package/src/widgets/LLMChat.vue +327 -0
- package/src/widgets/LinkSetting.vue +209 -0
- package/src/widgets/LinkSettingModal.vue +173 -0
- package/src/widgets/MenuItem1Setting.vue +78 -0
- package/src/widgets/ModalSetting.vue +94 -0
- package/src/widgets/MultiValueSetting.vue +121 -0
- package/src/widgets/MultiValueSetting2.vue +147 -0
- package/src/widgets/OGSettingModal.vue +103 -0
- package/src/widgets/ParagraphSetting.vue +89 -0
- package/src/widgets/PositionSetting.vue +209 -0
- package/src/widgets/PresetBar.vue +1011 -0
- package/src/widgets/PresetBarPivot.vue +330 -0
- package/src/widgets/PresetBarPivotColumnEdit.vue +186 -0
- package/src/widgets/PresetBarPivotValueEdit.vue +172 -0
- package/src/widgets/PresetSelector.vue +713 -0
- package/src/widgets/Review.vue +131 -0
- package/src/widgets/ReviewSetting.vue +201 -0
- package/src/widgets/SearchModalSetting.vue +70 -0
- package/src/widgets/Share.vue +164 -0
- package/src/widgets/ShareSetting.vue +99 -0
- package/src/widgets/SvgSetting.vue +133 -0
- package/src/widgets/TableSetting.vue +172 -0
- package/src/widgets/TestimonialSetting.vue +161 -0
- package/src/widgets/TextBlockSetting.vue +81 -0
- package/src/widgets/ThumbnailSetting.vue +49 -0
- package/src/widgets/WebComponentSelector.vue +205 -0
- package/src/widgets/WebDatasourceSelector.vue +153 -0
- package/src/widgets/WebLayoutSelector.vue +139 -0
- package/src/widgets/WebPageBuilder.vue +1962 -0
- package/src/widgets/YoutubeVideoSetting.vue +68 -0
- package/docs/components/ArrayList.md +0 -39
- package/docs/components/Box.md +0 -15
- package/docs/components/ChartBar.md +0 -34
- package/docs/components/DayTimeRange.md +0 -20
- package/docs/components/ErrorText.md +0 -15
- package/docs/components/FBLogin.md +0 -21
- package/docs/components/GHeatMaps.md +0 -16
- package/docs/components/GSignIn.md +0 -21
- package/docs/components/GmapsDirection.md +0 -20
- package/docs/components/IconMenu.md +0 -20
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
const { dashboardInstruction, parseDashboardResponse } = require('../instructions/dashboard')
|
|
2
|
+
|
|
3
|
+
const COLORS = [
|
|
4
|
+
'#5D9CEC', '#A0D468', '#FFCE54', '#FC6E51', '#48CFAD',
|
|
5
|
+
'#AC92EC', '#4FC1E9', '#ED5565', '#EC87C0', '#CCD1D9',
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
// ── Mixin methods ─────────────────────────────────────────────────────────────
|
|
9
|
+
// Expects the host class to have: this.model, this.conn
|
|
10
|
+
|
|
11
|
+
const DashboardMixin = {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Return metadata for a single datasource.
|
|
15
|
+
* Override in a controller to return different columns per datasource path or model.
|
|
16
|
+
* @param {{ datasource: Object }} params
|
|
17
|
+
* @returns {{ columns: Array<{ key: string, label: string, type: string }> }}
|
|
18
|
+
*/
|
|
19
|
+
async preAnalyze({ datasource }) {
|
|
20
|
+
const rawAttrs = this.model?.rawAttributes ?? {}
|
|
21
|
+
const columns = Object.entries(rawAttrs).map(([key, attr]) => {
|
|
22
|
+
const col = {
|
|
23
|
+
key,
|
|
24
|
+
label: attr.comment ?? key.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()),
|
|
25
|
+
type: attr.type?.key ?? String(attr.type ?? 'UNKNOWN'),
|
|
26
|
+
}
|
|
27
|
+
if (col.type === 'ENUM' && Array.isArray(attr.type?.values)) {
|
|
28
|
+
col.values = attr.type.values
|
|
29
|
+
}
|
|
30
|
+
return col
|
|
31
|
+
})
|
|
32
|
+
return { columns }
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Route to the correct handler based on type.
|
|
37
|
+
* Accepts the flat object emitted by Dashboard's dataFn.
|
|
38
|
+
* @param {{ type: string, path: string, filters: Object, xAxes: string[], xModifier: string,
|
|
39
|
+
* yAxes: string[], yModifier: string, columns: Object[], aggregation: string,
|
|
40
|
+
* field: string, page: number, pageSize: number }} params
|
|
41
|
+
*/
|
|
42
|
+
async analyze(params) {
|
|
43
|
+
switch (params.type) {
|
|
44
|
+
case 'BarChart': return this.analyzeBarChart(params)
|
|
45
|
+
case 'LineChart': return this.analyzeLineChart(params)
|
|
46
|
+
case 'PieChart': return this.analyzePieChart(params)
|
|
47
|
+
case 'DoughnutChart': return this.analyzeDoughnutChart(params)
|
|
48
|
+
case 'VirtualTable': return this.analyzeVirtualTable(params)
|
|
49
|
+
case 'Metric': return this.analyzeMetric(params)
|
|
50
|
+
default: return null
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
// ── Shared: build Sequelize where clause from filter array ─────────────────
|
|
55
|
+
|
|
56
|
+
_buildWhere(filters) {
|
|
57
|
+
const { Op } = this.Sequelize
|
|
58
|
+
const opMap = {
|
|
59
|
+
'=': Op.eq,
|
|
60
|
+
'!=': Op.ne,
|
|
61
|
+
'>': Op.gt,
|
|
62
|
+
'<': Op.lt,
|
|
63
|
+
'>=': Op.gte,
|
|
64
|
+
'<=': Op.lte,
|
|
65
|
+
'like': Op.like,
|
|
66
|
+
}
|
|
67
|
+
const list = Array.isArray(filters) ? filters : (filters?.filters ?? [])
|
|
68
|
+
const where = {}
|
|
69
|
+
for (const f of list) {
|
|
70
|
+
if (!f.field || f.value === '' || f.value === null || f.value === undefined) continue
|
|
71
|
+
const op = opMap[f.operator] ?? Op.eq
|
|
72
|
+
where[f.field] = { [op]: f.operator === 'like' ? `%${f.value}%` : f.value }
|
|
73
|
+
}
|
|
74
|
+
return where
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
// ── Metric ─────────────────────────────────────────────────────────────────
|
|
78
|
+
// Returns: { value: Number }
|
|
79
|
+
|
|
80
|
+
async analyzeMetric({ aggregation = 'count', field, filters = [] }) {
|
|
81
|
+
const { fn, col } = this.conn
|
|
82
|
+
const where = this._buildWhere(filters)
|
|
83
|
+
|
|
84
|
+
switch (aggregation) {
|
|
85
|
+
case 'count':
|
|
86
|
+
return { value: await this.model.count({ where }) }
|
|
87
|
+
|
|
88
|
+
case 'sum':
|
|
89
|
+
return { value: await this.model.sum(field, { where }) ?? 0 }
|
|
90
|
+
|
|
91
|
+
case 'avg': {
|
|
92
|
+
const [row] = await this.model.findAll({
|
|
93
|
+
attributes: [[ fn('AVG', col(field)), 'result' ]],
|
|
94
|
+
where,
|
|
95
|
+
raw: true,
|
|
96
|
+
})
|
|
97
|
+
return { value: parseFloat(row?.result ?? 0) }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
case 'min':
|
|
101
|
+
return { value: await this.model.min(field, { where }) ?? 0 }
|
|
102
|
+
|
|
103
|
+
case 'max':
|
|
104
|
+
return { value: await this.model.max(field, { where }) ?? 0 }
|
|
105
|
+
|
|
106
|
+
default:
|
|
107
|
+
return { value: 0 }
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
// ── Bar Chart ──────────────────────────────────────────────────────────────
|
|
112
|
+
// Returns: { labels: string[], datasets: [{ label, data, backgroundColor }] }
|
|
113
|
+
|
|
114
|
+
async analyzeBarChart({ xAxes = [], yAxes = [], filters = [] }) {
|
|
115
|
+
const xAxis = xAxes[0]
|
|
116
|
+
if (!xAxis) return { labels: [], datasets: [] }
|
|
117
|
+
const xField = xAxis.field ?? xAxis
|
|
118
|
+
const xModifier = (xAxis.aggregrate === 'none' || !xAxis.aggregrate) ? '' : xAxis.aggregrate
|
|
119
|
+
const where = this._buildWhere(filters)
|
|
120
|
+
return this._dashboardBarLineData({ xField, xModifier, yAxes, where })
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
// ── Line Chart ─────────────────────────────────────────────────────────────
|
|
124
|
+
// Returns: same as BarChart
|
|
125
|
+
|
|
126
|
+
async analyzeLineChart({ xAxes = [], yAxes = [], filters = [] }) {
|
|
127
|
+
const xAxis = xAxes[0]
|
|
128
|
+
if (!xAxis) return { labels: [], datasets: [] }
|
|
129
|
+
const xField = xAxis.field ?? xAxis
|
|
130
|
+
const xModifier = (xAxis.aggregrate === 'none' || !xAxis.aggregrate) ? '' : xAxis.aggregrate
|
|
131
|
+
const where = this._buildWhere(filters)
|
|
132
|
+
return this._dashboardBarLineData({ xField, xModifier, yAxes, where })
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
// ── Pie Chart ──────────────────────────────────────────────────────────────
|
|
136
|
+
// Returns: { labels: string[], datasets: [{ data, backgroundColor }] }
|
|
137
|
+
|
|
138
|
+
async analyzePieChart({ xAxes = [], filters = [] }) {
|
|
139
|
+
const xAxis = xAxes[0]
|
|
140
|
+
if (!xAxis) return { labels: [], datasets: [] }
|
|
141
|
+
const xField = xAxis.field ?? xAxis
|
|
142
|
+
const xModifier = (xAxis.aggregrate === 'none' || !xAxis.aggregrate) ? '' : xAxis.aggregrate
|
|
143
|
+
const where = this._buildWhere(filters)
|
|
144
|
+
return this._dashboardPieData({ xField, xModifier, where })
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
// ── Doughnut Chart ─────────────────────────────────────────────────────────
|
|
148
|
+
// Returns: same as PieChart
|
|
149
|
+
|
|
150
|
+
async analyzeDoughnutChart({ xAxes = [], filters = [] }) {
|
|
151
|
+
const xAxis = xAxes[0]
|
|
152
|
+
if (!xAxis) return { labels: [], datasets: [] }
|
|
153
|
+
const xField = xAxis.field ?? xAxis
|
|
154
|
+
const xModifier = (xAxis.aggregrate === 'none' || !xAxis.aggregrate) ? '' : xAxis.aggregrate
|
|
155
|
+
const where = this._buildWhere(filters)
|
|
156
|
+
return this._dashboardPieData({ xField, xModifier, where })
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
// ── Virtual Table ──────────────────────────────────────────────────────────
|
|
160
|
+
// Returns: { items: Object[], hasNext: boolean }
|
|
161
|
+
|
|
162
|
+
async analyzeVirtualTable({ columns = [], page = 0, pageSize = 20, filters = [] }) {
|
|
163
|
+
const attributes = columns.length > 0 ? columns.map(c => c.key) : undefined
|
|
164
|
+
const where = this._buildWhere(filters)
|
|
165
|
+
const limit = pageSize + 1
|
|
166
|
+
const offset = page * pageSize
|
|
167
|
+
|
|
168
|
+
const rows = await this.model.findAll({ attributes, where, limit, offset, raw: true })
|
|
169
|
+
const hasNext = rows.length > pageSize
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
items: hasNext ? rows.slice(0, pageSize) : rows,
|
|
173
|
+
hasNext,
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
// ── Private helpers ────────────────────────────────────────────────────────
|
|
178
|
+
|
|
179
|
+
async _dashboardBarLineData({ xField, xModifier = '', yAxes, where = {} }) {
|
|
180
|
+
const { fn, col, literal } = this.conn
|
|
181
|
+
|
|
182
|
+
const yAggFnMap = {
|
|
183
|
+
avg: (c) => fn('AVG', col(c)),
|
|
184
|
+
count: (c) => fn('COUNT', col(c)),
|
|
185
|
+
min: (c) => fn('MIN', col(c)),
|
|
186
|
+
max: (c) => fn('MAX', col(c)),
|
|
187
|
+
sum: (c) => fn('SUM', col(c)),
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ── X-axis expression (date grouping) ──────────────────────────────────
|
|
191
|
+
const xExprMap = {
|
|
192
|
+
day: () => fn('DATE', col(xField)),
|
|
193
|
+
week: () => fn('DATE_FORMAT', col(xField), '%Y-%u'),
|
|
194
|
+
month: () => fn('DATE_FORMAT', col(xField), '%Y-%m'),
|
|
195
|
+
year: () => fn('YEAR', col(xField)),
|
|
196
|
+
}
|
|
197
|
+
const hasXMod = Boolean(xModifier && xExprMap[xModifier])
|
|
198
|
+
const xExpr = hasXMod ? xExprMap[xModifier]() : col(xField)
|
|
199
|
+
const xAlias = hasXMod ? 'x_grp' : xField
|
|
200
|
+
const xAttr = hasXMod ? [xExpr, xAlias] : xField
|
|
201
|
+
|
|
202
|
+
// ── No y-axis: count by x ──────────────────────────────────────────────
|
|
203
|
+
if (!yAxes?.length) {
|
|
204
|
+
const rows = await this.model.findAll({
|
|
205
|
+
attributes: [ xAttr, [ fn('COUNT', col('*')), 'count' ] ],
|
|
206
|
+
where,
|
|
207
|
+
group: [ xExpr ],
|
|
208
|
+
order: [[ literal(xAlias), 'ASC' ]],
|
|
209
|
+
raw: true,
|
|
210
|
+
})
|
|
211
|
+
return {
|
|
212
|
+
labels: rows.map(r => r[xAlias]),
|
|
213
|
+
datasets: [{
|
|
214
|
+
label: 'Count',
|
|
215
|
+
data: rows.map(r => parseInt(r.count)),
|
|
216
|
+
backgroundColor: COLORS[0],
|
|
217
|
+
}],
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ── With y-axis fields ─────────────────────────────────────────────────
|
|
222
|
+
// Each y-axis is { field, aggregrate } or a plain string (legacy)
|
|
223
|
+
const yDefs = yAxes.map(y => ({
|
|
224
|
+
field: y.field ?? y.key ?? y,
|
|
225
|
+
label: y.label ?? y.field ?? y.key ?? y,
|
|
226
|
+
aggFn: yAggFnMap[y.aggregrate] ?? yAggFnMap.sum,
|
|
227
|
+
aggLbl: (y.aggregrate ?? 'sum').charAt(0).toUpperCase() + (y.aggregrate ?? 'sum').slice(1),
|
|
228
|
+
}))
|
|
229
|
+
|
|
230
|
+
const rows = await this.model.findAll({
|
|
231
|
+
attributes: [
|
|
232
|
+
xAttr,
|
|
233
|
+
...yDefs.map(y => [ y.aggFn(y.field), `agg_${y.field}` ]),
|
|
234
|
+
],
|
|
235
|
+
where,
|
|
236
|
+
group: [ xExpr ],
|
|
237
|
+
order: [[ literal(xAlias), 'ASC' ]],
|
|
238
|
+
raw: true,
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
labels: rows.map(r => r[xAlias]),
|
|
243
|
+
datasets: yDefs.map((y, i) => ({
|
|
244
|
+
label: `${y.aggLbl} of ${y.label}`,
|
|
245
|
+
data: rows.map(r => parseFloat(r[`agg_${y.field}`] ?? 0)),
|
|
246
|
+
backgroundColor: COLORS[i % COLORS.length],
|
|
247
|
+
})),
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
// ── Chat ───────────────────────────────────────────────────────────────────
|
|
252
|
+
// Expects the host class to have: this.llmChat(messages) → Promise<string>
|
|
253
|
+
// Returns: { reply: string, dashboard?: Object }
|
|
254
|
+
|
|
255
|
+
async chat(params) {
|
|
256
|
+
const { messages } = params
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
async _dashboardPieData({ xField, xModifier = '', where = {} }) {
|
|
263
|
+
const { fn, col, literal } = this.conn
|
|
264
|
+
|
|
265
|
+
const xExprMap = {
|
|
266
|
+
day: () => fn('DATE', col(xField)),
|
|
267
|
+
week: () => fn('DATE_FORMAT', col(xField), '%Y-%u'),
|
|
268
|
+
month: () => fn('DATE_FORMAT', col(xField), '%Y-%m'),
|
|
269
|
+
year: () => fn('YEAR', col(xField)),
|
|
270
|
+
}
|
|
271
|
+
const hasXMod = Boolean(xModifier && xExprMap[xModifier])
|
|
272
|
+
const xExpr = hasXMod ? xExprMap[xModifier]() : col(xField)
|
|
273
|
+
const xAlias = hasXMod ? 'x_grp' : xField
|
|
274
|
+
const xAttr = hasXMod ? [xExpr, xAlias] : xField
|
|
275
|
+
|
|
276
|
+
const rows = await this.model.findAll({
|
|
277
|
+
attributes: [ xAttr, [ fn('COUNT', col('*')), 'count' ] ],
|
|
278
|
+
where,
|
|
279
|
+
group: [ xExpr ],
|
|
280
|
+
order: [[ literal('count'), 'DESC' ]],
|
|
281
|
+
raw: true,
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
labels: rows.map(r => r[xAlias]),
|
|
286
|
+
datasets: [{
|
|
287
|
+
data: rows.map(r => parseInt(r.count)),
|
|
288
|
+
backgroundColor: rows.map((_, i) => COLORS[i % COLORS.length]),
|
|
289
|
+
}],
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Mix dashboard analysis methods into a controller class.
|
|
297
|
+
*
|
|
298
|
+
* @param {Function} ControllerClass
|
|
299
|
+
* @param {Object} [overrides] - Methods to assign on top of DashboardMixin, overriding defaults.
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* mixin(OrderController, {
|
|
303
|
+
* async preAnalyze({ datasource }) {
|
|
304
|
+
* return { columns: await getColumnsFor(datasource) }
|
|
305
|
+
* }
|
|
306
|
+
* })
|
|
307
|
+
*/
|
|
308
|
+
function mixin(ControllerClass, overrides = {}) {
|
|
309
|
+
for (const [key, fn] of Object.entries(DashboardMixin)) {
|
|
310
|
+
if (!(key in ControllerClass.prototype)) {
|
|
311
|
+
ControllerClass.prototype[key] = fn
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
Object.assign(ControllerClass.prototype, overrides)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
module.exports = { DashboardMixin, mixin }
|