From 82762e75eb57439a6057840bb4ccc263a375ae9c Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 20 Jun 2023 16:51:25 -0500 Subject: [PATCH] Stats add new stats visibility column to user groups (#4210) * add stats_visibility enum and backfill rake task for user_groups * remove detected trialing whitespace for usergroups backfill rake task * Update 20230613165746_add_stats_visibility_to_user_groups.rb * adding comments on stats visibility levels * update wording of comment on stats_visbility * update user group enum to take care of the case where we want to show aggregate stats publicly but only members can view individual stats (vs the case where only admins can view individual stats) * update user_group.rb stats_visibilty docs without trailing whitespace * update comments on stats visibilty levels --- app/models/user_group.rb | 20 +++++++++++++++++++ ...746_add_stats_visibility_to_user_groups.rb | 9 +++++++++ db/structure.sql | 13 ++++-------- lib/tasks/user_groups.rake | 11 ++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20230613165746_add_stats_visibility_to_user_groups.rb create mode 100644 lib/tasks/user_groups.rake diff --git a/app/models/user_group.rb b/app/models/user_group.rb index 7e16fd173..9d51da007 100644 --- a/app/models/user_group.rb +++ b/app/models/user_group.rb @@ -19,6 +19,26 @@ class UserGroup < ApplicationRecord has_many :collections, through: :owned_resources, source: :resource, source_type: "Collection" + ## + # Stats_Visibility Levels (Used for ERAS stats service) + # private_agg_only (default): Only members of a user group can view aggregate stats. Individual stats only viewable by only admins of the user group + # + # private_show_agg_and_ind: Only members of a user group can view aggregate stats. Individual stats is viewable by BOTH members and admins of the user group. + # + # public_agg_only: Anyone can view aggregate stats of the user group. Only admins of the user group can view individual stats. + # + # public_agg_show_ind_if_member: Anyone can view aggregate stats of the user group. Members and admins of the user group can view individual stats. + # + # public_show_all: Anyone can view aggregate stats of the user group and can view individual stats of the user group. + ## + enum stats_visibility: { + private_agg_only: 0, + private_show_agg_and_ind: 1, + public_agg_only: 2, + public_agg_show_ind_if_member: 3, + public_show_all: 4 + } + validates :display_name, presence: true validates :name, presence: true, uniqueness: { case_sensitive: false }, diff --git a/db/migrate/20230613165746_add_stats_visibility_to_user_groups.rb b/db/migrate/20230613165746_add_stats_visibility_to_user_groups.rb new file mode 100644 index 000000000..e61565665 --- /dev/null +++ b/db/migrate/20230613165746_add_stats_visibility_to_user_groups.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddStatsVisibilityToUserGroups < ActiveRecord::Migration[6.1] + def change + add_column :user_groups, :stats_visibility, :integer + # defaulting to private_agg_only stats_visibility view (where members can view aggregate stats but only admins can view detailed stats) + change_column_default :user_groups, :stats_visibility, from: nil, to: 0 + end +end diff --git a/db/structure.sql b/db/structure.sql index 2115ddc5c..8602eed30 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1,10 +1,3 @@ --- --- PostgreSQL database dump --- - --- Dumped from database version 11.15 (Debian 11.15-1.pgdg90+1) --- Dumped by pg_dump version 11.15 - SET statement_timeout = 0; SET lock_timeout = 0; SET idle_in_transaction_session_timeout = 0; @@ -1671,7 +1664,8 @@ CREATE TABLE public.user_groups ( display_name character varying, private boolean DEFAULT true NOT NULL, lock_version integer DEFAULT 0, - join_token character varying + join_token character varying, + stats_visibility integer DEFAULT 0 ); @@ -4581,6 +4575,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20211007125705'), ('20211124175756'), ('20211201164326'), -('20221018032140'); +('20221018032140'), +('20230613165746'); diff --git a/lib/tasks/user_groups.rake b/lib/tasks/user_groups.rake new file mode 100644 index 000000000..3b1581c4c --- /dev/null +++ b/lib/tasks/user_groups.rake @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +namespace :user_groups do + desc 'Backfill stats_visibility column default in batches' + task backfill_stats_visibility_column_default: :environment do + UserGroup.where(stats_visibility: nil).select(:id).find_in_batches do |user_group| + user_group_ids_to_update = user_group.map(&:id) + UserGroup.where(id: user_group_ids_to_update).update_all(stats_visibility: 0) + end + end +end