Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #385 from aleitner/master
Browse files Browse the repository at this point in the history
WIP [2.0.8]
  • Loading branch information
aleitner authored Oct 11, 2016
2 parents 70e97a7 + d4f8686 commit 9d7a8b9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 64 deletions.
77 changes: 30 additions & 47 deletions app/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ var userdata = new UserData(app.getPath('userData'));
var Logger = require('kad-logger-json');
var FsLogger = require('./lib/fslogger');
var TelemetryReporter = require('storj-telemetry-reporter');
var leveldown = require('leveldown');
var path = require('path');

// bootstrap helpers
helpers.ExternalLinkListener().bind(document);
Expand Down Expand Up @@ -265,13 +263,15 @@ var main = new Vue({
// Update by drive
var contractCountKey = 'contractCount_' + tab.id;
farmer.storageManager._storage.on('add',function(item){
tab.lastChange = new Date().toLocaleString();
var contracts = Number(localStorage.getItem(contractCountKey));
contracts += Object.keys(item.contracts).length;
localStorage.setItem(contractCountKey, contracts.toString());
tab.contracts.total = contracts;
});

farmer.storageManager._storage.on('update',function(previous, next){
tab.lastChange = new Date().toLocaleString();
var contracts = Number(localStorage.getItem(contractCountKey));
previous = Object.keys(previous.contracts).length;
next = Object.keys(next.contracts).length;
Expand All @@ -281,6 +281,7 @@ var main = new Vue({
});

farmer.storageManager._storage.on('delete',function(item){
tab.lastChange = new Date().toLocaleString();
var contracts = Number(localStorage.getItem(contractCountKey));
contracts -= Object.keys(item.contracts).length;
localStorage.setItem(contractCountKey, contracts.toString());
Expand Down Expand Up @@ -316,15 +317,21 @@ var main = new Vue({
}

if (err) {
self.stopFarming(null, tab);
self.error.message = err;
self.error.drive = tab.shortId;
$('#error').modal({
backdrop: 'static',
keyboard: false,
show: true}
);
logger.error(err.message);
self.stopFarming(null, tab);

if (userdata.appSettings.retryOnError === true) {
logger.warn('An error occurred. Restarting farmer...');
self.startFarming(event, index);
} else {
self.error.message = err;
self.error.drive = tab.shortId;
$('#error').modal({
backdrop: 'static',
keyboard: false,
show: true}
);
}
}
});
});
Expand Down Expand Up @@ -536,7 +543,7 @@ var main = new Vue({

Monitor.getPaymentAddressBalances({
keyPair: storj.KeyPair(tab.key),
_options: { payment: { address: tab.getAddress() } }
_options: { paymentAddress: tab.getAddress() }
}, function(err, stats) {
self.balance.sjcx = stats.payments.balances.sjcx || 0;
self.balance.sjct = stats.payments.balances.sjct || 0;
Expand Down Expand Up @@ -614,9 +621,20 @@ var main = new Vue({
this.showTab(this.current);

setInterval(function() {

//Delete old logs
if (self.userdata.appSettings.deleteOldLogs === true) {
FsLogger.prototype._deleteOldFiles(self.userdata.appSettings.logFolder,
function(err) {
if (err) {
return window.alert(err.message);
}
});
}

var tab = self.userdata.tabs[self.current];
self.getBalance(tab);
}, 3600000);
}, 7200000);

setInterval(function() {
var tab = self.userdata.tabs[self.current];
Expand Down Expand Up @@ -748,41 +766,6 @@ var appSettings = new Vue({
}
});
},
repairdb: function() {
$('#settings').modal('hide');
$('#repairing').modal({
backdrop: 'static',
keyboard: false,
show: true
});
var dataDir = this.userdata.tabs[this.current].storage.path;
var dbPath = path.join(dataDir,'farmer.db');
var db = leveldown(dbPath);

db.open({ createIfMissing: false }, function(err) {
if (err) {
$('#repairing').modal('hide');
return window.alert(err.message);
}

db.close(function(err) {
if (err) {
$('#repairing').modal('hide');
return window.alert(err.message);
}

leveldown.repair(dbPath, function(err) {
if (err) {
$('#repairing').modal('hide');
return window.alert(err.message);
}

$('#repairing').modal('hide');
window.alert('DB repair and compaction completed successfully!');
});
});
});
},
openLogFolder: function() {
shell.openExternal('file://' + this.userdata.appSettings.logFolder);
},
Expand Down
40 changes: 38 additions & 2 deletions app/lib/fslogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ FsLogger.prototype._newfile = function() {
* #_builddate
* @return {String} String of date in the format of 1337-06-66
*/
FsLogger.prototype._builddate = function() {
var dateObj = new Date();
FsLogger.prototype._builddate = function(dateObject) {
var dateObj = (!dateObject) ? new Date() : dateObject;
var month = dateObj.getMonth() + 1; //months from 1-12
var day = dateObj.getDate();
var year = dateObj.getFullYear();
Expand Down Expand Up @@ -186,4 +186,40 @@ FsLogger.prototype._shouldLog = function(type) {
return false;
};

/**
* Determine if message should be logged
* #_deleteOldFiles
* @param {String} folder - (optional) path to log files
* @param {Function} callback - (optional) perform action when completed
*/
FsLogger.prototype._deleteOldFiles = function(folder, callback) {
var self = this;
var logfolder = (!folder) ? self._logfolder : folder;

if (!callback) {
callback = function(err) {
if (err) {
self.emit('error', err);
}
};
}

fs.readdir(logfolder, function(err, files) {
if (err) {
return callback(err);
}

var days = [
self._builddate(),
self._builddate(new Date(Date.now() - 86400000))
];

files.forEach(function(file) {
if ((file.indexOf(days[0]) === -1) && (file.indexOf(days[1]) === -1)) {
fs.unlink(path.join(logfolder,file), callback);
}
});
});
};

module.exports = FsLogger;
3 changes: 2 additions & 1 deletion app/lib/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Tab(options) {
this.contracts = {
total: options.contracts.total || 0
};

this.usedspace = {
size: options.usedspace.size || 0,
unit: options.usedspace.unit || 'B'
Expand Down Expand Up @@ -77,6 +77,7 @@ function Tab(options) {
this.farmer = null;
this.spaceUsedPercent = 0;
this.connectedPeers = 0;
this.lastChange = options.lastChange || 'Pending...';
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/lib/userdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ UserData.DEFAULTS = {
reportTelemetry: true,
logFolder: '',
logLevel: 3,
silentMode: false
silentMode: false,
retryOnError: true,
deleteOldLogs: true
}
};
/**
Expand Down
37 changes: 24 additions & 13 deletions app/storjshare.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,18 @@ <h4 class="modal-title">Counting Shards</h4>
</div>
</div>
<div class="row">
<div class="form-group col-xs-6">
<label class="control-label" for="peers">Connected Peers</label>
<div class="form-group col-xs-3">
<label class="control-label" for="peers">Peers</label>
<input class="form-control" type="number" min="0" name="peers" v-model="userdata.tabs[current].connectedPeers" v-bind:disabled="userdata.tabs[current].farmer">
</div>
<div class="form-group col-xs-6">
<label for="contracts">Successful Contracts</label>
<div class="form-group col-xs-4">
<label for="contracts">Contracts</label>
<input class="form-control" type="number" min="0" name="contracts" v-model="userdata.tabs[current].contracts.total" v-bind:disabled="userdata.tabs[current].farmer">
</div>
<div class="form-group col-xs-5">
<label for="ping">Last Activity</label>
<input class="form-control" type="text" name="lastChange" v-model="userdata.tabs[current].lastChange" v-bind:disabled="userdata.tabs[current].farmer">
</div>
</div>
</section>
<section class="action">
Expand Down Expand Up @@ -307,6 +311,22 @@ <h4 class="modal-title">Advanced Settings</h4>
</div>
</div>

<div class="row form-group">
<div class="col-xs-12">
<label for="RetryOnError">
<input v-model="userdata.appSettings.retryOnError" v-on:change="changeSettings" id="RetryOnError" type="checkbox"> Automatically restart farming if an error occurs
</label>
</div>
</div>

<div class="row form-group">
<div class="col-xs-12">
<label for="DeleteOldLogs">
<input v-model="userdata.appSettings.deleteOldLogs" v-on:change="changeSettings" id="DeleteOldLogs" type="checkbox"> Delete Log files older than 2 days
</label>
</div>
</div>

<hr>

<div class="row form-group">
Expand Down Expand Up @@ -336,15 +356,6 @@ <h4 class="modal-title">Advanced Settings</h4>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-xs-12">
<label>Database Repair</label>
</div>
<div class="col-xs-12">
<div class="alert alert-warning" role="alert">Only use this button to attempt to repair a corrupted shard database or force compaction.</div>
<button class="btn btn-blue btn-block" type="button" v-on:click="repairdb">Repair Drive ({{userdata.tabs[current].shortId}})</button>
</div>
</div>


</div>
Expand Down

0 comments on commit 9d7a8b9

Please sign in to comment.