Commit 1caafb61aa1113217adf952c2928e2390e05633f
1 parent
09a2ed3aae
Exists in
master
History only rely on redis. Remove circular-buffer
Showing 4 changed files with 19 additions and 29 deletions Side-by-side Diff
lib/apiserver/index.js
... | ... | @@ -60,8 +60,8 @@ function onIncomingSms(req, res) { |
60 | 60 | }) |
61 | 61 | } |
62 | 62 | |
63 | -function pageHistory(req, res) { | |
64 | - res.json((history.dump() || []).reverse()); | |
63 | +async function pageHistory(req, res) { | |
64 | + res.json(await history.dump()); | |
65 | 65 | } |
66 | 66 | |
67 | 67 | app.use(function(req, res, next) { |
lib/history.js
... | ... | @@ -3,42 +3,38 @@ |
3 | 3 | const MAX_HISTORY = 1000; |
4 | 4 | |
5 | 5 | const redis = require('redis'); |
6 | -const CircularBuffer = require("circular-buffer"); | |
7 | 6 | |
8 | 7 | const config = require('komodo-sdk/config'); |
9 | 8 | const logger = require('komodo-sdk/logger'); |
10 | 9 | |
11 | 10 | const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' }); |
12 | -const history = new CircularBuffer(MAX_HISTORY); | |
13 | 11 | |
14 | 12 | const REDIS_KEYWORD = `SHAKIR_SMS_HISTORY_${config.name||'SMS'}`; |
15 | 13 | |
16 | -function fetchFromRedis() { | |
17 | - redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => { | |
18 | - reply.reverse().forEach((el) => { | |
19 | - try { | |
20 | - history.push(JSON.parse(el)); | |
21 | - } catch (e) { | |
22 | - logger.warn(`Error parsing JSON on redis history. ${e.toString()}`); | |
23 | - } | |
24 | - }); | |
25 | - logger.info(`History fetched from redis with ${reply.length} items.`); | |
26 | - }) | |
27 | -} | |
28 | -fetchFromRedis(); | |
29 | - | |
30 | 14 | function push(item) { |
31 | 15 | redisClient.LPUSH(REDIS_KEYWORD, JSON.stringify(item), () => { |
32 | 16 | redisClient.LTRIM(REDIS_KEYWORD, 0, MAX_HISTORY); |
33 | 17 | }); |
34 | - | |
35 | - history.push(item); | |
36 | 18 | } |
37 | 19 | |
38 | 20 | function dump() { |
39 | - const result = history.toarray(); | |
40 | - // console.log(JSON.stringify(result, null, 2)); | |
41 | - return result; | |
21 | + return new Promise((resolve) => { | |
22 | + const result = []; | |
23 | + | |
24 | + redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => { | |
25 | + reply.forEach((el) => { | |
26 | + try { | |
27 | + result.push(JSON.parse(el)); | |
28 | + } catch (e) { | |
29 | + logger.warn(`Error parsing JSON on redis history. ${e.toString()}`); | |
30 | + } | |
31 | + }); | |
32 | + logger.info(`History fetched from redis with ${reply.length} items.`); | |
33 | + | |
34 | + resolve(result); | |
35 | + }); | |
36 | + | |
37 | + }) | |
42 | 38 | } |
43 | 39 | |
44 | 40 | exports.push = push; |
package-lock.json
... | ... | @@ -468,11 +468,6 @@ |
468 | 468 | "upath": "^1.1.1" |
469 | 469 | } |
470 | 470 | }, |
471 | - "circular-buffer": { | |
472 | - "version": "1.0.2", | |
473 | - "resolved": "https://registry.npmjs.org/circular-buffer/-/circular-buffer-1.0.2.tgz", | |
474 | - "integrity": "sha1-+g4VLtYp92/iTd+J5y69AHhsWm4=" | |
475 | - }, | |
476 | 471 | "class-utils": { |
477 | 472 | "version": "0.3.6", |
478 | 473 | "resolved": "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz", |
package.json
... | ... | @@ -20,7 +20,6 @@ |
20 | 20 | "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", |
21 | 21 | "license": "ISC", |
22 | 22 | "dependencies": { |
23 | - "circular-buffer": "^1.0.2", | |
24 | 23 | "express": "^4.17.1", |
25 | 24 | "komodo-center-messaging-client-lib": "git+http://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git", |
26 | 25 | "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git", |