From 1caafb61aa1113217adf952c2928e2390e05633f Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <me@adhisimon.org>
Date: Tue, 30 Jul 2019 22:48:19 +0700
Subject: [PATCH] History only  rely on redis. Remove circular-buffer

---
 lib/apiserver/index.js |  4 ++--
 lib/history.js         | 38 +++++++++++++++++---------------------
 package-lock.json      |  5 -----
 package.json           |  1 -
 4 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/lib/apiserver/index.js b/lib/apiserver/index.js
index 432a024..098da61 100644
--- a/lib/apiserver/index.js
+++ b/lib/apiserver/index.js
@@ -60,8 +60,8 @@ function onIncomingSms(req, res) {
     })
 }
 
-function pageHistory(req, res) {
-    res.json((history.dump() || []).reverse());
+async function pageHistory(req, res) {
+    res.json(await history.dump());
 }
 
 app.use(function(req, res, next) {
diff --git a/lib/history.js b/lib/history.js
index 5468ed8..7c677f3 100644
--- a/lib/history.js
+++ b/lib/history.js
@@ -3,42 +3,38 @@
 const MAX_HISTORY = 1000;
 
 const redis = require('redis');
-const CircularBuffer = require("circular-buffer");
 
 const config = require('komodo-sdk/config');
 const logger = require('komodo-sdk/logger');
 
 const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' });
-const history = new CircularBuffer(MAX_HISTORY);
 
 const REDIS_KEYWORD = `SHAKIR_SMS_HISTORY_${config.name||'SMS'}`;
 
-function fetchFromRedis() {
-    redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => {
-        reply.reverse().forEach((el) => {
-            try {
-                history.push(JSON.parse(el));
-            } catch (e) {
-                logger.warn(`Error parsing JSON on redis history. ${e.toString()}`);
-            }            
-        });
-        logger.info(`History fetched from redis with ${reply.length} items.`);
-    })
-}
-fetchFromRedis();
-
 function push(item) {
     redisClient.LPUSH(REDIS_KEYWORD, JSON.stringify(item), () => {
         redisClient.LTRIM(REDIS_KEYWORD, 0, MAX_HISTORY);
     });
-
-    history.push(item);
 }
 
 function dump() {
-    const result = history.toarray();
-    // console.log(JSON.stringify(result, null, 2));
-    return result;
+    return new Promise((resolve) => {
+        const result = [];
+
+        redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => {
+            reply.forEach((el) => {
+                try {
+                    result.push(JSON.parse(el));
+                } catch (e) {
+                    logger.warn(`Error parsing JSON on redis history. ${e.toString()}`);
+                }            
+            });
+            logger.info(`History fetched from redis with ${reply.length} items.`);
+
+            resolve(result);
+        });
+    
+    })
 }
 
 exports.push = push;
diff --git a/package-lock.json b/package-lock.json
index b601613..68e92a0 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -468,11 +468,6 @@
         "upath": "^1.1.1"
       }
     },
-    "circular-buffer": {
-      "version": "1.0.2",
-      "resolved": "https://registry.npmjs.org/circular-buffer/-/circular-buffer-1.0.2.tgz",
-      "integrity": "sha1-+g4VLtYp92/iTd+J5y69AHhsWm4="
-    },
     "class-utils": {
       "version": "0.3.6",
       "resolved": "https://registry.npmjs.org/class-utils/-/class-utils-0.3.6.tgz",
diff --git a/package.json b/package.json
index f5be219..f7ba560 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,6 @@
   "author": "Adhidarma Hadiwinoto <me@adhisimon.org>",
   "license": "ISC",
   "dependencies": {
-    "circular-buffer": "^1.0.2",
     "express": "^4.17.1",
     "komodo-center-messaging-client-lib": "git+http://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git",
     "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
-- 
1.9.0