Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a Point model and code to import/cache data, tidy up use of tidy_…

…text and include updated database schema
  • Loading branch information...
commit 04439d90022dac366bdce500ea6f8267813bc2e8 1 parent e4b661a
@poggs authored
View
1  .gitignore
@@ -3,4 +3,3 @@ db/*.sqlite3
log/*.log
tmp/
config/database.yml
-data/static/*
View
19 LICENCE
@@ -0,0 +1,19 @@
+This code includes additional data licenced as follows:
+
+
+Location data
+-------------
+
+Filename:
+ data/static/locations.csv
+
+Author:
+ Tom Cairns of www.railmiles.org
+
+Licence:
+ CC BY-SA 3.0
+
+Comments:
+ Tom has added latitude and longitude data for all known STANOXes. This
+ data is under continual development, and will be kept up-to-date for use
+ with TSDBExplorer.
View
6 app/helpers/application_helper.rb
@@ -169,16 +169,16 @@ def decode_tiploc(obj)
decoded_tiploc = nil
if obj.is_a? Tiploc
- decoded_tiploc = tidy_text((obj.tps_description.blank? || obj.tps_description.nil?) ? obj.tiploc_code : obj.tps_description)
+ decoded_tiploc = obj.tps_description
elsif (obj.is_a? Location) || (obj.is_a? DailyScheduleLocation)
decoded_tiploc = $REDIS.hget('TIPLOC:' + obj.tiploc_code, 'description')
if decoded_tiploc.nil?
logger.warn "ApplicationHelper::decode_tiploc() failed to find #{obj.tiploc_code}"
- decoded_tiploc = obj.tiploc.nil? ? obj.tiploc_code : obj.tiploc.tps_description
+ decoded_tiploc = obj.tiploc.tps_description unless obj.tiploc.nil?
end
end
- return decoded_tiploc
+ return decoded_tiploc.nil? ? obj.tiploc_code : tidy_text(decoded_tiploc)
end
View
4 app/helpers/schedule_helper.rb
@@ -437,10 +437,10 @@ def show_location_name(loc, type)
tiploc = loc.tiploc_code
else
if type == :from
- text = "<strong>" + tidy_text(decode_tiploc(loc.basic_schedule.origin)) + "</strong>"
+ text = "<strong>" + decode_tiploc(loc.basic_schedule.origin) + "</strong>"
tiploc = loc.basic_schedule.origin.tiploc_code
else
- text = "<strong>" + tidy_text(decode_tiploc(loc.basic_schedule.terminate)) + "</strong>"
+ text = "<strong>" + decode_tiploc(loc.basic_schedule.terminate) + "</strong>"
tiploc = loc.basic_schedule.terminate.tiploc_code
end
end
View
26 app/models/point.rb
@@ -0,0 +1,26 @@
+#
+# This file is part of TSDBExplorer.
+#
+# TSDBExplorer 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 3 of the License, or (at your
+# option) any later version.
+#
+# TSDBExplorer 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 TSDBExplorer. If not, see <http://www.gnu.org/licenses/>.
+#
+# $Id$
+#
+
+class Point < ActiveRecord::Base
+
+ attr_accessible :full_name, :short_name, :stanox, :stanme, :tiploc, :latitude, :longitude
+
+ validates_presence_of :full_name, :stanox, :stanme, :tiploc
+
+end
View
2  app/views/common/_schedule.erb
@@ -26,7 +26,7 @@
<% if advanced_mode? %>
<td><%= s.train_identity %></td>
<% end %>
- <td><%= s.origin.departure %> <%= tidy_text(decode_tiploc(s.origin)) %> to <%= tidy_text(decode_tiploc(s.terminate)) %></td>
+ <td><%= s.origin.departure %> <%= decode_tiploc(s.origin) %> to <%= decode_tiploc(s.terminate) %></td>
<% if advanced_mode? %>
<td><%= s.power_type %><%= s.speed %></td>
<% end %>
View
4 app/views/common/_schedule_instance.erb
@@ -1,6 +1,6 @@
<div id="service_detail">
<div id="mode"><%= mode_icon_for(@schedule.status) %></div>
- <div id="headline"><%= time_only(@schedule.origin.public_departure) || @schedule.origin.departure %> <%= tidy_text(decode_tiploc(@schedule.origin)) %> to <%= tidy_text(decode_tiploc(@schedule.terminate)) %></div>
+ <div id="headline"><%= time_only(@schedule.origin.public_departure) || @schedule.origin.departure %> <%= decode_tiploc(@schedule.origin) %> to <%= decode_tiploc(@schedule.terminate) %></div>
<% unless @schedule.atoc_code == 'ZZ' %>
<div id="operator"><%= display_toc(@schedule.atoc_code) %></div>
<% end %>
@@ -14,7 +14,7 @@
<% if @schedule.departed_origin? %>
<p>Showing actual running data from <%= @schedule.runs_on %></p>
<% else %>
- <p>Real-time information will be available once the train has left <%= tidy_text(decode_tiploc(@schedule.origin)) %>.</p>
+ <p>Real-time information will be available once the train has left <%= decode_tiploc(@schedule.origin) %>.</p>
<% end %>
<% if @schedule.cancelled? %>
View
8 app/views/common/_schedule_point.erb
@@ -3,7 +3,7 @@
<% if advanced_mode? %>
<tr>
- <td><%= tidy_text(decode_tiploc(schedule_point)) %></td>
+ <td><%= decode_tiploc(schedule_point) %></td>
<td style="text-align: center;"><%= schedule_point.platform %></td>
<td style="text-align: center;"><%= schedule_point.arrival %></td>
<td style="text-align: center;"><%= schedule_point.public_arrival %></td>
@@ -16,7 +16,7 @@
<% elsif schedule_point.is_public? %>
<tr>
- <td><%= tidy_text(decode_tiploc(schedule_point)) %></td>
+ <td><%= decode_tiploc(schedule_point) %></td>
<td style="text-align: center;"><%= schedule_point.platform %></td>
<td style="text-align: center;"><%= schedule_point.public_arrival %></td>
<td style="text-align: center;"><%= schedule_point.public_departure %></td>
@@ -29,7 +29,7 @@
<% if advanced_mode? %>
<tr>
- <td><%= tidy_text(decode_tiploc(schedule_point)) %></td>
+ <td><%= decode_tiploc(schedule_point) %></td>
<td style="text-align: center;"><%= schedule_point.platform %></td>
<td style="text-align: center;"><%= schedule_point.actual_platform %></td>
<td style="text-align: center;"><%= time_only(schedule_point.arrival) %></td>
@@ -69,7 +69,7 @@
<% if schedule_point.is_public? %>
<tr>
- <td><%= tidy_text(decode_tiploc(schedule_point)) %></td>
+ <td><%= decode_tiploc(schedule_point) %></td>
<td style="text-align: center;"><%= schedule_point.platform %></td>
<td style="text-align: center;">
<% unless schedule_point.activity_tb == true || schedule_point.activity_u == true %>
View
2  app/views/schedule/_advanced_mode.html.erb
@@ -13,7 +13,7 @@
<tbody>
<% @schedule.locations.each do |l| %>
<%= content_tag('tr', '', { :style => l.pass.nil? ? nil : 'background: #eee;' }) do %>
- <td><%= tidy_text(decode_tiploc(l)) %><% if advanced_mode? %>&nbsp;<%= raw format_activities(l) %><% end %></td>
+ <td><%= decode_tiploc(l) %><% if advanced_mode? %>&nbsp;<%= raw format_activities(l) %><% end %></td>
<td><%= l.path %></td>
<td><%= l.platform %></td>
<td><%= l.line %></td>
View
2  app/views/schedule/_normal_mode.html.erb
@@ -10,7 +10,7 @@
<% @schedule.locations.each do |l| %>
<% if l.is_public? %>
<tr>
- <td style="text-align: left;"><%= tidy_text(decode_tiploc(l)) %>&nbsp;<%= raw format_activities(l) %></td>
+ <td style="text-align: left;"><%= decode_tiploc(l) %>&nbsp;<%= raw format_activities(l) %></td>
<td><%=raw l.platform || "&nbsp;" %></td>
<td><%= l.public_arrival %></td>
<td><%= l.public_departure %></td>
View
2  app/views/schedule/_schedule_headline.html.erb
@@ -4,7 +4,7 @@
<% if mode == 'schedule' %>
<%= schedule.data_source %> Schedule <%= schedule.train_uid %>
<% else %>
- <% if advanced_mode? %><span style="train_identity"><%= @schedule.train_identity %></span><% end %> <%= raw time_only(schedule.origin.public_departure) || tidy_wtt_time(schedule.origin.departure) %> <%= tidy_text(decode_tiploc(schedule.origin)) %> to <%= tidy_text(decode_tiploc(schedule.terminate)) %>
+ <% if advanced_mode? %><span style="train_identity"><%= @schedule.train_identity %></span><% end %> <%= raw time_only(schedule.origin.public_departure) || tidy_wtt_time(schedule.origin.departure) %> <%= decode_tiploc(schedule.origin) %> to <%= decode_tiploc(schedule.terminate) %>
<% end %>
</div>
<div class="subtitle"><%= display_operator(schedule.atoc_code) %></div>
View
6 app/views/schedule/_schedule_locations_realtime.html.erb
@@ -2,7 +2,7 @@
<% if @as_run.departed_origin? %>
Real-time information is available for this train, and is shown below
<% else %>
- Real-time information will be shown for this train after it has departed <%= tidy_text(decode_tiploc(@as_run.origin)) %>
+ Real-time information will be shown for this train after it has departed <%= decode_tiploc(@as_run.origin) %>
<% end %>
</div>
@@ -27,7 +27,7 @@
<tbody>
<% @as_run.locations.each do |l| %>
<tr>
- <td><%= tidy_text(decode_tiploc(l)) %></td>
+ <td><%= decode_tiploc(l) %></td>
<td><%= l.platform %></td>
<td><%= l.line %></td>
<td><%= l.path %></td>
@@ -55,7 +55,7 @@
<% @as_run.locations.each do |l| %>
<% if l.is_public? %>
<tr>
- <td><%= tidy_text(decode_tiploc(l)) %></td>
+ <td><%= decode_tiploc(l) %></td>
<td style="text-align: center;"><%=raw l.platform || "&nbsp;" %></td>
<td><%= format_time(l, 'arrival', :public) %></td>
<td><%= format_time(l, 'departure', :public) %></td>
View
2  app/views/schedule/search.html.erb
@@ -30,7 +30,7 @@
<% end %>
</td>
<td>
- <strong><%= raw format_location_time(s.origin, :departure) %> <%= tidy_text(decode_tiploc(s.origin)) %> to <%= tidy_text(decode_tiploc(s.terminate)) %></strong>
+ <strong><%= raw format_location_time(s.origin, :departure) %> <%= decode_tiploc(s.origin) %> to <%= decode_tiploc(s.terminate) %></strong>
<br />
Runs <%= run_details(s) %>
</td>
View
39 config/tsdbexplorer.yml
@@ -0,0 +1,39 @@
+#
+# This file is part of TSDBExplorer.
+#
+# TSDBExplorer 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 3 of the License, or (at your
+# option) any later version.
+#
+# TSDBExplorer 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 TSDBExplorer. If not, see <http://www.gnu.org/licenses/>.
+#
+# $Id$
+#
+
+AMQP_SERVER:
+ hostname: 'moorgate.lon2.poggs.net'
+ username: 'tdnet.dev'
+ password: 'test123'
+ vhost: 'tdnet'
+
+REDIS_SERVER:
+ hostname: 'localhost'
+ port: '6379'
+
+ANALYTICS:
+ tracking_id: 'UA-28424358-1'
+ domain_name: 'opentraintimes.com'
+
+DATA:
+ path: 'import'
+
+RESTRICTIONS:
+ category: ['QQ']
+ toc: ['QQ']
View
8,726 data/static/locations.csv
8,726 additions, 0 deletions not shown
View
35 db/migrate/20120507094627_create_points.rb
@@ -0,0 +1,35 @@
+#
+# This file is part of TSDBExplorer.
+#
+# TSDBExplorer 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 3 of the License, or (at your
+# option) any later version.
+#
+# TSDBExplorer 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 TSDBExplorer. If not, see <http://www.gnu.org/licenses/>.
+#
+# $Id$
+#
+
+class CreatePoints < ActiveRecord::Migration
+
+ def change
+ create_table :points do |t|
+ t.text :full_name
+ t.text :short_name
+ t.integer :stanox
+ t.string :stanme, :limit => 9
+ t.string :tiploc, :limit => 7
+ t.float :latitude
+ t.float :longitude
+ t.timestamps
+ end
+ end
+
+end
View
15 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120323163313) do
+ActiveRecord::Schema.define(:version => 20120507094627) do
create_table "associations", :force => true do |t|
t.string "main_train_uid"
@@ -307,9 +307,22 @@
add_index "locations", ["arrival"], :name => "index_locations_on_arrival"
add_index "locations", ["basic_schedule_uuid"], :name => "index_locations_on_basic_schedule_uuid"
add_index "locations", ["departure"], :name => "index_locations_on_departure"
+ add_index "locations", ["location_type"], :name => "index_locations_on_location_type"
add_index "locations", ["pass"], :name => "index_locations_on_pass"
add_index "locations", ["tiploc_code"], :name => "index_locations_on_tiploc_code"
+ create_table "points", :force => true do |t|
+ t.text "full_name"
+ t.text "short_name"
+ t.integer "stanox"
+ t.string "stanme", :limit => 9
+ t.string "tiploc", :limit => 7
+ t.float "latitude"
+ t.float "longitude"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "queued_messages", :force => true do |t|
t.string "queue_name"
t.string "message"
View
1  lib/tsdbexplorer.rb
@@ -22,6 +22,7 @@
require 'tsdbexplorer/geography.rb'
require 'tsdbexplorer/realtime.rb'
require 'tsdbexplorer/rsp.rb'
+require 'tsdbexplorer/import.rb'
module TSDBExplorer
View
69 lib/tsdbexplorer/import.rb
@@ -0,0 +1,69 @@
+#
+# This file is part of TSDBExplorer.
+#
+# TSDBExplorer 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 3 of the License, or (at your
+# option) any later version.
+#
+# TSDBExplorer 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 TSDBExplorer. If not, see <http://www.gnu.org/licenses/>.
+#
+# $Id$
+#
+
+module TSDBExplorer
+
+ module Import
+
+ # Import the location data set
+
+ def Import.locations(filename='data/static/locations.csv')
+
+ raise "Location data not found at #{filename}" unless File.readable?(filename)
+
+ # Wrap everything in a Redis transaction
+
+ $REDIS.multi do
+
+ old_tiplocs = $REDIS.keys("TIPLOC:*")
+
+ unless old_tiplocs.nil?
+ old_tiplocs.each do |t|
+ $REDIS.del(t)
+ end
+ end
+
+
+ old_stanox = $REDIS.keys("STANOX:*")
+
+ unless old_stanox.nil?
+ old_stanox.each do |s|
+ $REDIS.del(s)
+ end
+ end
+
+ location_records = Array.new
+
+ CSV.foreach(filename, { :col_sep => ";" }) do |location|
+ location_records << Point.new(:full_name => location[0], :short_name => location[1], :stanox => location[2], :stanme => location[3], :tiploc => location[5], :latitude => location[6], :longitude => location[7])
+ $REDIS.hset("TIPLOC:#{location[5]}", "full_name", location[0])
+ $REDIS.hset("TIPLOC:#{location[5]}", "short_name", location[0])
+ $REDIS.hset("TIPLOC:#{location[5]}", "stanme", location[3])
+ $REDIS.hset("STANOX:#{location[2]}", "tiploc", location[5])
+ end
+
+ location_result = Point.import(location_records)
+
+ end
+
+ end
+
+ end
+
+end
View
2  spec/helpers/application_helper_spec.rb
@@ -66,7 +66,7 @@
it "should return the description for a Location object referencing a known TIPLOC" do
TSDBExplorer::CIF::process_cif_file('test/fixtures/cif/record_bs_new_fullextract.cif')
location = Location.where(:tiploc_code => 'EUSTON').first
- decode_tiploc(location).should eql('LONDON EUSTON')
+ decode_tiploc(location).should eql('London Euston')
end
it "should return the Tiploc code for a Location object referring to an unknown TIPLOC" do
View
34 spec/models/point_spec.rb
@@ -0,0 +1,34 @@
+#
+# This file is part of TSDBExplorer.
+#
+# TSDBExplorer 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 3 of the License, or (at your
+# option) any later version.
+#
+# TSDBExplorer 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 TSDBExplorer. If not, see <http://www.gnu.org/licenses/>.
+#
+# $Id$
+#
+
+require 'spec_helper'
+
+describe Point do
+
+ it "should not be valid when first created" do
+ p = Point.new
+ p.should_not be_valid
+ end
+
+ it "should be valid with the minimum fields populated" do
+ p = Point.create(:full_name => 'Foo', :stanox => '00000', :stanme => 'FOOBARBAZ', :tiploc => 'FOOBARB')
+ p.should be_valid
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.