fix: optimize mindspace category counts
This commit is contained in:
+22
-14
@@ -180,24 +180,32 @@ export function createMindSpaceService(pool, options = {}) {
|
|||||||
`SELECT c.id, c.category_code, c.category_name, c.visibility_policy,
|
`SELECT c.id, c.category_code, c.category_name, c.visibility_policy,
|
||||||
c.ai_access_policy, c.publish_policy, c.is_system, c.sort_order,
|
c.ai_access_policy, c.publish_policy, c.is_system, c.sort_order,
|
||||||
CASE
|
CASE
|
||||||
WHEN c.category_code = 'draft' THEN COUNT(DISTINCT p.id)
|
WHEN c.category_code = 'draft' THEN COALESCE(pc.item_count, 0)
|
||||||
WHEN c.category_code = 'public' THEN COUNT(DISTINCT pr.page_id)
|
WHEN c.category_code = 'public' THEN COALESCE(pub.item_count, 0)
|
||||||
ELSE COUNT(DISTINCT a.id)
|
ELSE COALESCE(ac.item_count, 0)
|
||||||
END AS item_count
|
END AS item_count
|
||||||
FROM h5_space_categories c
|
FROM h5_space_categories c
|
||||||
LEFT JOIN h5_assets a
|
LEFT JOIN (
|
||||||
ON a.category_id = c.id
|
SELECT category_id, user_id, COUNT(*) AS item_count
|
||||||
AND a.user_id = c.user_id
|
FROM h5_assets
|
||||||
AND a.status <> 'deleted'
|
WHERE user_id = ? AND status <> 'deleted' AND source_type = 'upload'
|
||||||
LEFT JOIN h5_page_records p
|
GROUP BY category_id, user_id
|
||||||
ON p.category_id = c.id AND p.user_id = c.user_id AND p.status <> 'deleted'
|
) ac ON ac.category_id = c.id AND ac.user_id = c.user_id
|
||||||
LEFT JOIN h5_publish_records pr
|
LEFT JOIN (
|
||||||
ON pr.user_id = c.user_id AND pr.status = 'online'
|
SELECT category_id, user_id, COUNT(*) AS item_count
|
||||||
|
FROM h5_page_records
|
||||||
|
WHERE user_id = ? AND status <> 'deleted'
|
||||||
|
GROUP BY category_id, user_id
|
||||||
|
) pc ON pc.category_id = c.id AND pc.user_id = c.user_id
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT user_id, COUNT(DISTINCT page_id) AS item_count
|
||||||
|
FROM h5_publish_records
|
||||||
|
WHERE user_id = ? AND status = 'online'
|
||||||
|
GROUP BY user_id
|
||||||
|
) pub ON pub.user_id = c.user_id
|
||||||
WHERE c.user_id = ? AND c.space_id = ?
|
WHERE c.user_id = ? AND c.space_id = ?
|
||||||
GROUP BY c.id, c.category_code, c.category_name, c.visibility_policy,
|
|
||||||
c.ai_access_policy, c.publish_policy, c.is_system, c.sort_order
|
|
||||||
ORDER BY c.sort_order, c.category_name`,
|
ORDER BY c.sort_order, c.category_name`,
|
||||||
[userId, row.id],
|
[userId, userId, userId, userId, row.id],
|
||||||
);
|
);
|
||||||
|
|
||||||
const quotaBytes = asNumber(row.quota_bytes);
|
const quotaBytes = asNumber(row.quota_bytes);
|
||||||
|
|||||||
+2
-2
@@ -105,8 +105,8 @@ test('getSpace scopes space and categories to the authenticated user', async ()
|
|||||||
assert.equal(space.quota.availableBytes, 5 * 1024 * 1024 - 3072);
|
assert.equal(space.quota.availableBytes, 5 * 1024 * 1024 - 3072);
|
||||||
assert.equal(space.categories[0].code, 'private');
|
assert.equal(space.categories[0].code, 'private');
|
||||||
assert.deepEqual(calls[0].params, ['user-1']);
|
assert.deepEqual(calls[0].params, ['user-1']);
|
||||||
assert.deepEqual(calls[1].params, ['user-1', 'space-1']);
|
assert.deepEqual(calls[1].params, ['user-1', 'user-1', 'user-1', 'user-1', 'space-1']);
|
||||||
assert.doesNotMatch(calls[1].sql, /source_type\s*=\s*'upload'/);
|
assert.match(calls[1].sql, /source_type\s*=\s*'upload'/);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getSpace includes schedule snapshot when schedule service is available', async () => {
|
test('getSpace includes schedule snapshot when schedule service is available', async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user