monocart-reporter 2.9.6 → 2.9.7

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/lib/utils/pie.js CHANGED
@@ -1,148 +1,148 @@
1
- const Util = require('./util.js');
2
-
3
- const getSectorPath = function(x, y, r, from, till) {
4
- if (from === till) {
5
- return '';
6
- }
7
-
8
- const ds = [];
9
-
10
- const point = Util.point;
11
-
12
- // move to start point
13
- ds.push(`M${point(x, y)}`);
14
-
15
- // line to outer start point
16
- const osx = Math.cos(from) * r + x;
17
- const osy = Math.sin(from) * r + y;
18
- ds.push(`L${point(osx, osy)}`);
19
-
20
- const getLarge = (v) => {
21
- return v < Math.PI ? 0 : 1;
22
- };
23
-
24
- // 360 is a circle
25
- const radius = till - from;
26
- if (radius >= Math.PI * 2) {
27
- // draw two arc
28
- const tillM = from + radius * 0.5;
29
- const omx = Math.cos(tillM) * r + x;
30
- const omy = Math.sin(tillM) * r + y;
31
- ds.push(`A${point(r, r)} 0 ${getLarge(tillM - from)} 1 ${point(omx, omy)}`);
32
-
33
- // next arc
34
- const oex = Math.cos(till) * r + x;
35
- const oey = Math.sin(till) * r + y;
36
- ds.push(`A${point(r, r)} 0 ${getLarge(till - tillM)} 1 ${point(oex, oey)}`);
37
-
38
- } else {
39
- // small than 360
40
- // arc to outer end point
41
- const oex = Math.cos(till) * r + x;
42
- const oey = Math.sin(till) * r + y;
43
- ds.push(`A${point(r, r)} 0 ${getLarge(radius)} 1 ${point(oex, oey)}`);
44
- }
45
- // close
46
- ds.push('z');
47
-
48
- return ds.join(' ');
49
- };
50
-
51
- const getMovePos = (margin, from, till) => {
52
- const center = from + (till - from) * 0.5;
53
- const px = Math.cos(center) * margin;
54
- const py = Math.sin(center) * margin;
55
- return Util.point(px, py);
56
- };
57
-
58
- const generatePieChart = (pieDataList) => {
59
- const o = {
60
- ns: 'mcr-pie',
61
- width: 150,
62
- height: 150,
63
- margin: 10
64
- };
65
-
66
- const pieWidth = o.height;
67
- const pieHeight = o.height;
68
- const margin = o.margin;
69
-
70
- const x = pieWidth / 2;
71
- const y = pieHeight / 2;
72
- const r = pieHeight / 2 - margin;
73
-
74
- const list = [];
75
- // start from 12 clock
76
- let start = 0.75;
77
- pieDataList.forEach((item, i) => {
78
-
79
- const from = 2 * Math.PI * start;
80
- const percent = parseFloat(item.percent) / 100 + start;
81
- const till = 2 * Math.PI * percent;
82
- start = percent;
83
-
84
- const d = getSectorPath(x, y, r, from, till);
85
- const pos = getMovePos(margin, from, till);
86
- // console.log(pos);
87
-
88
- list.push({
89
- id: item.id,
90
- name: item.name,
91
- value: item.value,
92
- color: item.color,
93
- percent: item.percent,
94
- from: '0,0',
95
- to: '0,0',
96
- pos,
97
- d
98
- });
99
-
100
- });
101
-
102
- const pies = [];
103
-
104
- // draw nothing bg
105
- const total = pieDataList.map((item) => item.value).reduce((a, v) => a + v, 0);
106
- if (total === 0) {
107
- pies.push(`<circle cx="${x}" cy="${y}" r="${r}" fill="#f5f5f5" />`);
108
- } else {
109
- list.forEach((item) => {
110
- pies.push(`<g class="${o.ns}-path-${item.id}">`);
111
- pies.push(`<path d="${item.d}" fill="${item.color}" opacity="0.8" />`);
112
- pies.push(`<animateTransform from="${item.from}" to="${item.to}" pos="${item.pos}" attributeName="transform" type="translate" dur="0.2s" fill="freeze" repeatCount="1" restart="always" />`);
113
- pies.push('</g>');
114
- });
115
- }
116
-
117
- // legends
118
- const legendWidth = 200;
119
- const legendHeight = Math.floor((o.height - margin * 2) / list.length);
120
- const legends = [`<g transform="translate(${pieWidth + margin} ${margin})">`];
121
- list.forEach((item, i) => {
122
- const midY = legendHeight * 0.5;
123
- legends.push(`<g class="${o.ns}-legend-${item.id}" transform="translate(0 ${legendHeight * i})">`);
124
- legends.push(`<circle cx="10" cy="${midY}" r="10" fill="${item.color}" opacity="0.8" />`);
125
- legends.push(`<text x="30" y="${midY}" alignment-baseline="middle">${item.name}</text>`);
126
- legends.push(`<text x="115" y="${midY}" alignment-baseline="middle" text-anchor="middle">${Util.NF(item.value)}</text>`);
127
- legends.push(`<text x="${legendWidth - margin}" y="${midY}" alignment-baseline="middle" text-anchor="end">${item.percent}</text>`);
128
- legends.push('</g>');
129
- });
130
- legends.push('</g>');
131
-
132
- const height = o.height;
133
- const width = pieWidth + margin + legendWidth;
134
- o.width = width;
135
-
136
- const viewBox = `0 0 ${width} ${height}`;
137
- const ls = [`<svg viewBox="${viewBox}" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">`];
138
- ls.push(pies.join(''));
139
- ls.push(legends.join(''));
140
- ls.push('</svg>');
141
- o.svg = ls.join('');
142
-
143
- return o;
144
- };
145
-
146
- // ====================================================================================
147
-
148
- module.exports = generatePieChart;
1
+ const Util = require('./util.js');
2
+
3
+ const getSectorPath = function(x, y, r, from, till) {
4
+ if (from === till) {
5
+ return '';
6
+ }
7
+
8
+ const ds = [];
9
+
10
+ const point = Util.point;
11
+
12
+ // move to start point
13
+ ds.push(`M${point(x, y)}`);
14
+
15
+ // line to outer start point
16
+ const osx = Math.cos(from) * r + x;
17
+ const osy = Math.sin(from) * r + y;
18
+ ds.push(`L${point(osx, osy)}`);
19
+
20
+ const getLarge = (v) => {
21
+ return v < Math.PI ? 0 : 1;
22
+ };
23
+
24
+ // 360 is a circle
25
+ const radius = till - from;
26
+ if (radius >= Math.PI * 2) {
27
+ // draw two arc
28
+ const tillM = from + radius * 0.5;
29
+ const omx = Math.cos(tillM) * r + x;
30
+ const omy = Math.sin(tillM) * r + y;
31
+ ds.push(`A${point(r, r)} 0 ${getLarge(tillM - from)} 1 ${point(omx, omy)}`);
32
+
33
+ // next arc
34
+ const oex = Math.cos(till) * r + x;
35
+ const oey = Math.sin(till) * r + y;
36
+ ds.push(`A${point(r, r)} 0 ${getLarge(till - tillM)} 1 ${point(oex, oey)}`);
37
+
38
+ } else {
39
+ // small than 360
40
+ // arc to outer end point
41
+ const oex = Math.cos(till) * r + x;
42
+ const oey = Math.sin(till) * r + y;
43
+ ds.push(`A${point(r, r)} 0 ${getLarge(radius)} 1 ${point(oex, oey)}`);
44
+ }
45
+ // close
46
+ ds.push('z');
47
+
48
+ return ds.join(' ');
49
+ };
50
+
51
+ const getMovePos = (margin, from, till) => {
52
+ const center = from + (till - from) * 0.5;
53
+ const px = Math.cos(center) * margin;
54
+ const py = Math.sin(center) * margin;
55
+ return Util.point(px, py);
56
+ };
57
+
58
+ const generatePieChart = (pieDataList) => {
59
+ const o = {
60
+ ns: 'mcr-pie',
61
+ width: 150,
62
+ height: 150,
63
+ margin: 10
64
+ };
65
+
66
+ const pieWidth = o.height;
67
+ const pieHeight = o.height;
68
+ const margin = o.margin;
69
+
70
+ const x = pieWidth / 2;
71
+ const y = pieHeight / 2;
72
+ const r = pieHeight / 2 - margin;
73
+
74
+ const list = [];
75
+ // start from 12 clock
76
+ let start = 0.75;
77
+ pieDataList.forEach((item, i) => {
78
+
79
+ const from = 2 * Math.PI * start;
80
+ const percent = parseFloat(item.percent) / 100 + start;
81
+ const till = 2 * Math.PI * percent;
82
+ start = percent;
83
+
84
+ const d = getSectorPath(x, y, r, from, till);
85
+ const pos = getMovePos(margin, from, till);
86
+ // console.log(pos);
87
+
88
+ list.push({
89
+ id: item.id,
90
+ name: item.name,
91
+ value: item.value,
92
+ color: item.color,
93
+ percent: item.percent,
94
+ from: '0,0',
95
+ to: '0,0',
96
+ pos,
97
+ d
98
+ });
99
+
100
+ });
101
+
102
+ const pies = [];
103
+
104
+ // draw nothing bg
105
+ const total = pieDataList.map((item) => item.value).reduce((a, v) => a + v, 0);
106
+ if (total === 0) {
107
+ pies.push(`<circle cx="${x}" cy="${y}" r="${r}" fill="#f5f5f5" />`);
108
+ } else {
109
+ list.forEach((item) => {
110
+ pies.push(`<g class="${o.ns}-path-${item.id}">`);
111
+ pies.push(`<path d="${item.d}" fill="${item.color}" opacity="0.8" />`);
112
+ pies.push(`<animateTransform from="${item.from}" to="${item.to}" pos="${item.pos}" attributeName="transform" type="translate" dur="0.2s" fill="freeze" repeatCount="1" restart="always" />`);
113
+ pies.push('</g>');
114
+ });
115
+ }
116
+
117
+ // legends
118
+ const legendWidth = 200;
119
+ const legendHeight = Math.floor((o.height - margin * 2) / list.length);
120
+ const legends = [`<g transform="translate(${pieWidth + margin} ${margin})">`];
121
+ list.forEach((item, i) => {
122
+ const midY = legendHeight * 0.5;
123
+ legends.push(`<g class="${o.ns}-legend-${item.id}" transform="translate(0 ${legendHeight * i})">`);
124
+ legends.push(`<circle cx="10" cy="${midY}" r="10" fill="${item.color}" opacity="0.8" />`);
125
+ legends.push(`<text x="30" y="${midY}" alignment-baseline="middle">${item.name}</text>`);
126
+ legends.push(`<text x="115" y="${midY}" alignment-baseline="middle" text-anchor="middle">${Util.NF(item.value)}</text>`);
127
+ legends.push(`<text x="${legendWidth - margin}" y="${midY}" alignment-baseline="middle" text-anchor="end">${item.percent}</text>`);
128
+ legends.push('</g>');
129
+ });
130
+ legends.push('</g>');
131
+
132
+ const height = o.height;
133
+ const width = pieWidth + margin + legendWidth;
134
+ o.width = width;
135
+
136
+ const viewBox = `0 0 ${width} ${height}`;
137
+ const ls = [`<svg viewBox="${viewBox}" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">`];
138
+ ls.push(pies.join(''));
139
+ ls.push(legends.join(''));
140
+ ls.push('</svg>');
141
+ o.svg = ls.join('');
142
+
143
+ return o;
144
+ };
145
+
146
+ // ====================================================================================
147
+
148
+ module.exports = generatePieChart;
@@ -1,145 +1,145 @@
1
- const os = require('os');
2
-
3
- const getCpuMeasure = function() {
4
- let totalIdle = 0;
5
- let totalTick = 0;
6
- const cpus = os.cpus();
7
- const count = cpus.length;
8
- cpus.forEach((cpu) => {
9
- totalTick += Object.values(cpu.times).reduce((acc, tv) => acc + tv, 0);
10
- totalIdle += cpu.times.idle;
11
- });
12
- return {
13
- // totalTick: totalTick,
14
- avgIdle: totalIdle / count,
15
- avgTotal: totalTick / count
16
- };
17
- };
18
-
19
- const getCpuUsage = (time = 1000) => {
20
- return new Promise((resolve) => {
21
- const previousMeasure = getCpuMeasure();
22
- // const previousUsage = process.cpuUsage();
23
- setTimeout(() => {
24
-
25
- // total CPU
26
- const currentMeasure = getCpuMeasure();
27
- const totalIdleDiff = currentMeasure.avgIdle - previousMeasure.avgIdle;
28
- const totalAvgDiff = currentMeasure.avgTotal - previousMeasure.avgTotal;
29
- // to number for chart
30
- const totalPercent = parseFloat((100 - totalIdleDiff / totalAvgDiff * 100).toFixed(2));
31
-
32
- // process CPU
33
- // const currentUsage = process.cpuUsage(previousUsage);
34
- // microseconds to miliseconds
35
- // const processTime = (currentUsage.system + currentUsage.user) / 1000;
36
- // const totalDiff = currentMeasure.totalTick - previousMeasure.totalTick;
37
- // const processPercent = parseFloat((processTime / totalDiff * 100).toFixed(2));
38
-
39
- resolve({
40
- percent: totalPercent
41
- // process: processPercent
42
- });
43
- }, time);
44
- });
45
- };
46
-
47
- const getMemUsage = () => {
48
- const freeMem = os.freemem();
49
- // integer representing the Resident Set Size (RSS) in bytes.
50
- // const processMem = process.memoryUsage.rss();
51
- return {
52
- free: freeMem
53
- };
54
- };
55
-
56
-
57
- const getTickInfo = async () => {
58
- const cpu = await getCpuUsage();
59
- const mem = getMemUsage();
60
- return {
61
- cpu,
62
- mem,
63
- timestamp: Date.now()
64
- };
65
- };
66
-
67
- // =======================================================================================
68
-
69
- const getCpuInfo = function() {
70
- const cpus = os.cpus();
71
- const cpu = cpus[0];
72
- return {
73
- color: '#117DBB',
74
- count: cpus.length,
75
- model: cpu.model,
76
- speed: cpu.speed
77
- };
78
- };
79
-
80
- const getMemInfo = () => {
81
- return {
82
- color: '#8B12AE',
83
- total: os.totalmem()
84
- };
85
- };
86
-
87
- const getSystemInfo = () => {
88
- const cpu = getCpuInfo();
89
- const mem = getMemInfo();
90
-
91
- return {
92
- cpu,
93
- mem,
94
-
95
- // CPU architecture 'arm', 'arm64', 'ia32', 'x64'.
96
- arch: os.arch(),
97
- // 'aix', 'darwin', 'freebsd','linux', 'openbsd', 'sunos', 'win32'.
98
- platform: os.platform(),
99
- // Returns the operating system as a string.
100
- release: os.release(),
101
- // 'Linux', 'Darwin', 'Windows_NT'
102
- type: os.type(),
103
- // Returns a string identifying the kernel version.
104
- version: os.version(),
105
- // Returns the system uptime in number of seconds.
106
- uptime: os.uptime(),
107
-
108
- hostname: os.hostname(),
109
-
110
- // No Sensitive Data: tmpdir, homedir, userInfo
111
-
112
- // Added in: v18.9.0
113
- // os.machine(),
114
- // os.availableParallelism(),
115
-
116
- // os.networkInterfaces()
117
-
118
- pid: process.pid,
119
- node: process.versions.node,
120
- v8: process.versions.v8
121
- };
122
- };
123
-
124
- module.exports = {
125
- getSystemInfo,
126
- getTickInfo
127
- };
128
-
129
- // setInterval(async () => {
130
- // const info = await getSystemInfo();
131
- // console.log(info);
132
- // const tick = await getTickInfo();
133
- // console.log(tick);
134
- // }, 1000);
135
-
136
-
137
- // mock CPU load
138
- // take 500ms in 1000ms
139
- // = single cpu 50%
140
- // = total 8 cpus 50%/8 = 6.25%
141
-
142
- // setInterval(function() {
143
- // const now = Date.now();
144
- // while (Date.now() - now < 500) { }
145
- // }, 1000);
1
+ const os = require('os');
2
+
3
+ const getCpuMeasure = function() {
4
+ let totalIdle = 0;
5
+ let totalTick = 0;
6
+ const cpus = os.cpus();
7
+ const count = cpus.length;
8
+ cpus.forEach((cpu) => {
9
+ totalTick += Object.values(cpu.times).reduce((acc, tv) => acc + tv, 0);
10
+ totalIdle += cpu.times.idle;
11
+ });
12
+ return {
13
+ // totalTick: totalTick,
14
+ avgIdle: totalIdle / count,
15
+ avgTotal: totalTick / count
16
+ };
17
+ };
18
+
19
+ const getCpuUsage = (time = 1000) => {
20
+ return new Promise((resolve) => {
21
+ const previousMeasure = getCpuMeasure();
22
+ // const previousUsage = process.cpuUsage();
23
+ setTimeout(() => {
24
+
25
+ // total CPU
26
+ const currentMeasure = getCpuMeasure();
27
+ const totalIdleDiff = currentMeasure.avgIdle - previousMeasure.avgIdle;
28
+ const totalAvgDiff = currentMeasure.avgTotal - previousMeasure.avgTotal;
29
+ // to number for chart
30
+ const totalPercent = parseFloat((100 - totalIdleDiff / totalAvgDiff * 100).toFixed(2));
31
+
32
+ // process CPU
33
+ // const currentUsage = process.cpuUsage(previousUsage);
34
+ // microseconds to miliseconds
35
+ // const processTime = (currentUsage.system + currentUsage.user) / 1000;
36
+ // const totalDiff = currentMeasure.totalTick - previousMeasure.totalTick;
37
+ // const processPercent = parseFloat((processTime / totalDiff * 100).toFixed(2));
38
+
39
+ resolve({
40
+ percent: totalPercent
41
+ // process: processPercent
42
+ });
43
+ }, time);
44
+ });
45
+ };
46
+
47
+ const getMemUsage = () => {
48
+ const freeMem = os.freemem();
49
+ // integer representing the Resident Set Size (RSS) in bytes.
50
+ // const processMem = process.memoryUsage.rss();
51
+ return {
52
+ free: freeMem
53
+ };
54
+ };
55
+
56
+
57
+ const getTickInfo = async () => {
58
+ const cpu = await getCpuUsage();
59
+ const mem = getMemUsage();
60
+ return {
61
+ cpu,
62
+ mem,
63
+ timestamp: Date.now()
64
+ };
65
+ };
66
+
67
+ // =======================================================================================
68
+
69
+ const getCpuInfo = function() {
70
+ const cpus = os.cpus();
71
+ const cpu = cpus[0];
72
+ return {
73
+ color: '#117DBB',
74
+ count: cpus.length,
75
+ model: cpu.model,
76
+ speed: cpu.speed
77
+ };
78
+ };
79
+
80
+ const getMemInfo = () => {
81
+ return {
82
+ color: '#8B12AE',
83
+ total: os.totalmem()
84
+ };
85
+ };
86
+
87
+ const getSystemInfo = () => {
88
+ const cpu = getCpuInfo();
89
+ const mem = getMemInfo();
90
+
91
+ return {
92
+ cpu,
93
+ mem,
94
+
95
+ // CPU architecture 'arm', 'arm64', 'ia32', 'x64'.
96
+ arch: os.arch(),
97
+ // 'aix', 'darwin', 'freebsd','linux', 'openbsd', 'sunos', 'win32'.
98
+ platform: os.platform(),
99
+ // Returns the operating system as a string.
100
+ release: os.release(),
101
+ // 'Linux', 'Darwin', 'Windows_NT'
102
+ type: os.type(),
103
+ // Returns a string identifying the kernel version.
104
+ version: os.version(),
105
+ // Returns the system uptime in number of seconds.
106
+ uptime: os.uptime(),
107
+
108
+ hostname: os.hostname(),
109
+
110
+ // No Sensitive Data: tmpdir, homedir, userInfo
111
+
112
+ // Added in: v18.9.0
113
+ // os.machine(),
114
+ // os.availableParallelism(),
115
+
116
+ // os.networkInterfaces()
117
+
118
+ pid: process.pid,
119
+ node: process.versions.node,
120
+ v8: process.versions.v8
121
+ };
122
+ };
123
+
124
+ module.exports = {
125
+ getSystemInfo,
126
+ getTickInfo
127
+ };
128
+
129
+ // setInterval(async () => {
130
+ // const info = await getSystemInfo();
131
+ // console.log(info);
132
+ // const tick = await getTickInfo();
133
+ // console.log(tick);
134
+ // }, 1000);
135
+
136
+
137
+ // mock CPU load
138
+ // take 500ms in 1000ms
139
+ // = single cpu 50%
140
+ // = total 8 cpus 50%/8 = 6.25%
141
+
142
+ // setInterval(function() {
143
+ // const now = Date.now();
144
+ // while (Date.now() - now < 500) { }
145
+ // }, 1000);