Clean up counter module

- 카운터 모듈을 정리하여 불필요한 쿼리 및 트랜잭션 제거
- 매일 첫 방문시 쿼리 오류 발생하는 문제 해결
- 더이상 사용하지 않는 $site_srl 기준의 테이블은 삭제
This commit is contained in:
Kijin Sung 2021-01-08 20:53:54 +09:00
parent 350d0cd20c
commit 8d2b105847
21 changed files with 83 additions and 379 deletions

View file

@ -10,7 +10,7 @@ if(!defined('__XE__'))
* @brief Counter add-on
*/
// Execute if called_position is before_display_content
if($called_position == 'before_module_init' && Context::get('module') != 'admin' && Context::getResponseMethod() == 'HTML' && Context::isInstalled() && !isCrawler())
if($called_position == 'before_display_content' && Context::get('module') != 'admin' && Context::getResponseMethod() == 'HTML' && Context::isInstalled() && !isCrawler())
{
$oCounterController = getController('counter');
$oCounterController->counterExecute();

View file

@ -41,22 +41,19 @@ class counterAdminView extends counter
$oCounterModel = getModel('counter');
// get a total count and daily count
$site_module_info = Context::get('site_module_info');
$status = $oCounterModel->getStatus(array(0, $selected_date), $site_module_info->site_srl);
$status = $oCounterModel->getStatus(array(0, $selected_date));
Context::set('total_counter', $status[0]);
Context::set('selected_day_counter', $status[$selected_date]);
// get data by time, day, month, and year
$type = Context::get('type');
if(!$type)
{
$type = 'day';
Context::set('type', $type);
}
$detail_status = $oCounterModel->getHourlyStatus($type, $selected_date, $site_module_info->site_srl);
$detail_status = $oCounterModel->getHourlyStatus($type, $selected_date);
Context::set('detail_status', $detail_status);
// display

View file

@ -25,7 +25,13 @@ class counter extends ModuleObject
*/
function checkUpdate()
{
return FALSE;
$oDB = DB::getInstance();
if ($oDB->isTableExists('counter_site_status'))
{
return true;
}
return false;
}
/**
@ -35,7 +41,11 @@ class counter extends ModuleObject
*/
function moduleUpdate()
{
$oDB = DB::getInstance();
if ($oDB->isTableExists('counter_site_status'))
{
$oDB->dropTable('counter_site_status');
}
}
/**

View file

@ -37,178 +37,98 @@ class counterController extends counter
*/
function counterExecute()
{
$oDB = DB::getInstance();
$oDB->begin();
$site_module_info = Context::get('site_module_info');
$site_srl = (int) $site_module_info->site_srl;
// Check the logs
$oCounterModel = getModel('counter');
if($oCounterModel->isInsertedTodayStatus($site_srl))
if(CounterModel::isLogged())
{
if($oCounterModel->isLogged($site_srl))
{
// Register pageview
$this->insertPageView($site_srl);
}
else // If unregistered IP
{
// Leave logs
$this->insertLog($site_srl);
// Register unique and pageview
$this->insertUniqueVisitor($site_srl);
}
$this->insertPageView();
}
else // Register today's row if not exist
else
{
$this->insertTodayStatus(0, $site_srl);
// check user if the previous row exists
$this->insertUniqueVisitor();
}
$oDB->commit();
}
/**
* Leave logs
*
* @param integer $site_srl
* @return Object result of count query
* @return void
*/
function insertLog($site_srl = 0)
public function insertLog()
{
$args = new stdClass();
$args->regdate = date("YmdHis");
$args->regdate = date('YmdHis');
$args->user_agent = substr($_SERVER['HTTP_USER_AGENT'], 0, 250);
$args->site_srl = $site_srl;
return executeQuery('counter.insertCounterLog', $args);
$args->site_srl = 0;
executeQuery('counter.insertCounterLog', $args);
}
/**
* Register the unique visitor
*
* @param integer $site_srl
* @return void
*/
function insertUniqueVisitor($site_srl = 0)
public function insertUniqueVisitor()
{
$args = new stdClass();
$args->regdate = '0,' . date('Ymd');
$oDB = DB::getInstance();
$oDB->begin();
if($site_srl)
$args = new stdClass();
$args->regdate = [0, $date = date('Ymd')];
executeQuery('counter.updateCounterUnique', $args);
$affected_rows = $oDB->getAffectedRows();
if ($affected_rows == 1)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.updateSiteCounterUnique', $args);
$args = new stdClass;
$args->regdate = $date;
executeQuery('counter.insertTodayStatus', $args);
}
else
if ($affected_rows == 0)
{
$output = executeQuery('counter.updateCounterUnique', $args);
$args = new stdClass;
$args->regdate = 0;
executeQuery('counter.insertTodayStatus', $args);
}
$this->insertLog();
$oDB->commit();
}
/**
* Register pageview
*
* @param integer $site_srl
* @return void
*/
function insertPageView($site_srl = 0)
public function insertPageView()
{
$args = new stdClass;
$args->regdate = '0,' . date('Ymd');
if($site_srl)
{
$args->site_srl = $site_srl;
executeQuery('counter.updateSiteCounterPageview', $args);
}
else
{
executeQuery('counter.updateCounterPageview', $args);
}
$args->regdate = [0, date('Ymd')];
executeQuery('counter.updateCounterPageview', $args);
}
/**
* Add the total counter status
*
* @param integer $site_srl
* @return void
* @deprecated
*/
function insertTotalStatus($site_srl = 0)
public function insertTodayStatus()
{
$args = new stdClass();
$args->regdate = 0;
if($site_srl)
{
$args->site_srl = $site_srl;
executeQuery('counter.insertSiteTodayStatus', $args);
}
else
{
executeQuery('counter.insertTodayStatus', $args);
}
$this->insertUniqueVisitor();
}
/**
* Add today's counter status
*
* @param integer $regdate date(YYYYMMDD) type
* @param integer $site_srl
* @return void
* @deprecated
*/
function insertTodayStatus($regdate = 0, $site_srl = 0)
public function insertTotalStatus()
{
$args = new stdClass();
if($regdate)
{
$args->regdate = $regdate;
}
else
{
$args->regdate = date("Ymd");
}
if($site_srl)
{
$args->site_srl = $site_srl;
$query_id = 'counter.insertSiteTodayStatus';
$u_args = new stdClass();
$u_args->site_srl = $site_srl; // /< when inserting a daily row, attempt to inser total rows(where regdate=0) together
executeQuery($query_id, $u_args);
}
else
{
$query_id = 'counter.insertTodayStatus';
executeQuery($query_id); // /< when inserting a daily row, attempt to inser total rows(where regdate=0) together
}
$output = executeQuery($query_id, $args);
// Leave logs
$this->insertLog($site_srl);
// Register unique and pageview
$this->insertUniqueVisitor($site_srl);
}
/**
* Delete counter logs of the specific virtual site
*
* @param integer $site_srl
* @return void
* @deprecated
*/
function deleteSiteCounterLogs($site_srl)
public function deleteSiteCounterLogs()
{
$args = new stdClass();
$args->site_srl = $site_srl;
executeQuery('counter.deleteSiteCounter', $args);
executeQuery('counter.deleteSiteCounterLog', $args);
}
}
}
/* End of file counter.controller.php */
/* Location: ./modules/counter/counter.controller.php */

View file

@ -20,13 +20,12 @@ class counterModel extends counter
/**
* Verify logs
*
* @param integer $site_srl Site_srl
* @return bool
*/
function isLogged($site_srl = 0)
public static function isLogged()
{
$date = date('Ymd');
if (isset($_SESSION['counter_logged'][$date]) && $_SESSION['counter_logged'][$date])
if (isset($_SESSION['counter_logged'][$date]))
{
return true;
}
@ -34,7 +33,6 @@ class counterModel extends counter
$args = new stdClass();
$args->regdate = $date;
$args->ipaddress = \RX_CLIENT_IP;
$args->site_srl = $site_srl;
$output = executeQuery('counter.getCounterLog', $args);
$iplogged = $output->data->count ? true : false;
if ($iplogged)
@ -48,63 +46,25 @@ class counterModel extends counter
/**
* Check if a row of today's counter status exists
*
* @param integer $site_srl Site_srl
* @return bool
* @deprecated
*/
function isInsertedTodayStatus($site_srl = 0)
public static function isInsertedTodayStatus()
{
$args = new stdClass;
$args->regdate = date('Ymd');
$cache_key = 'counter:insertedTodayStatus:' . $site_srl . '_' . $args->regdate;
$insertedTodayStatus = Rhymix\Framework\Cache::get($cache_key);
if(!$insertedTodayStatus)
{
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteTodayStatus', $args);
}
else
{
$output = executeQuery('counter.getTodayStatus', $args);
}
$insertedTodayStatus = !!$output->data->count;
if($insertedTodayStatus)
{
Rhymix\Framework\Cache::set($cache_key, true, 0, true);
$_old_date = date('Ymd', strtotime('-1 day'));
Rhymix\Framework\Cache::delete('counter:insertedTodayStatus:' . $site_srl . '_' . $_old_date);
}
}
return $insertedTodayStatus;
}
/**
* Get access statistics for a given date
*
* @param mixed $selected_date Date(YYYYMMDD) list
* @param integer $site_srl Site_srl
* @return Object
*/
function getStatus($selected_date, $site_srl = 0)
public static function getStatus($selected_date)
{
// If more than one date logs are selected
$args = new stdClass();
$args->regdate = is_array($selected_date) ? join(',', $selected_date) : $selected_date;
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteCounterStatusDays', $args);
}
else
{
$output = executeQuery('counter.getCounterStatusDays', $args);
}
$output = executeQueryArray('counter.getCounterStatusDays', $args);
$status = $output->data;
if(!is_array($selected_date))
@ -112,7 +72,6 @@ class counterModel extends counter
return $status;
}
if(!is_array($status)) $status = array($status);
$output = array();
foreach($status as $val)
{
@ -127,10 +86,10 @@ class counterModel extends counter
*
* @param string $type Choice time interval (year, week, month, hour or DEFAULT)
* @param integer $selected_date Date(YYYYMMDD)
* @param integer $site_srl Site_srl
* @param integer $site_srl unused
* @return Object
*/
function getHourlyStatus($type = 'hour', $selected_date, $site_srl = 0, $isPageView = false)
public static function getHourlyStatus($type = 'hour', $selected_date, $site_srl = 0, $isPageView = false)
{
$max = 0;
$sum = 0;
@ -140,17 +99,7 @@ class counterModel extends counter
{
case 'year' :
// Get a date to start counting
if($site_srl)
{
$args = new stdClass();
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteStartLogDate', $args);
}
else
{
$output = executeQuery('counter.getStartLogDate');
}
$output = executeQuery('counter.getStartLogDate', new stdClass);
if(!($start_year = substr($output->data->regdate, 0, 4)))
{
$start_year = date("Y");
@ -161,17 +110,7 @@ class counterModel extends counter
$args = new stdClass();
$args->start_date = sprintf('%04d0000', $i);
$args->end_date = sprintf('%04d1231', $i);
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteCounterStatus', $args);
}
else
{
$output = executeQuery('counter.getCounterStatus', $args);
}
$output = executeQuery('counter.getCounterStatus', $args);
$count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
$status->list[$i] = $count;
@ -207,17 +146,7 @@ class counterModel extends counter
$args = new stdClass();
$args->start_date = $day;
$args->end_date = $day;
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteCounterStatus', $args);
}
else
{
$output = executeQuery('counter.getCounterStatus', $args);
}
$output = executeQuery('counter.getCounterStatus', $args);
$count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
$status->list[$day] = $count;
@ -234,17 +163,7 @@ class counterModel extends counter
$args = new stdClass();
$args->start_date = sprintf('%04d%02d00', $year, $i);
$args->end_date = sprintf('%04d%02d31', $year, $i);
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteCounterStatus', $args);
}
else
{
$output = executeQuery('counter.getCounterStatus', $args);
}
$output = executeQuery('counter.getCounterStatus', $args);
$count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
$status->list[$i] = $count;
@ -260,18 +179,7 @@ class counterModel extends counter
$args = new stdClass();
$args->start_date = sprintf('%08d%02d0000', $selected_date, $i);
$args->end_date = sprintf('%08d%02d5959', $selected_date, $i);
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteCounterLogStatus', $args);
}
else
{
$args->site_srl = 0;
$output = executeQuery('counter.getCounterLogStatus', $args);
}
$output = executeQuery('counter.getCounterLogStatus', $args);
$count = (int) $output->data->count;
$status->list[$i] = $count;
@ -291,17 +199,7 @@ class counterModel extends counter
$args = new stdClass();
$args->start_date = sprintf('%04d%02d%02d', $year, $month, $i);
$args->end_date = sprintf('%04d%02d%02d', $year, $month, $i);
if($site_srl)
{
$args->site_srl = $site_srl;
$output = executeQuery('counter.getSiteCounterStatus', $args);
}
else
{
$output = executeQuery('counter.getCounterStatus', $args);
}
$output = executeQuery('counter.getCounterStatus', $args);
$count = (int)($isPageView ? $output->data->pageview : $output->data->unique_visitor);
$status->list[$i] = $count;
@ -322,7 +220,7 @@ class counterModel extends counter
{
//for last week
$date1 = date('Ymd', strtotime('-1 week'));
$output1 = $this->getHourlyStatus('week', $date1);
$output1 = self::getHourlyStatus('week', $date1);
$tmp = array();
foreach($output1->list as $key => $value)
@ -333,7 +231,7 @@ class counterModel extends counter
//for this week
$date2 = date('Ymd');
$output2 = $this->getHourlyStatus('week', $date2);
$output2 = self::getHourlyStatus('week', $date2);
$tmp = array();
foreach($output2->list as $key => $value)
@ -350,7 +248,7 @@ class counterModel extends counter
{
//for last week
$date1 = date('Ymd', strtotime('-1 week'));
$output1 = $this->getHourlyStatus('week', $date1, 0, TRUE);
$output1 = self::getHourlyStatus('week', $date1, 0, TRUE);
$tmp = array();
foreach($output1->list as $key => $value)
@ -361,7 +259,7 @@ class counterModel extends counter
//for this week
$date2 = date('Ymd');
$output2 = $this->getHourlyStatus('week', $date2, 0, TRUE);
$output2 = self::getHourlyStatus('week', $date2, 0, TRUE);
$tmp = array();
foreach($output2->list as $key => $value)

View file

@ -1,8 +0,0 @@
<query id="deleteSiteCounter" action="delete">
<tables>
<table name="counter_site_status" />
</tables>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" filter="number" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,8 +0,0 @@
<query id="deleteSiteCounterLog" action="delete">
<tables>
<table name="counter_log" />
</tables>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" filter="number" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -6,7 +6,7 @@
<column name="count(*)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" default="0" pipe="and" />
<condition operation="equal" column="site_srl" var="site_srl" />
<condition operation="equal" column="ipaddress" var="ipaddress" pipe="and" />
<condition operation="like_prefix" column="regdate" var="regdate" notnull="notnull" pipe="and" />
</conditions>

View file

@ -6,7 +6,7 @@
<column name="count(*)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" notnull="notnull" pipe="and" />
<condition operation="equal" column="site_srl" var="site_srl" />
<condition operation="more" column="regdate" var="start_date" notnull="notnull" pipe="and" />
<condition operation="less" column="regdate" var="end_date" notnull="notnull" pipe="and" />
</conditions>

View file

@ -1,14 +0,0 @@
<query id="getSiteCounterStatus" action="select">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="sum(unique_visitor)" alias="unique_visitor" />
<column name="sum(pageview)" alias="pageview" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" default="0" />
<condition operation="more" column="regdate" var="start_date" notnull="notnull" pipe="and" />
<condition operation="less" column="regdate" var="end_date" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,12 +0,0 @@
<query id="getSiteCounterStatus" action="select">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="*" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" default="0" />
<condition operation="in" column="regdate" var="regdate" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,4 +1,4 @@
<query id="getSiteStartLogDate" action="select">
<query id="getSiteLogDate" action="select">
<tables>
<table name="counter_status" />
</tables>

View file

@ -1,12 +0,0 @@
<query id="getSiteStartLogDate" action="select">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="min(regdate)" alias="regdate" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" notnull="notnull" />
<condition operation="excess" column="regdate" default="1" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,12 +0,0 @@
<query id="getSiteTodayStatus" action="select">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="count(*)" alias="count" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" default="0" />
<condition operation="equal" column="regdate" var="regdate" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -3,8 +3,8 @@
<table name="counter_log" />
</tables>
<columns>
<column name="site_srl" var="site_srl" notnull="notnull" default="0" />
<column name="regdate" var="regdate" default="curdate()" notnull="notnull" />
<column name="site_srl" var="site_srl" default="0" />
<column name="regdate" var="regdate" notnull="notnull" default="curdate()" />
<column name="ipaddress" var="ipaddress" notnull="notnull" default="ipaddress()" />
<column name="user_agent" var="user_agent" />
</columns>

View file

@ -1,11 +0,0 @@
<query id="insertSiteTodayStatus" action="insert">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="site_srl" var="site_srl" notnull="notnull" default="0" />
<column name="regdate" var="regdate" default="0" notnull="notnull" />
<column name="unique_visitor" default="0" />
<column name="pageview" default="0" />
</columns>
</query>

View file

@ -4,7 +4,7 @@
</tables>
<columns>
<column name="regdate" var="regdate" default="0" notnull="notnull" />
<column name="unique_visitor" default="0" />
<column name="pageview" default="0" />
<column name="unique_visitor" var="unique_visitor" default="1" />
<column name="pageview" var="pageview" default="1" />
</columns>
</query>

View file

@ -1,12 +0,0 @@
<query id="updateSiteCounterPageview" action="update">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="pageview" default="plus(1)" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" default="0" />
<condition operation="in" column="regdate" var="regdate" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,13 +0,0 @@
<query id="updateSiteCounterUnique" action="update">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="unique_visitor" default="plus(1)" />
<column name="pageview" default="plus(1)" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" notnull="notnull" />
<condition operation="in" column="regdate" var="regdate" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,13 +0,0 @@
<query id="updateSiteTotalCounterUnique" action="update">
<tables>
<table name="counter_site_status" />
</tables>
<columns>
<column name="unique_visitor" default="plus(1)" />
<column name="pageview" default="plus(1)" />
</columns>
<conditions>
<condition operation="equal" column="site_srl" var="site_srl" notnull="notnull" />
<condition operation="equal" column="regdate" default="00000000" notnull="notnull" pipe="and" />
</conditions>
</query>

View file

@ -1,6 +0,0 @@
<table name="counter_site_status">
<column name="site_srl" type="number" size="11" notnull="notnull" unique="site_status" />
<column name="regdate" type="number" size="11" notnull="notnull" unique="site_status" />
<column name="unique_visitor" type="number" size="11" default="0" />
<column name="pageview" type="number" size="11" default="0" />
</table>