Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  1. /** Whiskers template file */
  2. const TEMPLATE_FILE = "admin/analytics.html";
  3. const includes = require('../includes/includes.js');
  4. //updates db
  5. const sql = require('../utils/sql');
  6. const generateData = function(templateContext)
  7. {
  8. return new Promise(function(resolve, reject)
  9. {
  10. var data = [];
  11. sql.getTraffic().then(function(traffic)
  12. {
  13. var start = traffic[0].date;
  14. var currentMonth = new Date(start.getUTCFullYear(), start.getMonth(), 1, 0,0,0);
  15. templateContext.start = JSON.stringify(currentMonth);
  16. var monthCount = 0;
  17. for(var i = 0; i < traffic.length; i++)
  18. {
  19. var currentDate = traffic[i].date;
  20. if(currentMonth.getMonth() != currentDate.getMonth())
  21. {
  22. var foo = new Object();
  23. foo.x = currentMonth;
  24. foo.y = monthCount;
  25. data.push(foo);
  26. monthCount = 0;
  27. currentMonth = new Date(currentDate.getUTCFullYear(), currentDate.getMonth(), 1, 0,0,0);
  28. }
  29. monthCount = monthCount + 1;
  30. }
  31. templateContext.finish = JSON.stringify(currentMonth);
  32. templateContext.dataset = JSON.stringify(data);
  33. resolve();
  34. });
  35. });
  36. };
  37. module.exports=
  38. {
  39. /**
  40. * Fetches context information for the admin blog page and handles post
  41. * data sent regarding editing blog.
  42. *
  43. * @param templateContext json object used as the template context
  44. * @returns {Promise} renders the template used for this page
  45. */
  46. main: function(templateContext)
  47. {
  48. return new Promise(function(resolve, reject)
  49. {
  50. Promise.all([includes.fetchTemplate(TEMPLATE_FILE), generateData(templateContext)]).then(function(template)
  51. {
  52. templateContext.adminPage = template[0];
  53. resolve();
  54. }).catch(function(error)
  55. {
  56. console.log("error in add admin blog.js");
  57. reject(error);
  58. });
  59. });
  60. }
  61. };