code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0090_games_stats_add_quantum.sql (about) 1 -- +goose Up 2 alter table game_reward_totals 3 add column if not exists total_rewards_quantum hugeint not null default 0; 4 5 -- Postgres is throwing hissy fits when we try to replace these views so we are forced to drop them 6 -- and then recreate them the way we want them 7 8 -- as game_stats and game_stats_current has a dependency on game_team_rankings, we have to drop them 9 -- first or we won't be able to drop game_team_rankings 10 drop view if exists game_stats; 11 drop view if exists game_stats_current; 12 drop view if exists game_team_rankings; 13 14 -- we need to drop these two as they have a dependency on game_team_rankings 15 create view game_team_rankings as 16 with team_games as ( 17 -- get the games where the entity scope is individuals 18 select distinct game_id from transfers 19 where dispatch_strategy ->> 'entity_scope' = '2' 20 and game_id is not null 21 ), team_totals as ( 22 select t.game_id, 23 t.asset_id, 24 t.epoch_id, 25 t.team_id, 26 sum(t.total_rewards) as total_rewards, 27 sum(t.total_rewards_quantum) as total_rewards_quantum 28 from game_reward_totals t 29 join team_games g on t.game_id = g.game_id 30 where t.team_id != '\x' 31 group by t.game_id, t.asset_id, t.epoch_id, t.team_id 32 ) 33 select game_id, 34 epoch_id, 35 team_id, 36 total_rewards, 37 total_rewards_quantum, 38 rank() over ( 39 partition by game_id, epoch_id order by total_rewards_quantum desc 40 ) as rank 41 from team_totals; 42 43 drop view if exists game_team_member_rankings; 44 create view game_team_member_rankings as 45 with team_games as ( 46 -- get the games where the entity scope is individuals 47 select distinct game_id from transfers 48 where dispatch_strategy ->> 'entity_scope' = '2' 49 and game_id is not null 50 ), team_totals as ( 51 select t.game_id, t.asset_id, t.team_id, t.party_id, t.epoch_id, sum(t.total_rewards) as total_rewards, sum(t.total_rewards_quantum) as total_rewards_quantum 52 from game_reward_totals t 53 join team_games g on t.game_id = g.game_id 54 where t.team_id != '\x' 55 group by t.game_id, t.asset_id, t.team_id, t.party_id, t.epoch_id 56 ) 57 select game_id, 58 epoch_id, 59 team_id, 60 party_id, 61 total_rewards, 62 total_rewards_quantum, 63 rank() over ( 64 partition by game_id, epoch_id, team_id order by total_rewards_quantum desc 65 ) as rank 66 from team_totals; 67 68 drop view if exists game_individual_rankings; 69 create view game_individual_rankings as 70 with individual_games as ( 71 -- get the games where the entity scope is individuals 72 select game_id from transfers 73 where dispatch_strategy ->> 'entity_scope' = '1' 74 and game_id is not null 75 ), individual_totals as ( 76 -- calculate the total rewards for each individual in each individual entity scoped game 77 select t.game_id, t.epoch_id, t.asset_id, t.party_id, sum(t.total_rewards) as total_rewards, sum(total_rewards_quantum) as total_rewards_quantum 78 from game_reward_totals t 79 join individual_games i on t.game_id = i.game_id 80 group by t.game_id, t.epoch_id, t.asset_id, t.party_id 81 ), individual_rankings as ( 82 -- rank the individuals for each game at each epoch 83 select game_id, 84 epoch_id, 85 party_id, 86 total_rewards_quantum, 87 rank() over ( 88 partition by game_id, epoch_id order by total_rewards_quantum desc 89 ) as rank 90 from individual_totals 91 ) 92 select it.game_id, 93 it.epoch_id, 94 it.party_id, 95 it.total_rewards, 96 ir.total_rewards_quantum, 97 ir.rank 98 from individual_totals it 99 join individual_rankings ir on it.game_id = ir.game_id and it.epoch_id = ir.epoch_id and it.party_id = ir.party_id; 100 101 create view game_stats as 102 with 103 game_epochs as ( 104 select distinct 105 game_id, epoch_id 106 from rewards 107 where 108 game_id is not null 109 ), 110 dispatch_strategies AS ( 111 SELECT DISTINCT 112 ON (game_id) game_id, dispatch_strategy 113 FROM transfers 114 WHERE 115 transfer_type = 'Recurring' 116 ORDER BY game_id, vega_time DESC 117 ), 118 game_rewards as ( 119 select 120 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 121 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 122 from 123 rewards r 124 join game_epochs ge on r.game_id = ge.game_id 125 and r.epoch_id = ge.epoch_id 126 JOIN dispatch_strategies t ON r.game_id = t.game_id 127 AND t.dispatch_strategy ->> 'entity_scope' = '2' 128 join team_members tm on r.party_id = tm.party_id 129 left join game_team_rankings tr on r.game_id = tr.game_id 130 and r.epoch_id = tr.epoch_id 131 and tm.team_id = tr.team_id 132 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 133 and r.epoch_id = tmr.epoch_id 134 and tm.team_id = tmr.team_id 135 and r.party_id = tmr.party_id 136 union all 137 select 138 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 139 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 140 from 141 rewards r 142 join game_epochs ge on r.game_id = ge.game_id 143 and r.epoch_id = ge.epoch_id 144 JOIN dispatch_strategies t ON r.game_id = t.game_id 145 AND t.dispatch_strategy ->> 'entity_scope' = '1' 146 left join game_individual_rankings tmr on r.game_id = tmr.game_id 147 and r.epoch_id = tmr.epoch_id 148 and r.party_id = tmr.party_id 149 ) 150 select * 151 from game_rewards; 152 153 create view game_stats_current as 154 with 155 game_epochs as ( 156 select game_id, max(epoch_id) as epoch_id 157 from rewards 158 where 159 game_id is not null 160 group by 161 game_id 162 ), 163 dispatch_strategies AS ( 164 SELECT DISTINCT 165 ON (game_id) game_id, dispatch_strategy 166 FROM transfers 167 WHERE 168 transfer_type = 'Recurring' 169 ORDER BY game_id, vega_time DESC 170 ), 171 game_rewards as ( 172 select 173 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 174 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 175 from 176 rewards r 177 join game_epochs ge on r.game_id = ge.game_id 178 and r.epoch_id = ge.epoch_id 179 JOIN dispatch_strategies t ON r.game_id = t.game_id 180 AND t.dispatch_strategy ->> 'entity_scope' = '2' 181 join team_members tm on r.party_id = tm.party_id 182 left join game_team_rankings tr on r.game_id = tr.game_id 183 and r.epoch_id = tr.epoch_id 184 and tm.team_id = tr.team_id 185 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 186 and r.epoch_id = tmr.epoch_id 187 and tm.team_id = tmr.team_id 188 and r.party_id = tmr.party_id 189 union all 190 select 191 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 192 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 193 from 194 rewards r 195 join game_epochs ge on r.game_id = ge.game_id 196 and r.epoch_id = ge.epoch_id 197 JOIN dispatch_strategies t ON r.game_id = t.game_id 198 AND t.dispatch_strategy ->> 'entity_scope' = '1' 199 left join game_individual_rankings tmr on r.game_id = tmr.game_id 200 and r.epoch_id = tmr.epoch_id 201 and r.party_id = tmr.party_id 202 ) 203 select * 204 from game_rewards; 205 206 create or replace view current_game_reward_totals as ( 207 with current_game_epochs as ( 208 select game_id, max(epoch_id) as epoch_id 209 from game_reward_totals 210 group by game_id 211 ) 212 select grt.* 213 from game_reward_totals grt 214 join current_game_epochs cge on grt.game_id = cge.game_id and grt.epoch_id = cge.epoch_id 215 ); 216 217 -- +goose Down 218 219 drop view if exists game_stats; 220 drop view if exists game_stats_current; 221 drop view if exists game_team_rankings; 222 223 create view game_team_rankings as 224 with team_games as ( 225 -- get the games where the entity scope is individuals 226 select distinct game_id from transfers 227 where dispatch_strategy ->> 'entity_scope' = '2' 228 and game_id is not null 229 ), team_totals as ( 230 select t.game_id, t.asset_id, t.epoch_id, t.team_id, sum(t.total_rewards) as total_rewards 231 from game_reward_totals t 232 join team_games g on t.game_id = g.game_id 233 where t.team_id != '\x' 234 group by t.game_id, t.asset_id, t.epoch_id, t.team_id 235 ) 236 select game_id, epoch_id, team_id, total_rewards, rank() over (partition by game_id, epoch_id order by total_rewards desc) as rank 237 from team_totals; 238 239 drop view if exists game_team_member_rankings; 240 create view game_team_member_rankings as 241 with team_games as ( 242 -- get the games where the entity scope is individuals 243 select distinct game_id from transfers 244 where dispatch_strategy ->> 'entity_scope' = '2' 245 and game_id is not null 246 ), team_totals as ( 247 select t.game_id, t.asset_id, t.team_id, t.party_id, t.epoch_id, sum(t.total_rewards) as total_rewards 248 from game_reward_totals t 249 join team_games g on t.game_id = g.game_id 250 where t.team_id != '\x' 251 group by t.game_id, t.asset_id, t.team_id, t.party_id, t.epoch_id 252 ) 253 select game_id, epoch_id, team_id, party_id, total_rewards, rank() over (partition by game_id, epoch_id, team_id order by total_rewards desc) as rank 254 from team_totals; 255 256 drop view if exists game_individual_rankings; 257 create view game_individual_rankings as 258 with individual_games as ( 259 -- get the games where the entity scope is individuals 260 select game_id from transfers 261 where dispatch_strategy ->> 'entity_scope' = '1' 262 and game_id is not null 263 ), individual_totals as ( 264 -- calculate the total rewards for each individual in each individual entity scoped game 265 select t.game_id, t.epoch_id, t.asset_id, t.party_id, sum(t.total_rewards) as total_rewards 266 from game_reward_totals t 267 join individual_games i on t.game_id = i.game_id 268 group by t.game_id, t.epoch_id, t.asset_id, t.party_id 269 ) 270 -- rank the individuals for each game at each epoch 271 select game_id, epoch_id, party_id, total_rewards, rank() over (partition by game_id, epoch_id order by total_rewards desc) as rank 272 from individual_totals; 273 274 create view game_stats as 275 with 276 game_epochs as ( 277 select distinct 278 game_id, epoch_id 279 from rewards 280 where 281 game_id is not null 282 ), 283 dispatch_strategies AS ( 284 SELECT DISTINCT 285 ON (game_id) game_id, dispatch_strategy 286 FROM transfers 287 WHERE 288 transfer_type = 'Recurring' 289 ORDER BY game_id, vega_time DESC 290 ), 291 game_rewards as ( 292 select 293 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tr.total_rewards as team_total_rewards, 'ENTITY_SCOPE_TEAMS' as entity_scope 294 from 295 rewards r 296 join game_epochs ge on r.game_id = ge.game_id 297 and r.epoch_id = ge.epoch_id 298 JOIN dispatch_strategies t ON r.game_id = t.game_id 299 AND t.dispatch_strategy ->> 'entity_scope' = '2' 300 join team_members tm on r.party_id = tm.party_id 301 left join game_team_rankings tr on r.game_id = tr.game_id 302 and r.epoch_id = tr.epoch_id 303 and tm.team_id = tr.team_id 304 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 305 and r.epoch_id = tmr.epoch_id 306 and tm.team_id = tmr.team_id 307 and r.party_id = tmr.party_id 308 union all 309 select 310 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, null as team_total_rewards, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 311 from 312 rewards r 313 join game_epochs ge on r.game_id = ge.game_id 314 and r.epoch_id = ge.epoch_id 315 JOIN dispatch_strategies t ON r.game_id = t.game_id 316 AND t.dispatch_strategy ->> 'entity_scope' = '1' 317 left join game_individual_rankings tmr on r.game_id = tmr.game_id 318 and r.epoch_id = tmr.epoch_id 319 and r.party_id = tmr.party_id 320 ) 321 select * 322 from game_rewards; 323 324 create view game_stats_current as 325 with 326 game_epochs as ( 327 select game_id, max(epoch_id) as epoch_id 328 from rewards 329 where 330 game_id is not null 331 group by 332 game_id 333 ), 334 dispatch_strategies AS ( 335 SELECT DISTINCT 336 ON (game_id) game_id, dispatch_strategy 337 FROM transfers 338 WHERE 339 transfer_type = 'Recurring' 340 ORDER BY game_id, vega_time DESC 341 ), 342 game_rewards as ( 343 select 344 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, 'ENTITY_SCOPE_TEAMS' as entity_scope 345 from 346 rewards r 347 join game_epochs ge on r.game_id = ge.game_id 348 and r.epoch_id = ge.epoch_id 349 JOIN dispatch_strategies t ON r.game_id = t.game_id 350 AND t.dispatch_strategy ->> 'entity_scope' = '2' 351 join team_members tm on r.party_id = tm.party_id 352 left join game_team_rankings tr on r.game_id = tr.game_id 353 and r.epoch_id = tr.epoch_id 354 and tm.team_id = tr.team_id 355 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 356 and r.epoch_id = tmr.epoch_id 357 and tm.team_id = tmr.team_id 358 and r.party_id = tmr.party_id 359 union all 360 select 361 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 362 from 363 rewards r 364 join game_epochs ge on r.game_id = ge.game_id 365 and r.epoch_id = ge.epoch_id 366 JOIN dispatch_strategies t ON r.game_id = t.game_id 367 AND t.dispatch_strategy ->> 'entity_scope' = '1' 368 left join game_individual_rankings tmr on r.game_id = tmr.game_id 369 and r.epoch_id = tmr.epoch_id 370 and r.party_id = tmr.party_id 371 ) 372 select * 373 from game_rewards; 374 375 create or replace view current_game_reward_totals as ( 376 with current_game_epochs as ( 377 select game_id, max(epoch_id) as epoch_id 378 from game_reward_totals 379 group by game_id 380 ) 381 select grt.* 382 from game_reward_totals grt 383 join current_game_epochs cge on grt.game_id = cge.game_id and grt.epoch_id = cge.epoch_id 384 );