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

[#46555] Extend Query resource in APIv3 by timestamps

Dombi Attila64c3ad1dcdc7
diff --git a/app/contracts/queries/base_contract.rb b/app/contracts/queries/base_contract.rb
index 98ee35ca516..03fb42596be 100644
--- a/app/contracts/queries/base_contract.rb
+++ b/app/contracts/queries/base_contract.rb
@@ -47,6 +47,7 @@ module Queries
attribute :column_names # => columns
attribute :filters
+ attribute :timestamps
attribute :sort_criteria # => sortBy
attribute :group_by # => groupBy
diff --git a/app/models/query/timestamps.rb b/app/models/query/timestamps.rb
index e1e6cf7f64a..c111d3c6550 100644
--- a/app/models/query/timestamps.rb
+++ b/app/models/query/timestamps.rb
@@ -32,6 +32,9 @@ module Query::Timestamps
included do
serialize :timestamps, Array
+ validates :timestamps, length: { maximum: 2 }
+ validate :timestamps_are_parsable
+
# Returns the timestamps the query should be evaluated at.
#
# In the database, the timestamps are stored as ISO8601 strings.
@@ -57,5 +60,15 @@ module Query::Timestamps
def historic?
timestamps != [Timestamp.now]
end
+
+ private
+
+ def timestamps_are_parsable
+ invalid_timestamps = timestamps.reject(&:valid?)
+
+ if invalid_timestamps.any?
+ errors.add :timestamps, :invalid, values: invalid_timestamps.join(", ")
+ end
+ end
end
end
diff --git a/app/models/timestamp.rb b/app/models/timestamp.rb
index 1ca124177ab..dd08895b232 100644
--- a/app/models/timestamp.rb
+++ b/app/models/timestamp.rb
@@ -140,6 +140,12 @@ class Timestamp
self != Timestamp.now
end
+ def valid?
+ self.class.parse(iso8601)
+ rescue StandardError
+ false
+ end
+
delegate :hash, to: :iso8601
class Exception < StandardError; end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8442830945c..6a82614d2e3 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -807,6 +807,10 @@ en:
sort_criteria:
invalid: "Can't sort by column: %{value}"
format: "%{message}"
+ timestamps:
+ invalid: "Timestamps contain invalid values: %{values}"
+ format: "%{message}"
+ too_long: "Timestamps are too long (maximum is %{count} values)."
group_by_hierarchies_exclusive: "is mutually exclusive with group by '%{group_by}'. You cannot activate both."
filters:
custom_fields:
diff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rb
index ec95bd6b9d8..e9bb47952bc 100644
--- a/lib/api/v3/queries/query_representer.rb
+++ b/lib/api/v3/queries/query_representer.rb
@@ -298,6 +298,9 @@ module API
property :timeline_labels
+ property :timestamps,
+ getter: ->(*) { timestamps.map(&:to_s) }
+
# Visible representation of the results
property :display_representation
diff --git a/lib/api/v3/queries/schemas/query_schema_representer.rb b/lib/api/v3/queries/schemas/query_schema_representer.rb
index bc301fd6e63..f6791a977ad 100644
--- a/lib/api/v3/queries/schemas/query_schema_representer.rb
+++ b/lib/api/v3/queries/schemas/query_schema_representer.rb
@@ -122,6 +122,12 @@ module API
has_default: true,
deprecated: true
+ schema :timestamps,
+ type: '[]Timestamp',
+ required: false,
+ writable: true,
+ has_default: true
+
schema :highlighting_mode,
type: 'String',
required: false,
diff --git a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb b/spec/lib/api/v3/queries/query_representer_parsing_spec.rb
index fe6a87e0b9c..7f441209baa 100644
--- a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb
+++ b/spec/lib/api/v3/queries/query_representer_parsing_spec.rb
@@ -169,4 +169,16 @@ describe API::V3::Queries::QueryRepresenter, 'parsing' do
end
end
end
+
+ describe 'timestamps' do
+ let(:timestamp_params) { [1.week.ago.iso8601, 'P0D'] }
+ let(:request_body) do
+ { 'timestamps' => timestamp_params }
+ end
+
+ it 'sets timestamps' do
+ expect(subject.timestamps)
+ .to eq([1.week.ago.iso8601, 'P0D'])
+ end
+ end
end
diff --git a/spec/lib/api/v3/queries/query_representer_rendering_spec.rb b/spec/lib/api/v3/queries/query_representer_rendering_spec.rb
index 11f378b5033..79fa02df747 100644
--- a/spec/lib/api/v3/queries/query_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/queries/query_representer_rendering_spec.rb
@@ -576,6 +576,10 @@ describe API::V3::Queries::QueryRepresenter do
let(:value) { query.timeline_labels }
end
+ it_behaves_like 'property', :timestamps do
+ let(:value) { query.timestamps }
+ end
+
it_behaves_like 'property', :public do
let(:value) { query.public }
end
@@ -596,16 +600,14 @@ describe API::V3::Queries::QueryRepresenter do
end
end
- describe 'timestamps' do
- it_behaves_like 'has UTC ISO 8601 date and time' do
- let(:date) { query.created_at }
- let(:json_path) { 'createdAt' }
- end
+ it_behaves_like 'has UTC ISO 8601 date and time' do
+ let(:date) { query.created_at }
+ let(:json_path) { 'createdAt' }
+ end
- it_behaves_like 'has UTC ISO 8601 date and time' do
- let(:date) { query.updated_at }
- let(:json_path) { 'updatedAt' }
- end
+ it_behaves_like 'has UTC ISO 8601 date and time' do
+ let(:date) { query.updated_at }
+ let(:json_path) { 'updatedAt' }
end
describe 'highlighting' do
diff --git a/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb b/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb
index 01ec1bc6590..32709e0cc9e 100644
--- a/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb
+++ b/spec/lib/api/v3/queries/schemas/query_schema_representer_spec.rb
@@ -274,6 +274,18 @@ describe API::V3::Queries::Schemas::QuerySchemaRepresenter do
end
end
+ describe 'timestamps' do
+ let(:path) { 'timestamps' }
+
+ it_behaves_like 'has basic schema properties' do
+ let(:type) { '[]Timestamp' }
+ let(:name) { Query.human_attribute_name('timestamps') }
+ let(:required) { false }
+ let(:writable) { true }
+ let(:has_default) { true }
+ end
+ end
+
describe 'show hierarchies' do
let(:path) { 'showHierarchies' }
diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rb
index ebf8be19c19..29712e574fa 100644
--- a/spec/requests/api/v3/queries/create_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/create_form_api_spec.rb
@@ -326,6 +326,7 @@ describe "POST /api/v3/queries/form" do
describe 'with all parameters given' do
let(:status) { create(:status) }
+ let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
let(:parameters) do
{
@@ -333,6 +334,7 @@ describe "POST /api/v3/queries/form" do
public: true,
sums: true,
showHierarchies: false,
+ timestamps:,
filters: [
{
name: "Status",
@@ -449,6 +451,10 @@ describe "POST /api/v3/queries/form" do
expect(form.dig("_embedded", "payload", "_links", "sortBy")).to eq sort_by
end
+ it 'has the timestamps set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq timestamps
+ end
+
context "with the project referred to by its identifier" do
let(:override_params) do
links = parameters[:_links]
@@ -549,6 +555,41 @@ describe "POST /api/v3/queries/form" do
end
end
+ context 'with invalid timestamps' do
+ context 'when one timestamp cannot be parsed' do
+ let(:override_params) do
+ { timestamps: ['invalid', 'P0D'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: invalid"
+ end
+ end
+
+ context 'when both timestamps cannot be parsed' do
+ let(:override_params) do
+ { timestamps: ['invalid', 'invalid2'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: invalid, invalid2"
+ end
+ end
+
+ context 'when there are too many values' do
+ let(:override_params) do
+ { timestamps: [2.weeks.ago.iso8601, 1.week.ago.iso8601, "P0D"] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps are too long (maximum is 2 values)."
+ end
+ end
+ end
+
context "with an unauthorized user trying to set the query public" do
let(:user) { create(:user) }
diff --git a/spec/requests/api/v3/queries/create_query_spec.rb b/spec/requests/api/v3/queries/create_query_spec.rb
index de657ea59b7..a5bb888b6db 100644
--- a/spec/requests/api/v3/queries/create_query_spec.rb
+++ b/spec/requests/api/v3/queries/create_query_spec.rb
@@ -29,14 +29,16 @@
require 'spec_helper'
describe "POST /api/v3/queries" do
- let(:user) { create(:admin) }
- let(:status) { create(:status) }
- let(:project) { create(:project) }
+ shared_let(:user) { create(:admin) }
+ shared_let(:status) { create(:status) }
+ shared_let(:project) { create(:project) }
+ shared_let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
let(:default_params) do
{
name: "Dummy Query",
showHierarchies: false,
+ timestamps:,
filters: [
{
name: "Status",
@@ -123,6 +125,7 @@ describe "POST /api/v3/queries" do
expect(query.user).to eq user
expect(query.project).to eq project
+ expect(query.timestamps).to eq(timestamps.map { |t| Timestamp.new(t) })
expect(query.filters.size).to eq 1
filter = query.filters.first
diff --git a/spec/requests/api/v3/queries/update_form_api_spec.rb b/spec/requests/api/v3/queries/update_form_api_spec.rb
index 320ed80f03a..f75605a0da3 100644
--- a/spec/requests/api/v3/queries/update_form_api_spec.rb
+++ b/spec/requests/api/v3/queries/update_form_api_spec.rb
@@ -35,6 +35,7 @@ describe "POST /api/v3/queries/form" do
let(:user) { create(:admin) }
let(:role) { create(:existing_role, permissions:) }
let(:permissions) { %i(view_work_packages manage_public_queries) }
+ let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
let!(:project) { create(:project_with_types, members: { user => role }) }
@@ -315,6 +316,7 @@ describe "POST /api/v3/queries/form" do
name: "Some Query",
public: true,
sums: true,
+ timestamps:,
showHierarchies: false,
filters: [
{
@@ -432,6 +434,10 @@ describe "POST /api/v3/queries/form" do
expect(form.dig("_embedded", "payload", "_links", "sortBy")).to eq sort_by
end
+ it 'has the timestamps set' do
+ expect(form.dig("_embedded", "payload", "timestamps")).to eq timestamps
+ end
+
context "with the project referred to by its identifier" do
let(:override_params) do
links = parameters[:_links]
@@ -516,6 +522,41 @@ describe "POST /api/v3/queries/form" do
end
end
+ context 'with invalid timestamps' do
+ context 'when one timestamp cannot be parsed' do
+ let(:override_params) do
+ { timestamps: ['invalid', 'P0D'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: invalid"
+ end
+ end
+
+ context 'when both timestamps cannot be parsed' do
+ let(:override_params) do
+ { timestamps: ['invalid', 'invalid2'] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps contain invalid values: invalid, invalid2"
+ end
+ end
+
+ context 'when there are too many values' do
+ let(:override_params) do
+ { timestamps: [2.weeks.ago.iso8601, 1.week.ago.iso8601, "P0D"] }
+ end
+
+ it "returns a validation error" do
+ expect(form.dig("_embedded", "validationErrors", "timestamps", "message"))
+ .to eq "Timestamps are too long (maximum is 2 values)."
+ end
+ end
+ end
+
context "with an unauthorized user trying to set the query public" do
let(:user) { create(:user) }
let(:permissions) { [:view_work_packages] }
diff --git a/spec/requests/api/v3/queries/update_query_spec.rb b/spec/requests/api/v3/queries/update_query_spec.rb
index ed416c87c74..e46a2155108 100644
--- a/spec/requests/api/v3/queries/update_query_spec.rb
+++ b/spec/requests/api/v3/queries/update_query_spec.rb
@@ -29,7 +29,11 @@
require 'spec_helper'
describe "PATCH /api/v3/queries/:id" do
- let(:user) { create(:admin) }
+ shared_let(:user) { create(:admin) }
+ shared_let(:status) { create(:status) }
+ shared_let(:project) { create(:project) }
+ shared_let(:timestamps) { [1.week.ago.iso8601, "P0D"] }
+
let!(:query) do
create(
:global_query,
@@ -45,6 +49,7 @@ describe "PATCH /api/v3/queries/:id" do
name: "Dummy Query",
public: true,
showHierarchies: false,
+ timestamps:,
filters: [
{
name: "Status",
@@ -98,8 +103,6 @@ describe "PATCH /api/v3/queries/:id" do
}
}
end
- let(:status) { create(:status) }
- let(:project) { create(:project) }
def json
JSON.parse last_response.body
@@ -137,6 +140,7 @@ describe "PATCH /api/v3/queries/:id" do
expect(query.public).to be true
expect(query.display_sums).to be false
+ expect(query.timestamps).to eq(timestamps.map { |t| Timestamp.new(t) })
expect(query.filters.size).to eq 1
filter = query.filters.first
diff --git a/spec/support/queries/shared_get_individual_query_examples.rb b/spec/support/queries/shared_get_individual_query_examples.rb
index f2066de4079..a7198528058 100644
--- a/spec/support/queries/shared_get_individual_query_examples.rb
+++ b/spec/support/queries/shared_get_individual_query_examples.rb
@@ -96,4 +96,48 @@ shared_examples_for 'GET individual query' do
.at_path('errorIdentifier')
end
end
+
+ context 'when providing valid timestamps' do
+ let(:path) do
+ params = CGI.escape(timestamps)
+ "#{base_path}?timestamps=#{params}"
+ end
+
+ context 'with a range without a work package' do
+ let(:timestamps) do
+ [2.hours.ago.iso8601, 1.hour.ago.iso8601].join(",")
+ end
+
+ it 'uses the provided timestamp' do
+ expect(last_response.body)
+ .to be_json_eql(0.to_json)
+ .at_path('_embedded/results/total')
+ end
+ end
+
+ context 'with a range containing a work package' do
+ let(:timestamps) do
+ [2.hours.ago.iso8601, 'P0D'].join(",")
+ end
+
+ it 'uses the provided timestamp' do
+ expect(last_response.body)
+ .to be_json_eql(1.to_json)
+ .at_path('_embedded/results/total')
+ end
+ end
+ end
+
+ context 'when providing an invalid timestamp' do
+ let(:path) do
+ params = CGI.escape('invalid')
+ "#{base_path}?timestamps=#{params}"
+ end
+
+ it 'returns an error' do
+ expect(last_response.body)
+ .to be_json_eql("urn:openproject-org:api:v3:errors:InvalidQuery".to_json)
+ .at_path('errorIdentifier')
+ end
+ end
end