summaryrefslogtreecommitdiff
path: root/app/Models/StatsDAOSQLite.php
blob: 9bfe8b20ab872623f9c6abf535cf7aa2e54aa031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

class FreshRSS_StatsDAOSQLite extends FreshRSS_StatsDAO {

	/**
	 * Calculates entry count per day on a 30 days period.
	 * Returns the result as a JSON object.
	 *
	 * @return JSON object
	 */
	public function calculateEntryCount() {
		$count = $this->initEntryCountArray();
		$period = parent::ENTRY_COUNT_PERIOD;

		// Get stats per day for the last 30 days
		$sql = <<<SQL
SELECT round(julianday(e.date, 'unixepoch') - julianday('now')) AS day,
COUNT(1) AS count
FROM {$this->prefix}entry AS e
WHERE strftime('%Y%m%d', e.date, 'unixepoch')
	BETWEEN strftime('%Y%m%d', 'now', '-{$period} days')
	AND strftime('%Y%m%d', 'now', '-1 day')
GROUP BY day
ORDER BY day ASC
SQL;
		$stm = $this->bd->prepare($sql);
		$stm->execute();
		$res = $stm->fetchAll(PDO::FETCH_ASSOC);

		foreach ($res as $value) {
			$count[(int) $value['day']] = (int) $value['count'];
		}

		return $this->convertToSerie($count);
	}

	/**
	 * Calculates entry average per day on a 30 days period.
	 *
	 * @return integer
	 */
	public function calculateEntryAverage() {
		$period = self::ENTRY_COUNT_PERIOD;

		// Get stats per day for the last 30 days
		$sql = <<<SQL
SELECT COUNT(1) / {$period} AS average
FROM {$this->prefix}entry AS e
WHERE strftime('%Y%m%d', e.date, 'unixepoch')
	BETWEEN strftime('%Y%m%d', 'now', '-{$period} days')
	AND strftime('%Y%m%d', 'now', '-1 day')
SQL;
		$stm = $this->bd->prepare($sql);
		$stm->execute();
		$res = $stm->fetch(PDO::FETCH_NAMED);

		return round($res['average'], 2);
	}

	protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) {
		if ($feed) {
			$restrict = "WHERE e.id_feed = {$feed}";
		} else {
			$restrict = '';
		}
		$sql = <<<SQL
SELECT strftime('{$period}', e.date, 'unixepoch') AS period
, COUNT(1) AS count
FROM {$this->prefix}entry AS e
{$restrict}
GROUP BY period
ORDER BY period ASC
SQL;

		$stm = $this->bd->prepare($sql);
		$stm->execute();
		$res = $stm->fetchAll(PDO::FETCH_NAMED);

		$repartition = array();
		foreach ($res as $value) {
			$repartition[(int) $value['period']] = (int) $value['count'];
		}

		return $this->convertToSerie($repartition);
	}

}