Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

adding and persisting attribute `Query#timestamps`

Sebastian Fiedlschuster49d14e6631a4
diff --git a/app/models/query.rb b/app/models/query.rb
index ac23c3b0987..7f49dd1eed9 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -28,6 +28,7 @@
class Query < ApplicationRecord
include Timelines
+ include Timestamps
include Highlighting
include ManualSorting
include Queries::Filters::AvailableFilters
diff --git a/app/models/query/timestamps.rb b/app/models/query/timestamps.rb
new file mode 100644
index 00000000000..378da2646b5
--- /dev/null
+++ b/app/models/query/timestamps.rb
@@ -0,0 +1,56 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+#
+module Query::Timestamps
+ extend ActiveSupport::Concern
+
+ included do
+ serialize :timestamps, Array
+
+ # Returns the timestamps the query should be evaluated at.
+ #
+ # In the database, the timestamps are stored as ISO8601 strings.
+ # This method returns the timestamps as array of `Timestamp` objects.
+ #
+ # Timestamps can be absolute (e.g. a certain date and time) or relative
+ # (e.g. three weeks ago). To convert a timestamp to a absolute time,
+ # call `timestamp.to_time`.
+ #
+ def timestamps
+ timestamps = super.collect { |timestamp_string|
+ Timestamp.new(timestamp_string)
+ }
+ return timestamps.any? ? timestamps : [Timestamp.now]
+ end
+
+ def timestamps=(array)
+ super(array.collect { |element| element.respond_to?(:iso8601) ? element.iso8601 : element })
+ end
+ end
+
+end
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
new file mode 100644
index 00000000000..dc9ca0bf685
--- /dev/null
+++ b/app/models/timestamp.rb
@@ -0,0 +1,75 @@
+class Timestamp
+
+ def initialize(arg = Timestamp.now.to_s)
+ if arg.kind_of? String
+ @timestamp_iso8601_string = arg
+ elsif arg.respond_to? :iso8601
+ @timestamp_iso8601_string = arg.iso8601
+ else
+ raise Timestamp::Exception, "Argument type not supported. Please provide a String or anything that responds to :iso8601, e.g. a Time."
+ end
+ end
+
+ def self.parse(iso8601_string)
+ if iso8601_string.start_with? "P" # ISO8601 "Period"
+ ActiveSupport::Duration.parse(iso8601_string)
+ else
+ Time.parse(iso8601_string)
+ end
+ Timestamp.new(iso8601_string)
+ end
+
+ def self.now
+ self.new(ActiveSupport::Duration.build(0).iso8601)
+ end
+
+ def relative?
+ to_s.first == "P" # ISO8601 "Period"
+ end
+
+ def to_s
+ iso8601
+ end
+
+ def to_str
+ to_s
+ end
+
+ def iso8601
+ @timestamp_iso8601_string.to_s
+ end
+
+ def inspect
+ "#<Timestamp \"#{to_s}\">"
+ end
+
+ def to_time
+ if relative?
+ Time.zone.now - to_duration * (to_duration.to_i.positive? ? 1 : -1)
+ else
+ Time.parse(self)
+ end
+ end
+
+ def to_duration
+ if relative?
+ ActiveSupport::Duration.parse(self)
+ else
+ raise Timestamp::Exception, "This timestamp is absolute and cannot be represented as ActiveSupport::Duration."
+ end
+ end
+
+ def as_json
+ to_s
+ end
+
+ def to_json
+ to_s
+ end
+
+ def ==(other_timestamp)
+ self.iso8601 == other_timestamp.iso8601
+ end
+
+ class Exception < StandardError; end
+end
\ No newline at end of file
diff --git a/db/migrate/20221029194419_add_timestamps_to_queries.rb b/db/migrate/20221029194419_add_timestamps_to_queries.rb
new file mode 100644
index 00000000000..8c1b7bff1a0
--- /dev/null
+++ b/db/migrate/20221029194419_add_timestamps_to_queries.rb
@@ -0,0 +1,5 @@
+class AddTimestampsToQueries < ActiveRecord::Migration[7.0]
+ def change
+ add_column :queries, :timestamps, :string
+ end
+end
diff --git a/spec/models/query/timestamps_spec.rb b/spec/models/query/timestamps_spec.rb
new file mode 100644
index 00000000000..1932a876389
--- /dev/null
+++ b/spec/models/query/timestamps_spec.rb
@@ -0,0 +1,99 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Query::Timestamps, type: :model do
+
+ describe "#timestamps" do
+ subject { query.timestamps }
+ describe "after setting timestamps to an array of ISO8601 Strings" do
+ let(:query) { Query.new }
+ before { query.timestamps = ["P-50Y", "2022-10-29T23:01:23Z"] }
+
+ it "returns an Array of Timestamp objects" do
+ expect(subject).to be_kind_of Array
+ expect(subject.map(&:class).uniq).to eq [Timestamp]
+ end
+
+ it "remembers which timestamp encodes a relative time" do
+ expect(subject.first.relative?).to eq true
+ expect(subject.last.relative?).to eq false
+ end
+ end
+
+ describe "after setting timestamps to an array of Times" do
+ let(:query) { Query.new }
+ before { query.timestamps = [50.years.ago, Time.zone.now] }
+
+ it "returns an Array of Timestamp objects" do
+ expect(subject).to be_kind_of Array
+ expect(subject.map(&:class).uniq).to eq [Timestamp]
+ end
+ end
+
+ describe "when not present" do
+ let(:query) { Query.new }
+ before { query.timestamps = [] }
+
+ it "still returns the timestamp corresponding to the present time" do
+ expect(subject).to eq [Timestamp.now]
+ end
+ end
+
+ describe "[persistence] when saving timestamps to a record" do
+ let(:query) { create :query }
+ before do
+ query.timestamps = ["P-50Y", "2022-10-29T23:01:23Z"]
+ query.save!
+ end
+
+ describe "after reloading the record from the database" do
+ let(:reloaded_query) { Query.find(query.id) }
+ subject { reloaded_query.timestamps }
+
+ it "returns an Array of Timestamp objects" do
+ expect(subject).to be_kind_of Array
+ expect(subject.map(&:class).uniq).to eq [Timestamp]
+ end
+
+ it "remembers which timestamp encodes a relative time" do
+ expect(subject.first.relative?).to eq true
+ expect(subject.last.relative?).to eq false
+ end
+
+ it "remembers the timestamp values" do
+ expect(subject.first.iso8601).to eq "P-50Y"
+ expect(subject.last.iso8601).to eq "2022-10-29T23:01:23Z"
+ end
+ end
+
+ end
+ end
+
+end
diff --git a/spec/models/timestamp_spec.rb b/spec/models/timestamp_spec.rb
new file mode 100644
index 00000000000..af1b5aef791
--- /dev/null
+++ b/spec/models/timestamp_spec.rb
@@ -0,0 +1,256 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Timestamp, type: :model do
+
+ describe ".new" do
+ describe "when calling without argument" do
+ subject { Timestamp.new }
+
+ it "returns Timestamp.now" do
+ expect(subject.to_s).to eq Timestamp.now.to_s
+ end
+ end
+
+ describe "when providing an ISO8601 String" do
+ subject { Timestamp.new("PT10S") }
+
+ it "returns a Timestamp" do
+ expect(subject).to be_kind_of Timestamp
+ end
+
+ specify "the argument is retrievable via to_s" do
+ expect(subject.to_s).to eq "PT10S"
+ end
+ end
+
+ describe "when providing a Time" do
+ let(:time) { Time.zone.now }
+ subject { Timestamp.new(time) }
+
+ it "returns an absolute Timestamp representing that time (up to full seconds)" do
+ expect(subject).to be_kind_of Timestamp
+ expect(subject.relative?).to eq false
+ expect(subject.to_time).to eq Time.at(time.to_i)
+ end
+ end
+
+ describe "when providing a non-supported object" do
+ subject { Timestamp.new(:foo) }
+
+ it "raises an error" do
+ expect { subject }.to raise_error Timestamp::Exception
+ end
+ end
+ end
+
+ describe ".now" do
+ subject { Timestamp.now }
+
+ it "returns a Timestamp" do
+ expect(subject).to be_kind_of Timestamp
+ end
+
+ it "returns a relative timestamp" do
+ expect(subject.relative?).to eq true
+ end
+
+ it "corresponds to a duration of 0 seconds (ago)" do
+ expect(subject.to_duration).to eq ActiveSupport::Duration.build(0)
+ end
+ end
+
+ describe ".parse" do
+ describe "when providing a valid ISO8601 duration" do
+ subject { Timestamp.parse("PT10S") }
+
+ it "returns a Timestamp representing a time ago that duration" do
+ expect(subject).to be_kind_of Timestamp
+ expect(subject.to_s).to eq "PT10S"
+ expect(subject.to_duration).to eq ActiveSupport::Duration.build(10)
+ expect(subject.relative?).to eq true
+ end
+ end
+
+ describe "when providing a valid ISO8601 time" do
+ subject { Timestamp.parse("2022-10-29T21:55:58Z") }
+
+ it "returns a Timestamp representing that absolute time" do
+ expect(subject).to be_kind_of Timestamp
+ expect(subject.to_s).to eq "2022-10-29T21:55:58Z"
+ expect(subject.to_time).to eq Time.parse("2022-10-29T21:55:58Z")
+ expect(subject.relative?).to eq false
+ end
+ end
+
+ describe "when providing something invalid" do
+ subject { Timestamp.parse("foo") }
+
+ it "raises an error" do
+ expect { subject }.to raise_error ArgumentError
+ end
+ end
+ end
+
+ describe "#relative?" do
+ subject { timestamp.relative?}
+ describe "for a timestamp representing an absolute time" do
+ let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }
+ it "returns false" do
+ expect(subject).to eq false
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now" do
+ let(:timestamp) { Timestamp.new("PT10S") }
+ it "returns true" do
+ expect(subject).to eq true
+ end
+ end
+ end
+
+ describe "#iso8601" do
+ subject { timestamp.iso8601 }
+ describe "for a timestamp representing an absolute time" do
+ let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }
+ it "returns an ISO8601 String representing that time" do
+ expect(subject).to eq timestamp.to_time.iso8601
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now" do
+ let(:timestamp) { Timestamp.new("PT10S") }
+ it "returns an ISO8601 String representing the duration between that time and now" do
+ expect(subject).to eq "PT10S"
+ expect(subject).to eq ActiveSupport::Duration.build(10).iso8601
+ end
+ end
+ end
+
+ describe "#to_s" do
+ subject { timestamp.to_s }
+ describe "for a timestamp representing an absolute time" do
+ let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }
+ it "returns an ISO8601 String representing that time" do
+ expect(subject).to eq timestamp.to_time.iso8601
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now" do
+ let(:timestamp) { Timestamp.new("PT10S") }
+ it "returns an ISO8601 String representing the duration between that time and now" do
+ expect(subject).to eq "PT10S"
+ expect(subject).to eq ActiveSupport::Duration.build(10).iso8601
+ end
+ end
+ end
+
+ describe "#to_json" do
+ subject { timestamp.to_json }
+ describe "for a timestamp representing an absolute time" do
+ let(:timestamp) { Timestamp.new(Time.zone.now - 1.year) }
+ it "returns an ISO8601 String representing that time" do
+ expect(subject).to eq timestamp.to_time.iso8601
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now" do
+ let(:timestamp) { Timestamp.new("PT10S") }
+ it "returns an ISO8601 String representing the duration between that time and now" do
+ expect(subject).to eq "PT10S"
+ expect(subject).to eq ActiveSupport::Duration.build(10).iso8601
+ end
+ end
+ end
+
+ describe "#to_time" do
+ subject { timestamp.to_time }
+ describe "for a timestamp representing an absolute time" do
+ let(:time) { Time.zone.now - 1.year }
+ let(:timestamp) { Timestamp.new(time) }
+ it "returns a Time representing that time (up to full seconds)" do
+ expect(subject).to be_kind_of Time
+ expect(subject).to eq Time.at(time.to_i)
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now" do
+ Timecop.freeze do
+ let(:timestamp) { Timestamp.new("PT10S") }
+ it "returns a Time converting the relative time to an absolute time (relative to now at evaluation time)" do
+ expect(subject).to be_kind_of Time
+ expect(Time.at(subject.to_i)).to eq Time.at(10.seconds.ago.to_i)
+ end
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now with negative duration" do
+ Timecop.freeze do
+ let(:timestamp) { Timestamp.new("PT-10S") }
+ it "returns a Time converting the relative time to an absolute time (relative to now at evaluation time)" do
+ expect(subject).to be_kind_of Time
+ expect(Time.at(subject.to_i)).to eq Time.at(10.seconds.ago.to_i)
+ end
+ end
+ end
+ end
+
+ describe "#to_duration" do
+ subject { timestamp.to_duration }
+ describe "for a timestamp representing an absolute time" do
+ let(:time) { Time.zone.now - 1.year }
+ let(:timestamp) { Timestamp.new(time) }
+ it "raises an error because the absolute time does not change when the current time progresses and therefore cannot be represented as constant duration" do
+ expect { subject }.to raise_error Timestamp::Exception
+ end
+ end
+ describe "for a timestamp representing a point in time relative to now" do
+ Timecop.freeze do
+ let(:timestamp) { Timestamp.new("PT10S") }
+ it "returns an ActiveSupport::Duration corresponding to the duration between the timestamp and now" do
+ expect(subject).to be_kind_of ActiveSupport::Duration
+ expect(subject).to eq ActiveSupport::Duration.build(10)
+ end
+ end
+ end
+ end
+
+ describe "passing a Timestamp to a where clause" do
+ subject { Query.where("updated_at < ?", timestamp) }
+ let(:timestamp) { Timestamp.new("PT10S") }
+
+ it "raises an error because the query interface requires a Time type" do
+ expect { subject }.to raise_error TypeError
+ end
+
+ describe "when converting the timestamp to_time" do
+ subject { Query.where("updated_at < ?", timestamp.to_time) }
+
+ it "raises no error" do
+ expect { subject }.not_to raise_error TypeError
+ end
+ end
+ end
+
+end
\ No newline at end of file