I must admit I was a little bummed when I couldn’t find a built-in way to expose data from Mint as something that Flash could eat.

Not a huge deal, data freedom is but a class away. The code below returns a JSON object of hourly visitor stats (total and unique) for the past 24 hours.

JSON format:
site_stats=[[hour-in-24hour-format,unique-visitors,total-visitors],…]

<?php
class mint_for_chumby {
    /*
    getJSONPast24
    -------------
    Returns a JSON object of the compiled statsfor the past 24 hours
    */
    function getJSONPast24() {
        $current_hour = 
            mktime(
                date('H'), 0, 0, date('m'), date('d'), date('Y')
            );

        echo "site_stats=[";

        //Cycle backwards through the past 24 hours
        for($hour_offset = 23; $hour_offset >= 0; $hour_offset--) {
            if($hour_offset < 23) {
                echo ",";
            }

            $this->getJSONForHour($current_hour, $hour_offset);
        }

        echo "]";
    }

    /*
    getJSONForHour
    --------------
    Takes a unix timestamp (rounded to the hour) and an hour offset, 
    and returns total number of hits for that hour
    */
    function getJSONForHour($current_hour, $past_hour_offset) {

        //Time range
        $start_time = $current_hour - ($past_hour_offset * 3600);
        $end_time = $current_hour - (($past_hour_offset - 1) * 3600);

        $total_visits = 0;
        $unique_visits = 0;
        $hour = date("H",$start_time);

        //Pull data from db, group sessions together to form uniques
        $query =  "SELECT COUNT(session_checksum) as repeat ";
        $query .= "FROM mint_visit ";
        $query .= "WHERE (dt > {$start_time} and dt < {$end_time}) ";
        $query .= "GROUP BY session_checksum";
        $stats_for_hour = mysql_query($query);

        while($stats_row = mysql_fetch_array($stats_for_hour)) {
            $unique_visits++;
            $total_visits += $stats_row['repeat'];
        }

        echo "[{$hour},{$unique_visits},{$total_visits}]";

    }
}
?>

Just call it with:

$mint_funcs = new mint_for_chumby();
$mint_funcs->getJSONPast24();
  • Digg
  • del.icio.us
  • feedmelinks
  • Reddit
  • NewsVine
  • StumbleUpon
  • Technorati

2 Comments

  1. Posted May 13, 2007 at 10:57 am | Permalink

    sweet. man i want to see some pics of the chumby in minty action. Do you have a front end for the data display yet or are you working on that. dRAUPP

  2. Posted May 13, 2007 at 11:12 am | Permalink

    The chumby widget is just about done (I’ll throw some actions shots up once its done). Still trying to figure out some way to use the onboard sensors to interact with it, thinking that a tilt based scroller might be pretty nifty.

Post a Comment

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

*
*