Skip to content

Commit

Permalink
clean-up state from date range timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Nov 21, 2018
1 parent 5143265 commit 239f880
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
13 changes: 8 additions & 5 deletions assets/src/js/components/Chart.js
Expand Up @@ -58,7 +58,7 @@ class Chart extends Component {
return; return;
} }


let daysDiff = Math.round((newProps.before-newProps.after)/24/60/60); let daysDiff = Math.round((newProps.dateRange[1]-newProps.dateRange[0])/24/60/60);
let stepHours = daysDiff > 1 ? 24 : 1; let stepHours = daysDiff > 1 ? 24 : 1;
this.setState({ this.setState({
diffInDays: daysDiff, diffInDays: daysDiff,
Expand All @@ -69,13 +69,13 @@ class Chart extends Component {
} }


paramsChanged(o, n) { paramsChanged(o, n) {
return o.siteId != n.siteId || o.before != n.before || o.after != n.after; return o.siteId != n.siteId || o.dateRange != n.dateRange;
} }


@bind @bind
prepareData(data) { prepareData(data) {
let startDate = new Date(this.props.after * 1000); let startDate = this.props.dateRange[0];
let endDate = new Date(this.props.before * 1000); let endDate = this.props.dateRange[1];
let newData = []; let newData = [];


// instantiate JS Date objects // instantiate JS Date objects
Expand Down Expand Up @@ -249,7 +249,10 @@ class Chart extends Component {
fetchData(props) { fetchData(props) {
this.setState({ loading: true }) this.setState({ loading: true })


Client.request(`/sites/${props.siteId}/stats/site?before=${props.before}&after=${props.after}`) let before = props.dateRange[1]/1000;
let after = props.dateRange[0]/1000;

Client.request(`/sites/${props.siteId}/stats/site?before=${before}&after=${after}`)
.then((d) => { .then((d) => {
// request finished; check if params changed in the meantime // request finished; check if params changed in the meantime
if( this.paramsChanged(props, this.props)) { if( this.paramsChanged(props, this.props)) {
Expand Down
30 changes: 11 additions & 19 deletions assets/src/js/components/DatePicker.js
Expand Up @@ -45,7 +45,8 @@ const availablePeriods = {
'qtd': { 'qtd': {
label: 'Qtd', label: 'Qtd',
start: function() { start: function() {
return new Date(now.getFullYear(), Math.ceil(now.getMonth() / 3), 1); let qs = Math.ceil((now.getMonth()+1) / 3) * 3 - 3;
return new Date(now.getFullYear(), qs, 1);


}, },
end: function() { end: function() {
Expand Down Expand Up @@ -79,10 +80,8 @@ class DatePicker extends Component {


this.state = { this.state = {
period: window.location.hash.substring(2) || window.localStorage.getItem('period') || defaultPeriod, period: window.location.hash.substring(2) || window.localStorage.getItem('period') || defaultPeriod,
before: 0, // UTC timestamp startDate: now,
after: 0, // UTC timestamp endDate: now,
startDate: null, // local date object
endDate: null, // local date object
} }
this.updateDatesFromPeriod(this.state.period) this.updateDatesFromPeriod(this.state.period)
} }
Expand All @@ -105,28 +104,21 @@ class DatePicker extends Component {
} }


@bind @bind
setDateRange(startDate, endDate, period) { setDateRange(start, end, period) {
// don't update state if start > end. user may be busy picking dates. // don't update state if start > end. user may be busy picking dates.
// TODO: show error // TODO: show error
if(startDate > endDate) { if(start > end) {
return; return;
} }


// include start & end day by forcing time // include start & end day by forcing time
startDate.setHours(0, 0, 0); start.setHours(0, 0, 0);
endDate.setHours(23, 59, 59); end.setHours(23, 59, 59);


// create unix timestamps from local date objects
let before, after;
before = Math.round((+endDate) / 1000);
after = Math.round((+startDate) / 1000);

this.setState({ this.setState({
period: period || '', period: period || '',
startDate: startDate, startDate: start,
endDate: endDate, endDate: end,
before: before,
after: after,
}); });


// use slight delay for updating rest of application to allow this function to be called again // use slight delay for updating rest of application to allow this function to be called again
Expand Down
6 changes: 4 additions & 2 deletions assets/src/js/components/Sidebar.js
Expand Up @@ -25,14 +25,16 @@ class Sidebar extends Component {
} }


paramsChanged(o, n) { paramsChanged(o, n) {
return o.siteId != n.siteId || o.before != n.before || o.after != n.after; return o.siteId != n.siteId || o.dateRange != n.dateRange;
} }


@bind @bind
fetchData(props) { fetchData(props) {
this.setState({ loading: true }) this.setState({ loading: true })
let before = props.dateRange[1]/1000;
let after = props.dateRange[0]/1000;


Client.request(`/sites/${props.siteId}/stats/site/agg?before=${props.before}&after=${props.after}`) Client.request(`/sites/${props.siteId}/stats/site/agg?before=${before}&after=${after}`)
.then((data) => { .then((data) => {
// request finished; check if timestamp range is still the one user wants to see // request finished; check if timestamp range is still the one user wants to see
if(this.paramsChanged(props, this.props)) { if(this.paramsChanged(props, this.props)) {
Expand Down
8 changes: 5 additions & 3 deletions assets/src/js/components/Table.js
Expand Up @@ -32,14 +32,16 @@ class Table extends Component {
} }


paramsChanged(o, n) { paramsChanged(o, n) {
return o.siteId != n.siteId || o.before != n.before || o.after != n.after; return o.siteId != n.siteId || o.dateRange != n.dateRange;
} }


@bind @bind
fetchData(props) { fetchData(props) {
this.setState({ loading: true }); this.setState({ loading: true });
let before = props.dateRange[1]/1000;
let after = props.dateRange[0]/1000;


Client.request(`/sites/${props.siteId}/stats/${props.endpoint}/agg?before=${props.before}&after=${props.after}&offset=${this.state.offset}&limit=${this.state.limit}`) Client.request(`/sites/${props.siteId}/stats/${props.endpoint}/agg?before=${before}&after=${after}&offset=${this.state.offset}&limit=${this.state.limit}`)
.then((d) => { .then((d) => {
// request finished; check if timestamp range is still the one user wants to see // request finished; check if timestamp range is still the one user wants to see
if( this.paramsChanged(props, this.props) ) { if( this.paramsChanged(props, this.props) ) {
Expand All @@ -53,7 +55,7 @@ class Table extends Component {
}); });


// fetch totals too // fetch totals too
Client.request(`/sites/${props.siteId}/stats/${props.endpoint}/agg/pageviews?before=${props.before}&after=${props.after}`) Client.request(`/sites/${props.siteId}/stats/${props.endpoint}/agg/pageviews?before=${before}&after=${after}`)
.then((d) => { .then((d) => {
this.setState({ this.setState({
total: d total: d
Expand Down
16 changes: 7 additions & 9 deletions assets/src/js/pages/dashboard.js
Expand Up @@ -25,8 +25,7 @@ class Dashboard extends Component {
super(props) super(props)


this.state = { this.state = {
before: 0, dateRange: [],
after: 0,
isPublic: document.cookie.indexOf('auth') < 0, isPublic: document.cookie.indexOf('auth') < 0,
site: defaultSite, site: defaultSite,
sites: [], sites: [],
Expand Down Expand Up @@ -68,8 +67,7 @@ class Dashboard extends Component {
@bind @bind
changeDateRange(s) { changeDateRange(s) {
this.setState({ this.setState({
before: s.before, dateRange: [ s.startDate, s.endDate ],
after: s.after,
period: s.period, period: s.period,
}) })
} }
Expand Down Expand Up @@ -147,7 +145,7 @@ class Dashboard extends Component {


return ( return (
<div class="app-page "> <div class="app-page ">
<div class={`rapper animated fadeInUp delayed_02s ${state.period} ` + classNames({ ltday: state.before - state.after < 86400 })}> <div class={`rapper animated fadeInUp delayed_02s ${state.period} ` + classNames({ ltday: state.dateRange[1] - state.dateRange[0] < 86400 })}>


<header class="section"> <header class="section">
<nav class="main-nav"> <nav class="main-nav">
Expand All @@ -164,16 +162,16 @@ class Dashboard extends Component {


<section class="section"> <section class="section">
<div class="boxes"> <div class="boxes">
<Sidebar siteId={state.site.id} before={state.before} after={state.after} /> <Sidebar siteId={state.site.id} dateRange={state.dateRange} />


<div class="box box-graph"> <div class="box box-graph">
<Chart siteId={state.site.id} before={state.before} after={state.after} /> <Chart siteId={state.site.id} dateRange={state.dateRange} />
</div> </div>
<div class="box box-pages"> <div class="box box-pages">
<Table endpoint="pages" headers={["Top pages", "Views", "Uniques"]} siteId={state.site.id} before={state.before} after={state.after} /> <Table endpoint="pages" headers={["Top pages", "Views", "Uniques"]} siteId={state.site.id} dateRange={state.dateRange} />
</div> </div>
<div class="box box-referrers"> <div class="box box-referrers">
<Table endpoint="referrers" headers={["Top referrers", "Views", "Uniques"]} siteId={state.site.id} before={state.before} after={state.after} showHostname="true" /> <Table endpoint="referrers" headers={["Top referrers", "Views", "Uniques"]} siteId={state.site.id} dateRange={state.dateRange} showHostname="true" />
</div> </div>
</div> </div>


Expand Down

0 comments on commit 239f880

Please sign in to comment.