1
0

Added custom Hitcounter component

This commit is contained in:
Mike Schwörer 2014-07-20 21:29:07 +02:00
parent d7e056a4ee
commit b5327bf993
5 changed files with 187 additions and 3 deletions

View File

@ -0,0 +1,122 @@
<?php
/**
* Created by PhpStorm.
* User: Mike
* Date: 20.07.14
* Time: 18:41
*/
class CHitCounter extends CApplicationComponent {
/* @var string $table_stats */
public $table_stats;
/* @var string $table_today */
public $table_today;
/* @var bool $updated */
private $updated = false;
/**
* Call this function on every view - it increments the hit counter if this is a new unique view
*
* @return bool true if this is a unique view
*/
public function increment()
{
if ($this->isBot()) // Do not track bots
return;
$this->tryUpdateStats();
$connection=Yii::app()->db;
$date_fmt = (new DateTime())->format('Y-m-d');
$ipaddr = $_SERVER['REMOTE_ADDR'];
$unique = $connection->createCommand("SELECT COUNT(*) FROM $this->table_today WHERE ipaddress = '$ipaddr'")->queryScalar() == 0;
if ($unique)
{
$connection->createCommand("INSERT INTO $this->table_today (date, ipaddress) VALUES ('$date_fmt', '$ipaddr');")->execute();
}
return $unique;
}
public function getTotalCount()
{
$count = Yii::app()->db->createCommand("SELECT SUM(count) FROM $this->table_stats")->queryScalar();
$count = (($count) ? $count : 0) + $this->getTodayCount();
return $count;
}
public function getTodayCount()
{
$this->tryUpdateStats();
return Yii::app()->db->createCommand("SELECT COUNT(*) FROM $this->table_today")->queryScalar();
}
/**
* @param DateTime $day
*
* @return int
*/
public function getCountForDay($day)
{
if ((new DateTime())->format('Y-m-d') == $day->format('Y-m-d'))
return $this->getTodayCount();
$fmt = $day->format('Y-m-d');
$count = Yii::app()->db->createCommand("SELECT count FROM ms4_hc_stats WHERE date = '$fmt'")->queryScalar();
return ($count) ? $count : 0;
}
//#############################################################
/**
* @return bool
*/
private function isBot()
{
return (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']));
}
private function tryUpdateStats()
{
if ($this->updated)
return; // Only update once per instance
$this->updated = true;
$connection=Yii::app()->db;
$row = $connection->createCommand("SELECT * FROM $this->table_today")->queryRow();
if (! $row)
return false; // nothing to update
$last_date = new DateTime($row['date']);
if ($last_date->format('Y-m-d') != (new DateTime())->format('Y-m-d'))
{
// Get last count
$lastday_count = $connection->createCommand("SELECT COUNT(*) FROM $this->table_today")->queryScalar();
// Insert into stats
$last_date_fmt = $last_date->format('Y-m-d');
$connection->createCommand("INSERT INTO $this->table_stats (date, count) VALUES ('$last_date_fmt', $lastday_count)")->execute();
// Delete today table
$connection->createCommand("DELETE FROM $this->table_today")->execute();
return true;
}
else
{
return false;
}
}
}

View File

@ -14,4 +14,11 @@ class MSController extends CController
public $js_scripts = array();
public $title = null;
public function beforeAction($e){
Yii::app()->hitcounter->increment();
return parent::beforeAction($e);
}
}

View File

@ -48,6 +48,14 @@ return ArrayX::merge(
// application components
'components' =>
[
'hitcounter' =>
[
'class' => 'CHitCounter',
'table_stats' => '{{hc_stats}}',
'table_today' => '{{hc_today}}',
],
'bootstrap' =>
[
'class' => 'bootstrap.components.TbApi',

View File

@ -187,7 +187,10 @@ class Program extends CActiveRecord
* @return string
*/
public function getDirectDownloadLink() {
return '/data/programs/' . $this->Name . '.zip' ;
if ($this->download_url == 'direkt' || is_null($this->download_url) || empty($this->download_url))
return '/data/programs/' . $this->Name . '.zip';
else
return $this->download_url;
}
/**

View File

@ -88,12 +88,10 @@ $this->breadcrumbs =
</div>
<div class="well well-small">
<h2>Program of the day</h2>
<hr>
<?php
$data = array();
$now = new DateTime();
@ -123,4 +121,50 @@ $this->breadcrumbs =
); ?>
</div>
<div class="well well-small">
<h2>Hit counter</h2>
<hr>
<?php
/* @var CHitCounter $hc */
$hc = Yii::app()->hitcounter;
?>
<strong>Hits (today):</strong> <?php echo $hc->getTodayCount(); ?><br />
<strong>Hits (total):</strong> <?php echo $hc->getTotalCount(); ?><br />
<hr>
<?php
$data = array();
$now = new DateTime();
for ($i = 0; $i < 24; $i++) {
$data[] =
[
'Date' => $now->format('d.m.Y :: D'),
'Count' => $hc->getCountForDay($now),
];
$now->modify('-1 day');
}
$this->widget('bootstrap.widgets.TbGridView',
[
'type' => TbHtml::GRID_TYPE_CONDENSED,
'dataProvider' => new CArrayDataProvider($data,
[
'keyField' => 'Date',
'Pagination' =>
[
'PageSize' => 100,
]
]),
]
); ?>
</div>
</div>