Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Add query arg for delay parameter on lines #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MTA_KEY=<<YOUR API KEY>>
44 changes: 43 additions & 1 deletion app/javascript/packs/goodserviceApp/components/landingPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import LinePane from "./linePane.jsx";
import StarredPane from "./starredPane.jsx";
import sampleData from "../data/sampleData.js";
import { Parallax, Background } from 'react-parallax';
import qs from 'query-string';
import * as Cookies from 'es-cookie';

const API_URL = '/api/info';
const TEST_DATA = false;

function getQueryStringValue (key) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this if we already import query-string?

return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

// Would write the value of the QueryString-variable called name to the console
console.log(getQueryStringValue("name"));

class LandingPage extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -241,9 +249,43 @@ class LandingPage extends React.Component {
if (TEST_DATA) {
this.setState({ trains: sampleData.routes, lines: sampleData.lines, loading: false });
} else {
let params = qs.parse(location.search);
let use_median = params.median && params.median === 'true';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tiny nitpick, let's keep variable names camelCase.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, don't think we need the first half of the evaluation if we are evaluating with ===?

let bad_service_threshold = parseInt(params.bad_service_threshold) || 2;

let statusFromLines = function(lines) {
for (let boro in lines) {
for (let line of lines[boro]) {
if (line.status == "Delay") {
// Let delays be handled as in Ruby for now
continue;
}

let scheduled = use_median ? line.median_scheduled_headway : line.max_scheduled_headway;
let actual = use_median ? line.median_actual_headway : line.max_actual_headway;
if (!scheduled) {
// Just use the default in the Ruby for now
continue;
}

if (actual - scheduled > bad_service_threshold) {
line.status = "Not Good";
} else {
line.status = "Good Service";
}
}
}
return lines;
}
fetch(API_URL)
.then(response => response.json())
.then(data => this.setState({ trains: data.routes, lines: data.lines, blogPost: data.blog_post, timestamp: data.timestamp, loading: false }))
.then(data => this.setState({
trains: data.routes,
lines: statusFromLines(data.lines),
blogPost: data.blog_post,
timestamp: data.timestamp,
loading: false
}))
}
}

Expand Down
100 changes: 72 additions & 28 deletions app/models/display/line_direction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,26 @@ def delay

def max_actual_headway
@max_actual_headway if @max_actual_headway
times = trips.map { |t|
time = t.upcoming_line_directions[line_direction]
compute_actual_headways
@max_actual_headway
end

# Facilitate M train shuffle (where M train stops for Broadway-Brooklyn line are reverse of J/Z trains)
if !time && t.route_id == "M" && line_direction.line.name == "Broadway (Brooklyn)"
time = t.find_time(last_stop_reverse)
end
time
}
times << trips.first.timestamp if times.size == 1
@max_actual_headway = times.sort.each_cons(2).map { |a,b| (b - a) / 60 }.max
def median_actual_headway
@median_actual_headway if @median_actual_headway
compute_actual_headways
@median_actual_headway
end

def max_scheduled_headway
@max_scheduled_headway if @max_scheduled_headway
return nil if stop_times[line_direction.last_stop].nil?

# Facilitate M train shuffle
if line_direction.line.name == "Broadway (Brooklyn)"
st = stop_times[line_direction.last_stop].select { |st| st.trip.route_internal_id != "M"}
st.concat(stop_times[last_stop_reverse].select { |st| st.trip.route_internal_id == "M"})
else
st = stop_times[line_direction.last_stop]
end
compute_scheduled_headways
@max_scheduled_headway
end

if line_direction.kind_of?(ExpressLineDirection)
st.reject! { |st|
stop_times[line_direction.local_line_direction.last_stop].any? { |local_st|
st.trip_internal_id == local_st.trip_internal_id
}
}
end
times = st.map(&:departure_time)
@max_scheduled_headway = treat_times(times).sort.each_cons(2).map { |a,b| (b - a) / 60 }.max
def median_scheduled_headway
@median_scheduled_headway if @median_scheduled_headway
compute_scheduled_headways
@median_scheduled_headway
end

def headway_discreprency
Expand Down Expand Up @@ -104,5 +90,63 @@ def treat_times(times)
end
times
end

def compute_actual_headways
times = trips.map { |t|
time = t.upcoming_line_directions[line_direction]

# Facilitate M train shuffle (where M train stops for Broadway-Brooklyn line are reverse of J/Z trains)
if !time && t.route_id == "M" && line_direction.line.name == "Broadway (Brooklyn)"
time = t.find_time(last_stop_reverse)
end
time
}
times << trips.first.timestamp if times.size == 1
headways = times.sort.each_cons(2).map { |a, b| (b - a) }
len = headways.size
if len == 0
@median_actual_headway = nil
@max_actual_headway = nil
return
end

@median_actual_headway = ((headways[(len - 1) / 2] + headways[len / 2]) / 2.0) / 60
@max_actual_headway = headways.max / 60
end

def compute_scheduled_headways
if stop_times[line_direction.last_stop].nil?
@median_scheduled_headway = nil
@max_scheduled_headway = nil
return nil
end

# Facilitate M train shuffle
if line_direction.line.name == "Broadway (Brooklyn)"
st = stop_times[line_direction.last_stop].select { |st| st.trip.route_internal_id != "M"}
st.concat(stop_times[last_stop_reverse].select { |st| st.trip.route_internal_id == "M"})
else
st = stop_times[line_direction.last_stop]
end

if line_direction.kind_of?(ExpressLineDirection)
st.reject! { |st|
stop_times[line_direction.local_line_direction.last_stop].any? { |local_st|
st.trip_internal_id == local_st.trip_internal_id
}
}
end
headways = treat_times(st.map(&:departure_time)).sort.each_cons(2).map { |a, b| (b - a) }
len = headways.size
if len == 0
@median_scheduled_headway = nil
@max_scheduled_headway = nil
return
end

@median_scheduled_headway = ((headways[(len - 1) / 2] + headways[len / 2]) / 60) / 2.0
@max_scheduled_headway = headways.max / 60
end

end
end
70 changes: 57 additions & 13 deletions app/models/display/route_line_direction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,60 @@ def delay

def max_actual_headway
@max_actual_headway if @max_actual_headway
compute_actual_headways
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert changes of this file until the feature is implemented for routes as well.

@max_actual_headway
end

def median_actual_headway
@median_actual_headway if @median_actual_headway
compute_actual_headways
@median_actual_headway
end

def max_scheduled_headway
@max_scheduled_headway if @max_scheduled_headway
compute_scheduled_headways
@max_scheduled_headway
end

def median_scheduled_headway
@median_scheduled_headway if @median_scheduled_headway
compute_scheduled_headways
@median_scheduled_headway
end

def headway_discreprency
return nil if trips.empty?
max_actual_headway - max_scheduled_headway if max_scheduled_headway
end

private

attr_accessor :trips, :stop_times, :line_direction, :route_id, :timestamp

def compute_actual_headways
times = trips.map { |t|
t.upcoming_line_directions[line_direction]
}
times << trips.first.timestamp if times.size == 1
@max_actual_headway = times.sort.each_cons(2).map { |a,b| (b - a) / 60 }.max
headways = times.sort.each_cons(2).map { |a, b| (b - a) }
len = headways.size
if len == 0
@max_actual_headway = nil
@median_actual_headway = nil
return
end
@median_actual_headway = ((headways[(len - 1) / 2] + headways[len / 2]) / 2.0) / 60
@max_actual_headway = headways.max / 60
end

def max_scheduled_headway
@max_scheduled_headway if @max_scheduled_headway
return nil if stop_times[line_direction.last_stop].nil?
def compute_scheduled_headways
if stop_times[line_direction.last_stop].nil?
@median_scheduled_headway = nil
@max_scheduled_headway = nil
return nil
end

st = stop_times[line_direction.last_stop].select { |st| st.trip.route_internal_id == route_id}
if line_direction.kind_of?(ExpressLineDirection)
st.reject! { |st|
Expand All @@ -41,18 +85,18 @@ def max_scheduled_headway
}
}
end
times = st.map(&:departure_time)
@max_scheduled_headway = treat_times(times).sort.each_cons(2).map { |a,b| (b - a) / 60 }.max
end
headways = treat_times(st.map(&:departure_time)).sort.each_cons(2).map { |a, b| (b - a) }
len = headways.size
if len == 0
@max_scheduled_headway = nil
@median_scheduled_headway = nil
return nil
end

def headway_discreprency
return nil if trips.empty?
max_actual_headway - max_scheduled_headway if max_scheduled_headway
@max_scheduled_headway = headways.max / 60
@median_scheduled_headway = ((headways[(len - 1) / 2] + headways[len / 2]) / 2.0) / 60
end

private

attr_accessor :trips, :stop_times, :line_direction, :route_id, :timestamp

def treat_times(times)
if (timestamp + 60.minutes).to_date == (timestamp.to_date + 1.day)
Expand Down
2 changes: 2 additions & 0 deletions app/models/schedule_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def self.headway_info(force_refresh: false)
type: ld.type,
max_actual_headway: ld.max_actual_headway,
max_scheduled_headway: ld.max_scheduled_headway,
median_actual_headway: ld.median_actual_headway,
median_scheduled_headway: ld.median_scheduled_headway,
delay: ld.delay,
routes: ld.routes.map { |route|
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"babel-preset-react": "^6.24.1",
"es-cookie": "^1.2.0",
"prop-types": "^15.6.2",
"query-string": "^6.2.0",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-hammerjs": "^1.0.1",
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4942,6 +4942,14 @@ query-string@^4.1.0:
object-assign "^4.1.0"
strict-uri-encode "^1.0.0"

query-string@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/query-string/-/query-string-6.2.0.tgz#468edeb542b7e0538f9f9b1aeb26f034f19c86e1"
integrity sha512-5wupExkIt8RYL4h/FE+WTg3JHk62e6fFPWtAZA9J5IWK1PfTfKkMS93HBUHcFpeYi9KsY5pFbh+ldvEyaz5MyA==
dependencies:
decode-uri-component "^0.2.0"
strict-uri-encode "^2.0.0"

querystring-es3@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73"
Expand Down Expand Up @@ -5831,6 +5839,11 @@ strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"

strict-uri-encode@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546"
integrity sha1-ucczDHBChi9rFC3CdLvMWGbONUY=

string-width@^1.0.1, string-width@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"
Expand Down