diff --git a/README.md b/README.md index ef09c75..7618474 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Ana. Open Source Web Analytics. This is nowhere near being usable, let alone stable. Treat as a proof of concept. -![Screenshot of the Ana dashboard](https://github.com/dannyvankooten/ana/raw/master/assets/img/screenshot.png?v=4) +![Screenshot of the Ana dashboard](https://github.com/dannyvankooten/ana/raw/master/assets/img/screenshot.png?v=5) ## Usage diff --git a/ROADMAP.md b/ROADMAP.md index 17cb867..dc68df6 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -5,7 +5,6 @@ This is a general draft document for thoughts and todo's, without any structure ### What's cooking? -- Compare period totals to previous period. - Allow setting custom limit in table overviews. - Allow sorting in table overviews. - Choose a OS license & settle on name. diff --git a/assets/img/screenshot.png b/assets/img/screenshot.png index 56b5424..621a85b 100644 Binary files a/assets/img/screenshot.png and b/assets/img/screenshot.png differ diff --git a/assets/js/components/CountWidget.js b/assets/js/components/CountWidget.js index 9a02e27..56073ef 100644 --- a/assets/js/components/CountWidget.js +++ b/assets/js/components/CountWidget.js @@ -1,13 +1,15 @@ 'use strict'; import { h, render, Component } from 'preact'; +const dayInSeconds = 60 * 60 * 24; class CountWidget extends Component { constructor(props) { super(props) this.state = { - count: 0 + count: 0, + previousCount: 0 } this.fetchData = this.fetchData.bind(this); @@ -21,7 +23,10 @@ class CountWidget extends Component { } fetchData(period) { - return fetch('/api/' + this.props.endpoint + '/count?period=' + period, { + const before = Math.round((+new Date() ) / 1000); + const after = before - ( period * dayInSeconds ); + + fetch(`/api/${this.props.endpoint}/count?before=${before}&after=${after}`, { credentials: 'include' }).then((r) => { if( r.ok ) { return r.json(); } @@ -29,14 +34,36 @@ class CountWidget extends Component { }).then((data) => { this.setState({ count: data }) }); + + // query previous period + const previousBefore = after; + const previousAfter = previousBefore - ( period * dayInSeconds ); + fetch(`/api/${this.props.endpoint}/count?before=${previousBefore}&after=${previousAfter}`, { + credentials: 'include' + }).then((r) => { + if( r.ok ) { return r.json(); } + throw new Error(); + }).then((data) => { + this.setState({ previousCount: data }) + }); + } + + renderPercentage() { + if( ! this.state.previousCount ) { + return ''; + } + + const percentage = Math.round(( this.state.count / this.state.previousCount * 100 - 100)) + return ( + 0 ? 'positive' : 'negative'}>{percentage}% + ) } render() { return ( -
-

{this.props.title}

-
{this.state.count}
+

{this.props.title}

+
{this.state.count} {this.renderPercentage()}
last {this.props.period} days
) diff --git a/assets/sass/_util.scss b/assets/sass/_util.scss index 9cdb834..651b2b3 100644 --- a/assets/sass/_util.scss +++ b/assets/sass/_util.scss @@ -16,6 +16,11 @@ clear: both; } +.tiny-margin{ + margin-top: 10px; + margin-bottom: 10px; +} + .small-margin { margin-top: 20px; margin-bottom: 20px; @@ -45,3 +50,15 @@ .center-text { text-align: center; } + +.positive { + color: limegreen; + + &:before { + content: "+"; + } +} + +.negative { + color: orangered; +} diff --git a/assets/sass/styles.scss b/assets/sass/styles.scss index 019622c..da4b374 100644 --- a/assets/sass/styles.scss +++ b/assets/sass/styles.scss @@ -101,3 +101,7 @@ h3{ h4 { font-size: 16px; } + +small { + font-size: 70%; +}