mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-03-03 18:51:09 +01:00
feat(peer): add speed and ratio,percent fields info model peer and auto count ratio and percent
This commit is contained in:
@@ -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});
|
||||
|
||||
Reference in New Issue
Block a user