code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0092_games_stats_views_materialized.sql (about)

     1  -- +goose Up
     2  
     3  drop view if exists game_stats;
     4  drop 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 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 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  create view game_stats as
   120  with
   121      game_epochs as (
   122          select distinct
   123              game_id, epoch_id
   124          from rewards
   125          where
   126              game_id is not null
   127      ),
   128      dispatch_strategies AS (
   129          SELECT DISTINCT
   130          ON (game_id) game_id, dispatch_strategy
   131          FROM transfers
   132          WHERE
   133          transfer_type = 'Recurring'
   134          ORDER BY game_id, vega_time DESC
   135          ),
   136          game_rewards as (
   137          select
   138          r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum,
   139          tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' 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' = '2'
   146          join team_members tm on r.party_id = tm.party_id
   147          left join game_team_rankings tr on r.game_id = tr.game_id
   148          and r.epoch_id = tr.epoch_id
   149          and tm.team_id = tr.team_id
   150          left join game_team_member_rankings tmr on r.game_id = tmr.game_id
   151          and r.epoch_id = tmr.epoch_id
   152          and tm.team_id = tmr.team_id
   153          and r.party_id = tmr.party_id
   154          union all
   155          select
   156          r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum,
   157          null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope
   158          from
   159          rewards r
   160          join game_epochs ge on r.game_id = ge.game_id
   161          and r.epoch_id = ge.epoch_id
   162          JOIN dispatch_strategies t ON r.game_id = t.game_id
   163          AND t.dispatch_strategy ->> 'entity_scope' = '1'
   164          left join game_individual_rankings tmr on r.game_id = tmr.game_id
   165          and r.epoch_id = tmr.epoch_id
   166          and r.party_id = tmr.party_id
   167          )
   168  select *
   169  from game_rewards;
   170  
   171  create view game_stats_current as
   172  with
   173      game_epochs as (
   174          select game_id, max(epoch_id) as epoch_id
   175          from rewards
   176          where
   177              game_id is not null
   178          group by
   179              game_id
   180      ),
   181      dispatch_strategies AS (
   182          SELECT DISTINCT
   183          ON (game_id) game_id, dispatch_strategy
   184          FROM transfers
   185          WHERE
   186          transfer_type = 'Recurring'
   187          ORDER BY game_id, vega_time DESC
   188          ),
   189          game_rewards as (
   190          select
   191          r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum,
   192          tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' 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' = '2'
   199          join team_members tm on r.party_id = tm.party_id
   200          left join game_team_rankings tr on r.game_id = tr.game_id
   201          and r.epoch_id = tr.epoch_id
   202          and tm.team_id = tr.team_id
   203          left join game_team_member_rankings tmr on r.game_id = tmr.game_id
   204          and r.epoch_id = tmr.epoch_id
   205          and tm.team_id = tmr.team_id
   206          and r.party_id = tmr.party_id
   207          union all
   208          select
   209          r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum,
   210          null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope
   211          from
   212          rewards r
   213          join game_epochs ge on r.game_id = ge.game_id
   214          and r.epoch_id = ge.epoch_id
   215          JOIN dispatch_strategies t ON r.game_id = t.game_id
   216          AND t.dispatch_strategy ->> 'entity_scope' = '1'
   217          left join game_individual_rankings tmr on r.game_id = tmr.game_id
   218          and r.epoch_id = tmr.epoch_id
   219          and r.party_id = tmr.party_id
   220          )
   221  select *
   222  from game_rewards;