Skip to content

Commit

Permalink
Merge pull request #140 from Amsterdam/hotfix/018-add-choices-guide-s…
Browse files Browse the repository at this point in the history
…tats

Hotfix/018 add choices guide stats
  • Loading branch information
nlsvgtr authored Mar 15, 2021
2 parents a0829fa + 178a061 commit 46eb110
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 15 deletions.
110 changes: 110 additions & 0 deletions src/middleware/search-results-static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Currently there are 2 implementations of search
* Both are not sufficient.
* This one only works for ideas properly
* The other one only works for users, combined with
*
* @type {Config}
*/
const config = require('config');
const fuzzysort = require('fuzzysort');

//middleware for adding search to
module.exports = function( req, res, next ) {
let search = req.query.search;

// if no search query isset move on
if (!search) {
next();
} else {
let list = req.results;

if ( !Array.isArray(search.criteria) ) search.criteria = [ search.criteria ];

let results = [];
search.criteria.forEach((criterium) => {

let key = Object.keys(criterium)[0];
let value = criterium[key];
// todo: optional { fields: [], value: '' } construct

let searchFields;
switch (key) {

case 'text':
searchFields = ['title', 'summary', 'description', 'niels'];
case 'title':
case 'summary':
case 'description':
searchFields = searchFields || [ key ];
let searchResult = fuzzysort.go(value, list, {
threshold: -300, // todo: volkomen arbitrair; misschien moet je hem kunnen meesturen
keys: searchFields,
});

results.push( searchResult );
break;

default:
}
});

// mergen van de resultaten
// TODO: dit is heel dom en bot en (dus) traag; denk er eens echt over na
let merged = [];
if (results.length == 1) {
merged = results[0];
} else {
if (search.options.andOr == 'and') {

merged = [];
for (let i=0; i<results.length; i++) {
results[i].map( result => {

let foundScore = -10000000000000;
for (let j=i+1; j<results.length; j++) {
let found = results[j].find( elem => elem.obj.id == result.obj.id );
if (found && foundScore) {
foundScore = Math.max(foundScore, found.score);
} else {
foundScore = undefined;
}
}

if (foundScore && foundScore != -10000000000000) {
// use highest score
result.score = Math.max(result.score, foundScore);
let found = merged.find( elem => elem.obj.id == result.obj.id );
if (!found) {
merged.push(result)
}
}

})
}
} else {
merged = [];
for (let i=0; i<results.length; i++) {
results[i].map( result => {
let found = merged.find( elem => elem.obj.id == result.obj.id );
if (found) {
// use highest score
found.score = Math.max(found.score, result.score)
} else {
merged.push(result)
}
})
}
}

}

merged = merged.sort( (a,b) => b.score - a.score )
merged = merged.map(elem => elem.obj);

req.results = merged

return next();
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Currently there are 2 implementations of search
* Both are not sufficient.
* This one only works for users properly when querying from
* The other one only works for ideas
*
* @type {Config}
*/
const config = require('config');
const fuzzysort = require('fuzzysort');

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/area.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');
const convertDbPolygonToLatLng = require('../../util/convert-db-polygon-to-lat-lng');
const {formatGeoJsonToPolygon} = require('../../util/geo-json-formatter');

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const createError = require('http-errors');
const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');

const router = require('express-promise-router')({ mergeParams: true });

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const mail = require('../../lib/mail');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');
const isJson = require('../../util/isJson');

const router = express.Router({mergeParams: true});
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/externalSite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');

const router = require('express-promise-router')({ mergeParams: true });
var createError = require('http-errors');
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/idea.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const mail = require('../../lib/mail');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');
const isJson = require('../../util/isJson');

const router = express.Router({ mergeParams: true });
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/newslettersignup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const auth = require('../../middleware/sequelize-authorization-middleware');
const mail = require('../../lib/mail');
const generateToken = require('../../util/generate-token');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');

const router = express.Router({ mergeParams: true });

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const createError = require('http-errors')
const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');

const router = require('express-promise-router')({mergeParams: true});

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config = require('config');
const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-user');
const oauthClients = require('../../middleware/oauth-clients');
const checkHostStatus = require('../../services/checkHostStatus')

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const express = require('express');
const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');

let router = express.Router({mergeParams: true});

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const db = require('../../db');
const auth = require('../../middleware/sequelize-authorization-middleware');
const pagination = require('../../middleware/pagination');
const {Op} = require('sequelize');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-user');
const fetch = require('node-fetch');
const rp = require('request-promise');

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const merge = require('merge');
const bruteForce = require('../../middleware/brute-force');
const {Op} = require('sequelize');
const pagination = require('../../middleware/pagination');
const searchResults = require('../../middleware/search-results');
const searchResults = require('../../middleware/search-results-static');

const router = express.Router({mergeParams: true});

Expand Down
14 changes: 10 additions & 4 deletions src/routes/stats/overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,25 @@ router.route('/')
{
key: 'argumentCountTotal',
description: 'Amount of arguments, total count',
sql: "SELECT count(arguments.id) AS counted FROM arguments LEFT JOIN ideas ON ideas.id = arguments.ideaId LEFT JOIN sites ON sites.id = ideas.siteId WHERE arguments.deletedAt IS NULL AND ideas.deletedAt IS NULL AND ideas.siteId=?",
sql: "SELECT count(arguments.id) AS counted FROM arguments LEFT JOIN ideas ON ideas.id = arguments.ideaId WHERE arguments.deletedAt IS NULL AND ideas.deletedAt IS NULL AND ideas.siteId=?",
variables: [req.params.siteId],
},
{
key: 'argumentForCountTotal',
description: 'Amount of arguments for an idea, total count',
sql: "SELECT count(arguments.id) AS counted FROM arguments LEFT JOIN ideas ON ideas.id = arguments.ideaId LEFT JOIN sites ON sites.id = ideas.siteId WHERE arguments.deletedAt IS NULL AND ideas.deletedAt IS NULL AND ideas.siteId=? AND arguments.sentiment = 'for'",
sql: "SELECT count(arguments.id) AS counted FROM arguments LEFT JOIN ideas ON ideas.id = arguments.ideaId WHERE arguments.deletedAt IS NULL AND ideas.deletedAt IS NULL AND ideas.siteId=? AND arguments.sentiment = 'for'",
variables: [req.params.siteId],
},
{
key: 'argumentAgainstCountTotal',
description: 'Amount of arguments against an idea, total count',
sql: "SELECT count(arguments.id) AS counted FROM arguments LEFT JOIN ideas ON ideas.id = arguments.ideaId LEFT JOIN sites ON sites.id = ideas.siteId WHERE arguments.deletedAt IS NULL AND ideas.deletedAt IS NULL AND ideas.siteId=? AND arguments.sentiment = 'against'",
sql: "SELECT count(arguments.id) AS counted FROM arguments LEFT JOIN ideas ON ideas.id = arguments.ideaId WHERE arguments.deletedAt IS NULL AND ideas.deletedAt IS NULL AND ideas.siteId=? AND arguments.sentiment = 'against'",
variables: [req.params.siteId],
},
{
key: 'choicesguideresultsCountTotal',
description: 'Amount of choices guide results',
sql: "SELECT count(choicesGuideResults.id) AS counted FROM choicesGuideResults LEFT JOIN choicesGuides ON choicesGuides.id = choicesGuideResults.choicesGuideId WHERE choicesGuideResults.deletedAt IS NULL AND choicesGuides.deletedAt IS NULL AND choicesGuides.siteId=?",
variables: [req.params.siteId],
},
];
Expand All @@ -231,7 +237,7 @@ router.route('/')
next();
} catch (e) {
console.log('Error while initialising SQL connection: ', e);
return next(createError(401, 'Error while initialising SQL connection: ', e));
return next(createError(500, 'Error while initialising SQL connection: ', e));
}
})
.get((req, res, next) => {
Expand Down

0 comments on commit 46eb110

Please sign in to comment.