Advertisement
Twilypastes

analytics.php

Mar 21st, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.77 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--
  3.     Author: Twily                2015
  4. -->
  5. <?php
  6.     error_reporting(0);
  7.     date_default_timezone_set("Europe/Oslo");
  8.    
  9.     /*  
  10.      *  Log Format:
  11.      *      <UNIX Timestamp>|<Filename>\n
  12.      *
  13.      *  Example (analytics.log):
  14.      *      ...
  15.      *      1425943113|firefox-css
  16.      *      1425943138|4chan-css
  17.      *      1425943190|homepage
  18.      *      ...
  19.      */
  20.  
  21.     $file   ="./analytics.log";
  22.     $tNow   =time();
  23.  
  24.     //$cDate =filectime($file);
  25.     $cDate  =1425945438;
  26.     $age    =$tNow-$cDate;
  27.  
  28.     if($age<=86400) {                       // <= 24 hours
  29.         $aText=" hour";
  30.         $age=floor($age/60/60);
  31.     } elseif($age>86400 && $age<=588000) {  // <=  7 days
  32.         $aText=" day";
  33.         $age=floor($age/60/60/24);
  34.     } else {                                // >   7 days
  35.         $aText=" week";
  36.         $age=floor($age/60/60/24/7);
  37.     }
  38.     $aText .=($age<>1)?'s':'';
  39.  
  40.     // Offset declarations
  41.     $tHour  =$tNow-(60*60);
  42.     $tDay   =$tNow-(60*60*24);
  43.     $tWeek  =$tNow-(60*60*24*7);
  44.     $tMonth =$tNow-(60*60*24*30);
  45.     $tYear  =$tNow-(60*60*24*365);
  46.  
  47.     $tToday =strtotime('00:00:00');
  48.  
  49.     // Read log line by line into multidimensional f(ull)Arr(ay)
  50.     $fArr=array();
  51.     $i=0;
  52.     $fh=fopen($file,'r') or die("Failed to read log file");
  53.     if($fh) {
  54.         while(!feof($fh)) {
  55.             $buffer=explode('|',fgets($fh,4096));
  56.  
  57.             if($buffer[0]>=$tWeek) { // Maximum time frame
  58.                 $fArr[$i]['time']=$buffer[0];
  59.                 $fArr[$i]['name']=substr($buffer[1],0,-1);
  60.  
  61.                 $i++;
  62.             }
  63.         }
  64.  
  65.         fclose($fh);
  66.     }
  67.  
  68.     // Calculate activity/hour between offset and now
  69.     function activity($offset) {
  70.         global $fArr,$max;
  71.  
  72.         unset($rArr);
  73.         $rArr=array();
  74.  
  75.         $max=1;
  76.         for($i=0;$i<count($fArr);$i++) {
  77.             if($fArr[$i]['time']>=$offset) {
  78.                 $dt=new DateTime('@'.$fArr[$i]['time']);
  79.                 $dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
  80.                 $hour=intval($dt->format('H'));
  81.  
  82.                 // The index of the r(elevant)Arr(ay) is equal to the current hour of the timestamps day
  83.                 $rArr[$hour]+=1;
  84.  
  85.                 // Set highest number of hits
  86.                 $max=($max<$rArr[$hour])?$rArr[$hour]:$max;
  87.             }
  88.         }
  89.  
  90.         return $rArr;
  91.     }
  92.  
  93.     // Calculate hits/file between offset and now
  94.     function hitcount($offset) {
  95.         global $fArr,$max;
  96.  
  97.         unset($rArr);
  98.         $rArr=array();
  99.  
  100.         for($i=0;$i<count($fArr);$i++) {
  101.             // Add everything from offset into r(elevant)Arr(ay)
  102.             if($fArr[$i]['time']>=$offset) array_push($rArr,$fArr[$i]['name']);
  103.         }
  104.  
  105.         // Sort alphabetically
  106.         // !!(Required for counting the number of repeating equal files in array)!!
  107.         sort($rArr);
  108.  
  109.         // Calculate hits/file in h(its)Arr(ay)
  110.         unset($hArr);
  111.         $hArr=array();
  112.  
  113.         $max=1; $j=-1;
  114.         for($i=0;$i<count($rArr);$i++) {
  115.             if($rArr[$i]==$hArr[$j]['name']) {
  116.                 // Add +1 to file hit (if equal to last one)
  117.                 $hArr[$j]['hits']++;
  118.  
  119.                 // Set highest number of hits
  120.                 $max=($max<$hArr[$j]['hits'])?$hArr[$j]['hits']:$max;
  121.             } else {
  122.                 // Set next file to 1 hit
  123.                 $j++;
  124.                 $hArr[$j]['hits']=1;
  125.             }
  126.  
  127.             $hArr[$j]['name']=$rArr[$i];
  128.         }
  129.  
  130.         rsort($hArr);
  131.         return $hArr;
  132.     }
  133.  
  134.     // Generate graph display data/row string
  135.     function generate($hits,$name) {
  136.         global $max;
  137.  
  138.         $hText  =($hits<>1)?' hits':' hit';
  139.         $percent=floor($hits*100/$max);
  140.  
  141.         $rowStr ="    <div class=\"row\">";
  142.         $rowStr.="<div class=\"cell\">".$name."</div>";
  143.         $rowStr.="<div class=\"cell\"><div class=\"bar\" style=\"width: ".$percent."%;\"><span>".$hits.$hText."</span></div></div>";
  144.         $rowStr.="</div>\n";
  145.  
  146.         return $rowStr;
  147.     }
  148. ?>
  149. <html>
  150. <head>
  151. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  152. <title>twily.info :: analytics</title>
  153.  
  154. <style type="text/css">
  155. html,body {
  156.     font-size: 10pt;
  157.     font-family: "Droid Sans", "DejaVu Sans", "Open Sans", "Liberation Sans", "Segoe UI", Sans;
  158.     background: #17181A;
  159.     color: #CECFD1;
  160.     margin: 0; padding: 12px 15px 12px 12px;
  161. }
  162.  
  163. a:link, a:visited { color: #CB873D; text-decoration: none; }
  164. a:hover, a:active { color: #F4AA5C; text-decoration: underline; }
  165.  
  166. hr {
  167.     margin: 24px auto; padding: 0;
  168.     width: 24px; height: 24px;
  169.     background: #27282B;
  170.     border: 0; border-radius: 24px;
  171. }
  172.  
  173. .table {
  174.     display: table;
  175.     font-family: "Droid Sans Mono", "DejaVu Sans Mono", "Liberation Mono", "Lucida Console", Monospace;
  176.     width: 100%; height: auto;
  177.     background: #111113; color: #AAABAD;
  178.     margin: 0 auto; padding: 3px;
  179.     margin-bottom: 24px;
  180. }
  181.     .table .row { display: table-row; }
  182.         .table .row .cell {
  183.             display: table-cell;
  184.             height: 20px;
  185.             padding: 1px;
  186.             vertical-align: middle;
  187.         }
  188.         .table .row .cell:nth-child(1) {
  189.             width: 175px;
  190.             text-align: right; padding-right: 6px;
  191.         }
  192.             .table .row .cell .bar {
  193.                 height: 20px;
  194.                 background: #CB873D;
  195.                 border-radius: 1px;
  196.             }
  197.                 .table .row .cell .bar span {
  198.                     white-space: nowrap; overflow: visible;
  199.                     padding-left: 4px;
  200.                     color: #111113; font-size: 8pt;
  201.                     line-height: 20px;
  202.                 }
  203.  
  204. .title {
  205.     text-align: center; font-weight: bold;
  206.     width: 100%; height: 18px; line-height: 18px;
  207.     background: #27282B;
  208.     margin: 4px auto; padding: 3px;
  209. }
  210.  
  211. #note { float: left; }
  212. #back { float: right; }
  213. </style>
  214. </head>
  215. <body>
  216.  
  217.  
  218. <div class="title">Top 10 Last 60 Minutes</div>         <!-- ---- 60 Minutes ---- -->
  219. <div class="table">
  220. <?php
  221.     // Draw statistics from o(utput)Arr(ay)
  222.     unset($oArr);
  223.     $oArr=array();
  224.     $oArr=hitcount($tHour);
  225.     for($i=0;$i<count($oArr) && $i<10;$i++) echo generate($oArr[$i]['hits'],$oArr[$i]['name']);
  226.  
  227. ?>
  228. </div>
  229.  
  230. <div class="title">Top 10 Last 24 Hours</div>           <!-- ----  24 Hours  ---- -->
  231. <div class="table">
  232. <?php
  233.     // Draw statistics from o(utput)Arr(ay)
  234.     unset($oArr);
  235.     $oArr=array();
  236.     $oArr=hitcount($tDay);
  237.     for($i=0;$i<count($oArr) && $i<10;$i++) echo generate($oArr[$i]['hits'],$oArr[$i]['name']);
  238. ?>
  239. </div>
  240.  
  241. <div class="title">Top 10 Last 7 Days</div>             <!-- ----   7 Days   ---- -->
  242. <div class="table">
  243. <?php
  244.     // Draw statistics from o(utput)Arr(ay)
  245.     unset($oArr);
  246.     $oArr=array();
  247.     $oArr=hitcount($tWeek);
  248.     for($i=0;$i<count($oArr) && $i<10;$i++) echo generate($oArr[$i]['hits'],$oArr[$i]['name']);
  249. ?>
  250. </div>
  251.  
  252. <hr />
  253.  
  254. <div class="title">Activity/Hour Last 7 Days <?php echo "(".date("T").")"; ?></div>      <!-- ----  Activity 7 Days  ---- -->
  255. <div class="table">
  256. <?php
  257.     // Draw statistics from o(utput)Arr(ay)
  258.     unset($oArr);
  259.     $oArr=array();
  260.     $oArr=activity($tWeek);
  261.     for($i=0;$i<count($oArr);$i++) echo generate($oArr[$i],sprintf('%02d',$i)." - ".((($i+1)==24)?"00":sprintf('%02d',($i+1)))." &nbsp; ".(($i>=6 && $i<18)?"&#9728;":"&#9790;"));
  262. ?>
  263. </div>
  264.  
  265. <div id="note">Analytics started <?php echo $age.$aText; ?> ago.</div> <!-- Visitors at this time: 90 746 -->
  266. <div id="back">
  267. <a href="javascript:location.reload();" target="_self">Refresh</a> |
  268. <a href="http://pastebin.com/v3GKeue3" target="_blank">Source</a> |
  269. <a href="/" target="_self">Return to twily.info</a>
  270. </div>
  271.  
  272.  
  273. </body>
  274. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement