@watsonserve/stock-base 0.0.14 → 0.0.16

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 +15 -11
  2. package/package.json +1 -1
  3. package/types/dao.d.ts +3 -2
package/dao.js CHANGED
@@ -1,15 +1,19 @@
1
1
  import { InfluxDB, Point } from '@influxdata/influxdb-client';
2
2
  import { request } from 'node:http';
3
3
  import { EnCurrency, EnMarket } from './stock.js';
4
- function getStartTime() {
5
- switch (new Date().getUTCDay()) {
4
+ export function latestDay() {
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)
@@ -84,7 +88,7 @@ export class InfluxDAO {
84
88
  await writeClient.flush();
85
89
  await writeClient.close();
86
90
  }
87
- async __query(fileds, start, markets, ncs) {
91
+ async __query(fileds, duration, markets, ncs) {
88
92
  const { origin: url, token, org, bucket } = this;
89
93
  const client = new InfluxDB({ url, token });
90
94
  const queryClient = client.getQueryApi(org);
@@ -93,7 +97,7 @@ export class InfluxDAO {
93
97
  const fdQuery = fileds.map(fd => `r["_field"] == "${fd}"`).join(' or ');
94
98
  const filters = [mkQuery, ncQuery, fdQuery].filter(Boolean).map(str => `|> filter(fn: (r) => ${str})`).join('\n');
95
99
  const fluxQuery = `from(bucket: "${bucket}")
96
- |> range(start: ${start})
100
+ |> range(start: ${duration[0]}, end: ${duration[1]})
97
101
  ${filters}
98
102
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`;
99
103
  return new Promise((resolve, reject) => {
@@ -110,12 +114,12 @@ 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 readFx(duration) {
118
+ const list = await this.__query([EnCurrency.SGD, EnCurrency.HKD, EnCurrency.CNY], duration, [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
+ readStock(ncs, duration) {
122
+ return this.__query(['c', 'v'], duration, [EnMarket.SGX, EnMarket.USA, EnMarket.HKEX], ncs);
119
123
  }
120
124
  async _readDividend(ncs) {
121
125
  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.14",
3
+ "version": "0.0.16",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
package/types/dao.d.ts CHANGED
@@ -24,6 +24,7 @@ interface IDiv {
24
24
  currency: EnCurrency;
25
25
  amount: number;
26
26
  }
27
+ export declare function latestDay(): string;
27
28
  export declare function toFxPoint(sgd: number, hkd: number, cny: number, timestamp: number): Point;
28
29
  export declare function toStockPoint(market: EnMarket, st: IStock, timestamp: number): Point;
29
30
  export declare function toDivPoints(market: string, nc: string, data: IDividend): Point[];
@@ -36,8 +37,8 @@ export declare class InfluxDAO {
36
37
  rm(measurement: string, start: string, stop: string): Promise<unknown>;
37
38
  writeToInfluxDB(points: Point[]): Promise<void>;
38
39
  private __query;
39
- readFxLatest(): Promise<IFxData>;
40
- readStockLatest(ncs: string[]): Promise<IStData[]>;
40
+ readFx(duration: string[]): Promise<IFxData>;
41
+ readStock(ncs: string[], duration: string[]): Promise<IStData[]>;
41
42
  _readDividend(ncs: string[]): Promise<Record<string, IDiv[]>>;
42
43
  private static ignore;
43
44
  readDividend(ncs: string[]): Promise<Record<string, IDiv[]>>;