From 344ffeb658600ff99fef945881cb6336ea56e9b5 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Thu, 30 Jul 2020 00:06:29 -0700 Subject: [PATCH] Fixed summary query. --- .eslintrc.json | 3 ++- components/PageviewsChart.js | 2 +- package.json | 1 + pages/api/website/[id]/pageviews.js | 7 +++++-- pages/api/website/[id]/summary.js | 5 ++--- yarn.lock | 5 +++++ 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 341903f2..1a9f88f6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -13,7 +13,8 @@ }, "plugins": ["react"], "rules": { - "react/react-in-jsx-scope": "off" + "react/react-in-jsx-scope": "off", + "react/prop-types": "off" }, "globals": { "React": "writable" diff --git a/components/PageviewsChart.js b/components/PageviewsChart.js index 20d5e02c..99fa529b 100644 --- a/components/PageviewsChart.js +++ b/components/PageviewsChart.js @@ -15,7 +15,7 @@ export default function PageviewsChart({ data, unit }) { switch (unit) { case 'day': if (n > 7) { - return index % (n / 15) === 0 ? format(d, 'MMM d') : ''; + return index % ~~(n / 15) === 0 ? format(d, 'MMM d') : ''; } return format(d, 'EEE M/d'); default: diff --git a/package.json b/package.json index 06fff41e..7804d7f7 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "cookie": "^0.4.1", "cors": "^2.8.5", "date-fns": "^2.14.0", + "date-fns-tz": "^1.0.10", "detect-browser": "^5.1.1", "dotenv": "^8.2.0", "formik": "^2.1.5", diff --git a/pages/api/website/[id]/pageviews.js b/pages/api/website/[id]/pageviews.js index 99e86949..578fbcbf 100644 --- a/pages/api/website/[id]/pageviews.js +++ b/pages/api/website/[id]/pageviews.js @@ -11,9 +11,12 @@ export default async (req, res) => { return res.status(400).end(); } + const start = new Date(+start_at); + const end = new Date(+end_at); + const [pageviews, uniques] = await Promise.all([ - getPageviewData(+id, new Date(+start_at), new Date(+end_at), tz, unit, '*'), - getPageviewData(+id, new Date(+start_at), new Date(+end_at), tz, unit, 'distinct session_id'), + getPageviewData(+id, start, end, tz, unit, '*'), + getPageviewData(+id, start, end, tz, unit, 'distinct session_id'), ]); return res.status(200).json({ pageviews, uniques }); diff --git a/pages/api/website/[id]/summary.js b/pages/api/website/[id]/summary.js index a1882ad5..c8b95599 100644 --- a/pages/api/website/[id]/summary.js +++ b/pages/api/website/[id]/summary.js @@ -1,6 +1,5 @@ import { getSummary } from 'lib/db'; import { useAuth } from 'lib/middleware'; -import { format } from 'date-fns'; export default async (req, res) => { await useAuth(req, res); @@ -9,8 +8,8 @@ export default async (req, res) => { const summary = await getSummary( +id, - format(new Date(+start_at), 'yyyy-MM-dd hh:mm:ss'), - format(new Date(+end_at), 'yyyy-MM-dd hh:mm:ss'), + new Date(+start_at).toISOString(), + new Date(+end_at).toISOString(), ); const stats = Object.keys(summary[0]).reduce((obj, key) => { diff --git a/yarn.lock b/yarn.lock index b3419677..f38a23e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3007,6 +3007,11 @@ d@1, d@^1.0.1: es5-ext "^0.10.50" type "^1.0.1" +date-fns-tz@^1.0.10: + version "1.0.10" + resolved "https://registry.yarnpkg.com/date-fns-tz/-/date-fns-tz-1.0.10.tgz#30fef0038f80534fddd8e133a6b8ca55ba313748" + integrity sha512-cHQAz0/9uDABaUNDM80Mj1FL4ODlxs1xEY4b0DQuAooO2UdNKvDkNbV8ogLnxLbv02Ru1HXFcot0pVvDRBgptg== + date-fns@^2.14.0: version "2.14.0" resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.14.0.tgz#359a87a265bb34ef2e38f93ecf63ac453f9bc7ba"