Commit 41bf078c827e961ef7757cd7cd54aed92a5e3ddb

Authored by Adhidarma Hadiwinoto
1 parent 4f37dbd7d2
Exists in master

DB pool status

Showing 1 changed file with 13 additions and 2 deletions Inline Diff

lib/webservice/router/priceplan.js
1 const express = require('express'); 1 const express = require('express');
2 const bodyParser = require('body-parser'); 2 const bodyParser = require('body-parser');
3 const logger = require('komodo-sdk/logger'); 3 const logger = require('komodo-sdk/logger');
4 const dbKomodo = require('../../db-komodo'); 4 const dbKomodo = require('../../db-komodo');
5 5
6 const DEBUG = process.env.NODE_ENV !== 'production'; 6 const DEBUG = process.env.NODE_ENV !== 'production';
7 logger.info(`PRODUCTION MODE: ${!DEBUG}`); 7 // logger.info(`PRODUCTION MODE: ${!DEBUG}`);
8 8
9 const router = express.Router(); 9 const router = express.Router();
10 module.exports = router; 10 module.exports = router;
11 11
12 const cache = {}; 12 const cache = {};
13 13
14 function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) { 14 function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) {
15 return new Promise((resolve) => { 15 return new Promise((resolve) => {
16 if (DEBUG) { 16 if (DEBUG) {
17 logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', { 17 logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', {
18 storeId, 18 storeId,
19 originStoreId, 19 originStoreId,
20 recursiveLevel, 20 recursiveLevel,
21 }); 21 });
22 } 22 }
23 23
24 if (cache[`STORE_ID_${storeId}`]) { 24 if (cache[`STORE_ID_${storeId}`]) {
25 logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', { 25 logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', {
26 storeId, 26 storeId,
27 priceplan: cache[`STORE_ID_${storeId}`].priceplan, 27 priceplan: cache[`STORE_ID_${storeId}`].priceplan,
28 }); 28 });
29 resolve({ 29 resolve({
30 priceplan: cache[`STORE_ID_${storeId}`].priceplan, 30 priceplan: cache[`STORE_ID_${storeId}`].priceplan,
31 recursiveLevel: null, 31 recursiveLevel: null,
32 }); 32 });
33 33
34 return; 34 return;
35 } 35 }
36 36
37 const query = ` 37 const query = `
38 -- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe 38 -- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe
39 SELECT SQL_CACHE parent_id, priceplan_name 39 SELECT SQL_CACHE parent_id, priceplan_name
40 FROM stores USE INDEX (PRIMARY) 40 FROM stores USE INDEX (PRIMARY)
41 WHERE id = ? 41 WHERE id = ?
42 LIMIT 1 42 LIMIT 1
43 `.trim(); 43 `.trim();
44 44
45 const values = [storeId]; 45 const values = [storeId];
46 46
47 dbKomodo.query(query, values, async (err, result) => { 47 dbKomodo.query(query, values, async (err, result) => {
48 if (err) { 48 if (err) {
49 const fullQuery = await dbKomodo.format(query, values); 49 const fullQuery = await dbKomodo.format(query, values);
50 logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, { 50 logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, {
51 storeId, originStoreId, recursiveLevel, fullQuery, 51 storeId, originStoreId, recursiveLevel, fullQuery,
52 }); 52 });
53 53
54 resolve({ 54 resolve({
55 priceplan: null, 55 priceplan: null,
56 recursiveLevel, 56 recursiveLevel,
57 }); 57 });
58 return; 58 return;
59 } 59 }
60 60
61 const data = result && result[0]; 61 const data = result && result[0];
62 62
63 if (!data) { 63 if (!data) {
64 logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel }); 64 logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel });
65 resolve({ 65 resolve({
66 priceplan: null, 66 priceplan: null,
67 recursiveLevel, 67 recursiveLevel,
68 }); 68 });
69 return; 69 return;
70 } 70 }
71 71
72 if (data.priceplan_name.toUpperCase() === 'MARKUP') { 72 if (data.priceplan_name.toUpperCase() === 'MARKUP') {
73 if (!data.parent_id) { 73 if (!data.parent_id) {
74 logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', { 74 logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', {
75 storeId, 75 storeId,
76 originStoreId, 76 originStoreId,
77 recursiveLevel, 77 recursiveLevel,
78 }); 78 });
79 79
80 resolve({ 80 resolve({
81 priceplan: null, 81 priceplan: null,
82 recursiveLevel, 82 recursiveLevel,
83 }); 83 });
84 return; 84 return;
85 } 85 }
86 86
87 if (DEBUG) { 87 if (DEBUG) {
88 logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', { 88 logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', {
89 storeId, 89 storeId,
90 originStoreId, 90 originStoreId,
91 recursiveLevel, 91 recursiveLevel,
92 parentId: data.parent_id, 92 parentId: data.parent_id,
93 }); 93 });
94 } 94 }
95 const dataFromParent = await lookupCorrectPriceplan( 95 const dataFromParent = await lookupCorrectPriceplan(
96 data.parent_id, originStoreId, recursiveLevel + 1, 96 data.parent_id, originStoreId, recursiveLevel + 1,
97 ); 97 );
98 98
99 resolve({ 99 resolve({
100 priceplan: dataFromParent.priceplan, 100 priceplan: dataFromParent.priceplan,
101 recursiveLevel: dataFromParent.recursiveLevel, 101 recursiveLevel: dataFromParent.recursiveLevel,
102 }); 102 });
103 return; 103 return;
104 } 104 }
105 105
106 cache[`STORE_ID_${storeId}`] = { 106 cache[`STORE_ID_${storeId}`] = {
107 priceplan: data.priceplan_name, 107 priceplan: data.priceplan_name,
108 recursiveLevel, 108 recursiveLevel,
109 }; 109 };
110 110
111 if (DEBUG) { 111 if (DEBUG) {
112 logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', { 112 logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', {
113 storeId, 113 storeId,
114 originStoreId, 114 originStoreId,
115 priceplanName: data.priceplan_name, 115 priceplanName: data.priceplan_name,
116 recursiveLevel, 116 recursiveLevel,
117 }); 117 });
118 } 118 }
119 119
120 resolve({ 120 resolve({
121 priceplan: data.priceplan_name, 121 priceplan: data.priceplan_name,
122 recursiveLevel, 122 recursiveLevel,
123 }); 123 });
124 }); 124 });
125 }); 125 });
126 } 126 }
127 127
128 async function pageShouldBe(req, res) { 128 async function pageShouldBe(req, res) {
129 if (!req.body) req.body = {}; 129 if (!req.body) req.body = {};
130 130
131 const startTime = new Date(); 131 const startTime = new Date();
132 132
133 const storeId = req.body.store_id || req.query.store_id; 133 const storeId = req.body.store_id || req.query.store_id;
134 if (!storeId) { 134 if (!storeId) {
135 res.json({ 135 res.json({
136 error: true, 136 error: true,
137 error_message: 'Undefined parameter store_id', 137 error_message: 'Undefined parameter store_id',
138 }); 138 });
139 return; 139 return;
140 } 140 }
141 141
142 logger.verbose('PRICEPLAN.pageShouldBe: Got a request', { 142 logger.verbose('PRICEPLAN.pageShouldBe: Got a request', {
143 method: req.method, 143 method: req.method,
144 storeId, 144 storeId,
145 }); 145 });
146 146
147 const data = await lookupCorrectPriceplan(storeId, storeId, 0); 147 const data = await lookupCorrectPriceplan(storeId, storeId, 0);
148 148
149 const deltaTime = Number(new Date() - startTime); 149 const deltaTime = Number(new Date() - startTime);
150 logger.info(`PRICEPLAN.pageShouldBe: Lookup finished ${deltaTime} ms`, { storeId }); 150 logger.info(`PRICEPLAN.pageShouldBe: Lookup finished ${deltaTime} ms`, {
151 storeId,
152 newPriceplan: data && data.priceplanName,
153 dbPoolStatus: {
154 // eslint-disable-next-line no-underscore-dangle,max-len
155 all: (dbKomodo.pool && dbKomodo.pool._allConnections && dbKomodo.pool._allConnections.length) || null,
156 // eslint-disable-next-line no-underscore-dangle,max-len
157 free: (dbKomodo.pool && dbKomodo.pool._freeConnections && dbKomodo.pool._freeConnections.length) || null,
158 // eslint-disable-next-line no-underscore-dangle,max-len
159 queue: (dbKomodo.pool && dbKomodo.pool._connectionQueue && dbKomodo.pool._connectionQueue.length) || null,
160 },
161 });
151 162
152 res.json({ 163 res.json({
153 error: false, 164 error: false,
154 error_message: null, 165 error_message: null,
155 store_id: storeId, 166 store_id: storeId,
156 recursive_level: (data && data.recursiveLevel) || null, 167 recursive_level: (data && data.recursiveLevel) || null,
157 priceplan_should_be: (data && data.priceplan) || null, 168 priceplan_should_be: (data && data.priceplan) || null,
158 raw: data, 169 raw: data,
159 }); 170 });
160 } 171 }
161 172
162 router.all('/should-be', bodyParser.urlencoded({ extended: false }), pageShouldBe); 173 router.all('/should-be', bodyParser.urlencoded({ extended: false }), pageShouldBe);
163 174