code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0105_games_stats_team_members_fix.sql (about) 1 -- +goose Up 2 3 drop materialized view if exists game_stats; 4 drop materialized view if exists game_stats_current; 5 6 -- These materialized views should only be updated once per epoch 7 create materialized view game_stats as 8 with 9 game_epochs as ( 10 select distinct 11 game_id, epoch_id 12 from rewards 13 where 14 game_id is not null 15 ), 16 dispatch_strategies AS ( 17 SELECT DISTINCT 18 ON (game_id) game_id, dispatch_strategy 19 FROM transfers 20 WHERE 21 transfer_type = 'Recurring' 22 ORDER BY game_id, vega_time DESC 23 ), 24 game_rewards as ( 25 select 26 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 27 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 28 from 29 rewards r 30 join game_epochs ge on r.game_id = ge.game_id 31 and r.epoch_id = ge.epoch_id 32 JOIN dispatch_strategies t ON r.game_id = t.game_id 33 AND t.dispatch_strategy ->> 'entity_scope' = '2' 34 join current_team_members tm on r.party_id = tm.party_id 35 left join game_team_rankings tr on r.game_id = tr.game_id 36 and r.epoch_id = tr.epoch_id 37 and tm.team_id = tr.team_id 38 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 39 and r.epoch_id = tmr.epoch_id 40 and tm.team_id = tmr.team_id 41 and r.party_id = tmr.party_id 42 union all 43 select 44 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 45 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 46 from 47 rewards r 48 join game_epochs ge on r.game_id = ge.game_id 49 and r.epoch_id = ge.epoch_id 50 JOIN dispatch_strategies t ON r.game_id = t.game_id 51 AND t.dispatch_strategy ->> 'entity_scope' = '1' 52 left join game_individual_rankings tmr on r.game_id = tmr.game_id 53 and r.epoch_id = tmr.epoch_id 54 and r.party_id = tmr.party_id 55 ) 56 select * 57 from game_rewards 58 with data; 59 60 create materialized view game_stats_current as 61 with 62 game_epochs as ( 63 select game_id, max(epoch_id) as epoch_id 64 from rewards 65 where 66 game_id is not null 67 group by 68 game_id 69 ), 70 dispatch_strategies AS ( 71 SELECT DISTINCT 72 ON (game_id) game_id, dispatch_strategy 73 FROM transfers 74 WHERE 75 transfer_type = 'Recurring' 76 ORDER BY game_id, vega_time DESC 77 ), 78 game_rewards as ( 79 select 80 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 81 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 82 from 83 rewards r 84 join game_epochs ge on r.game_id = ge.game_id 85 and r.epoch_id = ge.epoch_id 86 JOIN dispatch_strategies t ON r.game_id = t.game_id 87 AND t.dispatch_strategy ->> 'entity_scope' = '2' 88 join current_team_members tm on r.party_id = tm.party_id 89 left join game_team_rankings tr on r.game_id = tr.game_id 90 and r.epoch_id = tr.epoch_id 91 and tm.team_id = tr.team_id 92 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 93 and r.epoch_id = tmr.epoch_id 94 and tm.team_id = tmr.team_id 95 and r.party_id = tmr.party_id 96 union all 97 select 98 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 99 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 100 from 101 rewards r 102 join game_epochs ge on r.game_id = ge.game_id 103 and r.epoch_id = ge.epoch_id 104 JOIN dispatch_strategies t ON r.game_id = t.game_id 105 AND t.dispatch_strategy ->> 'entity_scope' = '1' 106 left join game_individual_rankings tmr on r.game_id = tmr.game_id 107 and r.epoch_id = tmr.epoch_id 108 and r.party_id = tmr.party_id 109 ) 110 select * 111 from game_rewards 112 with data; 113 114 -- +goose Down 115 116 drop materialized view if exists game_stats; 117 drop materialized view if exists game_stats_current; 118 119 -- These materialized views should only be updated once per epoch 120 create materialized view game_stats as 121 with 122 game_epochs as ( 123 select distinct 124 game_id, epoch_id 125 from rewards 126 where 127 game_id is not null 128 ), 129 dispatch_strategies AS ( 130 SELECT DISTINCT 131 ON (game_id) game_id, dispatch_strategy 132 FROM transfers 133 WHERE 134 transfer_type = 'Recurring' 135 ORDER BY game_id, vega_time DESC 136 ), 137 game_rewards as ( 138 select 139 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 140 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 141 from 142 rewards r 143 join game_epochs ge on r.game_id = ge.game_id 144 and r.epoch_id = ge.epoch_id 145 JOIN dispatch_strategies t ON r.game_id = t.game_id 146 AND t.dispatch_strategy ->> 'entity_scope' = '2' 147 join team_members tm on r.party_id = tm.party_id 148 left join game_team_rankings tr on r.game_id = tr.game_id 149 and r.epoch_id = tr.epoch_id 150 and tm.team_id = tr.team_id 151 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 152 and r.epoch_id = tmr.epoch_id 153 and tm.team_id = tmr.team_id 154 and r.party_id = tmr.party_id 155 union all 156 select 157 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 158 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 159 from 160 rewards r 161 join game_epochs ge on r.game_id = ge.game_id 162 and r.epoch_id = ge.epoch_id 163 JOIN dispatch_strategies t ON r.game_id = t.game_id 164 AND t.dispatch_strategy ->> 'entity_scope' = '1' 165 left join game_individual_rankings tmr on r.game_id = tmr.game_id 166 and r.epoch_id = tmr.epoch_id 167 and r.party_id = tmr.party_id 168 ) 169 select * 170 from game_rewards 171 with data; 172 173 create materialized view game_stats_current as 174 with 175 game_epochs as ( 176 select game_id, max(epoch_id) as epoch_id 177 from rewards 178 where 179 game_id is not null 180 group by 181 game_id 182 ), 183 dispatch_strategies AS ( 184 SELECT DISTINCT 185 ON (game_id) game_id, dispatch_strategy 186 FROM transfers 187 WHERE 188 transfer_type = 'Recurring' 189 ORDER BY game_id, vega_time DESC 190 ), 191 game_rewards as ( 192 select 193 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 194 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 195 from 196 rewards r 197 join game_epochs ge on r.game_id = ge.game_id 198 and r.epoch_id = ge.epoch_id 199 JOIN dispatch_strategies t ON r.game_id = t.game_id 200 AND t.dispatch_strategy ->> 'entity_scope' = '2' 201 join team_members tm on r.party_id = tm.party_id 202 left join game_team_rankings tr on r.game_id = tr.game_id 203 and r.epoch_id = tr.epoch_id 204 and tm.team_id = tr.team_id 205 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 206 and r.epoch_id = tmr.epoch_id 207 and tm.team_id = tmr.team_id 208 and r.party_id = tmr.party_id 209 union all 210 select 211 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 212 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 213 from 214 rewards r 215 join game_epochs ge on r.game_id = ge.game_id 216 and r.epoch_id = ge.epoch_id 217 JOIN dispatch_strategies t ON r.game_id = t.game_id 218 AND t.dispatch_strategy ->> 'entity_scope' = '1' 219 left join game_individual_rankings tmr on r.game_id = tmr.game_id 220 and r.epoch_id = tmr.epoch_id 221 and r.party_id = tmr.party_id 222 ) 223 select * 224 from game_rewards 225 with data;