Commit 89aa13cfd3f7b144bb85a5f1f594afd535136c2e
1 parent
2bdcc28605
Exists in
master
Query optimizer
Showing 1 changed file with 2 additions and 3 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.NODE_ENV !== 'production'; | 6 | const DEBUG = process.NODE_ENV !== 'production'; |
7 | 7 | ||
8 | const router = express.Router(); | 8 | const router = express.Router(); |
9 | module.exports = router; | 9 | module.exports = router; |
10 | 10 | ||
11 | const cache = {}; | 11 | const cache = {}; |
12 | 12 | ||
13 | function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) { | 13 | function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) { |
14 | return new Promise((resolve) => { | 14 | return new Promise((resolve) => { |
15 | if (DEBUG) { | 15 | if (DEBUG) { |
16 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', { | 16 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', { |
17 | storeId, | 17 | storeId, |
18 | originStoreId, | 18 | originStoreId, |
19 | recursiveLevel, | 19 | recursiveLevel, |
20 | }); | 20 | }); |
21 | } | 21 | } |
22 | 22 | ||
23 | if (cache[`STORE_ID_${storeId}`]) { | 23 | if (cache[`STORE_ID_${storeId}`]) { |
24 | logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', { | 24 | logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', { |
25 | storeId, | 25 | storeId, |
26 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, | 26 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, |
27 | }); | 27 | }); |
28 | resolve({ | 28 | resolve({ |
29 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, | 29 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, |
30 | recursiveLevel: null, | 30 | recursiveLevel: null, |
31 | }); | 31 | }); |
32 | 32 | ||
33 | return; | 33 | return; |
34 | } | 34 | } |
35 | 35 | ||
36 | const query = ` | 36 | const query = ` |
37 | -- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe | 37 | -- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe |
38 | SELECT | 38 | SELECT SQL_CACHE parent_id, priceplan_name |
39 | parent_id, priceplan_name | 39 | FROM stores USE INDEX (PRIMARY) |
40 | FROM stores | ||
41 | WHERE id = ? | 40 | WHERE id = ? |
42 | LIMIT 1 | 41 | LIMIT 1 |
43 | `.trim(); | 42 | `.trim(); |
44 | 43 | ||
45 | const values = [storeId]; | 44 | const values = [storeId]; |
46 | 45 | ||
47 | dbKomodo.query(query, values, async (err, result) => { | 46 | dbKomodo.query(query, values, async (err, result) => { |
48 | if (err) { | 47 | if (err) { |
49 | const fullQuery = await dbKomodo.format(query, values); | 48 | const fullQuery = await dbKomodo.format(query, values); |
50 | logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, { | 49 | logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, { |
51 | storeId, originStoreId, recursiveLevel, fullQuery, | 50 | storeId, originStoreId, recursiveLevel, fullQuery, |
52 | }); | 51 | }); |
53 | 52 | ||
54 | resolve({ | 53 | resolve({ |
55 | priceplan: null, | 54 | priceplan: null, |
56 | recursiveLevel, | 55 | recursiveLevel, |
57 | }); | 56 | }); |
58 | return; | 57 | return; |
59 | } | 58 | } |
60 | 59 | ||
61 | const data = result && result[0]; | 60 | const data = result && result[0]; |
62 | 61 | ||
63 | if (!data) { | 62 | if (!data) { |
64 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel }); | 63 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel }); |
65 | resolve({ | 64 | resolve({ |
66 | priceplan: null, | 65 | priceplan: null, |
67 | recursiveLevel, | 66 | recursiveLevel, |
68 | }); | 67 | }); |
69 | return; | 68 | return; |
70 | } | 69 | } |
71 | 70 | ||
72 | if (data.priceplan_name.toUpperCase() === 'MARKUP') { | 71 | if (data.priceplan_name.toUpperCase() === 'MARKUP') { |
73 | if (!data.parent_id) { | 72 | if (!data.parent_id) { |
74 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', { | 73 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', { |
75 | storeId, | 74 | storeId, |
76 | originStoreId, | 75 | originStoreId, |
77 | recursiveLevel, | 76 | recursiveLevel, |
78 | }); | 77 | }); |
79 | 78 | ||
80 | resolve({ | 79 | resolve({ |
81 | priceplan: null, | 80 | priceplan: null, |
82 | recursiveLevel, | 81 | recursiveLevel, |
83 | }); | 82 | }); |
84 | return; | 83 | return; |
85 | } | 84 | } |
86 | 85 | ||
87 | if (DEBUG) { | 86 | if (DEBUG) { |
88 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', { | 87 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', { |
89 | storeId, | 88 | storeId, |
90 | originStoreId, | 89 | originStoreId, |
91 | recursiveLevel, | 90 | recursiveLevel, |
92 | parentId: data.parent_id, | 91 | parentId: data.parent_id, |
93 | }); | 92 | }); |
94 | } | 93 | } |
95 | const dataFromParent = await lookupCorrectPriceplan( | 94 | const dataFromParent = await lookupCorrectPriceplan( |
96 | data.parent_id, originStoreId, recursiveLevel + 1, | 95 | data.parent_id, originStoreId, recursiveLevel + 1, |
97 | ); | 96 | ); |
98 | 97 | ||
99 | resolve({ | 98 | resolve({ |
100 | priceplan: dataFromParent.priceplan, | 99 | priceplan: dataFromParent.priceplan, |
101 | recursiveLevel: dataFromParent.recursiveLevel, | 100 | recursiveLevel: dataFromParent.recursiveLevel, |
102 | }); | 101 | }); |
103 | return; | 102 | return; |
104 | } | 103 | } |
105 | 104 | ||
106 | cache[`STORE_ID_${storeId}`] = { | 105 | cache[`STORE_ID_${storeId}`] = { |
107 | priceplan: data.priceplan_name, | 106 | priceplan: data.priceplan_name, |
108 | recursiveLevel, | 107 | recursiveLevel, |
109 | }; | 108 | }; |
110 | 109 | ||
111 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', { | 110 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', { |
112 | storeId, | 111 | storeId, |
113 | originStoreId, | 112 | originStoreId, |
114 | priceplanName: data.priceplan_name, | 113 | priceplanName: data.priceplan_name, |
115 | recursiveLevel, | 114 | recursiveLevel, |
116 | }); | 115 | }); |
117 | 116 | ||
118 | resolve({ | 117 | resolve({ |
119 | priceplan: data.priceplan_name, | 118 | priceplan: data.priceplan_name, |
120 | recursiveLevel, | 119 | recursiveLevel, |
121 | }); | 120 | }); |
122 | }); | 121 | }); |
123 | }); | 122 | }); |
124 | } | 123 | } |
125 | 124 | ||
126 | async function pageShouldBe(req, res) { | 125 | async function pageShouldBe(req, res) { |
127 | if (!req.body) req.body = {}; | 126 | if (!req.body) req.body = {}; |
128 | 127 | ||
129 | const storeId = req.body.store_id || req.query.store_id; | 128 | const storeId = req.body.store_id || req.query.store_id; |
130 | if (!storeId) { | 129 | if (!storeId) { |
131 | res.json({ | 130 | res.json({ |
132 | error: true, | 131 | error: true, |
133 | error_message: 'Undefined parameter store_id', | 132 | error_message: 'Undefined parameter store_id', |
134 | }); | 133 | }); |
135 | return; | 134 | return; |
136 | } | 135 | } |
137 | 136 | ||
138 | if (DEBUG) { | 137 | if (DEBUG) { |
139 | logger.verbose('PRICEPLAN.pageShouldBe: Got a request', { | 138 | logger.verbose('PRICEPLAN.pageShouldBe: Got a request', { |
140 | method: req.method, | 139 | method: req.method, |
141 | storeId, | 140 | storeId, |
142 | }); | 141 | }); |
143 | } | 142 | } |
144 | 143 | ||
145 | const data = await lookupCorrectPriceplan(storeId, storeId, 0); | 144 | const data = await lookupCorrectPriceplan(storeId, storeId, 0); |
146 | 145 | ||
147 | res.json({ | 146 | res.json({ |
148 | error: false, | 147 | error: false, |
149 | error_message: null, | 148 | error_message: null, |
150 | store_id: storeId, | 149 | store_id: storeId, |
151 | recursive_level: (data && data.recursiveLevel) || null, | 150 | recursive_level: (data && data.recursiveLevel) || null, |
152 | priceplan_should_be: (data && data.priceplan) || null, | 151 | priceplan_should_be: (data && data.priceplan) || null, |
153 | raw: data, | 152 | raw: data, |
154 | }); | 153 | }); |
155 | } | 154 | } |
156 | 155 | ||
157 | router.all('/should-be', bodyParser.urlencoded({ extended: false }), pageShouldBe); | 156 | router.all('/should-be', bodyParser.urlencoded({ extended: false }), pageShouldBe); |
158 | 157 |