Skip to content

Commit

Permalink
Feat/snackbar (#262)
Browse files Browse the repository at this point in the history
* chore: correct path for mount (#128)

* feat: add skåne gtfs feed

* feat: add snackbar

* Delete agency.txt

* fix: autohide snackbar + less info msgs

* chore: 3s timeout before closing

* chore: remove skane data

* feat: black bar instead

* Feat/no line shapes (#266)

* chore: correct path for mount (#128)

* feat: add skåne gtfs feed

* feat: remove lineshapes and calculate them from trips

* fix: remove skane, it's in cache

* fix: remove subscriptions before starting new experiment

* feat: move functionality to new menu (#263)

* chore: do not bust cache on .gitignore

* refactor: cleanup

* feat: add layer toggles to new menu

* refactor: cleanup

* feat: edit experiment modal

* chore: remove unused code

* refactor: moved reset button to new component

* fix: styling

* refactor: move modal to new component

* refactor: naming

* fix: styling

* refactor: cleaning up

* fix: modal styling

* feat: add logo

* feat: show modal with kibana link

* fix: naming

* feat: show experiment id

---------

Co-authored-by: Alexander Czigler <[email protected]>
  • Loading branch information
irony and Alexander Czigler authored Apr 26, 2023
1 parent ea65299 commit 27eabab
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/simulator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ const {
} = require('rxjs/operators')

const { virtualTime } = require('./lib/virtualTime')

const { safeId } = require('./lib/id')
const { read } = require('./config')
const statistics = require('./lib/statistics')
const { info, error } = require('./lib/log')
const { info, error, logStream } = require('./lib/log')
const { haversine, getNrOfPointsBetween } = require('./lib/distance')

const engine = {
Expand Down Expand Up @@ -40,6 +39,7 @@ const engine = {
statistics.collectExperimentMetadata(parameters)

const experiment = {
logStream,
busStops: regions.pipe(
filter((region) => region.stops),
mergeMap((region) => region.stops),
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/lib/dispatch/busDispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const busDispatch = async (buses, trips) => {

const kommunName = trips[0].kommun
info(
`Calling vroom for ${kommunName} with ${vehicles.length} buses and ${shipments.length} trips`
`Finding optimal route in ${kommunName} for ${vehicles.length} buses and ${shipments.length} trips`
)

const result = await plan({
Expand Down
4 changes: 2 additions & 2 deletions packages/simulator/lib/dispatch/dispatchCentral.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
retryWhen,
toArray,
} = require('rxjs/operators')
const { info, error, warn } = require('../log')
const { info, error, warn, debug } = require('../log')
const { clusterPositions } = require('../kmeans')

const dispatch = (cars, bookings) => {
Expand Down Expand Up @@ -52,7 +52,7 @@ const dispatch = (cars, bookings) => {
mergeAll(),
filter(({ bookings }) => bookings.length > 0),
tap(({ car, bookings }) =>
info(
debug(
`Plan ${car.id} (${car.fleet.name}) received ${bookings.length} bookings`
)
),
Expand Down
6 changes: 4 additions & 2 deletions packages/simulator/lib/dispatch/taxiDispatch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const { plan, taxiToVehicle, bookingToShipment } = require('../vroom')
const moment = require('moment')
const { error, debug, write } = require('../log')
const { error, debug, write, info } = require('../log')
const { virtualTime } = require('../virtualTime')

const taxiDispatch = async (taxis, bookings) => {
const vehicles = taxis.map(taxiToVehicle)
const shipments = bookings.map(bookingToShipment) // TODO: concat bookings from existing vehicles with previous assignments
debug('Calling vroom for taxi', vehicles.length, shipments.length)
info(
`Finding optimal route for ${vehicles.length} taxis and ${shipments.length} pickups`
)
write('🚕')
const result = await plan({ shipments, vehicles })
const virtualNow = await virtualTime.getTimeInMillisecondsAsPromise()
Expand Down
8 changes: 8 additions & 0 deletions packages/simulator/lib/log.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chalk = require('chalk')
const { ReplaySubject } = require('rxjs')

// eslint-disable-next-line no-undef
const LOG_LEVEL = process.env.LOG_LEVEL || 'info'
Expand All @@ -9,6 +10,8 @@ const logLevelIsAtLeastInfo =
const logLevelIsAtLeastWarn =
LOG_LEVEL.toUpperCase() === 'WARN' || logLevelIsAtLeastInfo

const logStream = new ReplaySubject(10)

const print = (logFn, titleFn, messageFn, title, message, data, ...rest) => {
if (data) {
logFn(
Expand All @@ -23,6 +26,7 @@ const print = (logFn, titleFn, messageFn, title, message, data, ...rest) => {
}

module.exports = {
logStream,
debug: (message, data, ...rest) => {
if (logLevelIsAtLeastDebug) {
print(
Expand All @@ -48,6 +52,10 @@ module.exports = {
)
},
info: (message, data, ...rest) => {
logStream.next(
message + ' ' + [data, ...rest].map((x) => JSON.stringify(x)).join(' ')
)

if (logLevelIsAtLeastInfo) {
print(
console.log,
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/lib/queueSubject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Subject, mergeMap, catchError, from } = require('rxjs')
const { debug, error } = require('./log')

const API_CALL_LIMIT = 5
const API_CALL_LIMIT = 3

const queueSubject = new Subject()

Expand Down
1 change: 0 additions & 1 deletion packages/simulator/lib/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ class Region {
this.taxis.pipe(
scan((acc, taxi) => acc.push(taxi) && acc, []),
debounceTime(1000),
tap((cars) => info('region taxis', cars.length)),
filter((taxis) => taxis.length > 0),
mergeMap((taxis) =>
merge(this.manualBookings, this.unhandledBookings).pipe(
Expand Down
4 changes: 2 additions & 2 deletions packages/simulator/lib/vehicles/truck.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { findBestRouteToPickupBookings } = require('../dispatch/truckDispatch')
const { info, warn } = require('../log')
const { info, warn, debug } = require('../log')
const Vehicle = require('./vehicle')

class Truck extends Vehicle {
Expand Down Expand Up @@ -53,7 +53,7 @@ class Truck extends Vehicle {
if (this.cargo.indexOf(this.booking) > -1)
return warn('Already picked up', this.id, this.booking.id)

info('Pickup cargo', this.id, this.booking.id)
debug('Pickup cargo', this.id, this.booking.id)
// this.cargo = [...this.cargo, this.booking?.passenger]
this.cargo.push(this.booking)
this.cargoEvents.next(this)
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/lib/vroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = {
async plan({ jobs, shipments, vehicles }) {
const result = await getFromCache({ jobs, shipments, vehicles })
if (result) {
info('Vroom cache hit')
debug('Vroom cache hit')
return result
}
debug('Vroom cache miss')
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/simulator/citizens.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getCitizensInSquare = (
workplaces,
kommunName
) => {
const nrOfCitizens = Math.floor(population * 0.01) // sample x% of the population
const nrOfCitizens = Math.floor(population * 0.001) // sample x% of the population
if (nrOfCitizens === 0) return from([])
const addresses = from(getAddressesInArea(position, area, nrOfCitizens)).pipe(
mergeAll()
Expand Down
4 changes: 2 additions & 2 deletions packages/simulator/streams/gtfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const key = process.env.TRAFIKLAB_KEY || 'aea471ef5dde447b87bc7bf4b971aaee'

const fs = require('fs')
const path = require('path')
const { info, error } = require('../lib/log')
const { info, error, debug } = require('../lib/log')

const AdmZip = require('adm-zip')
const fetch = require('node-fetch')
Expand Down Expand Up @@ -151,7 +151,7 @@ function gtfs(operator) {
case 'TEB planerad':
case 'VEN trafiken':
case 'Öresundståg':
info(
debug(
`Excluding route ${route.lineNumber} (${route.id}). Reason: ${route.description}`
)
return true
Expand Down
1 change: 1 addition & 0 deletions packages/simulator/web/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function subscribe(experiment, socket) {
defaultEmitters.includes('postombud') &&
require('./routes/postombud').register(experiment, socket),
require('./routes/time').register(experiment, socket),
require('./routes/log').register(experiment, socket),
]
.filter((f) => f)
.flat()
Expand Down
7 changes: 7 additions & 0 deletions packages/simulator/web/routes/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const register = (experiment, socket) => {
return [experiment.logStream.subscribe((item) => socket.emit('log', item))]
}

module.exports = {
register,
}
29 changes: 29 additions & 0 deletions packages/visualisation/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import ResetExperiment from './components/ResetExperiment'
import EditExperimentModal from './components/EditExperimentModal'
import Logo from './components/Logo'
import ExperimentDoneModal from './components/ExperimentDoneModal/index.jsx'
import { Snackbar, SnackbarContent } from '@mui/material'

import Slide from '@mui/material/Slide';

function TransitionUp(props) {
return <Slide {...props} direction="up" />
}
const App = () => {
const [activeCar, setActiveCar] = useState(null)
const [reset, setReset] = useState(false)
Expand All @@ -28,6 +34,8 @@ const App = () => {
const [experimentParameters, setExperimentParameters] = useState({})
const [currentParameters, setCurrentParameters] = useState({})
const [fleets, setFleets] = useState({})
const [latestLogMessage, setLatestLogMessage] = useState('')
const [snackbarOpen, setSnackbarOpen] = useState(false)
const [showEditExperimentModal, setShowEditExperimentModal] = useState(false)
const [showExperimentDoneModal, setShowExperimentDoneModal] = useState(false)
const [previousExperimentId, setPreviousExperimentId] = useState(null)
Expand Down Expand Up @@ -74,6 +82,7 @@ const App = () => {
setMeasureStations([])
setBusStops([])
setLineShapes([])
setLatestLogMessage('')
socket.emit('speed', speed) // reset speed on server
})

Expand Down Expand Up @@ -114,6 +123,11 @@ const App = () => {
setTime(time)
})

useSocket('log', (message) => {
setLatestLogMessage(message)
setSnackbarOpen(true)
})

const [bookings, setBookings] = React.useState([])
useSocket('bookings', (newBookings) => {
setReset(false)
Expand Down Expand Up @@ -333,6 +347,21 @@ const App = () => {
setShowEditExperimentModal={setShowEditExperimentModal}
experimentId={currentParameters.id}
/>

<Snackbar
sx={{ opacity: 0.8, bottom: 20 }}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
variant="filled"
open={snackbarOpen}
autoHideDuration={3000}
TransitionComponent={TransitionUp}
onClose={() => setSnackbarOpen(false)}
>
<SnackbarContent sx={{ backgroundColor: 'black', height: 20, margin: 3, color: 'white' }} message={latestLogMessage} />
</Snackbar>
</>
)
}
Expand Down

0 comments on commit 27eabab

Please sign in to comment.