dtable-statistic 5.0.48-alpha.10 → 5.0.48-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/dashboard.js +9 -0
- package/es/index.js +5 -2
- package/es/utils/formula-calc-worker/calc-with-worker.js +34 -5
- package/es/utils/formula-calc-worker/result-cache-manager.js +22 -0
- package/es/utils/formula-calc-worker/thread-manager.js +71 -0
- package/package.json +2 -2
- package/es/utils/formula-calc-worker/calc-formula-result.worker.js +0 -10
- package/es/utils/formula-calc-worker/calc-view-rows.worker.js +0 -10
package/es/dashboard.js
CHANGED
|
@@ -17,6 +17,7 @@ var _mobileDashboard = _interopRequireDefault(require("./mobile-dashboard"));
|
|
|
17
17
|
var _utils = require("./utils");
|
|
18
18
|
var _commonUtils = require("./utils/common-utils");
|
|
19
19
|
var _constants = require("./constants");
|
|
20
|
+
var _threadManager = _interopRequireDefault(require("./utils/formula-calc-worker/thread-manager"));
|
|
20
21
|
require("./locale");
|
|
21
22
|
require("./assets/css/dashboard.css");
|
|
22
23
|
require("./assets/css/theme.css");
|
|
@@ -123,9 +124,14 @@ class DashBoard extends _react.Component {
|
|
|
123
124
|
activeView
|
|
124
125
|
});
|
|
125
126
|
};
|
|
127
|
+
this.cancelCalculation = () => {
|
|
128
|
+
_threadManager.default.clearQueue();
|
|
129
|
+
_threadManager.default.terminateWorkers();
|
|
130
|
+
};
|
|
126
131
|
this.selectDashboard = selectedDashboardIdx => {
|
|
127
132
|
this.disabledUpdateLayout = true;
|
|
128
133
|
this.setSelectedDashboardIdx(selectedDashboardIdx);
|
|
134
|
+
this.cancelCalculation();
|
|
129
135
|
this.setState({
|
|
130
136
|
selectedDashboardIdx
|
|
131
137
|
});
|
|
@@ -158,6 +164,7 @@ class DashBoard extends _react.Component {
|
|
|
158
164
|
});
|
|
159
165
|
const selectedDashboardIdx = updatedStatistics.length - 1;
|
|
160
166
|
this.setSelectedDashboardIdx(selectedDashboardIdx);
|
|
167
|
+
this.cancelCalculation();
|
|
161
168
|
this.updateStatistics({
|
|
162
169
|
statistics: updatedStatistics,
|
|
163
170
|
selectedDashboardIdx
|
|
@@ -196,6 +203,7 @@ class DashBoard extends _react.Component {
|
|
|
196
203
|
}
|
|
197
204
|
});
|
|
198
205
|
this.setSelectedDashboardIdx(nextSelectedDashboardIdx);
|
|
206
|
+
this.cancelCalculation();
|
|
199
207
|
this.updateStatistics({
|
|
200
208
|
statistics: updatedStatistics,
|
|
201
209
|
selectedDashboardIdx: nextSelectedDashboardIdx
|
|
@@ -375,6 +383,7 @@ class DashBoard extends _react.Component {
|
|
|
375
383
|
componentWillUnmount() {
|
|
376
384
|
this.unsubscribeLocalDtableChanged();
|
|
377
385
|
this.unsubscribeRemoteDtableChange();
|
|
386
|
+
this.cancelCalculation();
|
|
378
387
|
}
|
|
379
388
|
render() {
|
|
380
389
|
const {
|
package/es/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var _react = _interopRequireDefault(require("react"));
|
|
|
5
5
|
var _dtableStore = require("dtable-store");
|
|
6
6
|
var _dashboard = _interopRequireDefault(require("./dashboard"));
|
|
7
7
|
var _calcWithWorker = require("./utils/formula-calc-worker/calc-with-worker");
|
|
8
|
+
var _resultCacheManager = require("./utils/formula-calc-worker/result-cache-manager");
|
|
8
9
|
class Statistic {
|
|
9
10
|
static getInitProps() {
|
|
10
11
|
if (!window || !window.app) {
|
|
@@ -44,7 +45,7 @@ class Statistic {
|
|
|
44
45
|
const rows = await (0, _calcWithWorker.calcViewRowsWithWorker)(view, table, dtableStore.value, username, userId, userDepartmentIdsMap);
|
|
45
46
|
return rows;
|
|
46
47
|
}
|
|
47
|
-
static async getTableFormulaResults(table, rows) {
|
|
48
|
+
static async getTableFormulaResults(table, rows, value, viewId) {
|
|
48
49
|
const dtableStore = window.app.dtableStore || {};
|
|
49
50
|
const {
|
|
50
51
|
username = null,
|
|
@@ -52,7 +53,7 @@ class Statistic {
|
|
|
52
53
|
userDepartmentIdsMap = null
|
|
53
54
|
} = dtableStore.dtableSettings;
|
|
54
55
|
const formulaColumns = _dtableStore.Views.getFormulaColumnsContainLinks(table);
|
|
55
|
-
const res = await (0, _calcWithWorker.calcFormulaResultsWithWorker)(table, rows, dtableStore.value, formulaColumns, username, userId, userDepartmentIdsMap);
|
|
56
|
+
const res = await (0, _calcWithWorker.calcFormulaResultsWithWorker)(table, rows, dtableStore.value, formulaColumns, username, userId, userDepartmentIdsMap, viewId);
|
|
56
57
|
return res;
|
|
57
58
|
}
|
|
58
59
|
static updateStatistics(dtableStore, statistics) {
|
|
@@ -73,6 +74,8 @@ class Statistic {
|
|
|
73
74
|
animation: true
|
|
74
75
|
};
|
|
75
76
|
if (!window.app) return;
|
|
77
|
+
_resultCacheManager.viewRowsCacheManager.clear();
|
|
78
|
+
_resultCacheManager.formulaResultsCacheManager.clear();
|
|
76
79
|
window.app.unmountWidget({
|
|
77
80
|
animation
|
|
78
81
|
});
|
|
@@ -10,16 +10,44 @@ exports.calcViewRowsWithWorker = calcViewRowsWithWorker;
|
|
|
10
10
|
var Comlink = _interopRequireWildcard(require("comlink"));
|
|
11
11
|
var _calc = _interopRequireDefault(require("./calc.worker"));
|
|
12
12
|
var _dtableStore = require("dtable-store");
|
|
13
|
+
var _resultCacheManager = require("./result-cache-manager");
|
|
14
|
+
var _threadManager = _interopRequireDefault(require("./thread-manager"));
|
|
15
|
+
function uuid() {
|
|
16
|
+
return Math.random().toString(36).slice(2, 9);
|
|
17
|
+
}
|
|
13
18
|
async function calcViewRowsWithWorker(view, table, value, username, userId, userDepartmentIdsMap) {
|
|
14
|
-
const
|
|
19
|
+
const key = view._id + table._id;
|
|
20
|
+
const cachedRows = _resultCacheManager.viewRowsCacheManager.get(key);
|
|
21
|
+
if (cachedRows) {
|
|
22
|
+
return cachedRows;
|
|
23
|
+
}
|
|
24
|
+
const workerId = uuid();
|
|
25
|
+
let worker;
|
|
26
|
+
try {
|
|
27
|
+
worker = await _threadManager.default.applyThread(workerId, _calc.default);
|
|
28
|
+
} catch (e) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
15
31
|
const calcWorker = Comlink.wrap(worker);
|
|
16
32
|
const cb = () => _dtableStore.Views.getViewRows(view, table, value, username, userId, userDepartmentIdsMap);
|
|
17
33
|
const rows = await calcWorker(Comlink.proxy(cb));
|
|
18
|
-
|
|
34
|
+
_resultCacheManager.viewRowsCacheManager.set(key, rows);
|
|
35
|
+
_threadManager.default.removeThread(workerId);
|
|
19
36
|
return rows;
|
|
20
37
|
}
|
|
21
|
-
async function calcFormulaResultsWithWorker(table, rows, value, formulaColumns, username, userId, userDepartmentIdsMap) {
|
|
22
|
-
const
|
|
38
|
+
async function calcFormulaResultsWithWorker(table, rows, value, formulaColumns, username, userId, userDepartmentIdsMap, viewId) {
|
|
39
|
+
const key = viewId + table._id;
|
|
40
|
+
const cachedResults = _resultCacheManager.formulaResultsCacheManager.get(key);
|
|
41
|
+
if (cachedResults) {
|
|
42
|
+
return cachedResults;
|
|
43
|
+
}
|
|
44
|
+
const workerId = uuid();
|
|
45
|
+
let worker;
|
|
46
|
+
try {
|
|
47
|
+
worker = await _threadManager.default.applyThread(workerId, _calc.default);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
23
51
|
const calcWorker = Comlink.wrap(worker);
|
|
24
52
|
const cb = () => _dtableStore.Views.getTableFormulaResults(table, rows, value, formulaColumns, {
|
|
25
53
|
username,
|
|
@@ -27,6 +55,7 @@ async function calcFormulaResultsWithWorker(table, rows, value, formulaColumns,
|
|
|
27
55
|
userDepartmentIdsMap
|
|
28
56
|
});
|
|
29
57
|
const results = await calcWorker(Comlink.proxy(cb));
|
|
30
|
-
|
|
58
|
+
_resultCacheManager.formulaResultsCacheManager.set(key, results);
|
|
59
|
+
_threadManager.default.removeThread(workerId);
|
|
31
60
|
return results;
|
|
32
61
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.viewRowsCacheManager = exports.formulaResultsCacheManager = void 0;
|
|
7
|
+
class ResultCacheManager {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.cache = new Map();
|
|
10
|
+
}
|
|
11
|
+
get(key) {
|
|
12
|
+
return this.cache.get(key);
|
|
13
|
+
}
|
|
14
|
+
set(key, value) {
|
|
15
|
+
this.cache.set(key, value);
|
|
16
|
+
}
|
|
17
|
+
clear() {
|
|
18
|
+
this.cache.clear();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const viewRowsCacheManager = exports.viewRowsCacheManager = new ResultCacheManager();
|
|
22
|
+
const formulaResultsCacheManager = exports.formulaResultsCacheManager = new ResultCacheManager();
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
class ThreadManager {
|
|
8
|
+
constructor(threadCount) {
|
|
9
|
+
const supportedThreads = navigator.hardwareConcurrency;
|
|
10
|
+
// prevent the browser from freezing by leaving 3 threads for the main thread
|
|
11
|
+
if (typeof supportedThreads === 'number' && supportedThreads - 3 > 0) {
|
|
12
|
+
this.threadCount = supportedThreads - 3;
|
|
13
|
+
} else {
|
|
14
|
+
this.threadCount = 2;
|
|
15
|
+
}
|
|
16
|
+
this.threadList = [];
|
|
17
|
+
this.queue = [];
|
|
18
|
+
}
|
|
19
|
+
async applyThread(id, workerConstructor) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
if (this.threadList.length < this.threadCount) {
|
|
22
|
+
const worker = new workerConstructor();
|
|
23
|
+
worker.id = id;
|
|
24
|
+
this.threadList.push(worker);
|
|
25
|
+
resolve(worker);
|
|
26
|
+
} else {
|
|
27
|
+
this.queue.push({
|
|
28
|
+
id,
|
|
29
|
+
emitter: resolve,
|
|
30
|
+
rejector: reject,
|
|
31
|
+
workerConstructor
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
removeThread(id) {
|
|
37
|
+
const index = this.threadList.findIndex(worker => worker.id === id);
|
|
38
|
+
if (index > -1) {
|
|
39
|
+
this.threadList[index] && this.threadList[index].terminate();
|
|
40
|
+
this.threadList.splice(index, 1);
|
|
41
|
+
}
|
|
42
|
+
if (this.queue.length > 0) {
|
|
43
|
+
const {
|
|
44
|
+
id,
|
|
45
|
+
emitter,
|
|
46
|
+
workerConstructor
|
|
47
|
+
} = this.queue.shift();
|
|
48
|
+
const worker = new workerConstructor();
|
|
49
|
+
worker.id = id;
|
|
50
|
+
this.threadList.push(worker);
|
|
51
|
+
emitter(worker);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
clearQueue() {
|
|
55
|
+
this.queue.forEach(_ref => {
|
|
56
|
+
let {
|
|
57
|
+
rejector
|
|
58
|
+
} = _ref;
|
|
59
|
+
return rejector();
|
|
60
|
+
});
|
|
61
|
+
this.queue = [];
|
|
62
|
+
}
|
|
63
|
+
terminateWorkers() {
|
|
64
|
+
this.threadList.forEach(item => {
|
|
65
|
+
item && item.terminate();
|
|
66
|
+
});
|
|
67
|
+
this.threadList = [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const threadManager = new ThreadManager();
|
|
71
|
+
var _default = exports.default = threadManager;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dtable-statistic",
|
|
3
|
-
"version": "5.0.48-alpha.
|
|
3
|
+
"version": "5.0.48-alpha.13",
|
|
4
4
|
"description": "statistics",
|
|
5
5
|
"main": "dist/dtable-statistic.js",
|
|
6
6
|
"author": "seafile",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"react-grid-layout": "^1.2.5",
|
|
19
19
|
"react-intl-universal": "^2.4.8",
|
|
20
20
|
"reactstrap": "8.9.0",
|
|
21
|
-
"sea-chart": "
|
|
21
|
+
"sea-chart": "0.0.95-alpha.6"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"dtable-ui-component": "~5.0.*",
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
-
var Comlink = _interopRequireWildcard(require("comlink"));
|
|
5
|
-
async function calcFormulaResults(callback) {
|
|
6
|
-
// const res = Views.getTableFormulaResults(table, rows, value, formulaColumns, { username, userId, userDepartmentIdsMap });
|
|
7
|
-
const res = await callback();
|
|
8
|
-
return res;
|
|
9
|
-
}
|
|
10
|
-
Comlink.expose(calcFormulaResults);
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
-
var Comlink = _interopRequireWildcard(require("comlink"));
|
|
5
|
-
async function calcViewRows(callback) {
|
|
6
|
-
// const rows = Views.getViewRows(view, table, value, username, userId, userDepartmentIdsMap);
|
|
7
|
-
const rows = await callback();
|
|
8
|
-
return rows;
|
|
9
|
-
}
|
|
10
|
-
Comlink.expose(calcViewRows);
|