mithril_aggregator/event_store/database/
migration.rs1use mithril_persistence::database::SqlMigration;
4
5pub fn get_migrations() -> Vec<SqlMigration> {
9 vec![
10 SqlMigration::new(
12 1,
13 r#"
14create table if not exists event (
15 event_id integer primary key asc autoincrement,
16 created_at text not null,
17 source text not null,
18 action text not null,
19 content text nul null
20);
21 "#,
22 ),
23 SqlMigration::new(
24 2,
25 r#"
26create view if not exists metrics_per_day as select metric_date as date, action as counter_name, sum(counter) value from
27 (
28 select action, json_extract(content, '$.content.value') counter, date(json_extract(content, '$.content.date')) metric_date
29 from event
30 where source='Metrics'
31 )
32group by action, date;
33create index metric_date_index on event(date(json_extract(content, '$.content.date')));
34 "#,
35 ),
36 SqlMigration::new(
37 3,
38 r#"
39create view signer_registration_summary as with
40 signer_version as (
41 select
42 json_extract(content, '$.content.party_id') as party_id,
43 json_extract(content, '$.headers.signer-node-version') as node_version,
44 json_extract(content, '$.headers.epoch') as epoch,
45 json_extract(content, '$.content.stake') as stakes
46 from event
47 where action='register_signer'
48 order by created_at desc
49 ),
50 stakes_version as (
51 select
52 party_id,
53 case
54 when instr(signer_version.node_version, '+') > 0
55 then substr(signer_version.node_version, 0, instr(signer_version.node_version, '+'))
56 else signer_version.node_version
57 end as version,
58 stakes,
59 cast(epoch as int) as epoch
60 from signer_version
61 group by party_id, epoch
62 ),
63 summed_stakes_version as (
64 select
65 epoch,
66 version,
67 party_id,
68 sum(stakes) over (partition by epoch) as total_epoch_stakes,
69 sum(stakes) over (partition by epoch, version) as stakes_version
70 from stakes_version
71 order by epoch desc
72 )
73select
74 epoch,
75 version,
76 total_epoch_stakes,
77 stakes_version,
78 printf('%02d %%', round((stakes_version * 100) / (total_epoch_stakes * 1.0))) as stakes_ratio,
79 count(party_id) as pool_count
80from summed_stakes_version
81group by epoch, version
82order by epoch desc, version desc;
83 "#,
84 ),
85 ]
86}