Add an Issue::Metrics model.

- And store the `first_associated_with_milestone_at` and
  `first_added_to_board_at` times, when an issue is saved.
This commit is contained in:
Timothy Andrew 2016-08-24 20:17:48 +05:30
parent 1bf2fe276f
commit 516c838a18
4 changed files with 78 additions and 1 deletions

View file

@ -23,6 +23,8 @@ class Issue < ActiveRecord::Base
has_many :events, as: :target, dependent: :destroy
has_one :metrics, dependent: :destroy
validates :project, presence: true
scope :cared, ->(user) { where(assignee_id: user) }
@ -36,6 +38,8 @@ class Issue < ActiveRecord::Base
scope :order_due_date_asc, -> { reorder('issues.due_date IS NULL, issues.due_date ASC') }
scope :order_due_date_desc, -> { reorder('issues.due_date IS NULL, issues.due_date DESC') }
after_save :record_metrics
attr_spammable :title, spam_title: true
attr_spammable :description, spam_description: true
@ -270,4 +274,9 @@ class Issue < ActiveRecord::Base
def check_for_spam?
project.public?
end
def record_metrics
metrics = Metrics.find_or_create_by(issue_id: self.id)
metrics.record!
end
end

View file

@ -0,0 +1,21 @@
class Issue::Metrics < ActiveRecord::Base
belongs_to :issue
def record!
if issue.milestone_id.present? && self.first_associated_with_milestone_at.blank?
self.first_associated_with_milestone_at = Time.now
end
if issue_assigned_to_list_label? && self.first_added_to_board_at.blank?
self.first_added_to_board_at = Time.now
end
self.save if self.changed?
end
private
def issue_assigned_to_list_label?
issue.labels.any? { |label| label.lists.present? }
end
end

View file

@ -0,0 +1,36 @@
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddTableIssueMetrics < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME = false
# When a migration requires downtime you **must** uncomment the following
# constant and define a short and easy to understand explanation as to why the
# migration requires downtime.
# DOWNTIME_REASON = ''
# When using the methods "add_concurrent_index" or "add_column_with_default"
# you must disable the use of transactions as these methods can not run in an
# existing transaction. When using "add_concurrent_index" make sure that this
# method is the _only_ method called in the migration, any other changes
# should go in a separate migration. This ensures that upon failure _only_ the
# index creation fails and can be retried or reverted easily.
#
# To disable transactions uncomment the following line and remove these
# comments:
# disable_ddl_transaction!
def change
create_table :issue_metrics do |t|
t.references :issue, index: { name: "index_issue_metrics" }, foreign_key: true, null: false
t.datetime 'first_associated_with_milestone_at'
t.datetime 'first_added_to_board_at'
t.timestamps null: false
end
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160823081327) do
ActiveRecord::Schema.define(version: 20160824124900) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -453,6 +453,16 @@ ActiveRecord::Schema.define(version: 20160823081327) do
add_index "identities", ["user_id"], name: "index_identities_on_user_id", using: :btree
create_table "issue_metrics", force: :cascade do |t|
t.integer "issue_id", null: false
t.datetime "first_associated_with_milestone_at"
t.datetime "first_added_to_board_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "issue_metrics", ["issue_id"], name: "index_issue_metrics", using: :btree
create_table "issues", force: :cascade do |t|
t.string "title"
t.integer "assignee_id"
@ -1150,6 +1160,7 @@ ActiveRecord::Schema.define(version: 20160823081327) do
add_index "web_hooks", ["project_id"], name: "index_web_hooks_on_project_id", using: :btree
add_foreign_key "boards", "projects"
add_foreign_key "issue_metrics", "issues"
add_foreign_key "lists", "boards"
add_foreign_key "lists", "labels"
add_foreign_key "personal_access_tokens", "users"