Skip to content

Commit

Permalink
Store points from service requests (#79)
Browse files Browse the repository at this point in the history
* Ignore byebug logs

* Store lat-lngs from service requests for #75

* Move instructions for adding real data

* Rely on the raw_data setter to set geometry

* Match raw request

* Update README.md

* Use CircleCI postgis image

* Ensure postgis extension is enabled in migration

* Replace postgres with postgis in production db connection string
  • Loading branch information
hampelm authored and bensheldon committed Aug 20, 2018
1 parent bc6b8e4 commit 345723e
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
PGHOST: localhost
PGUSER: open311status
RAILS_ENV: test
- image: postgres:10
- image: circleci/postgres:10-postgis
environment:
POSTGRES_USER: open311status
POSTGRES_DB: open311status_test
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
.byebug_history

# Ignore foreman environment
.env
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'
ruby File.read(File.join(File.dirname(__FILE__), '.ruby-version')).strip
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem 'activerecord-postgis-adapter'
gem 'autoprefixer-rails'
gem 'aws-sdk-s3'
gem 'babosa'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ GEM
activemodel (= 5.2.1)
activesupport (= 5.2.1)
arel (>= 9.0)
activerecord-postgis-adapter (5.2.1)
activerecord (~> 5.1)
rgeo-activerecord (~> 6.0)
activestorage (5.2.1)
actionpack (= 5.2.1)
activerecord (= 5.2.1)
Expand Down Expand Up @@ -260,6 +263,10 @@ GEM
redcarpet (3.4.0)
request_store (1.4.1)
rack (>= 1.4)
rgeo (1.1.1)
rgeo-activerecord (6.0.0)
activerecord (~> 5.0)
rgeo (~> 1.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.1)
Expand Down Expand Up @@ -356,6 +363,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-postgis-adapter
annotate
autoprefixer-rails
aws-sdk-s3
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,27 @@ bruhl:
jurisdiction: 'bruehl.de'
```
### Loading real data
By default, running `db:setup` will load cities and generate fake service
requests. To load cities, run `rake cities:load`. And to load service requests,
`rake cities:service_requests`

### Application Dependencies
1. Install Ruby with your ruby version manager of choice, like [rbenv](https://github.com/rbenv/rbenv) or [RVM](https://github.com/codeforamerica/howto/blob/master/Ruby.md)
2. Check the ruby version in `.ruby-version` and ensure you have it installed locally e.g. `rbenv install 2.5.1`
3. Install [bundler](https://bundler.io/) (the latest Heroku-compatible version): `gem install bundler`
4. [Install Postgres](https://github.com/codeforamerica/howto/blob/master/PostgreSQL.md). If setting up Postgres.app, you will also need to add the binary to your path. e.g. Add to your `~/.bashrc`:
`export PATH="$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin"`
`export PATH="$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin"`.
5. [Install PostGIS](https://postgis.net/install/), the Postgres geospatial extension, if it's not included in your distribution. Postgres.app comes with postgis.

### Application Setup

1. Install ruby gem dependencies: `bundle install`
2. Install node dependencies: `yarn install`
3. Create the databases and load schema and seeds: `bin/rails db:setup`
4. Run the tests: `bin/rspec`
5. Run the server: `bin/rails server`, and visit the web-browser: [`http://localhost:3000`](http://localhost:3000)
2. Create the databases and load schema and seeds: `bin/rails db:setup`
3. Run the tests: `bin/rspec`
4. Run the server: `bin/rails server`, and visit the web-browser: [`http://localhost:3000`](http://localhost:3000)

#### Migration guide

You may need to run `rake db:gis:setup` to enable PostGIS on your database.
3 changes: 3 additions & 0 deletions app/models/service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: service_requests
#
# id :bigint(8) not null, primary key
# geometry :geography({:srid geometry, 4326
# raw_data :json
# requested_datetime :datetime
# status :string
Expand All @@ -16,6 +17,7 @@
#
# index_service_requests_on_city_id (city_id)
# index_service_requests_on_city_id_and_service_request_id (city_id,service_request_id) UNIQUE
# index_service_requests_on_geometry (geometry) USING gist
# index_service_requests_on_status (status)
#

Expand All @@ -34,6 +36,7 @@ def raw_data=(json)
self[:status] = json['status']
self[:requested_datetime] = DateTime.iso8601(json['requested_datetime']) if json['requested_datetime'].present?
self[:updated_datetime] = DateTime.iso8601(json['updated_datetime']) if json['updated_datetime'].present?
self[:geometry] = "POINT(#{json['long']} #{json['lat']})" if json['lat'].present?
end
rescue => e
Raven.capture_exception e,
Expand Down
3 changes: 2 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defaults: &defaults
adapter: postgresql
adapter: postgis
encoding: unicode
pool: 20
username: <%= ENV['POSTGRES_USERNAME'] || ENV['USER'] %>
Expand All @@ -20,3 +20,4 @@ staging:
production:
database: open311status_production
<<: *defaults
url: <%= ENV.fetch("DATABASE_URL", "").gsub(/postgres/, 'postgis') %>
8 changes: 8 additions & 0 deletions db/migrate/20180819143420_add_geometry_to_service_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddGeometryToServiceRequests < ActiveRecord::Migration[5.2]
def change
enable_extension "postgis"

add_column :service_requests, :geometry, :geometry, geographic: true
add_index :service_requests, :geometry, using: :gist
end
end
3 changes: 3 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "postgis"

create_table "cities", force: :cascade do |t|
t.string "slug"
Expand Down Expand Up @@ -53,8 +54,10 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "city_id"
t.geography "geometry", limit: {:srid=>4326, :type=>"geometry", :geographic=>true}
t.index ["city_id", "service_request_id"], name: "index_service_requests_on_city_id_and_service_request_id", unique: true
t.index ["city_id"], name: "index_service_requests_on_city_id"
t.index ["geometry"], name: "index_service_requests_on_geometry", using: :gist
t.index ["status"], name: "index_service_requests_on_status"
end

Expand Down
4 changes: 4 additions & 0 deletions spec/factories/service_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
service_name { Faker::Commerce.department(2, true) }
description { Faker::Lorem.paragraph(1, false, 5) }
requested_datetime { 10.minutes.ago }
lat { Faker::Address.latitude }
long { Faker::Address.longitude }
end

raw_data do
Expand All @@ -19,6 +21,8 @@
'description' => description,
'status' => status,
'requested_datetime' => requested_datetime.iso8601,
'lat' => lat,
'long' => long,
}
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/models/service_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
it 'extracts :updated_datetime' do
expect(sr.updated_datetime).to eq DateTime.iso8601 json['updated_datetime']
end

it 'extracts a point' do
expect(sr.geometry.lat).to eq json['lat']
expect(sr.geometry.lon).to eq json['long']
end
end

describe '#slug' do
Expand Down

0 comments on commit 345723e

Please sign in to comment.