Skip to content

Commit

Permalink
skip requesting invalid range combos
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate-Wessel committed Sep 18, 2023
1 parent b1c5f15 commit 54ade00
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
19 changes: 19 additions & 0 deletions frontend/src/dateRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ export class DateRange extends Factor {
get endDateFormatted(){
return DateRange.dateFormatted(this.#endDate)
}
daysInRange(daysOptions){ // number of days covered by this dateRange
// TODO this will need to be revisited with the holiday options enabled
if( ! (this.isComplete && daysOptions.isComplete) ){
return undefined
}
// iterate each day in the range
let d = new Date(this.#startDate.valueOf())
let dayCount = 0
while(d < this.#endDate){
let dow = d.getUTCDay()
let isodow = dow == 0 ? 7 : dow
if( daysOptions.hasDay(isodow) ){
dayCount ++
}
// incrememnt, modified in-place
d.setUTCDate(d.getUTCDate() + 1)
}
return dayCount
}
}

function DateRangeElement({dateRange}){
Expand Down
32 changes: 15 additions & 17 deletions frontend/src/days.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { useContext } from 'react'
import { Factor } from './factor.js'
import { DataContext } from './Layout'

const daymap = {
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
7: 'Sunday'
}
const daylist = [
{ iso: 1, js: 1, label: 'Monday' },
{ iso: 2, js: 2, label: 'Tuesday' },
{ iso: 3, js: 3, label: 'Wednesday' },
{ iso: 4, js: 4, label: 'Thursday' },
{ iso: 5, js: 5, label: 'Friday' },
{ iso: 6, js: 6, label: 'Saturday' },
{ iso: 7, js: 0, label: 'Sunday' } // note the different numeric representation
]

export class Days extends Factor {
// initialize with all days included
#days = new Set(Object.keys(daymap).map(n => parseInt(n)))
#days = new Set(daylist.map(d=>d.iso))
constructor(dataContext){
super(dataContext)
}
Expand All @@ -25,10 +25,8 @@ export class Days extends Factor {
return this.#days.size > 0
}
addDay(number){
console.log('add',number)
if(Object.keys(daymap).includes(String(number))){
if( daylist.map(d=>d.iso).includes(parseInt(number)) ){
this.#days.add(parseInt(number))
console.log('it worked')
}
}
removeDay(number){
Expand All @@ -41,9 +39,9 @@ export class Days extends Factor {
if(this.#days.size == 7){
return 'all days of the week'
} else if(this.#days.size > 0){
return Object.entries(daymap).filter(([num,name])=>{
return this.#days.has(parseInt(num))
}).map(([num,name])=>name).join(', ')
return daylist
.filter( ({iso}) => this.#days.has(iso) )
.map( ({label}) => label ).join(', ')
}
return 'no days selected'
}
Expand All @@ -53,7 +51,7 @@ export class Days extends Factor {
function DaysElement({days}){
const { logActivity } = useContext(DataContext)
if(days.isActive){
return Object.entries(daymap).map( ([num,name]) => (
return daylist.map( ({ iso: num, label: name }) => (
<div key={num}>
<input name={name}
type='checkbox'
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/timeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class TimeRange extends Factor {
get endTimeFormatted(){
return TimeRange.timeFormatted(this.#endTime)
}
get hoursInRange(){ // how many hours are in the timeRange?
if(! this.isComplete){ return undefined }
return this.#endTime.getHours() - this.#startTime.getHours()
}
}

function TimeRangeElement({timeRange}){
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/travelTimeQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class TravelTimeQuery {
get dateRange(){ return this.#dateRange }
get days(){ return this.#days }
async fetchData(){
console.log(this.hoursInRange)
if( this.hoursInRange < 1 ){
return this.#travelTime = -999
}
return fetch(`${domain}/${this.URI}`)
.then( response => response.json() )
.then( data => {
Expand All @@ -38,13 +42,17 @@ export class TravelTimeQuery {
get hasData(){
return Boolean(this.#travelTime)
}
get hoursInRange(){ // number of hours covered by query options
return this.timeRange.hoursInRange * this.dateRange.daysInRange(this.days)
}
resultsRecord(type='json'){
const record = {
URI: this.URI,
corridor: `"${this.corridor.name}"`,
timeRange: `"${this.timeRange.name}"`,
dateRange: `"${this.dateRange.name}"`,
daysOfWeek: `"${this.days.name}"`,
hoursInRange: this.hoursInRange,
mean_travel_time_minutes: this.#travelTime
}
if(type=='json'){
Expand All @@ -55,6 +63,6 @@ export class TravelTimeQuery {
return 'invalid type requested'
}
static csvHeader(){
return 'URI,corridor,timeRange,dateRange,daysOfWeek,mean_travel_time_minutes'
return 'URI,corridor,timeRange,dateRange,daysOfWeek,hoursPossible,mean_travel_time_minutes'
}
}

0 comments on commit 54ade00

Please sign in to comment.