Skip to content

Commit

Permalink
Add CSV export & more lenient matching
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnstijn committed Oct 22, 2020
1 parent bf96d10 commit 8d438e0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 60 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h2>Data</h2>
<div>
<!-- @todo add click to save data -->
<button class="download" id="save-json-button">Save Data (JSON)</button>
<button class="download" id="save-csv-button">Save Data (CSV)</button>
<button class="download" id="save-gdf-button">Save Data (GDF)</button>
<button class="reset" id="reset-data-button">Reset Data</button>
</div>
Expand Down
155 changes: 96 additions & 59 deletions js/brightbeam.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const brightbeam = {

for(index in this.trackerStore) {
let tracker = this.trackerStore[index];
let matcher = new RegExp('^' + tracker['pattern']);
let matcher = new RegExp(tracker['pattern']);
if(hostname.match(matcher)) {
this.trackerCache[hostname] = tracker;
return tracker;
Expand Down Expand Up @@ -80,6 +80,7 @@ const brightbeam = {
addListeners() {
this.downloadJSONData();
this.downloadGDFData();
this.downloadCSVData();
this.resetData();
this.showGraphView();
this.showListView();
Expand Down Expand Up @@ -170,64 +171,6 @@ const brightbeam = {
return await storeChild.getNumThirdParties();
},

downloadGDFData() {
const saveData = document.getElementById('save-gdf-button');
let self = this;
let nodeFields = {
id: 'VARCHAR',
label: 'VARCHAR',
tracker_type: 'VARCHAR',
tracker_id: 'INTEGER',
company_id: 'INTEGER'
}
let edgeFields = {
source: 'VARCHAR',
target: 'VARCHAR',
directed: 'BOOLEAN'
}
saveData.addEventListener('click', async () => {
const network = await this.getNetwork();
let gdfData = [];

let nodedef = [];
for(let field in nodeFields) {
if(field == 'id') { field = 'name'; }
nodedef.push(field + ' ' + nodeFields[field]);
}
gdfData.push('nodedef>' + nodedef.join(',') + '\n');
network.nodes.forEach(function(node) {
let nodebuffer = [];
for(let field in nodeFields) {
nodebuffer.push(node[field]);
}
gdfData.push(nodebuffer.join(',') + '\n')
})

let edgedef = [];
for(let field in edgeFields) {
if(field == 'source') { field = 'from'; }
if(field == 'target') { field = 'to'; }
edgedef.push(field + ' ' + edgeFields[field]);
}
gdfData.push('edgedef>' + edgedef.join(',') + '\n');
network.edges.forEach(function(edge) {
let edgebuffer = [];
for(let field in edgeFields) {
edgebuffer.push(edge[field]);
}
gdfData.push(edgebuffer.join(',') + '\n')
})

const blob = new Blob(gdfData, {type : 'application/gdf'});
const url = window.URL.createObjectURL(blob);
const downloading = browser.downloads.download({
url : url,
filename : 'brightbeamData.gdf',
conflictAction : 'uniquify'
});
await downloading;
});
},

async getNetwork() {
const data = await storeChild.getAll();
Expand Down Expand Up @@ -321,6 +264,65 @@ const brightbeam = {
return network;
},

downloadGDFData() {
const saveData = document.getElementById('save-gdf-button');
let self = this;
let nodeFields = {
id: 'VARCHAR',
label: 'VARCHAR',
tracker_type: 'VARCHAR',
tracker_id: 'INTEGER',
company_id: 'INTEGER'
}
let edgeFields = {
source: 'VARCHAR',
target: 'VARCHAR',
directed: 'BOOLEAN'
}
saveData.addEventListener('click', async () => {
const network = await this.getNetwork();
let gdfData = [];

let nodedef = [];
for(let field in nodeFields) {
if(field == 'id') { field = 'name'; }
nodedef.push(field + ' ' + nodeFields[field]);
}
gdfData.push('nodedef>' + nodedef.join(',') + '\n');
network.nodes.forEach(function(node) {
let nodebuffer = [];
for(let field in nodeFields) {
nodebuffer.push(node[field]);
}
gdfData.push(nodebuffer.join(',') + '\n')
})

let edgedef = [];
for(let field in edgeFields) {
if(field == 'source') { field = 'from'; }
if(field == 'target') { field = 'to'; }
edgedef.push(field + ' ' + edgeFields[field]);
}
gdfData.push('edgedef>' + edgedef.join(',') + '\n');
network.edges.forEach(function(edge) {
let edgebuffer = [];
for(let field in edgeFields) {
edgebuffer.push(edge[field]);
}
gdfData.push(edgebuffer.join(',') + '\n')
})

const blob = new Blob(gdfData, {type : 'application/gdf'});
const url = window.URL.createObjectURL(blob);
const downloading = browser.downloads.download({
url : url,
filename : 'brightbeamData.gdf',
conflictAction : 'uniquify'
});
await downloading;
});
},

downloadJSONData() {
const saveData = document.getElementById('save-json-button');
saveData.addEventListener('click', async () => {
Expand All @@ -337,6 +339,41 @@ const brightbeam = {
});
},

downloadCSVData() {
const saveData = document.getElementById('save-csv-button');
saveData.addEventListener('click', async () => {
const network = await this.getNetwork();
let csvData = [];
csvData.push('source,name,type,aid,cid\n');

let nodeMap = [];
for(let index in network.nodes) {
let node = network.nodes[index];
nodeMap[node['id']] = node;
}

for(let index in network.edges) {
let edge = network.edges[index]
csvData.push([
nodeMap[edge.source]['id'],
nodeMap[edge.target]['label'],
nodeMap[edge.target]['tracker_type'],
nodeMap[edge.target]['company_id'],
nodeMap[edge.target]['tracker_id']
].join(',') + '\n')
}

const blob = new Blob(csvData,{type : 'text/csv'});
const url = window.URL.createObjectURL(blob);
const downloading = browser.downloads.download({
url : url,
filename : 'brightbeamData.csv',
conflictAction : 'uniquify'
});
await downloading;
});
},

stopGraphView() {
this.sigma = null;
document.getElementById('vis-graph').innerHTML = '';
Expand Down
2 changes: 1 addition & 1 deletion js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ const store = {

onTrackerList(url) {
for(let index in this.trackerList) {
if(url.replace(/^https?:\/\//, '').replace(/^www\./, '').match(new RegExp('^' + this.trackerList[index]))) {
if(url.replace(/^https?:\/\//, '').replace(/^www\./, '').match(new RegExp(this.trackerList[index]))) {
return true;
}
}
Expand Down

0 comments on commit 8d438e0

Please sign in to comment.