|
| 1 | +const crypto = require('crypto'); |
| 2 | + |
| 3 | +const moment = require('moment'); |
| 4 | +const should = require('should'); |
| 5 | +const spt = require('supertest'); |
| 6 | + |
| 7 | +const pluginManager = require('../../pluginManager.js'); |
| 8 | +const testUtils = require('../../../test/testUtils'); |
| 9 | + |
| 10 | +const request = spt(testUtils.url); |
| 11 | +const APP_ID = testUtils.get('APP_ID'); |
| 12 | +const API_KEY_ADMIN = testUtils.get('API_KEY_ADMIN'); |
| 13 | +const APP_KEY = testUtils.get('APP_KEY'); |
| 14 | + |
| 15 | +describe('Heatmap', async() => { |
| 16 | + const clickData = { |
| 17 | + type: 'click', |
| 18 | + x: 1353, |
| 19 | + y: 230, |
| 20 | + width: 1440, |
| 21 | + height: 3586, |
| 22 | + }; |
| 23 | + |
| 24 | + before(async() => { |
| 25 | + await request |
| 26 | + .get('/i') |
| 27 | + .query({ |
| 28 | + api_key: API_KEY_ADMIN, |
| 29 | + app_id: APP_ID, |
| 30 | + app_key: APP_KEY, |
| 31 | + device_id: 'heatmap_test', |
| 32 | + events: JSON.stringify([ |
| 33 | + { |
| 34 | + key: '[CLY]_view', |
| 35 | + count: 1, |
| 36 | + timestamp: moment('2010-01-02').valueOf(), |
| 37 | + hour: 21, |
| 38 | + segmentation: { |
| 39 | + name: 'Home', |
| 40 | + visit: 1, |
| 41 | + start: 1, |
| 42 | + exit: 1, |
| 43 | + bounce: 0, |
| 44 | + }, |
| 45 | + }, |
| 46 | + ]), |
| 47 | + }) |
| 48 | + .expect(200); |
| 49 | + |
| 50 | + await request |
| 51 | + .get('/i') |
| 52 | + .query({ |
| 53 | + api_key: API_KEY_ADMIN, |
| 54 | + app_id: APP_ID, |
| 55 | + app_key: APP_KEY, |
| 56 | + device_id: 'heatmap_test', |
| 57 | + events: JSON.stringify([ |
| 58 | + { |
| 59 | + key: '[CLY]_action', |
| 60 | + count: 1, |
| 61 | + timestamp: moment('2010-01-02').valueOf(), |
| 62 | + hour: 21, |
| 63 | + segmentation: { |
| 64 | + ...clickData, |
| 65 | + domain: 'https://doma.in', |
| 66 | + view: 'Home', |
| 67 | + }, |
| 68 | + }, |
| 69 | + ]), |
| 70 | + }) |
| 71 | + .expect(200); |
| 72 | + }); |
| 73 | + |
| 74 | + it('gets heatmap data from drill_events collection', async() => { |
| 75 | + const { body } = await request.post('/o/actions') |
| 76 | + .send({ |
| 77 | + api_key: API_KEY_ADMIN, |
| 78 | + app_id: APP_ID, |
| 79 | + app_key: APP_KEY, |
| 80 | + view: 'Home', |
| 81 | + period: JSON.stringify([moment('2010-01-01').valueOf(), moment('2010-01-31').valueOf()]), |
| 82 | + device: JSON.stringify({ type: 'all', displayText: 'All', minWidth: 0, maxWidth: 10240 }), |
| 83 | + actionType: 'click', |
| 84 | + }); |
| 85 | + |
| 86 | + const { data } = body; |
| 87 | + should(data.length).equal(1); |
| 88 | + should(data[0].sg).eql(clickData); |
| 89 | + }); |
| 90 | + |
| 91 | + it('gets heatmap data from old drill_events collection if union_with is true', async() => { |
| 92 | + const db = await pluginManager.dbConnection('countly_drill'); |
| 93 | + const oldCollectionName = 'drill_events' + crypto.createHash('sha1').update('[CLY]_action' + APP_ID).digest('hex'); |
| 94 | + |
| 95 | + const resp = await request.get('/o/apps/plugins?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID); |
| 96 | + const drillConfig = resp.body.plugins.drill; |
| 97 | + |
| 98 | + drillConfig.use_union_with = true; |
| 99 | + |
| 100 | + await request.post('/i/apps/update/plugins') |
| 101 | + .send({ |
| 102 | + app_id: APP_ID, |
| 103 | + api_key: API_KEY_ADMIN, |
| 104 | + args: { drill: drillConfig }, |
| 105 | + }); |
| 106 | + |
| 107 | + await db.collection(oldCollectionName).insertOne({ |
| 108 | + did: 'heatmap_test', |
| 109 | + sg: { |
| 110 | + ...clickData, |
| 111 | + domain: 'https://doma.in', |
| 112 | + view: 'Home', |
| 113 | + }, |
| 114 | + ts: moment('2010-01-02').valueOf(), |
| 115 | + up: { lv: 'Home' }, |
| 116 | + }); |
| 117 | + |
| 118 | + const { body } = await request.post('/o/actions') |
| 119 | + .send({ |
| 120 | + api_key: API_KEY_ADMIN, |
| 121 | + app_id: APP_ID, |
| 122 | + app_key: APP_KEY, |
| 123 | + view: 'Home', |
| 124 | + period: JSON.stringify([moment('2010-01-01').valueOf(), moment('2010-01-31').valueOf()]), |
| 125 | + device: JSON.stringify({ type: 'all', displayText: 'All', minWidth: 0, maxWidth: 10240 }), |
| 126 | + actionType: 'click', |
| 127 | + }); |
| 128 | + |
| 129 | + const { data } = body; |
| 130 | + should(data.length).equal(2); |
| 131 | + should(data[0].sg).eql(clickData); |
| 132 | + should(data[1].sg).eql(clickData); |
| 133 | + |
| 134 | + await db.collection(oldCollectionName).remove({ did: 'heatmap_test' }); |
| 135 | + |
| 136 | + db.close(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('does not get heatmap data from old drill_events collection if union_with is false', async() => { |
| 140 | + const db = await pluginManager.dbConnection('countly_drill'); |
| 141 | + const oldCollectionName = 'drill_events' + crypto.createHash('sha1').update('[CLY]_action' + APP_ID).digest('hex'); |
| 142 | + |
| 143 | + const resp = await request.get('/o/apps/plugins?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID); |
| 144 | + const drillConfig = resp.body.plugins.drill; |
| 145 | + |
| 146 | + drillConfig.use_union_with = false; |
| 147 | + |
| 148 | + await request.post('/i/apps/update/plugins') |
| 149 | + .send({ |
| 150 | + app_id: APP_ID, |
| 151 | + api_key: API_KEY_ADMIN, |
| 152 | + args: { drill: drillConfig }, |
| 153 | + }); |
| 154 | + |
| 155 | + await db.collection(oldCollectionName).insertOne({ |
| 156 | + did: 'heatmap_test', |
| 157 | + sg: { |
| 158 | + ...clickData, |
| 159 | + domain: 'https://doma.in', |
| 160 | + view: 'Home', |
| 161 | + }, |
| 162 | + ts: moment('2010-01-02').valueOf(), |
| 163 | + up: { lv: 'Home' }, |
| 164 | + }); |
| 165 | + |
| 166 | + const { body } = await request.post('/o/actions') |
| 167 | + .send({ |
| 168 | + api_key: API_KEY_ADMIN, |
| 169 | + app_id: APP_ID, |
| 170 | + app_key: APP_KEY, |
| 171 | + view: 'Home', |
| 172 | + period: JSON.stringify([moment('2010-01-01').valueOf(), moment('2010-01-31').valueOf()]), |
| 173 | + device: JSON.stringify({ type: 'all', displayText: 'All', minWidth: 0, maxWidth: 10240 }), |
| 174 | + actionType: 'click', |
| 175 | + }); |
| 176 | + |
| 177 | + const { data } = body; |
| 178 | + should(data.length).equal(1); |
| 179 | + should(data[0].sg).eql(clickData); |
| 180 | + |
| 181 | + drillConfig.use_union_with = true; |
| 182 | + |
| 183 | + await request.post('/i/apps/update/plugins') |
| 184 | + .send({ |
| 185 | + app_id: APP_ID, |
| 186 | + api_key: API_KEY_ADMIN, |
| 187 | + args: { drill: drillConfig }, |
| 188 | + }); |
| 189 | + |
| 190 | + await db.collection(oldCollectionName).remove({ did: 'heatmap_test' }); |
| 191 | + |
| 192 | + db.close(); |
| 193 | + }); |
| 194 | + |
| 195 | + after(async() => { |
| 196 | + const db = await pluginManager.dbConnection('countly_drill'); |
| 197 | + |
| 198 | + await db.collection('drill_events').remove({ did: 'heatmap_test' }); |
| 199 | + |
| 200 | + db.close(); |
| 201 | + }); |
| 202 | +}); |
0 commit comments