@watsonserve/stock-base 0.0.16 → 0.0.18

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/dao.js CHANGED
@@ -56,6 +56,7 @@ function groupBy(key, list, delKey = false) {
56
56
  return pre;
57
57
  }, {});
58
58
  }
59
+ const A_DAY_MS = 86400000;
59
60
  export class InfluxDAO {
60
61
  origin = '';
61
62
  org = '';
@@ -88,7 +89,7 @@ export class InfluxDAO {
88
89
  await writeClient.flush();
89
90
  await writeClient.close();
90
91
  }
91
- async __query(fileds, duration, markets, ncs) {
92
+ async __queryLatest(fileds, lt, markets, ncs) {
92
93
  const { origin: url, token, org, bucket } = this;
93
94
  const client = new InfluxDB({ url, token });
94
95
  const queryClient = client.getQueryApi(org);
@@ -96,9 +97,11 @@ export class InfluxDAO {
96
97
  const ncQuery = ncs.map(nc => `r["nc"] == "${nc}"`).join(' or ');
97
98
  const fdQuery = fileds.map(fd => `r["_field"] == "${fd}"`).join(' or ');
98
99
  const filters = [mkQuery, ncQuery, fdQuery].filter(Boolean).map(str => `|> filter(fn: (r) => ${str})`).join('\n');
100
+ const start = new Date(lt.getTime() - 30 * A_DAY_MS).toJSON().substring(0, 10);
99
101
  const fluxQuery = `from(bucket: "${bucket}")
100
- |> range(start: ${duration[0]}, end: ${duration[1]})
102
+ |> range(start: ${start}, stop: ${lt.toJSON()})
101
103
  ${filters}
104
+ |> last()
102
105
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`;
103
106
  return new Promise((resolve, reject) => {
104
107
  const list = [];
@@ -114,12 +117,12 @@ export class InfluxDAO {
114
117
  });
115
118
  }
116
119
  ;
117
- async readFx(duration) {
118
- const list = await this.__query([EnCurrency.SGD, EnCurrency.HKD, EnCurrency.CNY], duration, [EnMarket.FX], []);
120
+ async readLatestFx(lt) {
121
+ const list = await this.__queryLatest([EnCurrency.SGD, EnCurrency.HKD, EnCurrency.CNY], lt, [EnMarket.FX], []);
119
122
  return list[0];
120
123
  }
121
- readStock(ncs, duration) {
122
- return this.__query(['c', 'v'], duration, [EnMarket.SGX, EnMarket.USA, EnMarket.HKEX], ncs);
124
+ readLatestStock(ncs, lt) {
125
+ return this.__queryLatest(['c', 'v'], lt, [EnMarket.SGX, EnMarket.USA, EnMarket.HKEX], ncs);
123
126
  }
124
127
  async _readDividend(ncs) {
125
128
  const { origin: url, token, org, bucket } = this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watsonserve/stock-base",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
package/types/dao.d.ts CHANGED
@@ -36,9 +36,9 @@ export declare class InfluxDAO {
36
36
  constructor(dbConf: IDBConf);
37
37
  rm(measurement: string, start: string, stop: string): Promise<unknown>;
38
38
  writeToInfluxDB(points: Point[]): Promise<void>;
39
- private __query;
40
- readFx(duration: string[]): Promise<IFxData>;
41
- readStock(ncs: string[], duration: string[]): Promise<IStData[]>;
39
+ private __queryLatest;
40
+ readLatestFx(lt: Date): Promise<IFxData>;
41
+ readLatestStock(ncs: string[], lt: Date): Promise<IStData[]>;
42
42
  _readDividend(ncs: string[]): Promise<Record<string, IDiv[]>>;
43
43
  private static ignore;
44
44
  readDividend(ncs: string[]): Promise<Record<string, IDiv[]>>;