summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateus Cruz <mateuscolvr@gmail.com>2024-02-05 18:48:47 -0300
committerMateus Cruz <mateuscolvr@gmail.com>2024-02-05 18:48:47 -0300
commitb2aed14fd4a252d77b7ebaf42407472c47d1c98b (patch)
treed705b9f371d6d5cdf5018b6036da5749dc35f4f9
parent8878b1d92bdd8ee9b51ecde7e9cc88f62f264841 (diff)
add logs and fix sql
-rw-r--r--bin/main.ml12
-rw-r--r--docker-compose.yml19
-rw-r--r--lib/handler.ml12
-rw-r--r--nginx.conf3
-rw-r--r--script.sql13
5 files changed, 46 insertions, 13 deletions
diff --git a/bin/main.ml b/bin/main.ml
index 3f11200..03cc5ca 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -1,13 +1,23 @@
open Eio
open Piaf
+let setup_log ?style_renderer level =
+ Logs_threaded.enable ();
+ Fmt_tty.setup_std_outputs ?style_renderer ();
+ Logs.set_level ~all:true level;
+ Logs.set_reporter (Logs_fmt.reporter ())
+;;
+
let request_handler ~db_pool Server.{ request; _ } =
match Rinha.Router.match_route request.meth request.target with
| Some handler -> Result.get_ok @@ handler db_pool request
- | None -> Response.create `Not_found
+ | None ->
+ Logs.info (fun d -> d "Não encontrei %S\n" request.target);
+ Response.create `Not_found
;;
let () =
+ setup_log (Some Logs.Info);
Eio_main.run
@@ fun env ->
Switch.run
diff --git a/docker-compose.yml b/docker-compose.yml
index 9c58e3f..ccf1661 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -10,8 +10,8 @@ services:
deploy:
resources:
limits:
- cpus: "0.45"
- memory: "200mb"
+ cpus: "0.20"
+ memory: "150mb"
api02:
<<: *api
@@ -31,7 +31,7 @@ services:
resources:
limits:
cpus: "0.17"
- memory: "10MB"
+ memory: "110MB"
db:
image: postgres:latest
@@ -47,5 +47,16 @@ services:
deploy:
resources:
limits:
- cpus: "0.13"
+ cpus: "0.63"
memory: "140MB"
+
+ pgadmin:
+ image: dpage/pgadmin4
+ environment:
+ PGADMIN_DEFAULT_EMAIL: admin@admin.com
+ PGADMIN_DEFAULT_PASSWORD: admin
+ PGADMIN_LISTEN_PORT: 5050
+ ports:
+ - 5050:5050
+ depends_on:
+ - db
diff --git a/lib/handler.ml b/lib/handler.ml
index 4ee7189..a4d8cae 100644
--- a/lib/handler.ml
+++ b/lib/handler.ml
@@ -26,7 +26,9 @@ let create_transaction client_id (db_pool : Query.pool) (request : Request.t) =
in
Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK)
| Error _ -> Ok (Response.create (`Code 422)))
- | None -> Ok (Response.create `Not_found))
+ | None ->
+ Logs.info (fun m -> m "Não encontrei o cliente %d" client_id);
+ Ok (Response.create `Not_found))
db_pool
;;
@@ -57,7 +59,11 @@ let get_balance client_id (db_pool : Query.pool) (_request : Request.t) =
`Assoc [ "saldo", balance; "ultimas_transacoes", last_transactions ]
in
Ok (Response.of_string ~body:(Yojson.Safe.to_string json) `OK)
- | None -> Ok (Response.create `Not_found))
- | None -> Ok (Response.create `Not_found))
+ | None ->
+ Logs.info (fun m -> m "Não encontrei o extrato do cliente %d" client_id);
+ Ok (Response.create `Not_found))
+ | None ->
+ Logs.info (fun m -> m "Não encontrei o cliente %d" client_id);
+ Ok (Response.create `Not_found))
db_pool
;;
diff --git a/nginx.conf b/nginx.conf
index fc8933d..42d8b20 100644
--- a/nginx.conf
+++ b/nginx.conf
@@ -1,5 +1,5 @@
events {
- worker_connections 1000;
+ worker_connections 2000;
}
http {
@@ -16,6 +16,7 @@ http {
location / {
proxy_pass http://api;
+ proxy_read_timeout 30;
}
}
}
diff --git a/script.sql b/script.sql
index 7f3f3c0..9a2b3c2 100644
--- a/script.sql
+++ b/script.sql
@@ -15,20 +15,25 @@ CREATE TABLE transactions (
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
+CREATE INDEX idx_client_id_transactions ON transactions (client_id);
+
CREATE TABLE balances (
id SERIAL PRIMARY KEY,
client_id INTEGER REFERENCES clients,
value INTEGER NOT NULL
);
+CREATE INDEX idx_client_id_balances ON balances (client_id);
+
DO $$
BEGIN
INSERT INTO clients (name, mov_limit)
VALUES
- ('naruto', 1000 * 100),
- ('mob', 800 * 100),
- ('jojo', 10000 * 100),
- ('hellboy', 5000 * 100);
+ ('naruto', 100000),
+ ('mob', 80000),
+ ('jojo', 1000000),
+ ('hellboy', 10000000),
+ ('ultramega', 500000);
INSERT INTO balances (client_id, value)
SELECT id, 0 FROM clients;
END;