Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Herrmann committed Jan 22, 2025
1 parent 1344be1 commit f83522d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ResearchPlanDetailsAttachments extends Component {
this.confirmAttachmentImport = this.confirmAttachmentImport.bind(this);
this.showImportConfirm = this.showImportConfirm.bind(this);
this.hideImportConfirm = this.hideImportConfirm.bind(this);
this.checkUserQuota = this.checkUserQuota.bind(this);
this.isUserQuotaExceeded = this.isUserQuotaExceeded.bind(this);
}

componentDidMount() {
Expand Down Expand Up @@ -220,7 +220,7 @@ class ResearchPlanDetailsAttachments extends Component {
this.hideImportConfirm(attachment.id);
}

checkUserQuota() {
isUserQuotaExceeded() {
const { filteredAttachments } = this.state;
const totalSize = filteredAttachments.filter((attachment) => attachment.is_new && !attachment.is_deleted)
.reduce((acc, attachment) => acc + attachment.filesize, 0);
Expand Down Expand Up @@ -369,7 +369,7 @@ class ResearchPlanDetailsAttachments extends Component {
)}
</div>
))}
<Alert variant="warning" show={this.checkUserQuota()}>
<Alert variant="warning" show={this.isUserQuotaExceeded()}>
Uploading attachments will fail; User quota will be exceeded.
</Alert>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class ContainerDatasetModalContent extends Component {
this.handleAttachmentRemove = this.handleAttachmentRemove.bind(this);
this.handleAttachmentBackToInbox = this.handleAttachmentBackToInbox.bind(this);
this.classifyAttachments = this.classifyAttachments.bind(this);
this.checkUserQuota = this.checkUserQuota.bind(this);
this.isUserQuotaExceeded = this.isUserQuotaExceeded.bind(this);
this.state.attachmentGroups = this.classifyAttachments(props.datasetContainer.attachments);
}

Expand Down Expand Up @@ -468,7 +468,7 @@ export class ContainerDatasetModalContent extends Component {
);
}

checkUserQuota() {
isUserQuotaExceeded() {
const { filteredAttachments } = this.state;
const totalSize = filteredAttachments.filter((attachment) => attachment.is_new === true && !attachment.is_deleted)
.reduce((acc, attachment) => acc + attachment.filesize, 0);
Expand Down Expand Up @@ -649,7 +649,7 @@ export class ContainerDatasetModalContent extends Component {
&& renderGroup(attachmentGroups.Processed[groupName], `Processed: ${groupName}`, groupName))}
{attachmentGroups.Combined.length > 0 && renderGroup(attachmentGroups.Combined, 'Combined')}
</div>
<Alert variant="warning" show={this.checkUserQuota()}>
<Alert variant="warning" show={this.isUserQuotaExceeded()}>
Uploading attachments will fail; User quota will be exceeded.
</Alert>
</>
Expand Down
4 changes: 2 additions & 2 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def delete_file_and_thumbnail
attachment_attacher.destroy
end

def check_user_quota
def user_quota_exceeded?
user = User.find(created_for.nil? ? created_by : created_for)
if (user.used_space + attachment_data['metadata']['size']) > user.available_space &&
!user.available_space.zero?
Expand All @@ -296,7 +296,7 @@ def attach_file
attachment_attacher.attach(File.open(file_path, binmode: true))
raise 'File to large' unless valid?

raise 'User quota exceeded' unless check_user_quota
raise 'User quota exceeded' unless user_quota_exceeded?

attachment_attacher.create_derivatives

Expand Down
49 changes: 12 additions & 37 deletions db/functions/calculate_collection_space_v01.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,17 @@ returns bigint
language plpgsql
as $function$
declare
used_space_samples bigint default 0;
used_space_reactions bigint default 0;
used_space_wellplates bigint default 0;
used_space_screens bigint default 0;
used_space_research_plans bigint default 0;
used_space bigint default 0;
element_types text[] := array['Sample', 'Reaction', 'Wellplate', 'Screen', 'ResearchPlan'];
element_table text;
element_space bigint;
begin
select sum(calculate_element_space(sample_id, 'Sample')) into used_space_samples
from collections_samples
where collection_id = collectionId;

used_space = COALESCE(used_space_samples,0);

select sum(calculate_element_space(reaction_id, 'Reaction')) into used_space_reactions
from collections_reactions
where collection_id = collectionId;

used_space = used_space + COALESCE(used_space_reactions,0);

select sum(calculate_element_space(wellplate_id, 'Wellplate')) into used_space_wellplates
from collections_wellplates
where collection_id = collectionId;

used_space = used_space + COALESCE(used_space_wellplates,0);

select sum(calculate_element_space(screen_id, 'Screen')) into used_space_screens
from collections_screens
where collection_id = collectionId;

used_space = used_space + COALESCE(used_space_screens,0);

select sum(calculate_element_space(research_plan_id, 'ResearchPlan')) into used_space_research_plans
from collections_research_plans
where collection_id = collectionId;

used_space = used_space + COALESCE(used_space_research_plans,0);

return COALESCE(used_space,0);
end;$function$;
foreach element_table in array element_types loop
execute format('select sum(calculate_element_space(id, $1)) from collections_%s where collection_id = $2', lower(element_table))
into element_space
using element_table, collectionId;
used_space := used_space + coalesce(element_space, 0);
end loop;
return coalesce(used_space, 0);
end;
$function$;
71 changes: 34 additions & 37 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "short_label"
t.string "ancestry"
t.index ["ancestry"], name: "index_cellline_samples_on_ancestry"
end

create_table "channels", id: :serial, force: :cascade do |t|
Expand Down Expand Up @@ -2171,6 +2173,23 @@
END;
$function$
SQL
create_function :lab_record_layers_changes, sql_definition: <<-'SQL'
CREATE OR REPLACE FUNCTION public.lab_record_layers_changes()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
BEGIN
INSERT INTO layer_tracks (name, label, description, properties, identifier, created_by, created_at, updated_by, updated_at, deleted_by, deleted_at)
VALUES (OLD.name, OLD.label, OLD.description, OLD.properties, OLD.identifier, OLD.created_by, OLD.created_at, OLD.updated_by, OLD.updated_at, OLD.deleted_by, OLD.deleted_at);
EXCEPTION
WHEN OTHERS THEN
-- Ensure the main operation still completes successfully
END;
RETURN NEW;
END;
$function$
SQL
create_function :calculate_dataset_space, sql_definition: <<-'SQL'
CREATE OR REPLACE FUNCTION public.calculate_dataset_space(cid integer)
RETURNS bigint
Expand Down Expand Up @@ -2214,45 +2233,20 @@
LANGUAGE plpgsql
AS $function$
declare
used_space_samples bigint default 0;
used_space_reactions bigint default 0;
used_space_wellplates bigint default 0;
used_space_screens bigint default 0;
used_space_research_plans bigint default 0;
used_space bigint default 0;
element_types text[] := array['Sample', 'Reaction', 'Wellplate', 'Screen', 'ResearchPlan'];
element_table text;
element_space bigint;
begin
select sum(calculate_element_space(sample_id, 'Sample')) into used_space_samples
from collections_samples
where collection_id = collectionId;
used_space = COALESCE(used_space_samples,0);
select sum(calculate_element_space(reaction_id, 'Reaction')) into used_space_reactions
from collections_reactions
where collection_id = collectionId;
used_space = used_space + COALESCE(used_space_reactions,0);
select sum(calculate_element_space(wellplate_id, 'Wellplate')) into used_space_wellplates
from collections_wellplates
where collection_id = collectionId;
used_space = used_space + COALESCE(used_space_wellplates,0);
select sum(calculate_element_space(screen_id, 'Screen')) into used_space_screens
from collections_screens
where collection_id = collectionId;
used_space = used_space + COALESCE(used_space_screens,0);
select sum(calculate_element_space(research_plan_id, 'ResearchPlan')) into used_space_research_plans
from collections_research_plans
where collection_id = collectionId;
used_space = used_space + COALESCE(used_space_research_plans,0);
return COALESCE(used_space,0);
end;$function$
foreach element_table in array element_types loop
execute format('select sum(calculate_element_space(id, $1)) from collections_%s where collection_id = $2', lower(element_table))
into element_space
using element_table, collectionId;
used_space := used_space + coalesce(element_space, 0);
end loop;
return coalesce(used_space, 0);
end;
$function$
SQL
create_function :calculate_used_space, sql_definition: <<-'SQL'
CREATE OR REPLACE FUNCTION public.calculate_used_space(userid integer)
Expand Down Expand Up @@ -2364,6 +2358,9 @@
create_trigger :logidze_on_wells, sql_definition: <<-SQL
CREATE TRIGGER logidze_on_wells BEFORE INSERT OR UPDATE ON public.wells FOR EACH ROW WHEN ((COALESCE(current_setting('logidze.disabled'::text, true), ''::text) <> 'on'::text)) EXECUTE FUNCTION logidze_logger('null', 'updated_at')
SQL
create_trigger :lab_trg_layers_changes, sql_definition: <<-SQL
CREATE TRIGGER lab_trg_layers_changes AFTER UPDATE ON public.layers FOR EACH ROW EXECUTE FUNCTION lab_record_layers_changes()
SQL

create_view "v_samples_collections", sql_definition: <<-SQL
SELECT cols.id AS cols_id,
Expand Down

0 comments on commit f83522d

Please sign in to comment.