An charging system in Finagle with Scala
Charging System API built using Finagle with Scala.
Frameworks:
- Joda-Time:
SimpleDateFormat
is not thread-safe. - Scalatest: Test library for scala.
- Finagle: Finagle is built atop of Netty and is very efficient for distributed tasks, also uses Twitter Futures which are better at avoiding context switches. Because of these reasons I've choose it for the task.
- Redis:
Finagle-Redis
was choosed for this task. Jedis is another pretty good library but Finagle-Redis isnon-blocking
, which led me to choose it.
Create new transaction. Required fields are:
- customerId
- startTime
- volume
- activeStarting, and should be after current time
curl -H 'content-type: application/json' http://localhost:8088/transactions -d '{ "customerId": "john", "startTime": "2015-10-28T09:34:17Z", "endTime": "2015-10-28T16:45:13Z", "volume": 32.03 }'
Create new tariff. Required fields are:
- startFee
- hourlyFee
- feePerKWh
- activeStarting, and should be after current time
curl -H 'content-type: application/json' http://localhost:8088/tariffs -d '{ "startFee": 0.20, "hourlyFee": 1.00, "feePerKWh": 0.25, "activeStarting": "2015-08-18T06:00:00Z" }'
Overview of charge sessions.
curl http://localhost:8088/overview
Response:
pete,2014-10-27T13:32:14Z,2014-10-27T14:32:14Z,13.21,4.50 john,2014-10-28T09:34:17Z,2014-10-28T16:45:13Z,32.03,14.70
With content-type:"text/csv"
Customer invoice.
curl http://localhost:8088/invoices/2015/10/john
Response:
Dear john,
In October 2015, you have charged:
from 2015-10-28 09:34 to 2015-10-28 16:45: 32.03 kWh @ € 15.39
Total amount: € 15.39
Kind regards, Your dearest mobility provider, The Venerable Inertia
With content-type:"text/txt"
$ git clone [email protected]:leonmaia/charging-system-api.git
$ cd ./path/to/repo
$ vagrant up --provision
Upstart
is being used to run the api as a
ubuntu service.
$ git clone [email protected]:leonmaia/charging-system-api.git
$ cd ./path/to/repo
$ java -Dfile.encoding=UTF-8 -jar target/scala-2.11/charging-system-api-assembly-1.0.jar
$ git clone [email protected]:leonmaia/charging-system-api.git
$ cd ./path/to/repo
$ ./sbt run
Tests are run using sbt.
$ ./sbt test
Or you can view a coverage report:
$ ./sbt cov
Open index.html
file at ./path/to/repo/target/scala-2.11/scoverage-report