Usage: ( Here I supply an exchange rate of 140 )
{
"2013-10-09": {
"generate": 0.0533301,
"generate_count": 1,
"generate_usd": 7.466214
},
"2013-10-11": {
"generate": 0.01159667,
"generate_count": 1,
"generate_usd": 1.6235338
},
"2013-10-13": {
"generate": 0.04773484,
"generate_count": 3,
"generate_usd": 6.6828776
},
"2013-10-14": {
"generate": 0.14187266,
"generate_count": 2,
"generate_usd": 19.8621724
},
"2013-10-15": {
"generate": 0.27180136,
"generate_count": 3,
"generate_usd": 38.0521904
},
"2013-10-16": {
"immature": 0.23548219,
"immature_count": 3,
"immature_usd": 32.9675066
}
}{
"generate": 0.52633563,
"generate_count": 10,
"immature": 0.23548219,
"immature_count": 3
}{
"all_count": 13,
"all": 0.76181782,
"revenue_count": 13,
"revenue": 0.76181782,
"revenue_usd": 106.6544948
}
$fh = STDIN;
$buf = stream_get_contents( $fh );
$usd_rate = @$argv[1];
$data = json_decode( $buf );
$amounts = array();
$daily = array();
foreach( $data as $trans ) {
if( @$trans->amount ) {
if( @$amounts[$trans->category] ) {
$amounts[$trans->category] += $trans->amount;
$amounts[$trans->category . '_count'] += 1;
}
else {
$amounts[$trans->category] = $trans->amount;
$amounts[$trans->category . '_count'] = 1;
}
$date = date('Y-m-d', $trans->time );
if( @$daily[$date][$trans->category] ) {
$daily[$date][$trans->category] += $trans->amount;
$daily[$date][$trans->category . '_count'] += 1;
}
else {
$daily[$date][$trans->category] = $trans->amount;
$daily[$date][$trans->category . '_count'] = 1;
}
if( $usd_rate ) {
$daily[$date][$trans->category . '_usd'] = $daily[$date][$trans->category] * $usd_rate;
}
}
}
$summary = array();
$summary['all_count'] = @$amounts['generate_count'] + @$amounts['orphan_count'] + @$amounts['immature_count'];
$summary['all'] = @$amounts['generate'] + @$amounts['orphan'] + @$amounts['immature'];
$summary['revenue_count'] = @$amounts['generate_count'] + @$amounts['immature_count'];
$summary['revenue'] = @$amounts['generate'] + @$amounts['immature'];
if( $usd_rate ) {
$summary['revenue_usd'] = $summary['revenue'] * $usd_rate;
}
echo json_encode($daily, JSON_PRETTY_PRINT);
echo json_encode($amounts, JSON_PRETTY_PRINT);
echo json_encode($summary, JSON_PRETTY_PRINT);