feat(peer): add speed and ratio,percent fields info model peer and auto count ratio and percent

This commit is contained in:
OldHawk
2017-09-11 12:28:52 +08:00
parent a74f768a00
commit 9b849544a2

View File

@@ -40,10 +40,26 @@ var PeerSchema = new Schema({
type: Number,
default: 0
},
peer_uspeed: {
type: Number,
default: 0
},
peer_dspeed: {
type: Number,
default: 0
},
peer_ratio: {
type: Number,
default: 0
},
peer_left: {
type: Number,
default: 0
},
peer_percent: {
type: Number,
default: 0
},
peer_status: {
type: String,
default: 'leecher',
@@ -73,6 +89,48 @@ var PeerSchema = new Schema({
}
});
/**
* Hook a pre save method
*/
PeerSchema.pre('save', function (next) {
countRatio(this);
countPercent(this);
next();
});
/**
* Hook a pre update method
*/
PeerSchema.pre('update', function (next) {
countRatio(this);
countPercent(this);
next();
});
/**
* countRatio
* @param t
*/
function countRatio(p) {
if (p.peer_uploaded > 0 && p.peer_downloaded === 0) {
p.peer_ratio = -1;
} else if (p.peer_uploaded === 0 || p.peer_downloaded === 0) {
p.peer_ratio = 0;
} else {
p.peer_ratio = Math.round((p.peer_uploaded / p.peer_downloaded) * 100) / 100;
}
}
/**
* countPercent
* @param p
*/
function countPercent(p) {
p.peer_percent = Math.round((p.peer_downloaded / (p.peer_downloaded + p.peer_left)) * 10000) / 100;
}
PeerSchema.index({user: -1, startedat: -1});
PeerSchema.index({info_hash: -1, startedat: -1});
PeerSchema.index({torrent: -1, startedat: -1});