|
7 | 7 | param_epoch_duration = ${EPOCH_DURATION_IN_SECONDS?missing}; |
8 | 8 | -- +goose StatementEnd |
9 | 9 | -- +goose StatementBegin |
| 10 | +-- generate table to be used to updated balance_start |
| 11 | +CREATE TABLE IF NOT EXISTS _tmp_fix_epoch_balance_start |
| 12 | +( |
| 13 | + epoch Int64, |
| 14 | + t DateTime, |
| 15 | + validator_index UInt64, |
| 16 | + balance_start Int64, |
| 17 | +) ENGINE = ReplacingMergeTree() |
| 18 | +ORDER BY (validator_index, t) |
| 19 | +SETTINGS index_granularity=128, non_replicated_deduplication_window = 2048, replicated_deduplication_window = 2048; |
| 20 | +-- +goose StatementEnd |
| 21 | +-- +goose StatementBegin |
| 22 | +CREATE DICTIONARY IF NOT EXISTS _dict_fix_epoch_balance_start (validator_index Int64, t DateTime, epoch Int64, balance_start Int64) |
| 23 | +PRIMARY KEY t, validator_index SOURCE(CLICKHOUSE(TABLE _tmp_fix_epoch_balance_start DB currentDatabase())) |
| 24 | +LAYOUT(complex_key_direct()); |
| 25 | +-- +goose StatementEnd |
| 26 | +-- +goose StatementBegin |
| 27 | +INSERT INTO _tmp_fix_epoch_balance_start |
| 28 | +SELECT |
| 29 | + epoch, |
| 30 | + toStartOfHour({fork_epoch_ts:DateTime}) as t, |
| 31 | + validator_index, |
| 32 | + balance_start |
| 33 | +FROM _final_validator_dashboard_data_epoch |
| 34 | +WHERE epoch = GREATEST( |
| 35 | + 0, |
| 36 | + CEIL( |
| 37 | + ( |
| 38 | + toStartOfHour({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
| 39 | + ) / {epoch_duration:Int64} |
| 40 | + ) |
| 41 | + ) |
| 42 | +UNION ALL |
| 43 | +SELECT |
| 44 | + epoch, |
| 45 | + toStartOfDay({fork_epoch_ts:DateTime}) as t, |
| 46 | + validator_index, |
| 47 | + balance_start |
| 48 | +FROM _final_validator_dashboard_data_epoch |
| 49 | +WHERE epoch = GREATEST( |
| 50 | + 0, |
| 51 | + CEIL( |
| 52 | + ( |
| 53 | + toStartOfDay({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
| 54 | + ) / {epoch_duration:Int64} |
| 55 | + ) |
| 56 | + ) |
| 57 | +UNION ALL |
| 58 | +SELECT |
| 59 | + epoch, |
| 60 | + toMonday({fork_epoch_ts:DateTime}) as t, |
| 61 | + validator_index, |
| 62 | + balance_start |
| 63 | +FROM _final_validator_dashboard_data_epoch |
| 64 | +WHERE epoch = GREATEST( |
| 65 | + 0, |
| 66 | + CEIL( |
| 67 | + ( |
| 68 | + toMonday({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
| 69 | + ) / {epoch_duration:Int64} |
| 70 | + ) |
| 71 | + ) |
| 72 | +UNION ALL |
| 73 | +SELECT |
| 74 | + epoch, |
| 75 | + toStartOfMonth({fork_epoch_ts:DateTime}) as t, |
| 76 | + validator_index, |
| 77 | + balance_start |
| 78 | +FROM _final_validator_dashboard_data_epoch |
| 79 | +WHERE epoch = GREATEST( |
| 80 | + 0, |
| 81 | + CEIL( |
| 82 | + ( |
| 83 | + toStartOfMonth({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
| 84 | + ) / {epoch_duration:Int64} |
| 85 | + ) |
| 86 | + ) |
| 87 | +-- +goose StatementEnd |
| 88 | +-- +goose StatementBegin |
| 89 | +SYSTEM SYNC REPLICA _tmp_fix_epoch_balance_start LIGHTWEIGHT; |
| 90 | +-- +goose StatementEnd |
| 91 | +-- +goose StatementBegin |
10 | 92 | ALTER TABLE _final_validator_dashboard_data_hourly |
11 | 93 | UPDATE |
12 | | - epoch_start = GREATEST( |
13 | | - 0, |
14 | | - CEIL( |
15 | | - ( |
16 | | - toStartOfHour({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
17 | | - ) / {epoch_duration:Int64} |
18 | | - ) |
19 | | - ) |
| 94 | + epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)), |
| 95 | + balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index))) |
20 | 96 | WHERE t = toStartOfHour({fork_epoch_ts:DateTime}) |
21 | 97 | -- +goose StatementEnd |
22 | 98 | -- +goose StatementBegin |
23 | 99 | ALTER TABLE _final_validator_dashboard_data_daily |
24 | 100 | UPDATE |
25 | | - epoch_start = GREATEST( |
26 | | - 0, |
27 | | - CEIL( |
28 | | - ( |
29 | | - toStartOfDay({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
30 | | - ) / {epoch_duration:Int64} |
31 | | - ) |
32 | | - ) |
| 101 | + epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)), |
| 102 | + balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index))) |
33 | 103 | WHERE t = toStartOfDay({fork_epoch_ts:DateTime}) |
34 | 104 | -- +goose StatementEnd |
35 | 105 | -- +goose StatementBegin |
36 | 106 | ALTER TABLE _final_validator_dashboard_data_weekly |
37 | 107 | UPDATE |
38 | | - epoch_start = GREATEST( |
39 | | - 0, |
40 | | - CEIL( |
41 | | - ( |
42 | | - toMonday({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
43 | | - ) / {epoch_duration:Int64} |
44 | | - ) |
45 | | - ) |
| 108 | + epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)), |
| 109 | + balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index))) |
46 | 110 | WHERE t = toMonday({fork_epoch_ts:DateTime}) |
47 | 111 | -- +goose StatementEnd |
48 | 112 | -- +goose StatementBegin |
49 | 113 | ALTER TABLE _final_validator_dashboard_data_monthly |
50 | 114 | UPDATE |
51 | | - epoch_start = GREATEST( |
52 | | - 0, |
53 | | - CEIL( |
54 | | - ( |
55 | | - toStartOfMonth({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime} |
56 | | - ) / {epoch_duration:Int64} |
57 | | - ) |
58 | | - ) |
| 115 | + epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)), |
| 116 | + balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index))) |
59 | 117 | WHERE t = toStartOfMonth({fork_epoch_ts:DateTime}) |
60 | 118 | -- +goose StatementEnd |
61 | 119 | -- +goose ENVSUB OFF |
|
0 commit comments