Skip to content

Commit

Permalink
added data writing
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-de-jonge committed Feb 19, 2018
1 parent 39ef42b commit 189a62c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
5 changes: 4 additions & 1 deletion tracker-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules/*
.idea/*
dist/*
dist/*
data.csv
nodelist.json
transactionlist.json
19 changes: 19 additions & 0 deletions tracker-server/model/TransactionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TransactionList {
this.numberOfTransactions = 0;
this.numberOfChains = 0;
this.numberOfBlocks = 0;
this.numbersArray = [];
}

/**
Expand Down Expand Up @@ -91,6 +92,24 @@ class TransactionList {
averageNumberOfChains: averageNumberOfChains
};
}

/**
* Adds numbers data point to the array.
*/
addNumbersToArray() {
const numbers = this.getNumbers();
if(numbers.numberOfTransactions !== 0 || numbers.averageNumberOfBlocks !== 0 || numbers.averageNumberOfChains !== 0) {
this.numbersArray.push(numbers);
}
}

/**
* Gets the numbers array.
* @returns {Array}
*/
getNumbersArray() {
return this.numbersArray;
}
}

module.exports = TransactionList;
10 changes: 10 additions & 0 deletions tracker-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tracker-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
"dependencies": {
"body-parser": "latest",
"cookie-parser": "^1.4.3",
"csv-writer": "0.0.3",
"debug": "latest",
"ejs": "^2.5.7",
"express": "^4.16.2",
"fs": "0.0.1-security",
"jsnetworkx": "^0.3.4",
"morgan": "^1.9.0"
}
Expand Down
11 changes: 5 additions & 6 deletions tracker-server/public/javascripts/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ $(document).ready(function() {

var counter = 12;

$("#graph_button").on('click', function() {
edges[0].value += 1;
counter += 1;
$(".odometer").text(counter);
network.setData({nodes: nodes, edges: edges});
network.redraw();
$("#saveButton").on("click", function() {
$.ajax({
method: 'POST',
url: '/write-data'
});
});
});
29 changes: 29 additions & 0 deletions tracker-server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router();
import app from '../app';
import NodeList from '../model/NodeList';
const sseMW = require('./../helpers/sse');
const fs = require('fs');
import Transaction from '../model/Transaction';
import TransactionList from '../model/TransactionList';

Expand Down Expand Up @@ -113,6 +114,7 @@ router.post('/reset', (req, res) => {
app.nodeList = new NodeList();
app.transactionList = new TransactionList();
res.json({success: true});
initDataHeartbeat();
});

/**
Expand Down Expand Up @@ -146,6 +148,33 @@ function updateSseClients() {
{nodes: nodes, edges: edges, numbers: app.transactionList.getNumbers()}));
}

function initDataHeartbeat() {
setInterval(() => {
app.transactionList.addNumbersToArray();
}, 5000);
}

/**
* Write all data.
*/
router.post('/write-data', (req, res) => {
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: './data.csv',
header: [
{id: 'numberOfTransactions', title:'numberOfTransactions'},
{id: 'averageNumberOfBlocks', title:'averageNumberOfBlocks'},
{id: 'averageNumberOfChains', title:'averageNumberOfChains'}
]
});
fs.writeFile('./nodelist.json', JSON.stringify(app.nodeList, null, 2) , 'utf-8');
fs.writeFile('./transactionlist.json', JSON.stringify(app.transactionList, null, 2) , 'utf-8');
csvWriter.writeRecords(app.transactionList.getNumbersArray()).then(() => {
console.log("Writing to csv done!");
res.json(app.transactionList.getNumbersArray());
});
});

/**
* send a heartbeat signal to all SSE clients, once every interval seconds (or every 3 seconds if no interval is specified)
* @param interval - interval in seconds
Expand Down
2 changes: 1 addition & 1 deletion tracker-server/views/demo.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
</body>

<footer>

<button id="saveButton">Save data</button>
</footer>
</main>

0 comments on commit 189a62c

Please sign in to comment.