@watsonserve/stock-base 0.0.13 → 0.0.15

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.
Files changed (3) hide show
  1. package/dao.js +29 -10
  2. package/package.json +1 -1
  3. package/types/dao.d.ts +12 -3
package/dao.js CHANGED
@@ -2,14 +2,18 @@ import { InfluxDB, Point } from '@influxdata/influxdb-client';
2
2
  import { request } from 'node:http';
3
3
  import { EnCurrency, EnMarket } from './stock.js';
4
4
  function getStartTime() {
5
- switch (new Date().getUTCDay()) {
5
+ const now = new Date();
6
+ let stamp = now.getTime() - 86400000;
7
+ switch (now.getUTCDay()) {
6
8
  case 0: // Sunday
7
- return '-2d';
9
+ stamp -= 1 * 86400000;
10
+ break;
8
11
  case 1:
9
- return '-3d';
12
+ stamp -= 2 * 86400000;
13
+ break;
10
14
  default:
11
15
  }
12
- return '-1d';
16
+ return new Date(stamp).toJSON().substring(0, 10);
13
17
  }
14
18
  export function toFxPoint(sgd, hkd, cny, timestamp) {
15
19
  return new Point(EnMarket.FX)
@@ -110,14 +114,14 @@ export class InfluxDAO {
110
114
  });
111
115
  }
112
116
  ;
113
- async readFxLatest() {
114
- const list = await this.__query([EnCurrency.SGD, EnCurrency.HKD, EnCurrency.CNY], getStartTime(), [EnMarket.FX], []);
117
+ async readFxLatest(start = '') {
118
+ const list = await this.__query([EnCurrency.SGD, EnCurrency.HKD, EnCurrency.CNY], start || getStartTime(), [EnMarket.FX], []);
115
119
  return list[0];
116
120
  }
117
- readStockLatest(ncs) {
118
- return this.__query(['c', 'v'], getStartTime(), [EnMarket.SGX, EnMarket.USA, EnMarket.HKEX], ncs);
121
+ readStockLatest(ncs, start = '') {
122
+ return this.__query(['c', 'v'], start || getStartTime(), [EnMarket.SGX, EnMarket.USA, EnMarket.HKEX], ncs);
119
123
  }
120
- async readDividend(ncs) {
124
+ async _readDividend(ncs) {
121
125
  const { origin: url, token, org, bucket } = this;
122
126
  const client = new InfluxDB({ url, token });
123
127
  const queryClient = client.getQueryApi(org);
@@ -136,8 +140,23 @@ export class InfluxDAO {
136
140
  list.unshift({ time: new Date(_time), nc, currency, [_field]: _value });
137
141
  },
138
142
  error: reject,
139
- complete: () => resolve(Object.entries(groupBy('nc', list, true))),
143
+ complete: () => resolve(groupBy('nc', list, true)),
140
144
  });
141
145
  });
142
146
  }
147
+ static ignore = new Set(['BRK.B']);
148
+ async readDividend(ncs) {
149
+ const divs = await this._readDividend(ncs);
150
+ // const none = ncs.filter(nc => !divs[nc]);
151
+ for (const nc of ncs) {
152
+ if (!divs[nc] && InfluxDAO.ignore.has(nc)) {
153
+ divs[nc] = [];
154
+ // none.push(nc);
155
+ }
156
+ }
157
+ return divs;
158
+ }
159
+ async writeNoRecord(nc) {
160
+ InfluxDAO.ignore.add(nc);
161
+ }
143
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watsonserve/stock-base",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
package/types/dao.d.ts CHANGED
@@ -18,6 +18,12 @@ interface IStData {
18
18
  c: number;
19
19
  v: number;
20
20
  }
21
+ interface IDiv {
22
+ time: Date;
23
+ nc: string;
24
+ currency: EnCurrency;
25
+ amount: number;
26
+ }
21
27
  export declare function toFxPoint(sgd: number, hkd: number, cny: number, timestamp: number): Point;
22
28
  export declare function toStockPoint(market: EnMarket, st: IStock, timestamp: number): Point;
23
29
  export declare function toDivPoints(market: string, nc: string, data: IDividend): Point[];
@@ -30,8 +36,11 @@ export declare class InfluxDAO {
30
36
  rm(measurement: string, start: string, stop: string): Promise<unknown>;
31
37
  writeToInfluxDB(points: Point[]): Promise<void>;
32
38
  private __query;
33
- readFxLatest(): Promise<IFxData>;
34
- readStockLatest(ncs: string[]): Promise<IStData[]>;
35
- readDividend(ncs: string[]): Promise<[string, any[]][]>;
39
+ readFxLatest(start?: string): Promise<IFxData>;
40
+ readStockLatest(ncs: string[], start?: string): Promise<IStData[]>;
41
+ _readDividend(ncs: string[]): Promise<Record<string, IDiv[]>>;
42
+ private static ignore;
43
+ readDividend(ncs: string[]): Promise<Record<string, IDiv[]>>;
44
+ writeNoRecord(nc: string): Promise<void>;
36
45
  }
37
46
  export {};