Skip to content

Instantly share code, notes, and snippets.

@f5-rahm
Created November 17, 2022 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f5-rahm/9f967e7567be3dd9491352e6001b5c58 to your computer and use it in GitHub Desktop.
Save f5-rahm/9f967e7567be3dd9491352e6001b5c58 to your computer and use it in GitHub Desktop.
Update of Colin Walker's original Heatmap iRule to utilize Google's Geocharts
ltm rule /Common/heatmap_2022 {
when RULE_INIT priority 500 {
set static::geochart "
<html>
<head>
<title>Heatmap</title>
<script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>
</head>
<body>
<div style='text-align: center; padding: 15px;'><h3>Application Usage by US State</h3></div>
<div id='regions_div' style='width: 900px; height: 500px; margin: 0 auto;'></div>
<div style='text-align: center; padding: 15px;'><p><a href='/resetmap'>Reset Map</a></p></div>
<script>
google.charts.load('current', {
'packages':\['geochart'\],
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable(GEOLOCATION_DATA_LIST_OF_LISTS);
var options = {
region: 'US',
displayMode: 'regions',
resolution: 'provinces',
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
</script>
</body>
</html>
"
}
when HTTP_REQUEST priority 500 {
if { ![HTTP::has_responded]} {
if {[HTTP::uri] starts_with "/heatmap"} {
set geolocation_data "\[\['State','Requests'\],"
foreach state [table keys -subtable states --] {
append geolocation_data "\['[string map {"\'" ""} '$state]', [table lookup -subtable states -- $state]\],"
}
set geolocation_data "[string trimright $geolocation_data ","]\]"
#HTTP::respond 200 content $geolocation_data
HTTP::respond 200 content [string map [list "GEOLOCATION_DATA_LIST_OF_LISTS" $geolocation_data] $static::geochart]
} elseif {[HTTP::uri] starts_with "/resetmap"} {
foreach state [table keys -subtable states --] {
table delete -subtable states -- $state
}
HTTP::redirect "http://[HTTP::host]/heatmap"
} else {
set loc [whereis [IP::client_addr] state]
if {$loc eq ""} {
set ip [expr { int(rand()*223) }].[expr { int(rand()*255) }].[expr { int(rand()*255) }].[expr { int(rand()*255) }]
set loc [whereis $ip state]
}
if { [whereis $ip country] eq "US" } {
if {[table incr -subtable states -mustexist -- $loc] eq ""} {
table set -subtable states -- $loc 1 indefinite indefinite
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment