This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DDL.sql
278 lines (242 loc) · 4.59 KB
/
DDL.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
--
-- Product
--
CREATE TABLE IF NOT EXISTS t_product
(
pr_site VARCHAR(16) NOT NULL,
pr_code VARCHAR(32) NOT NULL,
pr_inst VARCHAR(32) NOT NULL,
pr_fund VARCHAR(32) NOT NULL,
pr_disp VARCHAR(16) NOT NULL,
pr_expr TIMESTAMP
);
DROP INDEX IF EXISTS t_product_0;
ALTER TABLE t_product
ADD CONSTRAINT t_product_0
PRIMARY KEY
(
pr_site,
pr_code
);
--
-- Evaluation
--
CREATE TABLE IF NOT EXISTS t_evaluation
(
ev_site VARCHAR(16) NOT NULL,
ev_unit VARCHAR(32) NOT NULL,
ev_ticker_site VARCHAR(16),
ev_ticker_code VARCHAR(32),
ev_convert_site VARCHAR(16),
ev_convert_code VARCHAR(32)
);
DROP INDEX IF EXISTS t_evaluation_0;
ALTER TABLE t_evaluation
ADD CONSTRAINT t_evaluation_0
PRIMARY KEY
(
ev_site,
ev_unit
);
--
-- Account
--
CREATE TABLE IF NOT EXISTS t_account
(
ac_site VARCHAR(16) NOT NULL,
ac_acct VARCHAR(16) NOT NULL,
ac_unit VARCHAR(16) NOT NULL,
ac_disp VARCHAR(16) NOT NULL
);
DROP INDEX IF EXISTS i_account_0;
ALTER TABLE t_account
ADD CONSTRAINT i_account_0
PRIMARY KEY
(
ac_site,
ac_acct,
ac_unit
);
--
-- Ticker
--
CREATE TABLE IF NOT EXISTS t_ticker
(
tk_site VARCHAR(16) NOT NULL,
tk_code VARCHAR(32) NOT NULL,
tk_time TIMESTAMP NOT NULL,
tk_ask DECIMAL(32, 16),
tk_bid DECIMAL(32, 16),
tk_ltp DECIMAL(32, 16)
);
DROP INDEX IF EXISTS i_ticker_0;
ALTER TABLE t_ticker
ADD CONSTRAINT i_ticker_0
PRIMARY KEY
(
tk_site,
tk_code,
tk_time
);
DROP INDEX IF EXISTS i_ticker_1;
CREATE INDEX i_ticker_1
ON t_ticker
(
tk_time,
tk_site,
tk_code
);
--
-- Balance
--
CREATE TABLE IF NOT EXISTS t_balance
(
bc_site VARCHAR(16) NOT NULL,
bc_acct VARCHAR(16) NOT NULL,
bc_unit VARCHAR(16) NOT NULL,
bc_time TIMESTAMP NOT NULL,
bc_amnt DECIMAL(32, 16)
);
DROP INDEX IF EXISTS i_balance_0;
ALTER TABLE t_balance
ADD CONSTRAINT i_balance_0
PRIMARY KEY
(
bc_site,
bc_acct,
bc_unit,
bc_time
);
DROP INDEX IF EXISTS i_balance_1;
CREATE INDEX i_balance_1
ON t_balance
(
bc_time,
bc_site,
bc_acct,
bc_unit
);
--
-- Position
--
CREATE TABLE IF NOT EXISTS t_position
(
ps_site VARCHAR(16) NOT NULL,
ps_code VARCHAR(32) NOT NULL,
ps_time TIMESTAMP NOT NULL,
ps_inst DECIMAL(32, 16),
ps_fund DECIMAL(32, 16)
);
DROP INDEX IF EXISTS i_position_0;
ALTER TABLE t_position
ADD CONSTRAINT i_position_0
PRIMARY KEY
(
ps_site,
ps_code,
ps_time
);
DROP INDEX IF EXISTS i_position_1;
CREATE INDEX i_position_1
ON t_position
(
ps_time,
ps_site,
ps_code
);
--
-- Transaction
--
CREATE TABLE IF NOT EXISTS t_transaction
(
tx_site VARCHAR(16) NOT NULL,
tx_code VARCHAR(32) NOT NULL,
tx_type VARCHAR(16) NOT NULL,
tx_acct VARCHAR(16) NOT NULL,
tx_oid VARCHAR(64) NOT NULL,
tx_eid VARCHAR(64) NOT NULL,
tx_time TIMESTAMP NOT NULL,
tx_inst DECIMAL(32, 16),
tx_fund DECIMAL(32, 16)
);
DROP INDEX IF EXISTS i_transaction_0;
ALTER TABLE t_transaction
ADD CONSTRAINT i_transaction_0
PRIMARY KEY
(
tx_site,
tx_code,
tx_type,
tx_acct,
tx_oid,
tx_eid
);
DROP INDEX IF EXISTS i_transaction_1;
CREATE INDEX i_transaction_1
ON t_transaction
(
tx_time,
tx_site,
tx_code,
tx_type,
tx_acct
);
--
-- Metric
--
-- [Grafana]
-- SELECT mc_time as time, mc_name as metric, sum(mc_amnt) FROM t_metric
-- WHERE mc_type = 'test' AND $__timeFilter(mc_time)
-- GROUP BY mc_time, mc_name
-- ORDER BY mc_time, mc_name
--
-- [Actual]
-- SELECT mc_time as time, mc_name as metric, sum(mc_amnt) as value FROM t_metric
-- WHERE mc_type = 'test' AND extract(epoch from mc_time)
-- BETWEEN extract(epoch from now() - INTERVAL '1 day') AND extract(epoch from now())
-- GROUP BY mc_time, mc_name
-- ORDER BY mc_time, mc_name
--
CREATE TABLE IF NOT EXISTS t_metric
(
mc_type VARCHAR(32) NOT NULL,
mc_time TIMESTAMP NOT NULL,
mc_name VARCHAR(32) NOT NULL,
mc_amnt DECIMAL(32, 16)
);
DROP INDEX IF EXISTS i_metric_0;
ALTER TABLE t_metric
ADD CONSTRAINT i_metric_0
PRIMARY KEY
(
mc_type,
mc_time,
mc_name
);
DROP INDEX IF EXISTS i_metric_1;
CREATE INDEX i_metric_1
ON t_metric
(
mc_type,
extract(EPOCH FROM mc_time),
mc_name
);
CREATE INDEX i_metric_2
ON t_metric
(
mc_time
);
--
-- Mock for Grafana's built-in function
--
CREATE OR REPLACE FUNCTION "$__timeFilter"(TIMESTAMP)
RETURNS BOOLEAN
AS $$
SELECT extract(EPOCH FROM $1) >= extract(EPOCH FROM t.now - INTERVAL '1 day')
AND
extract(EPOCH FROM $1) < extract(EPOCH FROM t.now)
FROM (SELECT now() AS now) AS t
$$
RETURNS NULL ON NULL
INPUT
LANGUAGE SQL;