-
Notifications
You must be signed in to change notification settings - Fork 3
/
doc_sizes.js
82 lines (72 loc) · 1.76 KB
/
doc_sizes.js
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
/**
* ### Перенумерация документов для кристаллита
*
* @module clear_bars
*
*/
/**
* ### Переменные окружения
* DEBUG "wb:*,-not_this"
* DBPWD admin
* DBUSER admin
* COUCHPATH http://oknosoft.ecookna.ru:5984/pl_events
*/
'use strict';
const debug = require('debug')('wb:next');
const PouchDB = require('pouchdb')
.plugin(require('pouchdb-find'));
debug('required');
let dsize = 0;
let prev = 0;
let dcount = 0;
// инициализируем параметры сеанса и метаданные
const {DBUSER, DBPWD, COUCHPATH} = process.env;
const src = new PouchDB(COUCHPATH, {
auth: {
username: DBUSER,
password: DBPWD
},
skip_setup: true,
ajax: {timeout: 100000}
});
src.info()
.then((info) => {
debug(`connected to ${info.host}, doc count: ${info.doc_count}`);
})
.then(next)
.then(() => debug(`ok`));
function sleep(time, res) {
return new Promise((resolve) => {
setTimeout(() => resolve(res), time);
});
}
function next(bookmark) {
// получаем в ОЗУ все номенклатуры и единицы измерения
return src.find({
selector: {
class_name: "cat.characteristics"
},
limit: 1000,
bookmark,
})
.then(({docs, bookmark}) => {
if(docs.length) {
dcount += docs.length;
for(const doc of docs) {
const str = JSON.stringify(doc);
if(str.length > dsize) {
dcount++;
if(dsize - 10000 > prev) {
prev = dsize;
debug(`${(prev/1000).toFixed()} Kb, ${dcount} docs`);
}
}
}
return sleep(1000)
.then(() => next(bookmark));
}
})
.catch(err => {
debug(err)
});
}