forked from ralmn/Batchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchCursor.ts
83 lines (60 loc) · 2.92 KB
/
batchCursor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/// <reference path="./batch.d.ts" />
import {BatchActivity, BatchSession} from './batch';
export abstract class BatchActivityCursor<T, U extends BatchSession> extends BatchActivity<T, U> {
private offset = 0;
public numberBS = 20;
public packSize = 20;
public selectData(callback, params?) {}
public splitData(datas) { return null; }
public abstract getCountData( callback : {(count : number): void}, params?: any ): void;
public abstract getData( starting: number, size: number ,callback : {(datas : T[]): void}, params?: any ): void;
public execute(params?: any){
this.log("Start cursor");
let that = this;
this.getCountData((count) => {
this.log(`total of elements : ${count}`);
if(count == 0){
this.finishBA();
return;
}
var startingTime = new Date().getTime();
let nbBS = Math.min( Math.ceil(count / this.packSize), this.numberBS) ;
this.currentBS = nbBS;
for(var bsNumber = 0; bsNumber < nbBS; bsNumber++){
var bs = this.createBatchSession();
bs.dirChaine = this.dirChaine;
let starting = that.offset;
that.offset += that.packSize;
that.getData(starting, that.packSize, (datas) => {
bs.execute(datas);
}, params)
bs.on('end', function(returningCode : number){
if(that.returningCode < returningCode ){
that.returningCode = returningCode;
this.log(`${this.bsID} => ${returningCode}`);
}
let now = new Date().getTime();
let diff = (now - startingTime);
let leftSec = ( count - that.offset ) * (that.offset / (diff / 1000 ));
that.log(`Time left : ${Math.round(leftSec)} sec - ETA : ${new Date(now + leftSec * 1000)}`);
that.log(` ${diff/that.offset} elem/s `)
if(that.offset >= count){
that.currentBS--;
if(that.currentBS == 0)
that.finishBA();
}else{
try{
let newOffset = that.offset;
that.offset += that.packSize;
that.getData(newOffset, that.packSize, (datas) => {
bs.execute(datas);
}, params)
}catch(error){
this.log(error);
}
}
})
}
}, params);
}
}