1
1
const ReadyResource = require ( 'ready-resource' )
2
2
const debounce = require ( 'debounceify' )
3
- const safetyCatch = require ( 'safety-catch' )
4
3
5
4
module . exports = class Monitor extends ReadyResource {
6
5
constructor ( drive , opts = { } ) {
@@ -9,15 +8,13 @@ module.exports = class Monitor extends ReadyResource {
9
8
this . blobs = null
10
9
this . name = opts . name
11
10
this . entry = opts . entry
12
- this . isDownload = opts . download === true
13
11
14
12
this . _boundOnAppend = debounce ( this . _onAppend . bind ( this ) )
15
13
this . _boundOnUpload = this . _onUpload . bind ( this )
16
14
this . _boundOnDownload = this . _onDownload . bind ( this )
17
15
this . drive . on ( 'close' , ( ) => this . close ( ) )
18
16
19
- // Updated on each upload/download event
20
- this . stats = {
17
+ const stats = {
21
18
startTime : 0 ,
22
19
percentage : 0 ,
23
20
peersCount : 0 ,
@@ -28,6 +25,10 @@ module.exports = class Monitor extends ReadyResource {
28
25
targetBytes : null ,
29
26
targetBlocks : null
30
27
}
28
+
29
+ // Updated on each upload/download event
30
+ this . uploadStats = { ...stats }
31
+ this . downloadStats = { ...stats }
31
32
}
32
33
33
34
async _open ( ) {
@@ -36,15 +37,6 @@ module.exports = class Monitor extends ReadyResource {
36
37
this . entry = await this . drive . entry ( this . name )
37
38
if ( this . entry ) this . _setEntryInfo ( )
38
39
39
- // load the local state of the file.
40
- // upload is a bit more tricky...
41
- if ( this . entry && this . isDownload ) {
42
- await this . _loadLocalState ( ) . catch ( safetyCatch ) . finally ( ( ) => {
43
- this . _calculateStats ( )
44
- this . emit ( 'update' )
45
- } )
46
- }
47
-
48
40
// Handlers
49
41
this . blobs . core . on ( 'append' , this . _boundOnAppend )
50
42
this . blobs . core . on ( 'upload' , this . _boundOnUpload )
@@ -65,53 +57,44 @@ module.exports = class Monitor extends ReadyResource {
65
57
}
66
58
67
59
_setEntryInfo ( ) {
68
- if ( this . stats . targetBytes || this . stats . targetBlocks ) return
69
- this . stats . targetBytes = this . entry . value . blob . byteLength
70
- this . stats . targetBlocks = this . entry . value . blob . blockLength
71
- this . stats . blockOffset = this . entry . value . blob . blockOffset
72
- this . stats . byteOffset = this . entry . value . blob . byteOffset
73
- }
60
+ if ( ! this . downloadStats . targetBytes || ! this . downloadStats . targetBlocks ) {
61
+ this . downloadStats . targetBytes = this . entry . value . blob . byteLength
62
+ this . downloadStats . targetBlocks = this . entry . value . blob . blockLength
63
+ }
74
64
75
- async _onUpload ( index , bytes , from ) {
76
- this . _updateStats ( index , bytes , from )
65
+ if ( ! this . uploadStats . targetBytes || ! this . uploadStats . targetBlocks ) {
66
+ this . uploadStats . targetBytes = this . entry . value . blob . byteLength
67
+ this . uploadStats . targetBlocks = this . entry . value . blob . blockLength
68
+ }
77
69
}
78
70
79
- async _onDownload ( index , bytes , from ) {
80
- this . _updateStats ( index , bytes , from )
71
+ _onUpload ( index , bytes , from ) {
72
+ this . _updateStats ( this . uploadStats , index , bytes , from )
81
73
}
82
74
83
- async _loadLocalState ( ) {
84
- // @TODO : There's a better way to do this?
85
- let blockIdx = this . stats . blockOffset
86
- while ( blockIdx <= this . stats . targetBlocks ) {
87
- if ( await this . blobs . core . core . bitfield . get ( blockIdx ) ) {
88
- const bytes = await this . blobs . core . core . blocks . get ( blockIdx )
89
- this . stats . totalBytes += bytes . length
90
- this . stats . blocks ++
91
- }
92
- blockIdx ++
93
- }
75
+ _onDownload ( index , bytes , from ) {
76
+ this . _updateStats ( this . downloadStats , index , bytes , from )
94
77
}
95
78
96
- _updateStats ( index , bytes , from ) {
79
+ _updateStats ( stats , index , bytes , from ) {
97
80
if ( ! this . entry || this . closing ) return
98
81
if ( ! isWithinRange ( index , this . entry ) ) return
99
82
100
- this . stats . peersCount = from . replicator . peers . length
101
- this . stats . blocks ++
102
- this . stats . monitoringBytes += bytes
103
- this . stats . totalBytes += bytes
83
+ stats . peersCount = from . replicator . peers . length
84
+ stats . blocks ++
85
+ stats . monitoringBytes += bytes
86
+ stats . totalBytes += bytes
104
87
105
- this . _calculateStats ( )
88
+ this . _calculateStats ( stats )
106
89
this . emit ( 'update' )
107
90
}
108
91
109
- _calculateStats ( ) {
110
- if ( ! this . stats . startTime ) this . stats . startTime = Date . now ( )
111
- this . stats . percentage = Number ( ( ( this . stats . totalBytes / this . stats . targetBytes ) * 100 ) . toFixed ( 2 ) )
112
- const timeElapsed = ( Date . now ( ) - this . stats . startTime ) / 1000
92
+ _calculateStats ( stats ) {
93
+ if ( ! stats . startTime ) stats . startTime = Date . now ( )
94
+ stats . percentage = Number ( ( ( stats . totalBytes / stats . targetBytes ) * 100 ) . toFixed ( 2 ) )
95
+ const timeElapsed = ( Date . now ( ) - stats . startTime ) / 1000
113
96
if ( timeElapsed > 0 ) {
114
- this . stats . speed = Math . floor ( this . stats . monitoringBytes / timeElapsed ) // Speed in bytes/sec
97
+ stats . speed = Math . floor ( stats . monitoringBytes / timeElapsed ) // bytes/sec
115
98
}
116
99
}
117
100
}
0 commit comments