forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
120 lines (105 loc) · 3.29 KB
/
base.js
File metadata and controls
120 lines (105 loc) · 3.29 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const got = require('got');
const async = require('async');
/**
* base model
*/
class base {
/**
* construct
* @param config
*/
constructor(config) {
this.config = config;
this.url = config.get('url').endsWith('/') ? config.get('url') : `${config.get('url')}/`;
this.token = config.get('token');
this._perPage = this.config ? this.config.get('_perPage') : 100;
this._parallel = this.config ? this.config.get('_parallel') : 4;
}
/**
* query the given path
* @param path
* @param page
* @param perPage
* @returns {Promise}
*/
get(path, page = 1, perPage = this._perPage) {
path += (path.includes('?') ? '&' : '?') + `private_token=${this.token}`;
path += `&page=${page}&per_page=${perPage}`;
return got(`${this.url}${path}`, {
json: true,
headers: {
'PRIVATE-TOKEN': this.token
}
});
}
/**
* query the given path and paginate automatically and in parallel
* through all available pages
* @param path
* @param perPage
* @param runners
* @returns {Promise}
*/
all(path, perPage = this._perPage, runners = this._parallel) {
return new Promise((resolve, reject) => {
let collect = [];
this.get(path, 1, perPage).then(response => {
response.body.forEach(item => collect.push(item));
let pages = parseInt(response.headers['x-total-pages']);
if (pages === 1) return resolve(collect);
let tasks = base.createGetTasks(path, pages, 2, perPage);
this.getParallel(tasks, collect, runners).then(() => {
resolve(collect);
}).catch(error => reject(error));
}).catch(err => reject(err));
});
}
/**
* perform the given worker function on the given tasks in parallel
* @param tasks
* @param worker
* @param runners
* @returns {Promise}
*/
parallel(tasks, worker, runners = this._parallel) {
return new Promise((resolve, reject) => {
async.eachLimit(Array.from(tasks), runners, worker, error => {
if (error) return reject(error);
resolve();
});
});
}
/**
* make multiple get requests by the given tasks and apply the
* data to the given set
* @param tasks
* @param collect
* @param runners
* @returns {Promise}
*/
getParallel(tasks, collect = [], runners = this._parallel) {
return this.parallel(tasks, (task, done) => {
this.get(task.path, task.page, task.perPage).then((response) => {
response.body.forEach(item => collect.push(item));
done();
}).catch(error => done(error));
}, runners);
}
/**
* create a task list to get all pages from
* the given path
* @param path
* @param to
* @param from
* @param perPage
* @returns {Array}
*/
static createGetTasks(path, to, from = 2, perPage = this._perPage) {
let tasks = [];
for (let i = from; i <= to; i++) {
tasks.push({path, perPage, page: i})
}
return tasks;
}
}
module.exports = base;