fix: Correct column names in analytics SQL queries
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
- Fixed getAgeDistribution: use CTE with sort_order to avoid column reference errors - Fixed getEngagementPattern: changed user_id to logged_by and created_at to started_at - Fixed getActivityByDay: changed created_at to started_at throughout All queries now match the actual database schema. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -131,14 +131,14 @@ export class DashboardService {
|
|||||||
async getActivityByDay(days: number = 7) {
|
async getActivityByDay(days: number = 7) {
|
||||||
const result = await this.userRepository.query(
|
const result = await this.userRepository.query(
|
||||||
`SELECT
|
`SELECT
|
||||||
TO_CHAR(created_at, 'Day') as day,
|
TO_CHAR(started_at, 'Day') as day,
|
||||||
SUM(CASE WHEN type = 'feeding' THEN 1 ELSE 0 END) as feeding,
|
SUM(CASE WHEN type = 'feeding' THEN 1 ELSE 0 END) as feeding,
|
||||||
SUM(CASE WHEN type = 'sleep' THEN 1 ELSE 0 END) as sleep,
|
SUM(CASE WHEN type = 'sleep' THEN 1 ELSE 0 END) as sleep,
|
||||||
SUM(CASE WHEN type = 'diaper' THEN 1 ELSE 0 END) as diapers
|
SUM(CASE WHEN type = 'diaper' THEN 1 ELSE 0 END) as diapers
|
||||||
FROM activities
|
FROM activities
|
||||||
WHERE created_at >= NOW() - INTERVAL '${days} days'
|
WHERE started_at >= NOW() - INTERVAL '${days} days'
|
||||||
GROUP BY TO_CHAR(created_at, 'Day'), EXTRACT(DOW FROM created_at)
|
GROUP BY TO_CHAR(started_at, 'Day'), EXTRACT(DOW FROM started_at)
|
||||||
ORDER BY EXTRACT(DOW FROM created_at)`,
|
ORDER BY EXTRACT(DOW FROM started_at)`,
|
||||||
);
|
);
|
||||||
|
|
||||||
return result.map((row: any) => ({
|
return result.map((row: any) => ({
|
||||||
@@ -151,33 +151,36 @@ export class DashboardService {
|
|||||||
|
|
||||||
async getAgeDistribution() {
|
async getAgeDistribution() {
|
||||||
const result = await this.userRepository.query(
|
const result = await this.userRepository.query(
|
||||||
`SELECT
|
`WITH age_groups AS (
|
||||||
CASE
|
SELECT
|
||||||
WHEN age_months BETWEEN 0 AND 5 THEN '0-6 months'
|
CASE
|
||||||
WHEN age_months BETWEEN 6 AND 11 THEN '6-12 months'
|
WHEN age_months BETWEEN 0 AND 5 THEN '0-6 months'
|
||||||
WHEN age_months BETWEEN 12 AND 23 THEN '1-2 years'
|
WHEN age_months BETWEEN 6 AND 11 THEN '6-12 months'
|
||||||
WHEN age_months BETWEEN 24 AND 35 THEN '2-3 years'
|
WHEN age_months BETWEEN 12 AND 23 THEN '1-2 years'
|
||||||
WHEN age_months BETWEEN 36 AND 47 THEN '3-4 years'
|
WHEN age_months BETWEEN 24 AND 35 THEN '2-3 years'
|
||||||
WHEN age_months BETWEEN 48 AND 59 THEN '4-5 years'
|
WHEN age_months BETWEEN 36 AND 47 THEN '3-4 years'
|
||||||
ELSE '5-6 years'
|
WHEN age_months BETWEEN 48 AND 59 THEN '4-5 years'
|
||||||
END as age,
|
ELSE '5-6 years'
|
||||||
COUNT(*) as count
|
END as age,
|
||||||
FROM (
|
CASE
|
||||||
SELECT EXTRACT(YEAR FROM AGE(birth_date)) * 12 + EXTRACT(MONTH FROM AGE(birth_date)) as age_months
|
WHEN age_months BETWEEN 0 AND 5 THEN 1
|
||||||
FROM children
|
WHEN age_months BETWEEN 6 AND 11 THEN 2
|
||||||
WHERE birth_date IS NOT NULL AND deleted_at IS NULL
|
WHEN age_months BETWEEN 12 AND 23 THEN 3
|
||||||
) ages
|
WHEN age_months BETWEEN 24 AND 35 THEN 4
|
||||||
GROUP BY age
|
WHEN age_months BETWEEN 36 AND 47 THEN 5
|
||||||
ORDER BY
|
WHEN age_months BETWEEN 48 AND 59 THEN 6
|
||||||
CASE age
|
ELSE 7
|
||||||
WHEN '0-6 months' THEN 1
|
END as sort_order
|
||||||
WHEN '6-12 months' THEN 2
|
FROM (
|
||||||
WHEN '1-2 years' THEN 3
|
SELECT EXTRACT(YEAR FROM AGE(birth_date)) * 12 + EXTRACT(MONTH FROM AGE(birth_date)) as age_months
|
||||||
WHEN '2-3 years' THEN 4
|
FROM children
|
||||||
WHEN '3-4 years' THEN 5
|
WHERE birth_date IS NOT NULL AND deleted_at IS NULL
|
||||||
WHEN '4-5 years' THEN 6
|
) ages
|
||||||
ELSE 7
|
)
|
||||||
END`,
|
SELECT age, COUNT(*) as count
|
||||||
|
FROM age_groups
|
||||||
|
GROUP BY age, sort_order
|
||||||
|
ORDER BY sort_order`,
|
||||||
);
|
);
|
||||||
|
|
||||||
return result.map((row: any) => ({
|
return result.map((row: any) => ({
|
||||||
@@ -189,11 +192,11 @@ export class DashboardService {
|
|||||||
async getEngagementPattern(days: number = 7) {
|
async getEngagementPattern(days: number = 7) {
|
||||||
const result = await this.userRepository.query(
|
const result = await this.userRepository.query(
|
||||||
`SELECT
|
`SELECT
|
||||||
EXTRACT(HOUR FROM created_at) as hour,
|
EXTRACT(HOUR FROM started_at) as hour,
|
||||||
COUNT(DISTINCT user_id) as sessions
|
COUNT(DISTINCT logged_by) as sessions
|
||||||
FROM activities
|
FROM activities
|
||||||
WHERE created_at >= NOW() - INTERVAL '${days} days'
|
WHERE started_at >= NOW() - INTERVAL '${days} days'
|
||||||
GROUP BY EXTRACT(HOUR FROM created_at)
|
GROUP BY EXTRACT(HOUR FROM started_at)
|
||||||
ORDER BY hour`,
|
ORDER BY hour`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user