{"id":15501525,"url":"https://github.com/envek/etatata","last_synced_at":"2025-06-15T09:09:05.509Z","repository":{"id":138467944,"uuid":"59372869","full_name":"Envek/etatata","owner":"Envek","description":"Dead simple microservice ETA calculator. Proud member of bycicle parade!","archived":false,"fork":false,"pushed_at":"2016-05-22T10:51:55.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-10T07:55:11.393Z","etag":null,"topics":["postgis","sinatra","test-task"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Envek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-21T17:26:45.000Z","updated_at":"2022-08-28T05:10:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"de61ae6f-2dbf-4a1f-914f-eb7ead6c6dba","html_url":"https://github.com/Envek/etatata","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"fe2214edd1abb2af4a55dfb1c491e0b926e65387"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Envek/etatata","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Envek%2Fetatata","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Envek%2Fetatata/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Envek%2Fetatata/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Envek%2Fetatata/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Envek","download_url":"https://codeload.github.com/Envek/etatata/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Envek%2Fetatata/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259949680,"owners_count":22936411,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["postgis","sinatra","test-task"],"created_at":"2024-10-02T09:04:41.990Z","updated_at":"2025-06-15T09:09:05.485Z","avatar_url":"https://github.com/Envek.png","language":"Ruby","readme":"# ETATATA\n\nMicroservice and JSON API to request an ETA of nearest vehicle calculated as [Haversine distance] in kilometers multiplied by factor of 1.5 minutes per kilometer. So, average distance to three nearest vehicles is 10 km, it will assume that one of them will arrive in 15 minutes.\n\n## Components\n\n### API\n\nA Ruby application built with [Sinatra] microframework which is good for small APIs with multithreaded Puma application server. Receives coordinates, validates it and sends to service via [MessagePack RPC].\n\n\n### Service\n\nA pure Ruby application, accepts requests via MessagePack RPC, fetches required data from database, calculates answers, caches them, and returns. That's it.\n\nUsed technologies:\n\n - [MessagePack] — lightweight replacement for JSON.\n - [MessagePack RPC] — eliminates HTTP overhead which is huge when your payload is small. In case of common request for this service reduces request-response size from 387 bytes for HTTP (compared to API application) to 164 for [MessagePack RPC].\n - [ActiveRecord] — handles connection pooling, data transformations, input data quotations and more.\n - [ActiveSupport::Cache] — cache abstraqction, allows to change cache backends easily.\n\nWhat is not implemented yet:\n\n - Concurrency — it's single process, single thread application that can handle only one request at a time.\n\n\n### Database\n\n[PostgreSQL] 9.5 with [PostGIS] 2.2. Open Source, very reliable, with strong guarantees of consistency, and support for a lot of data and index types.\n\nI store a vehicle coordinates in a column of `geography` type with a GIST index over it to speed up lookups.\n\nIt allows to find nearest cars with query like:\n\n```sql\nSELECT\n  ST_X(vehicles.position::geometry) AS longitude,\n  ST_Y(vehicles.position::geometry) AS latitude\nFROM vehicles\nWHERE available = true\nORDER BY vehicles.position \u003c-\u003e 'POINT(37.058515 55.923175)'::geography ASC\nLIMIT 3;\n```\n\nQuery plan for this query over 100 000 vehicles:\n\n```\nLimit  (cost=0.28..1.66 rows=3 width=56) (actual time=15.580..15.658 rows=3 loops=1)\n  -\u003e  Index Scan using vehicles_position_idx on vehicles  (cost=0.28..22812.28 rows=49500 width=56) (actual time=15.578..15.655 rows=3 loops=1)\n        Order By: (\"position\" \u003c-\u003e '0101000020E6100000A8A9656B7D8742400EBE30992AF64B40'::geography)\n        Filter: available\n        Rows Removed by Filter: 12\nPlanning time: 0.215 ms\nExecution time: 15.751 ms\n```\n\nThe `geography \u003c-\u003e geography` operator calculates distance on the sphere between given points ([see docs](http://postgis.net/docs/manual-2.2/geometry_distance_knn.html)). This is not the same as [Haversine distance] but both are algebraically equivalent (both are monotonically increasing), so we can get required number of nearest records with this function and recalculate [Haversine distance] only for them.\n\n\n## Launch\n\n### Via Docker\n\nYou will need to have installed:\n\n - Recent [Docker]\n - Recent [Docker Compose]\n\nThen just execute next command from this directory:\n\n    docker-compose up\n\nAccess API endpoint on URL like this: http://localhost:4567/?latitude=55.923175\u0026longitude=37.858515\n\n\n### Manually\n\nYou will need to have installed:\n\n - Recent MRI Ruby version (recommended: 2.3)\n - Recent PostgreSQL with PostGIS extension (recommended: 9.5 with 2.2)\n\nFollow these simple steps:\n\n 1. Create database and execute [db/setup.sql](db/setup.sql) script in it.\n\n 2. Install required gems by executing `bundle install` in both `eta_api` and `eta_service` directories.\n\n 3. Launch service with command like:\n\n        cd eta_service\n        env 'DATABASE_URL=postgresql://USER:PASSWORD@localhost/eta-db' ruby eta_service.rb\n\n 4. Launch API with command like:\n\n        cd eta_api\n        ruby eta_service.rb\n\n 5. Access API endpoint on URL like this: http://localhost:4567/?latitude=55.923175\u0026longitude=37.858515\n\n 6. …\n\n 7. PROFIT!\n\n\n## License\n\n\u003e The MIT License (MIT)\n\u003e Copyright (c) 2016 Andrey Novikov\n\u003e\n\u003e Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\u003e\n\u003e The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\u003e\n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n[Haversine distance]: https://en.wikipedia.org/wiki/Haversine_formula (gedesic distance: as crow flies)\n[MessagePack]: https://msgpack.org/\n[MessagePack RPC]: https://github.com/msgpack-rpc/msgpack-rpc\n[ActiveRecord]: https://github.com/rails/rails/tree/master/activerecord\n[ActiveSupport::Cache]: http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html\n[Sinatra]: http://www.sinatrarb.com/\n[PostgreSQL]: http://www.postgresql.org/\n[PostGIS]: http://postgis.net/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenvek%2Fetatata","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenvek%2Fetatata","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenvek%2Fetatata/lists"}