mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-06 20:05:55 +08:00
Reset counter for export in last 24 hours
Fix for check if user has avaiable exports, bit refactored increas_counter method.
This commit is contained in:
parent
7ef7e2397f
commit
caccf8d9f0
4 changed files with 54 additions and 27 deletions
|
@ -228,7 +228,7 @@ class TeamsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def export_projects
|
def export_projects
|
||||||
unless export_proj_requests_exceeded?
|
if current_user.has_available_exports?
|
||||||
current_user.increase_daily_exports_counter!
|
current_user.increase_daily_exports_counter!
|
||||||
|
|
||||||
generate_export_projects_zip
|
generate_export_projects_zip
|
||||||
|
@ -253,16 +253,7 @@ class TeamsController < ApplicationController
|
||||||
if @exp_projects.present?
|
if @exp_projects.present?
|
||||||
limit = (ENV['EXPORT_ALL_LIMIT_24_HOURS'] || 3).to_i
|
limit = (ENV['EXPORT_ALL_LIMIT_24_HOURS'] || 3).to_i
|
||||||
curr_num = current_user.export_vars['num_of_export_all_last_24_hours']
|
curr_num = current_user.export_vars['num_of_export_all_last_24_hours']
|
||||||
if export_proj_requests_exceeded?
|
if current_user.has_available_exports?
|
||||||
render json: {
|
|
||||||
html: render_to_string(
|
|
||||||
partial: 'projects/export/error.html.erb',
|
|
||||||
locals: { limit: limit }
|
|
||||||
),
|
|
||||||
title: t('projects.export_projects.error_title'),
|
|
||||||
status: 'error'
|
|
||||||
}
|
|
||||||
else
|
|
||||||
render json: {
|
render json: {
|
||||||
html: render_to_string(
|
html: render_to_string(
|
||||||
partial: 'projects/export/modal.html.erb',
|
partial: 'projects/export/modal.html.erb',
|
||||||
|
@ -272,6 +263,15 @@ class TeamsController < ApplicationController
|
||||||
),
|
),
|
||||||
title: t('projects.export_projects.modal_title')
|
title: t('projects.export_projects.modal_title')
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
render json: {
|
||||||
|
html: render_to_string(
|
||||||
|
partial: 'projects/export/error.html.erb',
|
||||||
|
locals: { limit: limit }
|
||||||
|
),
|
||||||
|
title: t('projects.export_projects.error_title'),
|
||||||
|
status: 'error'
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -337,13 +337,6 @@ class TeamsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def export_proj_requests_exceeded?
|
|
||||||
# Check if user has enough requests for the day
|
|
||||||
limit = (ENV['EXPORT_ALL_LIMIT_24_HOURS'] || 3).to_i
|
|
||||||
!limit.zero? \
|
|
||||||
&& current_user.export_vars['num_of_export_all_last_24_hours'] >= limit
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_samples_zip
|
def generate_samples_zip
|
||||||
zip = ZipExport.create(user: current_user)
|
zip = ZipExport.create(user: current_user)
|
||||||
zip.generate_exportable_zip(
|
zip.generate_exportable_zip(
|
||||||
|
|
|
@ -58,7 +58,7 @@ class User < ApplicationRecord
|
||||||
default_variables(
|
default_variables(
|
||||||
export_vars: {
|
export_vars: {
|
||||||
num_of_export_all_last_24_hours: 0,
|
num_of_export_all_last_24_hours: 0,
|
||||||
last_export_timestamp: Date.today.to_time.to_i
|
last_export_timestamp: Time.now.utc.beginning_of_day.to_i
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -516,15 +516,28 @@ class User < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def increase_daily_exports_counter!
|
def increase_daily_exports_counter!
|
||||||
if Time.at(export_vars['last_export_timestamp'] || 0).to_date == Date.today
|
range = Time.now.utc.beginning_of_day.to_i..Time.now.utc.end_of_day.to_i
|
||||||
export_vars['num_of_export_all_last_24_hours'] += 1
|
last_export = export_vars[:last_export_timestamp] || 0
|
||||||
|
|
||||||
|
if range.cover?(last_export)
|
||||||
|
export_vars[:num_of_export_all_last_24_hours] += 1
|
||||||
else
|
else
|
||||||
export_vars['last_export_timestamp'] = Date.today.to_time.to_i
|
export_vars[:last_export_timestamp] = Time.now.utc.to_i
|
||||||
export_vars['num_of_export_all_last_24_hours'] = 1
|
export_vars[:num_of_export_all_last_24_hours] = 1
|
||||||
end
|
end
|
||||||
save
|
save
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def has_available_exports?
|
||||||
|
limit = (Rails.application.secrets.export_all_limit_24h || 3).to_i
|
||||||
|
last_export = export_vars[:last_export_timestamp]
|
||||||
|
|
||||||
|
# limit 0 means unlimited exports
|
||||||
|
return true if limit.zero? || last_export < Time.now.utc.beginning_of_day.to_i
|
||||||
|
|
||||||
|
export_vars[:num_of_export_all_last_24_hours] < limit
|
||||||
|
end
|
||||||
|
|
||||||
def global_activity_filter(filters, search_query)
|
def global_activity_filter(filters, search_query)
|
||||||
query_teams = teams.pluck(:id)
|
query_teams = teams.pluck(:id)
|
||||||
query_teams &= filters[:teams].map(&:to_i) if filters[:teams]
|
query_teams &= filters[:teams].map(&:to_i) if filters[:teams]
|
||||||
|
|
|
@ -61,16 +61,21 @@ development:
|
||||||
secret_key_base: 22f2adf8f5cb73351da28f2292daa840cc2a414ae00ae605b175a8d5c73932f7e5b8ff8ef8f1554a7f1064f9869b15347f7709f0daa6ccb24c50f3cace304f64
|
secret_key_base: 22f2adf8f5cb73351da28f2292daa840cc2a414ae00ae605b175a8d5c73932f7e5b8ff8ef8f1554a7f1064f9869b15347f7709f0daa6ccb24c50f3cace304f64
|
||||||
system_notifications_uri: <%= ENV["SYSTEM_NOTIFICATIONS_URI"] %>
|
system_notifications_uri: <%= ENV["SYSTEM_NOTIFICATIONS_URI"] %>
|
||||||
system_notifications_channel: <%= ENV["SYSTEM_NOTIFICATIONS_CHANNEL"] %>
|
system_notifications_channel: <%= ENV["SYSTEM_NOTIFICATIONS_CHANNEL"] %>
|
||||||
|
export_all_limit_24h: <%= ENV['EXPORT_ALL_LIMIT_24_HOURS'] %>
|
||||||
<<: *common
|
<<: *common
|
||||||
|
|
||||||
test:
|
test:
|
||||||
secret_key_base: f3719934e04fa8871cf5d33d5c60f05e1b8995e0315265aef9f8b878da49bd2d386eb25ce35545b469a94ccf22f91e0052b93a15194b4f57b0c8d6ce8b150e1e
|
secret_key_base: f3719934e04fa8871cf5d33d5c60f05e1b8995e0315265aef9f8b878da49bd2d386eb25ce35545b469a94ccf22f91e0052b93a15194b4f57b0c8d6ce8b150e1e
|
||||||
system_notifications_uri: 'system-notifications-service.test'
|
system_notifications_uri: 'system-notifications-service.test'
|
||||||
system_notifications_channel: 'test-channel'
|
system_notifications_channel: 'test-channel'
|
||||||
|
export_all_limit_24h: '3'
|
||||||
|
|
||||||
<<: *common
|
<<: *common
|
||||||
|
|
||||||
production:
|
production:
|
||||||
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
||||||
system_notifications_uri: <%= ENV["SYSTEM_NOTIFICATIONS_URI"] %>
|
system_notifications_uri: <%= ENV["SYSTEM_NOTIFICATIONS_URI"] %>
|
||||||
system_notifications_channel: <%= ENV["SYSTEM_NOTIFICATIONS_CHANNEL"] %>
|
system_notifications_channel: <%= ENV["SYSTEM_NOTIFICATIONS_CHANNEL"] %>
|
||||||
|
export_all_limit_24h: <%= ENV['EXPORT_ALL_LIMIT_24_HOURS'] %>
|
||||||
|
|
||||||
<<: *common
|
<<: *common
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe User, type: :model do
|
describe User, type: :model do
|
||||||
|
@ -211,14 +213,14 @@ describe User, type: :model do
|
||||||
}.from(0).to(1)
|
}.from(0).to(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'sets last_export_timestamp on today' do
|
it 'sets last_export_timestamp on today\'s timestamp' do
|
||||||
user.export_vars['last_export_timestamp'] = Date.yesterday.to_time.to_i
|
user.export_vars['last_export_timestamp'] = Date.yesterday.to_time.to_i
|
||||||
user.save
|
user.save
|
||||||
|
|
||||||
expect { user.increase_daily_exports_counter! }
|
expect { user.increase_daily_exports_counter! }
|
||||||
.to change {
|
.to change {
|
||||||
user.reload.export_vars['last_export_timestamp']
|
user.reload.export_vars['last_export_timestamp']
|
||||||
}.to(Date.today.to_time.to_i)
|
}.to(Time.now.utc.beginning_of_day.to_i..Time.now.utc.end_of_day.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'sets new counter for today' do
|
it 'sets new counter for today' do
|
||||||
|
@ -236,14 +238,14 @@ describe User, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when last_export_timestamp not exists (existing users)' do
|
context 'when last_export_timestamp not exists (existing users)' do
|
||||||
it 'sets last_export_timestamp on today' do
|
it 'sets last_export_timestamp on today\'s timestamp' do
|
||||||
user.export_vars.delete('last_export_timestamp')
|
user.export_vars.delete('last_export_timestamp')
|
||||||
user.save
|
user.save
|
||||||
|
|
||||||
expect { user.increase_daily_exports_counter! }
|
expect { user.increase_daily_exports_counter! }
|
||||||
.to change {
|
.to change {
|
||||||
user.reload.export_vars['last_export_timestamp']
|
user.reload.export_vars['last_export_timestamp']
|
||||||
}.from(nil).to(Date.today.to_time.to_i)
|
}.from(nil).to(Time.now.utc.beginning_of_day.to_i..Time.now.utc.end_of_day.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'starts count reports with 1' do
|
it 'starts count reports with 1' do
|
||||||
|
@ -259,6 +261,20 @@ describe User, type: :model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'has_available_exports?' do
|
||||||
|
let(:user) { create :user }
|
||||||
|
|
||||||
|
it 'returns true when user has avaiable export' do
|
||||||
|
expect(user.has_available_exports?).to be_truthy
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns false when user has no avaiable export' do
|
||||||
|
user.export_vars['num_of_export_all_last_24_hours'] = 3
|
||||||
|
|
||||||
|
expect(user.has_available_exports?).to be_falsey
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'Associations' do
|
describe 'Associations' do
|
||||||
it { is_expected.to have_many(:system_notifications) }
|
it { is_expected.to have_many(:system_notifications) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue