wenay-common2 1.0.42 → 1.0.43

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.
@@ -1,29 +1,32 @@
1
1
  export type tApiKey = string;
2
- type tType = "UID" | "IP" | (string & {});
2
+ type tType = "UID" | "IP" | tApiKey;
3
3
  type tFunc = {
4
4
  timeStamp?: number;
5
5
  type: tType;
6
6
  weight: number;
7
7
  };
8
- export declare function funcTimeW(maxHistoryElements?: number): {
8
+ export declare function funcTimeW(): {
9
9
  dStatic: {
10
10
  [key: string]: [number, number][];
11
11
  };
12
+ data: any[];
12
13
  add(item: tFunc): void;
13
14
  cleanByTime(type: tType, ms?: number): void;
14
15
  weight(type: tType, ms?: number): number;
15
- byWeight(type: tType, weight?: number, timeNow?: number): number;
16
- removeKey(type: tType): void;
16
+ weightNow(type: tType, ms?: number): number;
17
+ byWeight(type: tType, weight?: number): number;
18
+ byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
17
19
  };
18
20
  export declare const FuncTimeWait: {
19
21
  dStatic: {
20
22
  [key: string]: [number, number][];
21
23
  };
24
+ data: any[];
22
25
  add(item: tFunc): void;
23
26
  cleanByTime(type: tType, ms?: number): void;
24
27
  weight(type: tType, ms?: number): number;
25
- byWeight(type: tType, weight?: number, timeNow?: number): number;
26
- removeKey(type: tType): void;
28
+ weightNow(type: tType, ms?: number): number;
29
+ byWeight(type: tType, weight?: number): number;
30
+ byWeightTimeNow(type: tType, timeNow?: number, weight?: number): number;
27
31
  };
28
- export declare function testFuncTimeW(): void;
29
32
  export {};
@@ -2,74 +2,89 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FuncTimeWait = void 0;
4
4
  exports.funcTimeW = funcTimeW;
5
- exports.testFuncTimeW = testFuncTimeW;
6
- const common_1 = require("./core/common");
7
- function funcTimeW(maxHistoryElements = 800) {
5
+ function funcTimeW() {
8
6
  const dStatic = {};
9
- function getInsertIndex(arr, timeStamp) {
10
- if (arr.length === 0)
11
- return 0;
12
- if (timeStamp >= arr[arr.length - 1][0])
13
- return arr.length;
14
- if (timeStamp <= arr[0][0])
15
- return 0;
16
- const index = (0, common_1.BSearch)(arr, timeStamp, (a, b) => a[0] - b, "greatOrEqual", "ascend");
17
- return index === -1 ? arr.length : index;
18
- }
19
- function cleanupEmptyKey(type) {
20
- if (dStatic[type] && dStatic[type].length === 0) {
21
- delete dStatic[type];
22
- }
23
- }
7
+ const data = [];
24
8
  return {
25
9
  dStatic,
10
+ data,
26
11
  add(item) {
27
- const arr = (dStatic[item.type] ??= []);
28
- const timeStamp = item.timeStamp ?? Date.now();
29
- const insertIndex = getInsertIndex(arr, timeStamp);
30
- if (insertIndex === arr.length) {
31
- arr.push([timeStamp, item.weight]);
32
- }
33
- else {
34
- arr.splice(insertIndex, 0, [timeStamp, item.weight]);
12
+ if (!dStatic[item.type]) {
13
+ dStatic[item.type] = [];
35
14
  }
15
+ dStatic[item.type].push([item.timeStamp ?? Date.now(), item.weight]);
36
16
  },
37
17
  cleanByTime(type, ms = 60 * 1000) {
38
18
  const arr = dStatic[type];
39
19
  if (!arr || arr.length === 0)
40
20
  return;
41
- const cutoff = Date.now() - ms;
42
- if (arr[0][0] > cutoff)
21
+ const timeStamp = Date.now();
22
+ if (arr[0][0] > timeStamp - ms)
43
23
  return;
44
- const cutIndex = (0, common_1.BSearch)(arr, cutoff, (a, b) => a[0] - b, "greatOrEqual", "ascend");
45
- if (cutIndex === -1) {
46
- arr.splice(0, arr.length);
24
+ let cutIndex = 0;
25
+ while (cutIndex < arr.length && arr[cutIndex][0] < timeStamp - ms) {
26
+ cutIndex++;
47
27
  }
48
- else if (cutIndex > 0) {
28
+ if (cutIndex > 0) {
49
29
  arr.splice(0, cutIndex);
50
30
  }
51
- cleanupEmptyKey(type);
52
31
  },
53
32
  weight(type, ms = 60 * 1000) {
54
33
  const arr = dStatic[type];
55
34
  if (!arr || arr.length === 0)
56
35
  return 0;
57
- const cutoff = Date.now() - ms;
36
+ const timeStamp = Date.now();
58
37
  let sum = 0;
59
38
  let i = arr.length - 1;
60
39
  for (; i >= 0; i--) {
61
40
  const [_time, _weight] = arr[i];
62
- if (_time < cutoff)
41
+ if (_time < timeStamp - ms)
63
42
  break;
64
43
  sum += _weight;
65
44
  }
66
45
  if (i >= 0) {
67
46
  arr.splice(0, i + 1);
68
- cleanupEmptyKey(type);
69
47
  }
70
48
  return sum;
71
49
  },
72
- byWeight(type, weight = 50000, timeNow = Date.now()) {
50
+ weightNow(type, ms = 60 * 1000) {
51
+ const arr = dStatic[type];
52
+ if (!arr || arr.length === 0)
53
+ return 0;
54
+ const timeStamp = Date.now();
55
+ let sum = 0;
56
+ let i = arr.length - 1;
57
+ for (; i >= 0; i--) {
58
+ const [_time, _weight] = arr[i];
59
+ if (_time < timeStamp - ms)
60
+ break;
61
+ sum += _weight;
62
+ }
63
+ if (i >= 0) {
64
+ arr.splice(0, i + 1);
65
+ }
66
+ return sum;
67
+ },
68
+ byWeight(type, weight = 50000) {
69
+ const arr = dStatic[type];
70
+ if (!arr || arr.length === 0)
71
+ return 0;
72
+ let sum = 0;
73
+ let i = arr.length - 1;
74
+ let result = 0;
75
+ for (; i >= 0; i--) {
76
+ sum += arr[i][1];
77
+ if (sum > weight) {
78
+ result = arr[i + 1]?.[0] ?? 0;
79
+ break;
80
+ }
81
+ }
82
+ if (i > 800) {
83
+ arr.splice(0, i - 800);
84
+ }
85
+ return result;
86
+ },
87
+ byWeightTimeNow(type, timeNow = Date.now(), weight = 50000) {
73
88
  const arr = dStatic[type];
74
89
  if (!arr || arr.length === 0)
75
90
  return 0;
@@ -89,28 +104,11 @@ function funcTimeW(maxHistoryElements = 800) {
89
104
  break;
90
105
  }
91
106
  }
92
- if (i > maxHistoryElements) {
93
- arr.splice(0, i - maxHistoryElements);
94
- cleanupEmptyKey(type);
107
+ if (i > 800) {
108
+ arr.splice(0, i - 800);
95
109
  }
96
110
  return result;
97
111
  },
98
- removeKey(type) {
99
- delete dStatic[type];
100
- }
101
112
  };
102
113
  }
103
114
  exports.FuncTimeWait = funcTimeW();
104
- function testFuncTimeW() {
105
- const tracker = funcTimeW();
106
- const type = "UID";
107
- const now = Date.now();
108
- tracker.add({ type, weight: 1, timeStamp: now });
109
- tracker.add({ type, weight: 2, timeStamp: now - 500 });
110
- tracker.add({ type, weight: 3, timeStamp: now + 500 });
111
- console.log("funcTimeW order", tracker.dStatic);
112
- tracker.cleanByTime(type, 200);
113
- console.log("funcTimeW cleanByTime", tracker.dStatic);
114
- const w = tracker.weight(type, 1000);
115
- console.log("funcTimeW weight 1s", w, tracker.dStatic);
116
- }
@@ -1,5 +1,4 @@
1
- import { TF } from "../Common/Time";
2
- import type { CBar } from "./Bars";
1
+ import { CBar, TF } from "wenay-common2";
3
2
  type RequestInfo = any;
4
3
  type RequestInit = any;
5
4
  type Response = any;
@@ -1,22 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LoadQuoteBase = LoadQuoteBase;
4
+ const wenay_common2_1 = require("wenay-common2");
4
5
  const funcTimeWait_1 = require("../Common/funcTimeWait");
5
- const common_1 = require("../Common/core/common");
6
6
  function LoadQuoteBase(setting, data) {
7
- const { base, maxLoadBars, intervalToName } = setting;
7
+ const { base, maxLoadBars, countConnect, intervalToName } = setting;
8
+ const maxLoadBars2 = setting.maxLoadBars2 ?? maxLoadBars;
8
9
  const startMap = new Map();
9
10
  const keyName = setting.nameKey ?? "loadKey";
10
11
  const time = setting.time ?? 60000;
11
12
  const other = data;
12
13
  const getDataEl = (a) => setting.controlTimeToNumber?.(a);
13
14
  async function waitLimit(weight = 1) {
14
- const oldestTs = funcTimeWait_1.FuncTimeWait.byWeight(keyName, setting.countConnect);
15
- const t1 = oldestTs > 0 ? oldestTs - Date.now() + time + 1 : 0;
15
+ const t1 = funcTimeWait_1.FuncTimeWait.byWeight(keyName, setting.countConnect) - (Date.now() - time) + 1;
16
16
  if (t1 > 0) {
17
- await (0, common_1.sleepAsync)(t1);
17
+ funcTimeWait_1.FuncTimeWait.add({ type: keyName, weight: weight, timeStamp: Date.now() + t1 });
18
+ await (0, wenay_common2_1.sleepAsync)(t1);
19
+ }
20
+ else {
21
+ funcTimeWait_1.FuncTimeWait.add({ type: keyName, weight: weight, timeStamp: Date.now() });
18
22
  }
19
- funcTimeWait_1.FuncTimeWait.add({ type: keyName, weight: weight, timeStamp: Date.now() });
20
23
  }
21
24
  const mapTimeToName = new Map(intervalToName.map((e) => [e.time.sec, e]));
22
25
  const _fetch = other?.fetch ?? fetch;
@@ -26,82 +29,136 @@ function LoadQuoteBase(setting, data) {
26
29
  throw "_fetch - не определен";
27
30
  if (!infoTF)
28
31
  throw "нет такого таймфрейма";
32
+ let lastTime;
29
33
  const nameForMap = info.exchangeName + info.symbol + infoTF.name;
30
34
  let leftTime = startMap.get(nameForMap);
31
35
  if (!leftTime) {
32
36
  await waitLimit();
33
- leftTime = await setting.funcFistTime({
34
- symbol: info.symbol, baseURL: base, interval: infoTF.name,
35
- fetch: _fetch, intervalTF: info.tf, waitLimit
36
- });
37
- startMap.set(nameForMap, leftTime);
37
+ try {
38
+ leftTime = await setting.funcFistTime({ symbol: info.symbol, baseURL: base, interval: infoTF.name, fetch: _fetch, intervalTF: info.tf, waitLimit });
39
+ startMap.set(nameForMap, leftTime);
40
+ }
41
+ catch (e) {
42
+ if (data?.error == true)
43
+ throw e;
44
+ else
45
+ return [];
46
+ }
38
47
  }
39
- const time1 = Math.max(info.time1.valueOf(), leftTime.valueOf());
40
- const time2 = info.time2.valueOf();
48
+ const [time1, time2] = [Math.max(info.time1.valueOf(), leftTime.valueOf()), info.time2.valueOf()];
41
49
  if (time2 <= time1) {
42
50
  return [];
43
51
  }
44
- const [tNewer, tOlder] = info.right ? [time2, time1] : [time2, time1];
52
+ const [t1, t2] = info.right ? [time1, time2] : [time2, time1];
45
53
  const arr = [];
46
54
  const interval = infoTF.time.valueOf();
55
+ const map = [];
47
56
  if (maxLoadBars instanceof Date) {
48
- const step = maxLoadBars.valueOf();
49
- arr.push(tNewer);
50
- let span = tNewer - tOlder;
51
- while (span > step) {
52
- arr.push(arr[arr.length - 1] - step);
53
- span -= step;
57
+ const [step1] = [
58
+ maxLoadBars.valueOf()
59
+ ];
60
+ arr.push(lastTime = t1);
61
+ let barsTime = (t1 - t2);
62
+ if (barsTime <= maxLoadBars.valueOf())
63
+ arr.push(t2);
64
+ else {
65
+ barsTime -= maxLoadBars.valueOf();
66
+ arr.push(lastTime = lastTime - step1);
67
+ for (; barsTime > 0; barsTime -= maxLoadBars.valueOf())
68
+ arr.push(lastTime = lastTime - step1);
69
+ if (barsTime < 0)
70
+ arr.push(t2);
54
71
  }
55
- if (arr[arr.length - 1] > tOlder)
56
- arr.push(tOlder);
57
72
  }
58
- else if (typeof maxLoadBars === "number") {
59
- const step = maxLoadBars * interval;
60
- arr.push(tNewer);
61
- let bars = (tNewer - tOlder) / interval;
62
- while (bars > maxLoadBars) {
63
- arr.push(arr[arr.length - 1] - step);
73
+ else if (typeof maxLoadBars == "number") {
74
+ const [step1] = [
75
+ maxLoadBars * interval,
76
+ ];
77
+ arr.push(lastTime = t1);
78
+ let bars = (t1 - t2) / interval;
79
+ if (bars <= maxLoadBars)
80
+ arr.push(t2);
81
+ else {
64
82
  bars -= maxLoadBars;
83
+ arr.push(lastTime = lastTime - step1);
84
+ for (; bars > 0; bars -= maxLoadBars)
85
+ arr.push(lastTime = lastTime - step1);
86
+ arr.push(t2);
65
87
  }
66
- if (arr[arr.length - 1] > tOlder)
67
- arr.push(tOlder);
68
88
  }
69
- const result = [];
70
- for (let i = 0; i < arr.length - 1; i++) {
71
- const endTime = arr[i];
72
- const startTime = arr[i + 1];
73
- if (startTime >= endTime)
89
+ for (let i = 1; i < arr.length; i++) {
90
+ if (arr[i].valueOf() >= arr[i - 1].valueOf())
74
91
  continue;
75
- await waitLimit();
76
- const reqData = {
77
- maxLoadBars,
78
- fetch: _fetch,
79
- baseURL: base,
80
- symbol: info.symbol,
81
- interval: infoTF.name,
82
- startTime: new Date(startTime),
83
- endTime: new Date(endTime),
84
- limit: maxLoadBars,
85
- intervalTF: info.tf,
86
- waitLimit
87
- };
88
- try {
89
- let res = await setting.funcLoad(reqData);
90
- if (setting.controlTimeToNumber && res.length > 0) {
91
- const t1 = getDataEl(res[0]);
92
- const t2 = getDataEl(res.at(-1));
93
- if (t1 != null && t2 != null && t1 > t2) {
92
+ const loader = async () => {
93
+ const data = {
94
+ maxLoadBars: maxLoadBars,
95
+ fetch: _fetch,
96
+ baseURL: base,
97
+ symbol: info.symbol,
98
+ interval: infoTF.name,
99
+ startTime: new Date(arr[i]),
100
+ endTime: new Date(arr[i - 1]),
101
+ limit: maxLoadBars,
102
+ intervalTF: info.tf,
103
+ waitLimit
104
+ };
105
+ await waitLimit();
106
+ let res = await setting.funcLoad(data);
107
+ if (setting.controlTimeToNumber && res.length) {
108
+ let [t1, t2] = [
109
+ getDataEl(res[0]),
110
+ getDataEl(res.at(-1))
111
+ ];
112
+ if (t1 && t2 && t1 > t2) {
94
113
  res = res.reverse();
95
114
  }
96
115
  }
97
- result.unshift(...res);
116
+ return res;
117
+ };
118
+ map.push(loader());
119
+ }
120
+ const resulI = await Promise.allSettled(map);
121
+ const result = [];
122
+ for (let i = resulI.length - 1; i >= 0; i--) {
123
+ const el = resulI[i];
124
+ if (el.status == "fulfilled") {
125
+ result.push(...el.value);
98
126
  }
99
- catch (e) {
100
- console.error(e);
101
- if (data?.error === true)
102
- throw e;
127
+ if (el.status == "rejected") {
128
+ console.error(el.reason);
129
+ if (data?.error == true)
130
+ throw el.reason;
103
131
  }
104
132
  }
105
133
  return result;
106
134
  };
107
135
  }
136
+ async function test() {
137
+ const arr = [];
138
+ for (let i = 0; i < 10000; i++) {
139
+ arr[i] = { time: Date.now() - i * wenay_common2_1.TF.H1.msec, price: i };
140
+ }
141
+ let ress = [];
142
+ const tt = LoadQuoteBase({
143
+ base: "",
144
+ countConnect: 2,
145
+ funcFistTime: async ({}) => {
146
+ const time = new Date(arr.at(-1).time);
147
+ console.log("funcFistTime: ", time);
148
+ return time;
149
+ },
150
+ nameKey: "cd",
151
+ maxLoadBars: 100,
152
+ time: 50,
153
+ intervalToName: [{ name: "1", time: wenay_common2_1.TF.H1 }],
154
+ funcLoad: (data) => {
155
+ ress.push(data.startTime);
156
+ ress.push(data.endTime);
157
+ return (async () => [{ time: 5 }])();
158
+ }
159
+ });
160
+ const res = await tt({ symbol: "s", time2: new Date(), tf: wenay_common2_1.TF.H1, time1: new Date(2015) });
161
+ ress.sort((a, b) => a.valueOf() - b.valueOf());
162
+ console.log(ress);
163
+ console.log(res);
164
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wenay-common2",
3
- "version": "1.0.42",
3
+ "version": "1.0.43",
4
4
  "description": "Common library",
5
5
  "strict": true,
6
6
  "main": "lib/index.js",