From 4627ed7a6fff8420694c001d7c8c29df76e91a71 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 23 Sep 2025 21:03:49 +0200 Subject: [PATCH 1/8] Speed up scheduling of visits suggestions job after import --- CHANGELOG.md | 1 + app/services/imports/create.rb | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e35e26e5..5394d6ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Stats will now properly reflect countries and cities visited after importing new points. - `GET /api/v1/points will now return correct latitude and longitude values. #1502 - Deleting an import will now trigger stats recalculation for affected months. #1789 +- Importing process should now schedule visits suggestions job a lot faster. ## Changed diff --git a/app/services/imports/create.rb b/app/services/imports/create.rb index 58079188..d920f374 100644 --- a/app/services/imports/create.rb +++ b/app/services/imports/create.rb @@ -78,12 +78,11 @@ class Imports::Create def schedule_visit_suggesting(user_id, import) return unless user.safe_settings.visits_suggestions_enabled? - points = import.points.order(:timestamp) + min_max = import.points.pluck('MIN(timestamp), MAX(timestamp)').first + return if min_max.compact.empty? - return if points.none? - - start_at = Time.zone.at(points.first.timestamp) - end_at = Time.zone.at(points.last.timestamp) + start_at = Time.zone.at(min_max[0]) + end_at = Time.zone.at(min_max[1]) VisitSuggestingJob.perform_later(user_id:, start_at:, end_at:) end From 6a0cc112dc79c1efe0c2c2891d18f817539c9809 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 23 Sep 2025 21:14:55 +0200 Subject: [PATCH 2/8] Introduce limit for trial users: max 5 imports, 10MB per file --- .../controllers/direct_upload_controller.js | 13 ++++++- app/models/import.rb | 10 ++++++ app/views/imports/_form.html.erb | 8 +++++ spec/models/import_spec.rb | 36 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/javascript/controllers/direct_upload_controller.js b/app/javascript/controllers/direct_upload_controller.js index cc58436e..3b239c49 100644 --- a/app/javascript/controllers/direct_upload_controller.js +++ b/app/javascript/controllers/direct_upload_controller.js @@ -6,7 +6,8 @@ export default class extends Controller { static targets = ["input", "progress", "progressBar", "submit", "form"] static values = { url: String, - userTrial: Boolean + userTrial: Boolean, + currentImportsCount: Number } connect() { @@ -51,6 +52,16 @@ export default class extends Controller { const files = this.inputTarget.files if (files.length === 0) return + // Check import count limits for trial users + if (this.userTrialValue && this.currentImportsCountValue >= 5) { + const message = 'Import limit reached. Trial users can only create up to 5 imports. Please upgrade your account to import more files.' + showFlashMessage('error', message) + + // Clear the file input + this.inputTarget.value = '' + return + } + // Check file size limits for trial users if (this.userTrialValue) { const MAX_FILE_SIZE = 11 * 1024 * 1024 // 11MB in bytes diff --git a/app/models/import.rb b/app/models/import.rb index b1abde92..e394d3e0 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -15,6 +15,7 @@ class Import < ApplicationRecord validates :name, presence: true, uniqueness: { scope: :user_id } validate :file_size_within_limit, if: -> { user.trial? } + validate :import_count_within_limit, if: -> { user.trial? } enum :status, { created: 0, processing: 1, completed: 2, failed: 3 } @@ -69,6 +70,15 @@ class Import < ApplicationRecord errors.add(:file, 'is too large. Trial users can only upload files up to 10MB.') end + def import_count_within_limit + return unless new_record? + + existing_imports_count = user.imports.count + return unless existing_imports_count >= 5 + + errors.add(:base, 'Trial users can only create up to 5 imports. Please upgrade your account to import more files.') + end + def recalculate_stats years_and_months_tracked.each do |year, month| Stats::CalculatingJob.perform_later(user.id, year, month) diff --git a/app/views/imports/_form.html.erb b/app/views/imports/_form.html.erb index 95d16411..16fd3cd5 100644 --- a/app/views/imports/_form.html.erb +++ b/app/views/imports/_form.html.erb @@ -11,6 +11,13 @@
File format is automatically detected during upload.
+ <% if current_user.trial? %> +
+ Trial limitations: Max 5 imports, 10MB per file. +
+ Current imports: <%= current_user.imports.count %>/5 +
+ <% end %> @@ -18,6 +25,7 @@ controller: "direct-upload", direct_upload_url_value: rails_direct_uploads_url, direct_upload_user_trial_value: current_user.trial?, + direct_upload_current_imports_count_value: current_user.imports.count, direct_upload_target: "form" } do |form| %>