Newer
Older
* Class AnalyticsPodcastByCountryModel
* Model for analytics_podcasts_by_country table in database
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;
use CodeIgniter\Model;
class AnalyticsPodcastByCountryModel extends Model
protected $table = 'analytics_podcasts_by_country';
protected $allowedFields = [];
protected $returnType = \App\Entities\AnalyticsPodcastsByCountry::class;
protected $useSoftDeletes = false;
protected $useTimestamps = false;
/**
* Gets country data for a podcast
*
* @param int $podcastId
*
* @return array
*/

Benjamin Bellamy
committed
public function getDataWeekly(int $podcastId): array

Benjamin Bellamy
committed
if (
!($found = cache(

Benjamin Bellamy
committed
"{$podcastId}_analytics_podcast_by_country_weekly"

Benjamin Bellamy
committed
))
) {
$oneWeekAgo=date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`country_code` as `labels`')
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`date` >' => $oneWeekAgo,
->findAll(9);
$found[] = $this->db
->query(
"SELECT
\"Other\" AS `labels`,
SUM(`others`.`values`) AS `values`
FROM
(SELECT SUM(`hits`) AS `values`
FROM {$this->db->prefixTable($this->table)}
WHERE `date` > $oneWeekAgo
GROUP BY `country_code`
ORDER BY `values` DESC
LIMIT 18446744073709551610 OFFSET 9
) AS `others`
GROUP BY `labels`"
)
->getRow(0);
cache()->save(

Benjamin Bellamy
committed
"{$podcastId}_analytics_podcast_by_country_weekly",

Benjamin Bellamy
committed
$found,
600
);
}
return $found;
}
/**
* Gets country data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataYearly(int $podcastId): array
{
if (
!($found = cache(

Benjamin Bellamy
committed
"{$podcastId}_analytics_podcast_by_country_yearly"

Benjamin Bellamy
committed
))
) {
$oneYearAgo = date('Y-m-d', strtotime('-1 year'));

Benjamin Bellamy
committed
$found = $this->select('`country_code` as `labels`')
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`date` >' => $oneYearAgo,

Benjamin Bellamy
committed
])
->groupBy('`labels`')
->orderBy('`values`', 'DESC')
->findAll(10);
$found[] = $this->db
->query(
"SELECT
\"Other\" AS `labels`,
SUM(`others`.`values`) AS `values`
FROM
(SELECT SUM(`hits`) AS `values`
FROM {$this->db->prefixTable($this->table)}
WHERE `date` > $oneyearago
GROUP BY `country_code`
ORDER BY `values` DESC
LIMIT 18446744073709551610 OFFSET 9
) AS `others`
GROUP BY `labels`"
)
->getRow(0);

Benjamin Bellamy
committed
cache()->save(

Benjamin Bellamy
committed
"{$podcastId}_analytics_podcast_by_country_yearly",
);
}
return $found;
}
}