Skip to content

Commit

Permalink
Merge pull request #113 from laem/socket
Browse files Browse the repository at this point in the history
Socket
  • Loading branch information
laem authored May 30, 2023
2 parents 9fc3713 + bcc17b8 commit 58204a2
Show file tree
Hide file tree
Showing 28 changed files with 712 additions and 413 deletions.
34 changes: 34 additions & 0 deletions <
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import AWS from 'aws-sdk'

export const BUCKET_NAME = process.env.BUCKET_NAME
const S3_ENDPOINT_URL = process.env.S3_ENDPOINT_URL
const ID = process.env.ACCESS_KEY_ID
const SECRET = process.env.ACCESS_KEY

// Create S3 service object
export const s3 = new AWS.S3({
endpoint: S3_ENDPOINT_URL,
credentials: {
accessKeyId: ID,
secretAccessKey: SECRET,
},
})

export const testStorage = async () => {
try {
const data = await s3
.getObject({
Bucket: BUCKET_NAME,
Key: 'yo.txt',
})
.promise()

console.log(
`Successfully read test file from ${BUCKET_NAME} : S3 storage works.`
)

console.log(`<<${data.Body.toString('utf-8')}>>`)
} catch (e) {
console.log('Problem fetching S3 test object', e)
}
}
178 changes: 0 additions & 178 deletions Classement.js

This file was deleted.

2 changes: 1 addition & 1 deletion algorithmVersion.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 'v2'
export default 'v3'
27 changes: 18 additions & 9 deletions computeCycling.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const computeRoseDirection = (bearing) =>
: bearing < 90
? 'nord-est'
: 'sud-est'
export const ridesPromises = (points) =>
export const createRidesPromises = (points) =>
points
.map((p, i) => {
const point1 = point([p.lon, p.lat])
Expand All @@ -153,8 +153,6 @@ export const ridesPromises = (points) =>
.filter((p) => p.notSame)
.sort((pa, pb) => pa.d - pb.d)

console.log('NP', nearestPoints)

const firstX = nearestPoints
.slice(0, nearestPointsLimit)
.map((p) => p.point)
Expand All @@ -172,8 +170,6 @@ export const ridesPromises = (points) =>
}, [])
.map((p) => p.point)

console.log('MOST', mostInterestingXPoints)

return mostInterestingXPoints.map((p2, j) =>
new Promise((resolve) =>
setTimeout(resolve, itineraryRequestDelay * (i + j))
Expand All @@ -182,19 +178,30 @@ export const ridesPromises = (points) =>
})
.flat()

const itineraryRequestDelay = 150 // This is fined tuned to handle the brouter server on my computer. It can fail requests at 100
const itineraryRequestDelay = 80 // This is fined tuned to handle the brouter server on my computer. It can fail requests at 100

export const isValidRide = (ride) =>
// Exclude itineraries that include a ferry route.
// TODO maybe we should just exclude the subrides that are ferry ? Doesn't matter much on the final result
ride.features &&
!getMessages(ride).some((ride) => ride[9].includes('route=ferry'))

export default async (ville) => {
export default async (ville, inform = () => null) => {
inform({ loading: `Les points vont être téléchargés` })
const points = await pointsProcess(ville)
inform({ loading: `Points téléchargés : ${points.length} points` })
const pointsCenter = computePointsCenter(points)

const rides = await Promise.all(ridesPromises(points))
let resolvedPromisesCount = 0
const ridesPromises = createRidesPromises(points)
ridesPromises.map((promise) =>
promise.then(() => {
resolvedPromisesCount += 1

inform({ loading: `🧭 ${resolvedPromisesCount} itinéraires calculés` })
})
)
const rides = await Promise.all(ridesPromises)

const filteredRides = rides.filter(isValidRide)

Expand All @@ -207,5 +214,7 @@ export default async (ville) => {
.map((r) => r.features)
.flat()

return { pointsCenter, points, segments, score, rides }
const result = { pointsCenter, points, segments, score, rides }
inform({ data: result })
return result
}
36 changes: 27 additions & 9 deletions cyclingPointsRequests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
const OverpassInstance = 'https://overpass-api.de/api/interpreter'

export const request = (name, requestCore) => `
const testHasLevel = (name) => /.+\.\d$/.test(name)
const splitName = (name) => name.split('.')

export const processName = (name) =>
testHasLevel(name) ? splitName(name)[0] : name

export const request = (name, requestCore) => {
const isId = /^\d+$/.test(name)
const hasLevel = testHasLevel(name)

const processedName = processName(name),
level = hasLevel && splitName(name)[1],
levelModifier = hasLevel ? `[admin_level="${level}"]` : ''

return `
[out:json][timeout:25];
( ${
/^\d+$/.test(name) ? `area(${3600000000 + +name})` : `area[name="${name}"]`
}; )->.searchArea;
isId
? `area(${3600000000 + +name})`
: `area[name="${processedName}"]${levelModifier}`
}; )->.searchArea;
(
${requestCore}
);
Expand All @@ -14,6 +30,7 @@ out body;
>;
out skel qt;
`
}

export const requestCores = {
stops: `
Expand All @@ -29,10 +46,11 @@ export const requestCores = {
relation["amenity"="townhall"](area.searchArea);
`,
}
export const overpassRequestURL = (city, requestCoreName) =>
encodeURI(
`${OverpassInstance}?data=${request(
decodeURIComponent(city),
requestCores[requestCoreName]
)}`
export const overpassRequestURL = (city, requestCoreName) => {
const requestContent = request(
decodeURIComponent(city),
requestCores[requestCoreName]
)
console.log(requestContent)
return encodeURI(`${OverpassInstance}?data=${requestContent}`)
}
8 changes: 7 additions & 1 deletion geoStudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ const findCity = (ville) =>

export const OverpassInstance = 'https://overpass-api.de/api/interpreter'

export const compute = (ville, exceptions0) => {
export const compute = (ville, inform = () => null) => {
const exceptions = {}
const overpassRequest = makeRequest(ville),
request = `${OverpassInstance}?data=${overpassRequest}`

console.log('On va lancer les requêtes pour ', ville)
inform({ loading: `On va lancer les requêtes pour ${ville}` })

return (
Promise.all([
Expand Down Expand Up @@ -83,9 +84,13 @@ export const compute = (ville, exceptions0) => {
const typesCount = countTypes(features)
const [polygons, meanStreetWidth, streetsWithWidthCount] =
linesToPolygons(ville, features)
inform({
loading: `Donnés OSM récupérées, ${polygons.length} polygones`,
})

console.log('will merge')
const mergedPolygons0 = await mergePolygons2(polygons)
inform({ loading: `La fusion a été opérée` })

console.log('merged, will exclude')
const toCoord = (f) => f.coordinates
Expand Down Expand Up @@ -113,6 +118,7 @@ export const compute = (ville, exceptions0) => {
//typesCount,
//geojson,
}
inform({ data: result })
return result
})
)
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"scripts": {
"clean": "rm -rf cache/*",
"s": "node -r esm server.js",
"s": "DEBUG=socket* node -r esm server.js",
"start": "node --insecure-http-parser --optimize_for_size --max_old_space_size=3200 -r esm server.js --watch",
"start-prod": "NODE_ENV=production node --insecure-http-parser --optimize_for_size --max_old_space_size=3200 -r esm server.js",
"comment": "https://devcenter.heroku.com/articles/node-memory-use"
Expand Down Expand Up @@ -75,6 +75,8 @@
"react-mapbox-gl": "^5.1.1",
"react-refresh": "^0.14.0",
"react-router-dom": "^6.8.2",
"socket.io": "^4.6.1",
"socket.io-client": "^4.6.1",
"style-loader": "^3.3.1",
"styled-components": "^5.3.6",
"turf-point": "^2.0.1",
Expand Down
Loading

1 comment on commit 58204a2

@vercel
Copy link

@vercel vercel bot commented on 58204a2 May 30, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.