Skip to content

Commit

Permalink
Fixed geoip filter
Browse files Browse the repository at this point in the history
Rewrote logic for creating list of chart rows
  • Loading branch information
jdorn committed Jun 13, 2012
1 parent 4d64c61 commit b0cf06a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 51 deletions.
18 changes: 13 additions & 5 deletions classes/filters/geoipFilter.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
<?php
class geoipFilter extends FilterBase {
public static function filter($value, $options = array(), $report=null) {
$record = @geoip_record_by_name($value);
$record = geoip_record_by_name($value['raw_value']);

if($record) {
$value = $record['city'];
$value['value'] = $record['city'];
if($record['country_code'] !== 'US') {
$value .= ' '.$record['country_name'];
$value['value'] .= ' '.$record['country_name'];
}
else {
$value .= ', '.$record['region'];
$value['value'] .= ', '.$record['region'];
}
$value['chartval'] = array('Latitude'=>$record['latitude'],'Longitude'=>$record['longitude'],'Location'=>$value['value']);
}
else {
$value['chartval'] = array('Latitude'=>0, 'Longitude'=>0, 'Location'=>'Unknown');
}

return trim($value);

$value['value'] = trim($value['value']);

return $value;
}
}
101 changes: 62 additions & 39 deletions classes/headers/ChartHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,68 +82,90 @@ public static function filterRowTwoDim($num, $row, &$report) {
) {
return;
}

$i = 1;
$chartrowvals = array();
foreach($row['values'] as $key=>$value) {
//determine if this column should appear in a chart
$column_in_chart = false;
$x = false;

$vals = array();
$i = 1;
foreach($row['values'] as $key=>$value) {
$value['i'] = $i;

if(!$xval && $i===1 && !isset($report->options['Charts'][$num]['x'])) {
$column_in_chart = true;
$x = true;
}
elseif(!$xval && isset($report->options['Charts'][$num]['x'])
&& (
$value['key']===$report->options['Charts'][$num]['x']
|| "$i" === "".$report->options['Charts'][$num]['x']
)
) {
$column_in_chart = true;
$x = true;
}
elseif(!isset($report->options['Charts'][$num]['y'])) {
$column_in_chart = true;
if(isset($value['chartval']) && is_array($value['chartval'])) {
foreach($value['chartval'] as $ckey=>$cval) {
$value['raw_value'] = trim($cval,'%$ ');
$value['value'] = $cval;
$value['key'] = $ckey;
$value['first'] = !$vals;
$vals[] = $value;
}
}
elseif(in_array($value['key'],$report->options['Charts'][$num]['y'],true)
|| in_array("$i",$report->options['Charts'][$num]['y'])
) {
$column_in_chart = true;
else {
$value['raw_value'] = trim($value['raw_value'],'%$ ');
$vals[] = $value;
}

$i++;
}

$chartrowvals = array();
$chartrowvals_x = array();

//loop through the row's values
foreach($vals as $key=>$value) {
$i = $value['i'];

//if the x column is explicitly defined and this is one of them
$is_x = isset($report->options['Charts'][$num]['x']) && (in_array("$i",$report->options['Charts'][$num]['x']) || in_array($key,$report->options['Charts'][$num]['x'],true));

//if the x column is not explicitly defined, assume it is the first column
if(!isset($report->options['Charts'][$num]['x']) && $i==1) $is_x = true;

//if the y column is explicitly defined and this is one of them
$is_y = !$is_x && isset($report->options['Charts'][$num]['y']) && (in_array("$i",$report->options['Charts'][$num]['y']) || in_array($key,$report->options['Charts'][$num]['y'],true));

//if the y column is not explicitly defined, assume every column is a y column
if(!$is_x && !isset($report->options['Charts'][$num]['y'])) $is_y = true;


//determine if this column should appear in a chart
$column_in_chart = $is_x || $is_y;

if(!$column_in_chart) {
continue;
}

if($x) {
$xval = array(
$chartval = is_numeric($value['raw_value'])? $value['raw_value'] : '"'.$value['raw_value'].'"';

if($is_x) {
array_push($chartrowvals_x, array(
'key'=>$value['key'],
'value'=>$value['raw_value'],
'first'=>true
);
'value'=>$chartval
));
}
else {
$chartrowvals[] = array(
array_push($chartrowvals, array(
'key'=>$value['key'],
'value'=>floatval($value['raw_value']),
'first'=>false
);
'value'=>$chartval
));
}
}

$chartrowvals_x = array_reverse($chartrowvals_x);

foreach($chartrowvals_x as $val) {
array_unshift($chartrowvals,$val);
}

array_unshift($chartrowvals,$xval);
$is_first = true;
foreach($chartrowvals as &$value) {
$value['first'] = $is_first;
$is_first = false;
}

//echo "<pre>".print_r($chartrowvals,true)."</pre>";
$first = count($report->options['Charts'][$num]['Rows']);
$numrows = count($report->options['Charts'][$num]['Rows']);

$report->options['Charts'][$num]['Rows'][] = array(
'values'=>$chartrowvals,
'first'=>!$first
'first'=>!$numrows
);
}

Expand Down Expand Up @@ -195,7 +217,7 @@ public static function filterRowOneDim($num, $row, &$report) {
}

public static function filterRow($row, &$report) {
if($row['first']) return;
//if($row['first']) return;

foreach($report->options['Charts'] as $num=>$value) {
if(isset($report->options['Charts'][$num]['buckets'])) {
Expand All @@ -213,5 +235,6 @@ public static function beforeRender(&$report) {
foreach($report->options['Rows'] as $key=>$row) {
self::filterRow($row,$report);
}

}
}
2 changes: 1 addition & 1 deletion templates/html/page.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{#has_charts}}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
google.load("visualization", "1", {packages:["corechart"]});
google.load("visualization", "1", {packages:["corechart","geochart"]});
</script>
{{/has_charts}}
<style>
Expand Down
16 changes: 10 additions & 6 deletions templates/html/report.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,29 @@ h1 {

{{#has_charts}}
<script type="text/javascript">
drawCharts();
function drawCharts() {
{{#Charts}}
var data_{{num}} = google.visualization.arrayToDataTable([
{{#Rows}}{{#first}}
[{{#values}}{{^first}}",{{/first}}"{{key}}{{/values}}"]
{{/first}}
,[{{#values}}{{#first}}"{{value}}"{{/first}}{{^first}},{{value}}{{/first}}{{/values}}]
{{#Rows}}
{{#first}}
[{{#values}}{{^first}},{{/first}}{{^is_array}}"{{key}}"{{/is_array}}{{/values}}]
{{/first}}
,[{{#values}}{{^first}},{{/first}}{{{value}}}{{/values}}]
{{/Rows}}
]);
var options_{{num}} = {
title: '{{title}}'
title: '{{title}}',
displayMode: 'markers',
colorAxis: {colors: ['blue', 'red']},
sizeAxis: {minValue: 1, maxSize: 10}
};
var chart_{{num}} = new google.visualization.{{type}}(document.getElementById('chart_div_{{num}}'));
chart_{{num}}.draw(data_{{num}}, options_{{num}});
{{/Charts}}
}
drawCharts();
</script>

{{#Charts}}
Expand Down

0 comments on commit b0cf06a

Please sign in to comment.