Skip to content

Commit

Permalink
better handling of totals when multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
kmonaghan committed Jan 4, 2013
1 parent d874ecd commit a5752a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
26 changes: 19 additions & 7 deletions public/ajax/daily.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
$parts = explode('/', $_GET['from']);
$startTime = strtotime($parts[2] . '-' . $parts[1] . '-' . $parts[0]);
}
else
else
{
$startTime = strtotime('32 days ago');
}
Expand Down Expand Up @@ -81,7 +81,7 @@
$where .= ' AND apple_identifier = ?';

$params[] = $_GET['apple_identifier'];

$apps = array($_GET['apple_identifier'] => $apps[$_GET['apple_identifier']]);
}

Expand All @@ -90,12 +90,12 @@
while($currentTime <= $endTime)
{
$currentDate = date('Y-m-d', $currentTime);

foreach($apps as $appleIdentifier => $value)
{
$data[$currentDate][$appleIdentifier] = array('units' => 0);
$data[$currentDate][$appleIdentifier] = array('units' => 0);
}

$currentTime = strtotime('+1 day', $currentTime);
}
$where = ($where) ? ' WHERE ' . $where : '';
Expand All @@ -105,7 +105,11 @@
$order = ' ORDER BY begin_date';


$query = "SELECT sum(units) as units, apple_identifier, begin_date FROM daily_raw " . $where . $group . $order;
$totalquery = "SELECT sum(units) as units, apple_identifier FROM daily_raw " . $where . ' GROUP BY apple_identifier ORDER BY units DESC';

$sth_overalltotals = $dbh->prepare($totalquery);

$query = "SELECT sum(units) as units, apple_identifier, begin_date FROM daily_raw " . $where . ' ' . $group . $order;

$sth_total = $dbh->prepare($query);

Expand All @@ -120,10 +124,18 @@
{
$sth_total->bindValue($count, $value);
$sth_proceeds->bindValue($count, $value);
$sth_overalltotals->bindValue($count, $value);
$count++;
}
}

$totals = array();
$sth_overalltotals->execute();
while ($row = $sth_overalltotals->fetch(PDO::FETCH_ASSOC))
{
$totals[$row['apple_identifier']] = (int)$row['units'];
}

$sth_total->execute();

while ($row = $sth_total->fetch(PDO::FETCH_ASSOC))
Expand All @@ -148,4 +160,4 @@
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

echo json_encode(array('apps' => $apps, 'data' => $data, 'proceeds' => $proceeds, 'total' => count($data), 'title' => $title, 'start_date' => date('d/m/Y',$startTime), 'end_date' => date('d/m/Y',$endTime)));
echo json_encode(array('apps' => $apps, 'data' => $data, 'proceeds' => $proceeds, 'totals' => $totals, 'total' => count($data), 'title' => $title, 'start_date' => date('d/m/Y',$startTime), 'end_date' => date('d/m/Y',$endTime)));
32 changes: 29 additions & 3 deletions public/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css"></link>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jshashtable.js"></script>
<script type="text/javascript" src="js/jquery.numberformatter-1.2.2.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart", "geochart"]});

Expand All @@ -25,15 +27,21 @@

function drawChart(items)
{
$('#app_list').empty();

var data = new google.visualization.DataTable();
data.addColumn('string', 'Day');

var appPosition = new Array();

var appName = new Array();
var totals = new Array();
var count = 1;

$.each(items.apps, function(i,item){
data.addColumn('number', item);
appPosition[i] = count;
appName[i] = item;
totals[i] = 0;
count++;
});

Expand All @@ -45,6 +53,7 @@

$.each(dateitem, function(i,item){
data.setValue(count, appPosition[i], parseInt(item.units));
totals[i] += parseInt(item.units);
});

count++;
Expand All @@ -56,6 +65,10 @@
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 980, height: 480, title: items.title});

$.each(items.totals, function(i,total){
$('#app_list').append('<tr><td><div class="view_app" id="' + i + '">' + appName[i] + '</div></td><td>' + total + '</td>');
});

var proceeds = '';
$.each(items.proceeds, function(i,bought) {

Expand Down Expand Up @@ -150,8 +163,19 @@
$('#view').live('click', function() {
updateChart();
});

$('.view_app').live('click', function() {
$("#app-select").val($(this).attr('id'));
updateChart();
});
});
</script>
<style type="text/css">
.view_app {
color: blue;
cursor: pointer
}
</style>
</head>

<body>
Expand All @@ -169,7 +193,9 @@
<input type="text" id="to" name="to"/>
<input type="button" id="view" value="View" />
<div id="chart_div"></div>
<table id="app_list"></table>
<div id="proceeds"></div>
<div id='map_canvas'></div>

</body>
</html>
</html>

0 comments on commit a5752a8

Please sign in to comment.