Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FeedVersion: filter calendar dates #867

Merged
merged 8 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/controllers/api/v1/feed_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def index
@feed_versions = @feed_versions.where(sha1: sha1s)
end

if params[:calendar_coverage_begins_at_or_before].present?
@feed_versions = @feed_versions.where_calendar_coverage_begins_at_or_before(
params[:calendar_coverage_begins_at_or_before]
)
end

if params[:calendar_coverage_begins_at_or_after].present?
@feed_versions = @feed_versions.where_calendar_coverage_begins_at_or_after(
params[:calendar_coverage_begins_at_or_after]
)
end

if params[:calendar_coverage_includes].present?
@feed_versions = @feed_versions.where_calendar_coverage_includes(
params[:calendar_coverage_includes]
)
end

if params[:feed_onestop_id].present?
feed_onestop_ids = params[:feed_onestop_id].split(',')
@feed_versions = @feed_versions.where(feed: Feed.where(onestop_id: feed_onestop_ids))
Expand Down
20 changes: 19 additions & 1 deletion app/models/feed_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#
# Indexes
#
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_earliest_calendar_date (earliest_calendar_date)
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_latest_calendar_date (latest_calendar_date)
#

class FeedVersion < ActiveRecord::Base
Expand All @@ -50,6 +52,22 @@ class FeedVersion < ActiveRecord::Base
joins('INNER JOIN current_feeds ON feed_versions.id = current_feeds.active_feed_version_id')
}

scope :where_calendar_coverage_begins_at_or_before, -> (date) {
date = date.is_a?(Date) ? date : Date.parse(date)
where('earliest_calendar_date <= ?', date)
}

scope :where_calendar_coverage_begins_at_or_after, -> (date) {
date = date.is_a?(Date) ? date : Date.parse(date)
where('earliest_calendar_date >= ?', date)
}

scope :where_calendar_coverage_includes, -> (date) {
date = date.is_a?(Date) ? date : Date.parse(date)
where('earliest_calendar_date <= ?', date)
.where('latest_calendar_date >= ?', date)
}

def succeeded(timestamp)
self.update(imported_at: timestamp)
self.feed.update(last_imported_at: self.imported_at)
Expand Down
4 changes: 3 additions & 1 deletion app/serializers/feed_version_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#
# Indexes
#
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_earliest_calendar_date (earliest_calendar_date)
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_latest_calendar_date (latest_calendar_date)
#

class FeedVersionSerializer < ApplicationSerializer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddCalendarIndexesToFeedVersions < ActiveRecord::Migration
def change
add_index :feed_versions, :earliest_calendar_date
add_index :feed_versions, :latest_calendar_date
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161103231227) do
ActiveRecord::Schema.define(version: 20161129205145) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -304,6 +304,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "feed_version_id"
t.string "gtfs_id"
end

add_index "entities_imported_from_feed", ["entity_type", "entity_id"], name: "index_entities_imported_from_feed_on_entity_type_and_entity_id", using: :btree
Expand Down Expand Up @@ -365,7 +366,9 @@
t.string "md5_raw"
end

add_index "feed_versions", ["earliest_calendar_date"], name: "index_feed_versions_on_earliest_calendar_date", using: :btree
add_index "feed_versions", ["feed_type", "feed_id"], name: "index_feed_versions_on_feed_type_and_feed_id", using: :btree
add_index "feed_versions", ["latest_calendar_date"], name: "index_feed_versions_on_latest_calendar_date", using: :btree

create_table "issues", force: :cascade do |t|
t.integer "created_by_changeset_id"
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/api/v1/feed_versions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@
})
end

it 'calendar_coverage_begins_at_or_before' do
fv1 = create(:feed_version, earliest_calendar_date: '2016-01-01', latest_calendar_date: '2017-01-01')
fv2 = create(:feed_version, earliest_calendar_date: '2016-02-01', latest_calendar_date: '2017-02-01')
get :index, calendar_coverage_begins_at_or_before: '2016-01-01'
expect_json({feed_versions: -> (feed_versions) {
expect(feed_versions.map { |fv| fv[:sha1]}).to match_array([fv1.sha1])
}})
end

it 'calendar_coverage_begins_at_or_after' do
fv1 = create(:feed_version, earliest_calendar_date: '2016-01-01', latest_calendar_date: '2017-01-01')
fv2 = create(:feed_version, earliest_calendar_date: '2016-02-01', latest_calendar_date: '2017-02-01')
get :index, calendar_coverage_begins_at_or_after: '2016-02-01'
expect_json({feed_versions: -> (feed_versions) {
expect(feed_versions.map { |fv| fv[:sha1]}).to match_array([fv2.sha1])
}})
end

it 'calendar_coverage_includes' do
fv1 = create(:feed_version, earliest_calendar_date: '2016-01-01', latest_calendar_date: '2017-01-01')
fv2 = create(:feed_version, earliest_calendar_date: '2016-02-01', latest_calendar_date: '2017-02-01')
get :index, calendar_coverage_includes: '2017-01-15'
expect_json({feed_versions: -> (feed_versions) {
expect(feed_versions.map { |fv| fv[:sha1]}).to match_array([fv2.sha1])
}})
end

it 'filters by SHA1 hash' do
create_list(:feed_version, 2)
sha1 = FeedVersion.first.sha1
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/feed_version_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#
# Indexes
#
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_earliest_calendar_date (earliest_calendar_date)
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_latest_calendar_date (latest_calendar_date)
#

FactoryGirl.define do
Expand Down
48 changes: 47 additions & 1 deletion spec/models/feed_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,56 @@
#
# Indexes
#
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_earliest_calendar_date (earliest_calendar_date)
# index_feed_versions_on_feed_type_and_feed_id (feed_type,feed_id)
# index_feed_versions_on_latest_calendar_date (latest_calendar_date)
#

describe FeedVersion do
context 'calendar scopes' do
before(:each) do
@fv1 = create(:feed_version, earliest_calendar_date: '2016-01-01', latest_calendar_date: '2017-01-01')
@fv2 = create(:feed_version, earliest_calendar_date: '2016-02-01', latest_calendar_date: '2017-02-01')
@fv3 = create(:feed_version, earliest_calendar_date: '2016-03-01', latest_calendar_date: '2017-03-01')
end

context '.where_calendar_coverage_begins_at_or_before' do
it 'finds FeedVersions with coverage before a date' do
expect(FeedVersion.where_calendar_coverage_begins_at_or_before('2016-04-15')).to match_array([@fv1, @fv2, @fv3])
end
it 'finds FeedVersions with coverage on or before a date' do
expect(FeedVersion.where_calendar_coverage_begins_at_or_before('2016-02-01')).to match_array([@fv1, @fv2])
end
it 'finds FeedVersions with coverage on a date' do
expect(FeedVersion.where_calendar_coverage_begins_at_or_before('2016-01-01')).to match_array([@fv1])
end
end

context '.where_calendar_coverage_begins_at_or_after' do
it 'finds FeedVersions with coverage after a date' do
expect(FeedVersion.where_calendar_coverage_begins_at_or_after('2015-12-01')).to match_array([@fv1, @fv2, @fv3])
end
it 'finds FeedVersions with coverage on or after a date' do
expect(FeedVersion.where_calendar_coverage_begins_at_or_after('2016-02-01')).to match_array([@fv2, @fv3])
end
it 'finds FeedVersions with coverage on a date' do
expect(FeedVersion.where_calendar_coverage_begins_at_or_after('2016-03-01')).to match_array([@fv3])
end
end

context '.where_calendar_coverage_includes' do
it 'finds FeedVersions with coverage including a date' do
expect(FeedVersion.where_calendar_coverage_includes('2016-04-01')).to match_array([@fv1, @fv2, @fv3])
end
it 'finds FeedVersions with coverage including, inclusive' do
expect(FeedVersion.where_calendar_coverage_includes('2016-02-01')).to match_array([@fv1, @fv2])
end
it 'excludes FeedVersions outside coverage range' do
expect(FeedVersion.where_calendar_coverage_includes('2017-01-15')).to match_array([@fv2, @fv3])
end
end
end

context '#compute_and_set_hashes' do
it 'computes file hashes' do
feed_version = create(:feed_version_bart)
Expand Down