From 516c838a1846d049814765afa85c28a3c14a5b9f Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Wed, 24 Aug 2016 20:17:48 +0530 Subject: [PATCH] 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. --- app/models/issue.rb | 9 +++++ app/models/issue/metrics.rb | 21 +++++++++++ .../20160824124900_add_table_issue_metrics.rb | 36 +++++++++++++++++++ db/schema.rb | 13 ++++++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 app/models/issue/metrics.rb create mode 100644 db/migrate/20160824124900_add_table_issue_metrics.rb diff --git a/app/models/issue.rb b/app/models/issue.rb index 788611305fe..1aa0cef884e 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -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 diff --git a/app/models/issue/metrics.rb b/app/models/issue/metrics.rb new file mode 100644 index 00000000000..4436696cc1a --- /dev/null +++ b/app/models/issue/metrics.rb @@ -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 diff --git a/db/migrate/20160824124900_add_table_issue_metrics.rb b/db/migrate/20160824124900_add_table_issue_metrics.rb new file mode 100644 index 00000000000..256c1b7c15c --- /dev/null +++ b/db/migrate/20160824124900_add_table_issue_metrics.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 5a105a91ad1..2c580a164bd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"