diff --git a/client/src/app-components/stats.js b/client/src/app-components/stats.js index eec8191f..c283d705 100644 --- a/client/src/app-components/stats.js +++ b/client/src/app-components/stats.js @@ -20,6 +20,11 @@ const ID = { 'COMMENT': 3 }; +const statsPeriod = { + 'WEEK': 7, + 'MONTH': 30 +}; + class Stats extends React.Component { static propTypes = { @@ -33,14 +38,14 @@ class Stats extends React.Component { return { name: name, values: [] - } + } }), showed: [0], period: 0 }; componentDidMount() { - this.retrieve(7); + this.retrieve('WEEK'); } render() { @@ -58,7 +63,7 @@ class Stats extends React.Component { 'stats': true, 'stats_staff': this.props.type === 'staff' }; - + return classNames(classes); } @@ -90,7 +95,7 @@ class Stats extends React.Component { getDropDownProps() { return { - items: ['LAST_7_DAYS', 'LAST_30_DAYS', 'LAST_90_DAYS', 'LAST_365_DAYS'].map((name) => { + items: Object.keys(statsPeriod).map(key => 'LAST_' + statsPeriod[key] + '_DAYS').map((name) => { return { content: i18n(name), icon: '' @@ -102,9 +107,7 @@ class Stats extends React.Component { } onDropDownChange(event) { - let val = [7, 30, 90, 365]; - - this.retrieve(val[event.index]); + this.retrieve(Object.keys(statsPeriod)[event.index]); } getStatsChartProps() { @@ -116,23 +119,7 @@ class Stats extends React.Component { }; } - retrieve(period) { - let periodName; - - switch (period) { - case 30: - periodName = 'MONTH'; - break; - case 90: - periodName = 'QUARTER'; - break; - case 365: - periodName = 'YEAR'; - break; - default: - periodName = 'WEEK'; - } - + retrieve(periodName) { API.call({ path: '/system/get-stats', data: this.getApiCallData(periodName) @@ -172,7 +159,7 @@ class Stats extends React.Component { return showed; } - + getStrokes() { return this.props.type === 'general' ? generalStrokes : staffStrokes; } @@ -196,4 +183,4 @@ class Stats extends React.Component { } } -export default Stats; \ No newline at end of file +export default Stats; diff --git a/server/controllers/system/get-stats.php b/server/controllers/system/get-stats.php index 3dfb1f2f..284efe3e 100755 --- a/server/controllers/system/get-stats.php +++ b/server/controllers/system/get-stats.php @@ -1,5 +1,6 @@ generationNewStats(); + $this->generateNewStats(); + $this->deleteLastStats(); $staffId = Controller::request('staffId'); @@ -56,7 +58,7 @@ class GetStatsController extends Controller { } } - public function generationNewStats() { + public function generateNewStats() { $lastStatDay = Setting::getSetting('last-stat-day'); $previousCurrentDate = floor(Date::getPreviousDate() / 10000); $currentDate = floor(Date::getCurrentDate() / 10000); @@ -109,6 +111,13 @@ class GetStatsController extends Controller { } } + public function deleteLastStats() { + $removeOlderThanDays = 31; + $oldDate = floor(Date::getPreviousDate($removeOlderThanDays) / 10000); + + RedBean::exec("DELETE FROM stat WHERE date < $oldDate"); + } + public function generateGeneralStat($type, $date) { $value = Log::count('type=? AND date LIKE ?',[$type, $date->format('Ymd') . '%']); $stat = new Stat(); @@ -161,4 +170,4 @@ class GetStatsController extends Controller { return $daysToRetrieve; } -} \ No newline at end of file +} diff --git a/server/libs/Date.php b/server/libs/Date.php index 6921789f..7705ce7e 100755 --- a/server/libs/Date.php +++ b/server/libs/Date.php @@ -4,7 +4,7 @@ class Date { return date('YmdHi'); } - public static function getPreviousDate() { - return date('YmdHi', strtotime(' -1 day ')); + public static function getPreviousDate($days = 1) { + return date('YmdHi', strtotime(" -$days day ")); } } diff --git a/tests/system/get-stats.rb b/tests/system/get-stats.rb index 9df68e90..9d6241eb 100644 --- a/tests/system/get-stats.rb +++ b/tests/system/get-stats.rb @@ -85,25 +85,25 @@ describe'/system/get-stats' do d = Date.today.prev_day yesterday = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day + d = Date.today.prev_day(2) yesterday2 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day + d = Date.today.prev_day(3) yesterday3 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(4) yesterday4 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(5) yesterday5 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(6) yesterday6 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(7) yesterday7 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(8) yesterday8 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(9) yesterday9 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(10) yesterday10 = d.strftime("%Y%m%d") - d = Date.today.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day.prev_day + d = Date.today.prev_day(11) yesterday11 = d.strftime("%Y%m%d") assertData(11, yesterday3, 'CREATE_TICKET', '1') @@ -157,4 +157,26 @@ describe'/system/get-stats' do assertData(20, yesterday11, 'CLOSE', '0') assertData(21, yesterday11, 'ASSIGN', '0') end + + it 'should remove very old stats' do + prev_days_to_remove = 32; + + stats_count = $database.query('SELECT COUNT(*) as qt FROM stat').fetch_hash['qt']; + date = Date.today.prev_day(prev_days_to_remove).strftime("%Y%m%d"); + + $database.query('INSERT INTO stat VALUES(\'\', '+ date +', \'CLOSE\', 1, 0, NULL)'); + $database.query('INSERT INTO stat VALUES(\'\', '+ date +', \'SINGUP\', 1, 0, NULL)'); + $database.query('INSERT INTO stat VALUES(\'\', '+ date +', \'COMMENT\', 1, 0, NULL)'); + stats_count_updated = $database.query('SELECT COUNT(*) as qt FROM stat').fetch_hash['qt']; + (stats_count).should.not.equal(stats_count_updated) + + request('/system/get-stats', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + period: 'MONTH', + staffId: '1' + }) + stats_count_updated = $database.query('SELECT COUNT(*) as qt FROM stat').fetch_hash['qt']; + (stats_count).should.equal(stats_count_updated) + end end