From cc426833165934ade919c6ac5043c3a17bf794fc Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 23 Sep 2022 10:07:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=B4=BB=E8=B7=83?= =?UTF-8?q?=E5=BA=A6=E8=AE=A1=E7=AE=97=E8=AE=BF=E9=97=AE=E9=87=8F=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/repositories_controller.rb | 9 ++++-- app/models/timeable_visit_record.rb | 28 +++++++++++++++++++ ...923012923_create_timeable_visit_records.rb | 13 +++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 app/models/timeable_visit_record.rb create mode 100644 db/migrate/20220923012923_create_timeable_visit_records.rb diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 32bb1f0eb..5ee1fdde3 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -47,8 +47,13 @@ class RepositoriesController < ApplicationController end def entries - @project.increment!(:visits) - CacheAsyncSetJob.perform_later("project_common_service", {visits: 1}, @project.id) + @week_project_visit_record, @month_project_visit_record = TimeableVisitRecord.build(@project.id) + if @week_project_visit_record.visits < 300 && @month_project_visit_record.visits < 1000 + @week_project_visit_record.increment!(:visits) + @month_project_visit_record.increment!(:visits) + @project.increment!(:visits) + CacheAsyncSetJob.perform_later("project_common_service", {visits: 1}, @project.id) + end if @project.educoder? @entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder.repo_name) else diff --git a/app/models/timeable_visit_record.rb b/app/models/timeable_visit_record.rb new file mode 100644 index 000000000..974e785e7 --- /dev/null +++ b/app/models/timeable_visit_record.rb @@ -0,0 +1,28 @@ +# == Schema Information +# +# Table name: timeable_visit_records +# +# id :integer not null, primary key +# time :string(255) +# project_id :integer +# visits :integer +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_timeable_visit_records_on_project_id (project_id) +# index_timeable_visit_records_on_time (time) +# + +class TimeableVisitRecord < ApplicationRecord + + belongs_to :project + + def self.build(project_id) + week = TimeableVisitRecord.find_or_create_by!(time: Date.today.cweek, project_id: project_id) + month = TimeableVisitRecord.find_or_create_by!(time: Date.today.strftime("%Y%m"), project_id: project_id) + + return week, month + end +end diff --git a/db/migrate/20220923012923_create_timeable_visit_records.rb b/db/migrate/20220923012923_create_timeable_visit_records.rb new file mode 100644 index 000000000..ec105d20e --- /dev/null +++ b/db/migrate/20220923012923_create_timeable_visit_records.rb @@ -0,0 +1,13 @@ +class CreateTimeableVisitRecords < ActiveRecord::Migration[5.2] + def change + create_table :timeable_visit_records do |t| + t.string :time + t.references :project + t.integer :visits, default: 0 + + t.timestamps + end + + add_index :timeable_visit_records, :time + end +end