Feeling Dirty After So Much SQL

Feeling Dirty After So Much SQL

What this does is go through my Apache log in my MySQL DB and calculate unique hits, overall hits, amount of bandwidth used per month, and only for the past 3 months! Making sure each month begins and ends on the first and last day of the month, not using timestamps from the current date! Phew. Brain hurts.

$size = $this->Show->Episode->Log->find('all', array('conditions'=>array('Log.filecode'=>$filecodes,'UNIX_TIMESTAMP(`timestamp`)
BETWEEN
UNIX_TIMESTAMP(DATE_FORMAT(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), \'%Y%m01000000\'))
AND
UNIX_TIMESTAMP(DATE_FORMAT(LAST_DAY(NOW()), \'%Y%m%d235959\'))'), 'group'=>array('YEAR(timestamp)', 'MONTH(timestamp)'),
'fields'=>array('Log.*', 'UNIX_TIMESTAMP(timestamp) as timestamp', 'SUM(Log.size) AS size', 'COUNT(*) as hits', 'COUNT(distinct ip) as uhits'), 'order'=>array('timestamp DESC')));

Posted by VoiDeT

Categorised under CakePHP
Bookmark the permalink or leave a trackback.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or