dtable-statistic 5.0.54 → 5.0.55
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 +11 -8
- package/es/utils/formula-calc-worker/calc-with-worker.js +85 -0
- package/es/utils/formula-calc-worker/calc.worker.js +18 -0
- package/es/utils/formula-calc-worker/result-cache-manager.js +53 -0
- package/es/utils/formula-calc-worker/thread-manager.js +71 -0
- package/es/utils/hasReactElement.js +37 -0
- package/package.json +3 -2
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
|
@@ -4,6 +4,8 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
var _react = _interopRequireDefault(require("react"));
|
|
5
5
|
var _dtableStore = require("dtable-store");
|
|
6
6
|
var _dashboard = _interopRequireDefault(require("./dashboard"));
|
|
7
|
+
var _calcWithWorker = require("./utils/formula-calc-worker/calc-with-worker");
|
|
8
|
+
var _resultCacheManager = require("./utils/formula-calc-worker/result-cache-manager");
|
|
7
9
|
class Statistic {
|
|
8
10
|
static getInitProps() {
|
|
9
11
|
if (!window || !window.app) {
|
|
@@ -33,16 +35,17 @@ class Statistic {
|
|
|
33
35
|
deletePluginSettings: this.deletePluginSettings.bind(this, dtableStore)
|
|
34
36
|
};
|
|
35
37
|
}
|
|
36
|
-
static getViewRows(view, table) {
|
|
38
|
+
static async getViewRows(view, table) {
|
|
37
39
|
const dtableStore = window.app.dtableStore || {};
|
|
38
40
|
const {
|
|
39
41
|
username = null,
|
|
40
42
|
userId = null,
|
|
41
43
|
userDepartmentIdsMap = null
|
|
42
44
|
} = dtableStore.dtableSettings;
|
|
43
|
-
|
|
45
|
+
const rows = await (0, _calcWithWorker.calcViewRowsWithWorker)(view, table, dtableStore.value, username, userId, userDepartmentIdsMap);
|
|
46
|
+
return rows;
|
|
44
47
|
}
|
|
45
|
-
static getTableFormulaResults(table, rows) {
|
|
48
|
+
static async getTableFormulaResults(table, rows, value, viewId) {
|
|
46
49
|
const dtableStore = window.app.dtableStore || {};
|
|
47
50
|
const {
|
|
48
51
|
username = null,
|
|
@@ -50,11 +53,8 @@ class Statistic {
|
|
|
50
53
|
userDepartmentIdsMap = null
|
|
51
54
|
} = dtableStore.dtableSettings;
|
|
52
55
|
const formulaColumns = _dtableStore.Views.getFormulaColumnsContainLinks(table);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
userId,
|
|
56
|
-
userDepartmentIdsMap
|
|
57
|
-
});
|
|
56
|
+
const res = await (0, _calcWithWorker.calcFormulaResultsWithWorker)(table, rows, dtableStore.value, formulaColumns, username, userId, userDepartmentIdsMap, viewId);
|
|
57
|
+
return res;
|
|
58
58
|
}
|
|
59
59
|
static updateStatistics(dtableStore, statistics) {
|
|
60
60
|
dtableStore.updateStatisticsList(statistics);
|
|
@@ -74,6 +74,9 @@ class Statistic {
|
|
|
74
74
|
animation: true
|
|
75
75
|
};
|
|
76
76
|
if (!window.app) return;
|
|
77
|
+
_resultCacheManager.viewRowsCacheManager.clear();
|
|
78
|
+
_resultCacheManager.formulaResultsCacheManager.clear();
|
|
79
|
+
(0, _resultCacheManager.clearCachedBase)();
|
|
77
80
|
window.app.unmountWidget({
|
|
78
81
|
animation
|
|
79
82
|
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.calcFormulaResultsWithWorker = calcFormulaResultsWithWorker;
|
|
9
|
+
exports.calcViewRowsWithWorker = calcViewRowsWithWorker;
|
|
10
|
+
var Comlink = _interopRequireWildcard(require("comlink"));
|
|
11
|
+
var _calc = _interopRequireDefault(require("./calc.worker"));
|
|
12
|
+
var _resultCacheManager = require("./result-cache-manager");
|
|
13
|
+
var _threadManager = _interopRequireDefault(require("./thread-manager"));
|
|
14
|
+
var _lodashEs = require("lodash-es");
|
|
15
|
+
function uuid() {
|
|
16
|
+
return Math.random().toString(36).slice(2, 9);
|
|
17
|
+
}
|
|
18
|
+
async function calcViewRowsWithWorker(view, table, value, username, userId, userDepartmentIdsMap) {
|
|
19
|
+
const key = view._id + table._id;
|
|
20
|
+
const cachedRows = _resultCacheManager.viewRowsCacheManager.get(key);
|
|
21
|
+
if (cachedRows) {
|
|
22
|
+
return cachedRows;
|
|
23
|
+
}
|
|
24
|
+
if (!_resultCacheManager.cachedBase) {
|
|
25
|
+
(0, _resultCacheManager.initCachedBase)(value);
|
|
26
|
+
}
|
|
27
|
+
const clonedTable = (0, _lodashEs.cloneDeep)(table);
|
|
28
|
+
// Symbol can't passed to workers
|
|
29
|
+
clonedTable.columns = clonedTable.columns.map(c => {
|
|
30
|
+
delete c.editor;
|
|
31
|
+
delete c.formatter;
|
|
32
|
+
return c;
|
|
33
|
+
});
|
|
34
|
+
const workerId = uuid();
|
|
35
|
+
let worker;
|
|
36
|
+
try {
|
|
37
|
+
worker = await _threadManager.default.applyThread(workerId, _calc.default);
|
|
38
|
+
} catch (e) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const calcWorker = Comlink.wrap(worker);
|
|
42
|
+
const rows = await calcWorker.getViewRows(view, clonedTable, _resultCacheManager.cachedBase, username, userId, userDepartmentIdsMap);
|
|
43
|
+
if (rows && Object.keys(rows).length) {
|
|
44
|
+
_resultCacheManager.viewRowsCacheManager.set(key, rows);
|
|
45
|
+
}
|
|
46
|
+
_threadManager.default.removeThread(workerId);
|
|
47
|
+
return rows;
|
|
48
|
+
}
|
|
49
|
+
async function calcFormulaResultsWithWorker(table, rows, value, formulaColumns, username, userId, userDepartmentIdsMap, viewId) {
|
|
50
|
+
const key = viewId + table._id;
|
|
51
|
+
const cachedResults = _resultCacheManager.formulaResultsCacheManager.get(key);
|
|
52
|
+
if (cachedResults) {
|
|
53
|
+
return cachedResults;
|
|
54
|
+
}
|
|
55
|
+
if (!_resultCacheManager.cachedBase) {
|
|
56
|
+
(0, _resultCacheManager.initCachedBase)(value);
|
|
57
|
+
}
|
|
58
|
+
const clonedTable = (0, _lodashEs.cloneDeep)(table);
|
|
59
|
+
// Symbol can't passed to workers
|
|
60
|
+
clonedTable.columns = clonedTable.columns.map(c => {
|
|
61
|
+
delete c.editor;
|
|
62
|
+
delete c.formatter;
|
|
63
|
+
return c;
|
|
64
|
+
});
|
|
65
|
+
let clonedFormulaColumns = (0, _lodashEs.cloneDeep)(formulaColumns);
|
|
66
|
+
clonedFormulaColumns = clonedFormulaColumns.map(c => {
|
|
67
|
+
delete c.editor;
|
|
68
|
+
delete c.formatter;
|
|
69
|
+
return c;
|
|
70
|
+
});
|
|
71
|
+
const workerId = uuid();
|
|
72
|
+
let worker;
|
|
73
|
+
try {
|
|
74
|
+
worker = await _threadManager.default.applyThread(workerId, _calc.default);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
const calcWorker = Comlink.wrap(worker);
|
|
79
|
+
const results = await calcWorker.getTableFormulaResults(clonedTable, rows, _resultCacheManager.cachedBase, clonedFormulaColumns, username, userId, userDepartmentIdsMap);
|
|
80
|
+
if (results && Object.keys(results).length) {
|
|
81
|
+
_resultCacheManager.formulaResultsCacheManager.set(key, results);
|
|
82
|
+
}
|
|
83
|
+
_threadManager.default.removeThread(workerId);
|
|
84
|
+
return results;
|
|
85
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
+
var Comlink = _interopRequireWildcard(require("comlink"));
|
|
5
|
+
var _dtableStore = require("dtable-store");
|
|
6
|
+
const workerFunctions = {
|
|
7
|
+
async getViewRows(view, table, value, username, userId, userDepartmentIdsMap) {
|
|
8
|
+
return _dtableStore.Views.getViewRows(view, table, value, username, userId, userDepartmentIdsMap);
|
|
9
|
+
},
|
|
10
|
+
async getTableFormulaResults(table, rows, value, formulaColumns, username, userId, userDepartmentIdsMap) {
|
|
11
|
+
return _dtableStore.Views.getTableFormulaResults(table, rows, value, formulaColumns, {
|
|
12
|
+
username,
|
|
13
|
+
userId,
|
|
14
|
+
userDepartmentIdsMap
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
Comlink.expose(workerFunctions);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.cachedBase = void 0;
|
|
7
|
+
exports.clearCachedBase = clearCachedBase;
|
|
8
|
+
exports.formulaResultsCacheManager = void 0;
|
|
9
|
+
exports.initCachedBase = initCachedBase;
|
|
10
|
+
exports.viewRowsCacheManager = void 0;
|
|
11
|
+
var _lodashEs = require("lodash-es");
|
|
12
|
+
class ResultCacheManager {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.cache = new Map();
|
|
15
|
+
}
|
|
16
|
+
get(key) {
|
|
17
|
+
return this.cache.get(key);
|
|
18
|
+
}
|
|
19
|
+
set(key, value) {
|
|
20
|
+
this.cache.set(key, value);
|
|
21
|
+
}
|
|
22
|
+
clear() {
|
|
23
|
+
this.cache.clear();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const viewRowsCacheManager = exports.viewRowsCacheManager = new ResultCacheManager();
|
|
27
|
+
const formulaResultsCacheManager = exports.formulaResultsCacheManager = new ResultCacheManager();
|
|
28
|
+
let cachedBase = exports.cachedBase = null;
|
|
29
|
+
function initCachedBase(value) {
|
|
30
|
+
// don't clone rows cause they are really big
|
|
31
|
+
const tableIdRowMap = {};
|
|
32
|
+
value.tables.forEach(t => {
|
|
33
|
+
tableIdRowMap[t._id] = t === null || t === void 0 ? void 0 : t.rows;
|
|
34
|
+
t.rows && delete t.rows;
|
|
35
|
+
});
|
|
36
|
+
const clonedBase = (0, _lodashEs.cloneDeep)(value);
|
|
37
|
+
clonedBase.tables.forEach(t => {
|
|
38
|
+
// delete symbols
|
|
39
|
+
t.columns = t.columns.map(c => {
|
|
40
|
+
delete c.editor;
|
|
41
|
+
delete c.formatter;
|
|
42
|
+
return c;
|
|
43
|
+
});
|
|
44
|
+
t.rows = tableIdRowMap[t._id];
|
|
45
|
+
});
|
|
46
|
+
value.tables.forEach(t => {
|
|
47
|
+
t.rows = tableIdRowMap[t._id];
|
|
48
|
+
});
|
|
49
|
+
exports.cachedBase = cachedBase = clonedBase;
|
|
50
|
+
}
|
|
51
|
+
function clearCachedBase() {
|
|
52
|
+
exports.cachedBase = cachedBase = null;
|
|
53
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.containsReactElementSymbol = containsReactElementSymbol;
|
|
7
|
+
const REACT_ELEMENT_TYPE = Symbol.for('react.element');
|
|
8
|
+
function containsReactElementSymbol(obj) {
|
|
9
|
+
let parent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
10
|
+
if (obj === null || typeof obj !== 'object') {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Check if the current object is a React element
|
|
15
|
+
if (obj.$$typeof === REACT_ELEMENT_TYPE) {
|
|
16
|
+
console.log('Parent object containing Symbol(react.element):', parent);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Recursively check all properties of the object
|
|
21
|
+
for (let key in obj) {
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
23
|
+
if (containsReactElementSymbol(obj[key], obj)) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Check symbol properties as well
|
|
30
|
+
const symbolProperties = Object.getOwnPropertySymbols(obj);
|
|
31
|
+
for (let symbol of symbolProperties) {
|
|
32
|
+
if (containsReactElementSymbol(obj[symbol], obj)) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dtable-statistic",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.55",
|
|
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": "~0.0.
|
|
21
|
+
"sea-chart": "~0.0.101"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"dtable-ui-component": "~5.1.*",
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"dtable-store": "4.3.6",
|
|
56
56
|
"dtable-ui-component": "~5.1.0",
|
|
57
57
|
"dtable-utils": "5.0.0",
|
|
58
|
+
"dtable-web-api": "^5.0.3",
|
|
58
59
|
"eslint": "^8.3.0",
|
|
59
60
|
"eslint-config-react-app": "^7.0.1",
|
|
60
61
|
"eslint-webpack-plugin": "^3.1.1",
|