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:
Urban Rotnik 2019-04-04 14:35:16 +02:00
parent 7ef7e2397f
commit caccf8d9f0
4 changed files with 54 additions and 27 deletions

View file

@ -228,7 +228,7 @@ class TeamsController < ApplicationController
end
def export_projects
unless export_proj_requests_exceeded?
if current_user.has_available_exports?
current_user.increase_daily_exports_counter!
generate_export_projects_zip
@ -253,16 +253,7 @@ class TeamsController < ApplicationController
if @exp_projects.present?
limit = (ENV['EXPORT_ALL_LIMIT_24_HOURS'] || 3).to_i
curr_num = current_user.export_vars['num_of_export_all_last_24_hours']
if export_proj_requests_exceeded?
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
if current_user.has_available_exports?
render json: {
html: render_to_string(
partial: 'projects/export/modal.html.erb',
@ -272,6 +263,15 @@ class TeamsController < ApplicationController
),
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
@ -337,13 +337,6 @@ class TeamsController < ApplicationController
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
zip = ZipExport.create(user: current_user)
zip.generate_exportable_zip(

View file

@ -58,7 +58,7 @@ class User < ApplicationRecord
default_variables(
export_vars: {
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
def increase_daily_exports_counter!
if Time.at(export_vars['last_export_timestamp'] || 0).to_date == Date.today
export_vars['num_of_export_all_last_24_hours'] += 1
range = Time.now.utc.beginning_of_day.to_i..Time.now.utc.end_of_day.to_i
last_export = export_vars[:last_export_timestamp] || 0
if range.cover?(last_export)
export_vars[:num_of_export_all_last_24_hours] += 1
else
export_vars['last_export_timestamp'] = Date.today.to_time.to_i
export_vars['num_of_export_all_last_24_hours'] = 1
export_vars[:last_export_timestamp] = Time.now.utc.to_i
export_vars[:num_of_export_all_last_24_hours] = 1
end
save
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)
query_teams = teams.pluck(:id)
query_teams &= filters[:teams].map(&:to_i) if filters[:teams]

View file

@ -61,16 +61,21 @@ development:
secret_key_base: 22f2adf8f5cb73351da28f2292daa840cc2a414ae00ae605b175a8d5c73932f7e5b8ff8ef8f1554a7f1064f9869b15347f7709f0daa6ccb24c50f3cace304f64
system_notifications_uri: <%= ENV["SYSTEM_NOTIFICATIONS_URI"] %>
system_notifications_channel: <%= ENV["SYSTEM_NOTIFICATIONS_CHANNEL"] %>
export_all_limit_24h: <%= ENV['EXPORT_ALL_LIMIT_24_HOURS'] %>
<<: *common
test:
secret_key_base: f3719934e04fa8871cf5d33d5c60f05e1b8995e0315265aef9f8b878da49bd2d386eb25ce35545b469a94ccf22f91e0052b93a15194b4f57b0c8d6ce8b150e1e
system_notifications_uri: 'system-notifications-service.test'
system_notifications_channel: 'test-channel'
export_all_limit_24h: '3'
<<: *common
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
system_notifications_uri: <%= ENV["SYSTEM_NOTIFICATIONS_URI"] %>
system_notifications_channel: <%= ENV["SYSTEM_NOTIFICATIONS_CHANNEL"] %>
export_all_limit_24h: <%= ENV['EXPORT_ALL_LIMIT_24_HOURS'] %>
<<: *common

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper'
describe User, type: :model do
@ -211,14 +213,14 @@ describe User, type: :model do
}.from(0).to(1)
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.save
expect { user.increase_daily_exports_counter! }
.to change {
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
it 'sets new counter for today' do
@ -236,14 +238,14 @@ describe User, type: :model do
end
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.save
expect { user.increase_daily_exports_counter! }
.to change {
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
it 'starts count reports with 1' do
@ -259,6 +261,20 @@ describe User, type: :model do
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
it { is_expected.to have_many(:system_notifications) }
end