Преглед изворни кода

:recycle: moving match queue routes | renaming match queue classes

tags/0.0.1
j пре 4 година
родитељ
комит
6c47553439

+ 12
- 0
backend/db/migrations/20211003195015_create_match_queues_table.js Прегледај датотеку

@@ -0,0 +1,12 @@
1
+exports.up = function (knex) {
2
+    return knex.schema.createTable('match_queues', function (table) {
3
+        table.increments('match_queue_id').primary()
4
+        table.integer('profile_id').notNullable()
5
+        table.integer('target_id').notNullable()
6
+        table.boolean('is_deleted').notNullable()
7
+    })
8
+}
9
+
10
+exports.down = function (knex) {
11
+    return knex.schema.dropTable('match_queues')
12
+}

+ 11
- 0
backend/db/mock.js Прегледај датотеку

@@ -1423,4 +1423,15 @@ module.exports = {
1423 1423
         { message_id: 8, user_id: 1, grouping_id: 5, message: 'hello!' },
1424 1424
         { message_id: 9, user_id: 3, grouping_id: 5, message: 'meow' },
1425 1425
     ],
1426
+    match_queues: [
1427
+        { match_queue_id: 1, profile_id: 4, target_id: 1, is_deleted: false },
1428
+        { match_queue_id: 2, profile_id: 4, target_id: 3, is_deleted: false },
1429
+        { match_queue_id: 3, profile_id: 4, target_id: 2, is_deleted: false },
1430
+        { match_queue_id: 4, profile_id: 1, target_id: 4, is_deleted: false },
1431
+        { match_queue_id: 5, profile_id: 1, target_id: 6, is_deleted: false },
1432
+        { match_queue_id: 6, profile_id: 1, target_id: 8, is_deleted: false },
1433
+        { match_queue_id: 7, profile_id: 2, target_id: 8, is_deleted: false },
1434
+        { match_queue_id: 8, profile_id: 2, target_id: 6, is_deleted: false },
1435
+        { match_queue_id: 9, profile_id: 2, target_id: 4, is_deleted: false },
1436
+    ],
1426 1437
 }

+ 11
- 0
backend/db/seeds/09-match_queues.js Прегледај датотеку

@@ -0,0 +1,11 @@
1
+const mock = require('../mock')
2
+
3
+exports.seed = function (knex) {
4
+    // Deletes ALL existing entries
5
+    return knex('match_queues')
6
+        .truncate()
7
+        .then(function () {
8
+            // Inserts seed entries
9
+            return knex('match_queues').insert(mock.match_queues)
10
+        })
11
+}

+ 5
- 0
backend/lib/index.js Прегледај датотеку

@@ -2,6 +2,7 @@ const UserPlugin = require('./plugins/user')
2 2
 const MembershipPlugin = require('./plugins/membership')
3 3
 const SurveyPlugin = require('./plugins/survey')
4 4
 const ProfilePlugin = require('./plugins/profile')
5
+const MatchQueuePlugin = require('./plugins/matchqueue')
5 6
 
6 7
 /**
7 8
  * A Hapi server instance
@@ -27,6 +28,10 @@ exports.plugin = {
27 28
             routes: { prefix: '/user' },
28 29
         })
29 30
 
31
+        await server.register(MatchQueuePlugin, {
32
+            routes: { prefix: '/matchqueue' },
33
+        })
34
+
30 35
         await server.register(MembershipPlugin, {
31 36
             routes: { prefix: '/membership' },
32 37
         })

+ 5
- 9
backend/lib/models/matchqueue.js Прегледај датотеку

@@ -1,21 +1,17 @@
1 1
 const Schwifty = require('@hapipal/schwifty')
2 2
 const Joi = require('joi')
3 3
 
4
-const Profile = require('./profile')
5
-// const User = require('./user')
6
-// const Membership = require('./membership')
7
-
8
-module.exports = class MatchQue extends Schwifty.Model {
4
+module.exports = class MatchQueue extends Schwifty.Model {
9 5
     static get tableName() {
10
-        return 'matchque'
6
+        return 'match_queues'
11 7
     }
12 8
 
13 9
     static get joiSchema() {
14 10
         return Joi.object({
15
-            match_id: Joi.number(),
11
+            match_queue_id: Joi.number(),
16 12
             profile_id: Joi.number().required(),
17
-            profile_id_2: Joi.number().required(),
18
-            deleted: Joi.boolean().required(),
13
+            target_id: Joi.number().required(),
14
+            is_deleted: Joi.boolean().required(),
19 15
         })
20 16
     }
21 17
 }

+ 6
- 8
backend/lib/plugins/matchqueue.js Прегледај датотеку

@@ -2,11 +2,10 @@ const Objection = require('objection')
2 2
 const Schmervice = require('@hapipal/schmervice')
3 3
 const Schwifty = require('@hapipal/schwifty')
4 4
 
5
-const MatchQueService = require('../services/matchqueue')
5
+const MatchQueueService = require('../services/matchqueue')
6 6
 
7
-const MatchQueModel = require('../models/matchqueue')
8
-const MatchQueRoute = require('../routes/matchqueue/matchqueue')
9
-const MatchQueChooseRoute = require('../routes/matchqueue/choosematch')
7
+const MatchQueueModel = require('../models/matchqueue')
8
+const MatchQueueChooseRoute = require('../routes/matchqueue/choosematch')
10 9
 
11 10
 module.exports = {
12 11
     name: 'matchqueue-plugin',
@@ -14,7 +13,7 @@ module.exports = {
14 13
     register: async (server, options) => {
15 14
         await server.register(Schwifty)
16 15
 
17
-        await server.registerModel(MatchQueModel)
16
+        await server.registerModel(MatchQueueModel)
18 17
 
19 18
         server.bind({
20 19
             transaction: fn => Objection.transaction(server.knex(), fn),
@@ -22,9 +21,8 @@ module.exports = {
22 21
 
23 22
         await server.register(Schmervice)
24 23
 
25
-        server.registerService(MatchQueService)
24
+        server.registerService(MatchQueueService)
26 25
 
27
-        await server.route(MatchQueRoute)
28
-        await server.route(MatchQueChooseRoute)
26
+        await server.route(MatchQueueChooseRoute)
29 27
     },
30 28
 }

+ 2
- 0
backend/lib/plugins/profile.js Прегледај датотеку

@@ -11,6 +11,7 @@ const ProfileScoreRoute = require('../routes/profile/score')
11 11
 const ProfileUpdateRoute = require('../routes/profile/update')
12 12
 const ProfileRespondRoute = require('../routes/profile/respond')
13 13
 const ProfileMatchRoute = require('../routes/profile/match')
14
+const ProfileQueueRoute = require('../routes/profile/queue')
14 15
 
15 16
 module.exports = {
16 17
     name: 'profile-plugin',
@@ -33,5 +34,6 @@ module.exports = {
33 34
         await server.route(ProfileRespondRoute)
34 35
         await server.route(ProfileUpdateRoute)
35 36
         await server.route(ProfileMatchRoute)
37
+        await server.route(ProfileQueueRoute)
36 38
     },
37 39
 }

+ 0
- 79
backend/lib/routes/matchqueue/matchqueue.js Прегледај датотеку

@@ -1,79 +0,0 @@
1
-'use strict'
2
-
3
-const Joi = require('joi')
4
-
5
-const pluginConfig = {
6
-    handlerType: 'matchque',
7
-    docs: {
8
-        description: 'inserts scored matches',
9
-        notes: 'Waits for Scoring Service, and inserts any that does not exist in MatchQue Table',
10
-    },
11
-}
12
-
13
-const validators = {
14
-    //     /** Validate the header (cookie check) */
15
-    //     // headers: true,
16
-
17
-    //     /** Validate the route params (/active/{thing}) */
18
-    params: Joi.object({ profile_id: Joi.number() }),
19
-
20
-    //     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
21
-    //     // query: true,
22
-
23
-    //     /** Validate the incoming payload (POST method) */
24
-
25
-    payload: Joi.object({
26
-        maxDistance: Joi.number().required(),
27
-        distanceUnit: Joi.string().required(),
28
-    }),
29
-}
30
-
31
-module.exports = {
32
-    method: 'POST',
33
-    path: '/{profile_id}/matches',
34
-    options: {
35
-        ...pluginConfig.docs,
36
-        tags: ['api'],
37
-        /** Protect this route with authentication? */
38
-        auth: false,
39
-        handler: async function (request, h) {
40
-            const { profile_id } = request.params
41
-            const { maxDistance, distanceUnit } = request.payload
42
-            const { matchQueService, profileService } =
43
-                request.server.services()
44
-            // get results back from profileServices.scoreProfilesFor(profile_id, maxDistance, distanceUnit)
45
-            const potentials = await profileService.scoreProfilesFor(
46
-                profile_id,
47
-                maxDistance,
48
-                distanceUnit,
49
-            )
50
-            let potentialProfileIds = potentials.map(
51
-                potential => potential.profile_id,
52
-            )
53
-            potentialProfileIds = [...new Set(potentialProfileIds)]
54
-
55
-            await matchQueService.insertScoredProfilesIntoMatchQue(
56
-                profile_id,
57
-                potentialProfileIds,
58
-            )
59
-
60
-            return matchQueService.getPotentials(profile_id)
61
-        },
62
-        /** Validate based on validators object */
63
-        validate: {
64
-            ...validators,
65
-            failAction: 'log',
66
-        },
67
-
68
-        // couldn't get validate server response working...
69
-
70
-        /** Validate the server response */
71
-        // response: {
72
-        //     schema: Joi.object({
73
-        //         ok: Joi.bool(),
74
-        //         handler: Joi.string(),
75
-        //         data: Joi.object(),
76
-        //     }),
77
-        // },
78
-    },
79
-}

+ 48
- 0
backend/lib/routes/profile/queue.js Прегледај датотеку

@@ -0,0 +1,48 @@
1
+'use strict'
2
+
3
+const Joi = require('joi')
4
+
5
+const pluginConfig = {
6
+    handlerType: 'profile',
7
+    docs: {
8
+        description: 'Returns previously scored profiles',
9
+        notes: 'returns from the MatchQueue Table',
10
+    },
11
+}
12
+
13
+const validators = {
14
+    params: Joi.object({ profile_id: Joi.number() }),
15
+}
16
+
17
+module.exports = {
18
+    method: 'GET',
19
+    path: '/{profile_id}/queue',
20
+    options: {
21
+        ...pluginConfig.docs,
22
+        tags: ['api'],
23
+        /** Protect this route with authentication? */
24
+        auth: false,
25
+        handler: async function (request, h) {
26
+            const { profile_id } = request.params
27
+            const { matchQueueService } = request.server.services()
28
+
29
+            return await matchQueueService.getPotentials(profile_id)
30
+        },
31
+        /** Validate based on validators object */
32
+        validate: {
33
+            ...validators,
34
+            failAction: 'log',
35
+        },
36
+
37
+        // couldn't get validate server response working...
38
+
39
+        /** Validate the server response */
40
+        // response: {
41
+        //     schema: Joi.object({
42
+        //         ok: Joi.bool(),
43
+        //         handler: Joi.string(),
44
+        //         data: Joi.object(),
45
+        //     }),
46
+        // },
47
+    },
48
+}

+ 33
- 17
backend/lib/routes/profile/score.js Прегледај датотеку

@@ -20,7 +20,7 @@ const validators = {
20 20
     }),
21 21
 
22 22
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
23
-    query:Joi.object({
23
+    query: Joi.object({
24 24
         max_distance: Joi.number(),
25 25
         unit: Joi.string(),
26 26
     }),
@@ -45,27 +45,43 @@ module.exports = {
45 45
         auth: false,
46 46
 
47 47
         handler: async function (request, h) {
48
-            const { profileService } = request.services()
48
+            const { profileService, matchQueueService } =
49
+                request.server.services()
50
+
49 51
             const profileId = request.params.profile_id
50 52
             const maxDistanceMiles = request.query.max_distance
51
-            const distanceUnit = request.query.unit ? request.query.unit : 'mile'
52
-            const profiles = await profileService.scoreProfilesFor(profileId, maxDistanceMiles, distanceUnit)
53
+            const distanceUnit = request.query.unit
54
+                ? request.query.unit
55
+                : 'mile'
56
+            const profiles = await profileService.scoreProfilesFor(
57
+                profileId,
58
+                maxDistanceMiles,
59
+                distanceUnit,
60
+            )
61
+            await matchQueueService.insertScoredProfilesIntoMatchQueue(
62
+                profileId,
63
+                profiles.map(profile => profile.profile_id),
64
+            )
53 65
             try {
54
-                if(!profiles){
66
+                if (!profiles) {
55 67
                     throw new RangeError('Unable to score profiles')
56 68
                 }
57
-                
58
-                return h.response({
59
-                    ok: true,
60
-                    handler: pluginConfig.handlerType,
61
-                    data: profiles,
62
-                }).code(200)
69
+
70
+                return h
71
+                    .response({
72
+                        ok: true,
73
+                        handler: pluginConfig.handlerType,
74
+                        data: profiles,
75
+                    })
76
+                    .code(200)
63 77
             } catch (err) {
64
-                return h.response({
65
-                    ok: false,
66
-                    handler: pluginConfig.handlerType,
67
-                    data: { error: `${err}` },
68
-                }).code(409)
78
+                return h
79
+                    .response({
80
+                        ok: false,
81
+                        handler: pluginConfig.handlerType,
82
+                        data: { error: `${err}` },
83
+                    })
84
+                    .code(409)
69 85
             }
70 86
         },
71 87
 
@@ -88,7 +104,7 @@ module.exports = {
88 104
                     handler: Joi.string(),
89 105
                     data: responseSchemas.error,
90 106
                 }),
91
-            }
107
+            },
92 108
         },
93 109
     },
94 110
 }

+ 18
- 18
backend/lib/services/matchqueue.js Прегледај датотеку

@@ -1,15 +1,15 @@
1 1
 const Schmervice = require('@hapipal/schmervice')
2 2
 
3
-module.exports = class MatchQueService extends Schmervice.Service {
3
+module.exports = class MatchQueueService extends Schmervice.Service {
4 4
     constructor(...args) {
5 5
         super(...args)
6 6
     }
7 7
 
8 8
     async getPotentials(profileId) {
9
-        const { MatchQue } = this.server.models()
10
-        const allPotentials = await MatchQue.query()
9
+        const { MatchQueue } = this.server.models()
10
+        const allPotentials = await MatchQueue.query()
11 11
             .where('profile_id', profileId)
12
-            .andWhere('deleted', false)
12
+            .andWhere('is_deleted', false)
13 13
         return allPotentials
14 14
     }
15 15
     /**
@@ -17,21 +17,21 @@ module.exports = class MatchQueService extends Schmervice.Service {
17 17
      * @param {number} profileId
18 18
      * @param {array} potentialProfileIds
19 19
      */
20
-    async insertScoredProfilesIntoMatchQue(profileId, potentialProfileIds) {
21
-        const { MatchQue } = this.server.models()
20
+    async insertScoredProfilesIntoMatchQueue(profileId, potentialProfileIds) {
21
+        const { MatchQueue } = this.server.models()
22 22
 
23 23
         // returns an array of all matches for the profileId where the profile_id_2 already exists in the potentialProfileIds array
24
-        await MatchQue.query()
24
+        await MatchQueue.query()
25 25
             .patch({
26
-                deleted: true,
26
+                is_deleted: true,
27 27
             })
28 28
             .where('profile_id', profileId)
29 29
 
30 30
         for (let potentialProfileId of potentialProfileIds) {
31
-            await MatchQue.query().insert({
31
+            await MatchQueue.query().insert({
32 32
                 profile_id: profileId,
33
-                profile_id_2: potentialProfileId,
34
-                deleted: false,
33
+                target_id: potentialProfileId,
34
+                is_deleted: false,
35 35
             })
36 36
         }
37 37
 
@@ -40,24 +40,24 @@ module.exports = class MatchQueService extends Schmervice.Service {
40 40
     /**
41 41
      * Set the rows deleted as true, does NOT DELETE from database
42 42
      * @param {number} profileId
43
-     * @param {number} profileId2
43
+     * @param {number} targetId
44 44
      * @param {boolean} reinsert
45 45
      * @returns
46 46
      */
47
-    async markAsDeleted(profileId, profileId2, reinsert) {
48
-        const { MatchQue } = this.server.models()
49
-        await MatchQue.query()
47
+    async markAsDeleted(profileId, targetId, reinsert) {
48
+        const { MatchQueue } = this.server.models()
49
+        await MatchQueue.query()
50 50
             .patch({
51 51
                 deleted: true,
52 52
             })
53 53
             .where('profile_id', profileId)
54
-            .andWhere('profile_id_2', profileId2)
54
+            .andWhere('target_id', targetId)
55 55
             .first()
56 56
 
57 57
         if (reinsert) {
58
-            await MatchQue.query().insert({
58
+            await MatchQueue.query().insert({
59 59
                 profile_id: profileId,
60
-                profile_id_2: profileId2,
60
+                target_id: targetId,
61 61
                 deleted: false,
62 62
             })
63 63
         }

+ 13
- 19
backend/lib/services/profile.js Прегледај датотеку

@@ -3,7 +3,6 @@ const cosineSimilarity = require('compute-cosine-similarity')
3 3
 const haversine = require('haversine')
4 4
 
5 5
 const runMatch = (allYins, allYangs) => {
6
-    balanceSides(allYins, allYangs)
7 6
     console.log(allYins.length, ':', allYangs.length)
8 7
     // You only need to engage from one side
9 8
     engageEveryone(allYins)
@@ -30,14 +29,6 @@ const engageEveryone = allYins => {
30 29
         })
31 30
     } while (!done)
32 31
 }
33
-const balanceSides = (yins, yangs) => {
34
-    let diff = Math.abs(yangs.length - yins.length)
35
-    let smallerList = yangs.length < yins.length ? yangs : yins
36
-    const totalProfiles = yangs.length + yins.length + 1
37
-    for (let i = 0; i < diff; i++) {
38
-        smallerList.push(new ProfileFacade(totalProfiles + i))
39
-    }
40
-}
41 32
 class ProfileFacade {
42 33
     constructor(id, matchQueue) {
43 34
         this.realId = id ? id : undefined
@@ -351,9 +342,20 @@ module.exports = class ProfileService extends Schmervice.Service {
351 342
 
352 343
         // Grab all profiles with matchQueues
353 344
         let allProfiles = await Profile.query().withGraphFetched('user')
354
-
345
+        const seekerIds = allProfiles
346
+            .filter(profile => profile.user.is_poster == 0)
347
+            .map(profile => profile.profile_id)
348
+        const posterIds = allProfiles
349
+            .filter(profile => profile.user.is_poster == 1)
350
+            .map(profile => profile.profile_id)
351
+        let diff = Math.abs(posterIds.length - seekerIds.length)
352
+        let smallerList =
353
+            posterIds.length < seekerIds.length ? posterIds : seekerIds
354
+        // ADD DUMMY IDS TO THE SMALLER LIST
355
+        for (let d = 0; d < diff; d++) {
356
+            smallerList.push(allProfiles.length + d)
357
+        }
355 358
         // !:FAKE Score everyone
356
-        // const profileIds = allProfiles.map(profile => profile.profile_id)
357 359
         const scoredProfileQueuesById = {}
358 360
         for (let profile of allProfiles) {
359 361
             const profileQueue = await this.scoreProfilesFor(
@@ -377,14 +379,6 @@ module.exports = class ProfileService extends Schmervice.Service {
377 379
             return new ProfileFacade(profile.profile_id, profileFacadeQueue)
378 380
         })
379 381
         // // ! FAKE -- END
380
-
381
-        const seekerIds = allProfiles
382
-            .filter(profile => profile.user.is_poster == 0)
383
-            .map(profile => profile.profile_id)
384
-        const posterIds = allProfiles
385
-            .filter(profile => profile.user.is_poster == 1)
386
-            .map(profile => profile.profile_id)
387
-
388 382
         const yins = seekerIds.map(id => allProfileFacadesWithQueue[id])
389 383
         const yangs = posterIds.map(id => allProfileFacadesWithQueue[id])
390 384
 

Loading…
Откажи
Сачувај