81 lines
2.1 KiB
JavaScript
81 lines
2.1 KiB
JavaScript
import { getSamplePosts } from '../utils/sample-content.js'
|
|
import { getPostgresClient } from './postgres-client.js'
|
|
|
|
/**
|
|
* 공개 블로그 누적 통계를 조회한다.
|
|
* @returns {Promise<{
|
|
* totalVisitors: number,
|
|
* totalPageViews: number,
|
|
* totalPosts: number,
|
|
* totalComments: number,
|
|
* totalPages: number,
|
|
* totalTags: number,
|
|
* totalMembers: number
|
|
* }>} 공개 블로그 누적 통계
|
|
*/
|
|
export const getPublicBlogStatistics = async () => {
|
|
const sql = getPostgresClient()
|
|
|
|
if (!sql) {
|
|
return {
|
|
totalVisitors: 0,
|
|
totalPageViews: 0,
|
|
totalPosts: getSamplePosts().filter((post) => post.status === 'published').length,
|
|
totalComments: 0,
|
|
totalPages: 0,
|
|
totalTags: 0,
|
|
totalMembers: 0
|
|
}
|
|
}
|
|
|
|
const rows = await sql`
|
|
SELECT
|
|
(
|
|
SELECT COALESCE(SUM(visitors), 0)::int
|
|
FROM site_analytics_daily
|
|
) AS "totalVisitors",
|
|
(
|
|
SELECT COALESCE(SUM(page_views), 0)::int
|
|
FROM site_analytics_daily
|
|
) AS "totalPageViews",
|
|
(
|
|
SELECT COUNT(*)::int
|
|
FROM posts
|
|
WHERE status = 'published'
|
|
AND (published_at IS NULL OR published_at <= now())
|
|
) AS "totalPosts",
|
|
(
|
|
SELECT COUNT(*)::int
|
|
FROM comments
|
|
INNER JOIN posts ON posts.id = comments.post_id
|
|
WHERE comments.status = 'published'
|
|
AND posts.status = 'published'
|
|
AND (posts.published_at IS NULL OR posts.published_at <= now())
|
|
) AS "totalComments",
|
|
(
|
|
SELECT COUNT(*)::int
|
|
FROM pages
|
|
WHERE status = 'published'
|
|
) AS "totalPages",
|
|
(
|
|
SELECT COUNT(*)::int
|
|
FROM tags
|
|
) AS "totalTags",
|
|
(
|
|
SELECT COUNT(*)::int
|
|
FROM users
|
|
) AS "totalMembers"
|
|
`
|
|
const row = rows[0] || {}
|
|
|
|
return {
|
|
totalVisitors: Number(row.totalVisitors || 0),
|
|
totalPageViews: Number(row.totalPageViews || 0),
|
|
totalPosts: Number(row.totalPosts || 0),
|
|
totalComments: Number(row.totalComments || 0),
|
|
totalPages: Number(row.totalPages || 0),
|
|
totalTags: Number(row.totalTags || 0),
|
|
totalMembers: Number(row.totalMembers || 0)
|
|
}
|
|
}
|