Commit 6224382333828cb479e6ad8fb463c0c877045806
1 parent
5986078357
Exists in
master
Messages counter
Showing 2 changed files with 20 additions and 0 deletions Side-by-side Diff
lib/messages-archive.js
1 | 1 | 'use strict'; |
2 | 2 | |
3 | +const redis = require('redis'); | |
4 | + | |
5 | +const config = require('./config'); | |
3 | 6 | const logger = require('komodo-sdk/logger'); |
4 | 7 | const db = require('./db-mysql'); |
5 | 8 | |
6 | 9 | const DIRECTION_INCOMING = 0; |
7 | 10 | const DIRECTION_OUTGOING = 1; |
8 | 11 | |
12 | +const redisClient = config.redis && redis.createClient(config.redis); | |
13 | + | |
14 | +function composeRedisCounterKeyword(origin, direction) { | |
15 | + const directionLabel = direction == DIRECTION_OUTGOING ? 'OUT' : 'IN'; | |
16 | + return `CALMA_MESSAGE_COUNTER_${origin}_${directionLabel}`; | |
17 | +} | |
18 | + | |
19 | +function incrementCounter(origin, direction) { | |
20 | + redisClient && redisClient.INCR(composeRedisCounterKeyword(origin, direction), () => {}); | |
21 | +} | |
22 | + | |
9 | 23 | /** |
10 | 24 | * Menyimpan pesan ke dalam archive histori pesan di database |
11 | 25 | * |
... | ... | @@ -19,6 +33,11 @@ const DIRECTION_OUTGOING = 1; |
19 | 33 | * @param {number} direction - 0: incoming, 1: outgoing |
20 | 34 | */ |
21 | 35 | function insert(params, direction) { |
36 | + incrementCounter( | |
37 | + params.origin_label || params.origin, | |
38 | + params.direction | |
39 | + ); | |
40 | + | |
22 | 41 | if (!db.pool) return; |
23 | 42 | |
24 | 43 | const query = `INSERT INTO messages SET ?`; |
package.json