I'm trying to display data from Keen.io we're collecting from an iOS app where we report what devices of ours the app is used to communicate with. In particular I'm trying to find where there are more than one of our device.
An entry from the Keen collection looks like:
{
"iOS_OpenUDID": "hexadecimal",
"iOS_CountryCode": "two-letter country code",
"Device": {
"SerialNumber": "numbers",
"Barcode": "numbers",
"IP": "IP address",
"Model": "string",
},
"iOS_Timezone": "name of timezone",
}
Most of this is adapted from various different examples and tutorials, and as far as I can tell, it works. Examples I remember using are:
I have no idea though if it's 'good' code or 'awful', so please give constructive criticism:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Cache-Control" content="no-cache, mustrevalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<title>Multiple Devices</title>
<script type="text/javascript">
var Keen=Keen||{configure:function(e){this._cf=e},addEvent:function(e,t,n,i){this._eq=this._eq||[],this._eq.push([e,t,n,i])},setGlobalProperties:function(e){this._gp=e},onChartsReady:function(e){this._ocrq=this._ocrq||[],this._ocrq.push(e)}};(function(){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src=("https:"==document.location.protocol?"https://":"http://")+"dc8na2hxrj29i.cloudfront.net/code/keen-2.1.0-min.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)})();
Keen.configure({
projectId: "REMOVED",
readKey: "REMOVED"
});
// From http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
function eliminateDuplicates(arr) {
var i,
len=arr.length,
out=[],
obj={};
for (i=0;i<len;i++) {
obj[arr[i]]=0;
}
for (i in obj) {
out.push(i);
}
return out;
}
google.load('visualization', '1', {packages:['table']});
google.setOnLoadCallback(drawTable);
function drawTable() {
var datatable = new google.visualization.DataTable();
datatable.addColumn('string', 'iOS OpenUDID');
datatable.addColumn('string', 'Models (Barcode, Serial Number, IP address)');
datatable.addColumn('string', 'App Brand');
datatable.addColumn('string', 'iOS Timezone');
var table = new google.visualization.Table(document.getElementById('table_div'));
var metric = new Keen.Metric("Devices", {
analysisType: "count_unique",
targetProperty: "Device.IP",
groupBy: "iOS_OpenUDID",
filters: [
{"property_name":"iOS_OpenUDID","operator":"exists","property_value":true},
]
});
metric.getResponse(function(response){
// console.log(response);
for (var id = 0; id < response.result.length; id++) {
if (response.result[id].result > 1) {
// console.log(response.result[id]);
var query = "https://api.keen.io/3.0/projects/REMOVED/queries/extraction?api_key=DEVICES&event_collection=Devices&timezone=43200&target_property=Device.Model&group_by=Device.Model&filters=%5B%7B%22property_name%22%3A%22iOS_OpenUDID%22%2C%22operator%22%3A%22eq%22%2C%22property_value%22%3A%22" + response.result[id].iOS_OpenUDID + "%22%7D%5D";
$.getJSON(query, function(data) {
// console.log(data);
var arr = [];
for (var i = 0; i < data.result.length; i++) {
if (data.result[i] !== undefined && data.result[i] !== null) {
var string = data.result[i].Device["Model"];
var string2 = "";
if (data.result[i].Device["Barcode"] !== undefined && data.result[i].Device["Barcode"] !== null) {
string2 += data.result[i].Device["Barcode"];
}
if (data.result[i].Device["SerialNumber"] !== undefined && data.result[i].Device["SerialNumber"] !== null) {
if (string2.length > 0) {
string2 += ", ";
}
string2 += data.result[i].Device["SerialNumber"];
}
if (data.result[i].Device["IP"] !== undefined && data.result[i].Device["IP"] !== null) {
if (string2.length > 0) {
string2 += ", ";
}
string2 += data.result[i].Device["IP"];
}
if (string2.length > 0) {
string += " (" + string2 + ")";
}
arr.push(string);
}
};
arr = eliminateDuplicates(arr);
var models = "";
for (var i = 0; i < arr.length; i++) {
if (models.length > 0) {
models += ", ";
}
models += arr[i];
}
datatable.addRow(
[data.result[0]["iOS_OpenUDID"], models, data.result[0]["Brand"], data.result[0]["iOS_Timezone"]]
);
table.draw(datatable, {showRowNumber: true});
})
.fail(function(jqXHR, textStatus, errorThrown) { alert('getJSON request failed! ' + textStatus); });
}
}
});
table.draw(datatable, {showRowNumber: true});
}
</script>
<style type="text/css">
body { padding-top: 70px; }
</style>
</head>
<body>
<div id='table_div'></div>
</body>
</html>