dashboar changes
Deploy Node App / deploy (push) Successful in 15s

This commit is contained in:
Gitea
2026-06-16 11:36:41 +05:30
parent bea7933c48
commit 8424c54410
5 changed files with 229 additions and 35 deletions
+77
View File
@@ -0,0 +1,77 @@
const postgre = require('../database/postgre');
const analytics = async (req, res) => {
try {
const query = `
SELECT
'24h' AS period,
COUNT(DISTINCT session_id) AS sessions,
COUNT(*) FILTER (WHERE status = 1) AS chats,
COUNT(DISTINCT user_id) AS users,
COUNT(*) AS queries
FROM useraskquestion
WHERE created >= NOW() - INTERVAL '24 hours'
UNION ALL
SELECT
'7d',
COUNT(DISTINCT session_id),
COUNT(*) FILTER (WHERE status = 1),
COUNT(DISTINCT user_id),
COUNT(*)
FROM useraskquestion
WHERE created >= NOW() - INTERVAL '7 days'
UNION ALL
SELECT
'30d',
COUNT(DISTINCT session_id),
COUNT(*) FILTER (WHERE status = 1),
COUNT(DISTINCT user_id),
COUNT(*)
FROM useraskquestion
WHERE created >= NOW() - INTERVAL '30 days'
UNION ALL
SELECT
'all',
COUNT(DISTINCT session_id),
COUNT(*) FILTER (WHERE status = 1),
COUNT(DISTINCT user_id),
COUNT(*)
FROM useraskquestion;
`;
const result = await postgre.query(query);
const response = {};
result.rows.forEach(row => {
response[row.period] = {
sessions: Number(row.sessions),
chats: Number(row.chats),
users: Number(row.users),
queries: Number(row.queries),
};
});
return res.status(200).json({
success: true,
data: response,
});
} catch (error) {
console.error("Analytics Error:", error);
return res.status(500).json({
success: false,
message: error.message,
});
}
};
module.exports = {
analytics,
};