Skip to content

Commit

Permalink
Add pageviews route
Browse files Browse the repository at this point in the history
  • Loading branch information
pergpau committed May 23, 2021
1 parent 12ba316 commit 56427d5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,13 @@ module.exports = {
res.status(500).send(msg.generell_error)
}
},
getPageVisitStats: async (req, res) => {
try {
const visits = await App.getPageVisitStatsFromDB()
res.status(200).send(visits)
} catch (error) {
console.log(error)
res.status(500).send(msg.generell_error)
}
},
}
2 changes: 2 additions & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ module.exports = app => {

router.get("/brukere", auth, admin, userController.getAllUsers)

router.get("/visits", auth, admin, appController.getPageVisitStats)

// Andre app-ruter

router.get("/statistikk", appController.getStatistikk)
Expand Down
8 changes: 8 additions & 0 deletions app/services/appService.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,5 +429,13 @@ module.exports = {
registerVisit: async () => {
const query = `INSERT INTO page_traffic (timestamp) VALUES (CURRENT_TIMESTAMP)`
await db.query(query)
},
getPageVisitStatsFromDB: async () => {
const query = `SELECT DATE_FORMAT(timestamp, '%d-%c') AS dato, count(*) AS antall
FROM page_traffic AS pt
WHERE timestamp BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
GROUP BY dato`
const result = await db.query(query)
return result
}
}
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"dependencies": {
"bcrypt": "^5.0.0",
"body-parser": "^1.19.0",
"browserslist": "^4.16.6",
"caniuse-lite": "^1.0.30001223",
"cookie-session": "^1.4.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
Expand Down

0 comments on commit 56427d5

Please sign in to comment.