-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f25b888
commit a21a3a5
Showing
7 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
DROP TABLE IF EXISTS yellow_monthly_reports CASCADE; | ||
DROP TABLE IF EXISTS fhv_weekly_reports CASCADE; | ||
|
||
CREATE TABLE yellow_monthly_reports ( | ||
month date, | ||
trips_per_day integer, | ||
farebox_per_day integer, | ||
unique_drivers integer, | ||
unique_medallions integer, | ||
medallions_per_day integer, | ||
avg_days_medallions_on_road numeric, | ||
avg_hours_per_day_per_medallion numeric, | ||
avg_days_drivers_on_road numeric, | ||
avg_hours_per_day_per_driver numeric, | ||
avg_minutes_per_trip numeric, | ||
percent_trips_paid_with_credit_card numeric | ||
); | ||
|
||
CREATE UNIQUE INDEX index_yellow_monthly_uniq ON yellow_monthly_reports (month); | ||
|
||
CREATE TABLE fhv_weekly_reports ( | ||
base_number varchar, | ||
wave_number integer, | ||
base_name varchar, | ||
dba varchar, | ||
year integer, | ||
week_number integer, | ||
pickup_start_date date, | ||
pickup_end_date date, | ||
total_dispatched_trips integer, | ||
unique_dispatched_vehicles integer, | ||
dba_category varchar | ||
); | ||
|
||
CREATE UNIQUE INDEX index_fhv_weekly_uniq ON fhv_weekly_reports (base_number, year, week_number); | ||
CREATE INDEX index_fhv_on_category ON fhv_weekly_reports (dba_category, pickup_end_date); | ||
|
||
CREATE VIEW fhv_weekly_reports_intermediate_view AS | ||
SELECT | ||
*, | ||
LAG(unique_dispatched_vehicles, 1) OVER (PARTITION BY base_number ORDER BY year, week_number) AS prev_uniq_vehicles, | ||
LEAD(unique_dispatched_vehicles, 1) OVER (PARTITION BY base_number ORDER BY year, week_number) AS next_uniq_vehicles | ||
FROM fhv_weekly_reports; | ||
|
||
CREATE VIEW fhv_weekly_reports_view AS | ||
SELECT | ||
*, | ||
(unique_dispatched_vehicles > 5 | ||
AND unique_dispatched_vehicles::numeric / prev_uniq_vehicles > 1.4 | ||
AND unique_dispatched_vehicles::numeric / next_uniq_vehicles > 1.4) AS unreliable_vehicles_count | ||
FROM fhv_weekly_reports_intermediate_view; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'rubygems' | ||
require 'csv' | ||
require 'roo' | ||
require 'rest-client' | ||
require 'active_support' | ||
require 'active_support/core_ext' | ||
|
||
# yellow taxi data | ||
yellow_monthly_data_url = "http://www.nyc.gov/html/tlc/downloads/csv/data_reports_monthly_indicators_yellow.csv" | ||
yellow_monthly_data = CSV.parse(RestClient.get(yellow_monthly_data_url)) | ||
yellow_monthly_data.shift | ||
|
||
CSV.open("yellow_monthly_data.csv", "wb") do |csv| | ||
yellow_monthly_data.each do |row| | ||
csv << [ | ||
Date.strptime(row[0], "%B-%y").end_of_month, | ||
row[1].gsub(",", "").to_i, | ||
row[2].gsub(",", "").to_i, | ||
row[3].gsub(",", "").to_i, | ||
row[4].gsub(",", "").to_i, | ||
row[5].gsub(",", "").to_i, | ||
row[6].to_f, | ||
row[7].to_f, | ||
row[8].to_f, | ||
row[9].to_f, | ||
row[10].presence.try(:to_f), | ||
row[11].gsub("%", "").to_f | ||
] | ||
end | ||
end | ||
|
||
# FHV weekly data (includes Uber and Lyft) | ||
fhv_weekly_data_url = "http://www.nyc.gov/html/tlc/downloads/excel/data_reports_weekly_fhv_base.xlsx" | ||
fhv = Roo::Spreadsheet.open(fhv_weekly_data_url) | ||
|
||
header_row_index = (fhv.first_row..fhv.last_row).detect do |i| | ||
fhv.row(i).compact.size > 5 | ||
end | ||
header_row = fhv.row(header_row_index) | ||
|
||
column_indexes_to_select = header_row.map.with_index do |name, idx| | ||
idx if name.present? | ||
end.compact | ||
|
||
data_row_range = (header_row_index + 1)..fhv.last_row | ||
|
||
CSV.open("fhv_weekly_data.csv", "wb") do |csv| | ||
data_row_range.each do |i| | ||
row = fhv.row(i) | ||
|
||
values = column_indexes_to_select.map { |col_idx| row[col_idx] } | ||
|
||
dba_string = values[3] | ||
|
||
values << if dba_string =~ /^uber/i | ||
"uber" | ||
elsif dba_string =~ /^lyft/i | ||
"lyft" | ||
else | ||
"other" | ||
end | ||
|
||
csv << values | ||
end | ||
end | ||
|
||
# create tables and import data | ||
system(%{psql nyc-taxi-data -f create_statistics_tables.sql}) | ||
system(%{cat yellow_monthly_data.csv | psql nyc-taxi-data -c "COPY yellow_monthly_reports FROM stdin CSV;"}) | ||
system(%{cat fhv_weekly_data.csv | psql nyc-taxi-data -c "COPY fhv_weekly_reports FROM stdin CSV;"}) |