Skip to content

Commit

Permalink
Merge pull request #134 from ezpaarse-project/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
felixleo22 authored Oct 20, 2023
2 parents 7d94697 + bb9e946 commit 2080ac3
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ezunpaywall",
"version": "1.2.2",
"version": "1.3.2",
"description": "",
"author": "felixleo22",
"license": "CeCILL",
Expand Down
2 changes: 1 addition & 1 deletion src/apikey/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ezunpaywall-apikey",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"author": "felixleo22",
Expand Down
21 changes: 16 additions & 5 deletions src/enrich/lib/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ async function enrichInFile(data, enrichConfig, state) {
response = await requestGraphql(data, args, index, state.apikey);
} catch (err) {
logger.error(`[graphql] Cannot request graphql service at ${config.get('graphql.host')}/graphql`, JSON.stringify(err?.response?.data?.errors));
await fail(state);
return;
throw err;
}

// enrichment
Expand Down Expand Up @@ -336,7 +335,14 @@ async function processEnrichCSV(id, index, args, state, prefix, separator) {
data = [];
await parser.pause();

await enrichInFile(copyData, enrichConfig, state);
try {
await enrichInFile(copyData, enrichConfig, state);
} catch (err) {
logger.error(`[job csv] Cannot enrich in file [${enrichedFile}]`, err);
await fail(state);
return;
}

await parser.resume();
}
},
Expand All @@ -349,9 +355,14 @@ async function processEnrichCSV(id, index, args, state, prefix, separator) {
enrichConfig.headers = await enrichHeaderCSV(headers, args);
await writeHeaderCSV(headers, separator, enrichedFile);
}
await enrichInFile(data, enrichConfig, state);
try {
await enrichInFile(data, enrichConfig, state);
} catch (err) {
logger.error(`[job csv] Cannot enrich in file [${enrichedFile}]`, err);
await fail(state);
return;
}
}

logger.info(`[job csv] ${state.enrichedLines}/${state.linesRead} enriched lines`);
}

Expand Down
19 changes: 15 additions & 4 deletions src/enrich/lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ async function enrichInFile(data, enrichConfig, state) {
response = await requestGraphql(data, args, index, state.apikey);
} catch (err) {
logger.error(`[graphql] Cannot request graphql service at ${config.get('graphql.host')}/graphql`, JSON.stringify(err?.response?.data?.errors));
await fail(state);
return;
throw err;
}
// enrichment
const enrichedData = enrichArray(data, response);
Expand Down Expand Up @@ -221,14 +220,26 @@ async function processEnrichJSON(id, index, args, prefix, state) {
}

if (data.length === 1000) {
await enrichInFile(data, enrichConfig, state);
try {
await enrichInFile(data, enrichConfig, state);
} catch (err) {
logger.error(`[job jsonl] Cannot enrich in file [${enrichedFile}]`, err);
await fail(state);
return;
}
data = [];
}
}

// last insertion
if (data.length !== 0) {
await enrichInFile(data, enrichConfig, state);
try {
await enrichInFile(data, enrichConfig, state);
} catch (err) {
logger.error(`[job jsonl] Cannot enrich in file [${enrichedFile}]`, err);
await fail(state);
return;
}
}
logger.info(`[job jsonl] ${state.enrichedLines}/${state.linesRead} enriched lines`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/enrich/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ezunpaywall-enrich",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"author": "felixleo22",
Expand Down
2 changes: 1 addition & 1 deletion src/fakeUnpaywall/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fakeunpaywall",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"author": "felixleo22",
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "client",
"version": "1.3.0",
"version": "1.3.2",
"private": true,
"scripts": {
"build": "nuxt build",
Expand Down
7 changes: 6 additions & 1 deletion src/graphql/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { expressMiddleware } = require('@apollo/server/express4');

const { pingRedis, startConnectionRedis } = require('./lib/services/redis');

const getNumberOfDOI = require('./lib/middlewares/args');
const auth = require('./lib/middlewares/auth');

const logger = require('./lib/logger');
Expand Down Expand Up @@ -55,7 +56,11 @@ const server = new ApolloServer({
await server.start();

app.use('/graphql', cors(), json(), auth, expressMiddleware(server, {
context: async ({ req }) => req,
context: async ({ req }) => {
req.countDOI = getNumberOfDOI(req);

return req;
},
}));

app.listen(3000, async () => {
Expand Down
32 changes: 32 additions & 0 deletions src/graphql/lib/middlewares/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function getNumberOfDOI(req) {
const patternBetweenBracket = /.*?(\[.*?\]).*?$/i;
// BODY
// {
// query: 'query ($dois: [ID!]!) {GetByDOI(dois: $dois) {doi, is_oa}}',
// variables: { dois: [ '10.1186/s40510-015-0109-6' ] }
// }
if (req?.body?.variables?.dois) {
return req.body.variables.dois.length;
}
// BODY
// query: '{GetByDOI(dois:["10.1186/s40510-015-0109-6","Coin Coin"]){doi, is_oa}}'
if (req?.body?.query) {
const match = patternBetweenBracket.exec(req?.body?.query);
if (match?.length >= 1) {
const listOfDOI = JSON.parse(match[1]);
return listOfDOI.length;
}
}
// query: '{ GetByDOI(dois:["10.1186/s40510-015-0109-6"]) { doi, is_oa } }
if (req?.query?.query) {
const match = patternBetweenBracket.exec(req.query.query);
if (match?.length >= 1) {
const listOfDOI = JSON.parse(match[1]);
return listOfDOI.length;
}
}

return 0;
}

module.exports = getNumberOfDOI;
10 changes: 4 additions & 6 deletions src/graphql/lib/resolvers/getByDOI.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function flatten(obj) {
return flattened;
}

async function GetByDOI(parent, args, req, info) {
const apikey = req.get('X-API-KEY');
async function GetByDOI(parent, args, contextValue, info) {
const apikey = contextValue.get('X-API-KEY');

if (!apikey) {
throw Error('Not authorized');
Expand Down Expand Up @@ -80,9 +80,9 @@ async function GetByDOI(parent, args, req, info) {
}
}

let index = req?.get('index');
let index = contextValue?.get('index');

const { attributes } = req;
const { attributes } = contextValue;

if (!index) {
index = config.get('elasticsearch.indexAlias');
Expand All @@ -101,8 +101,6 @@ async function GetByDOI(parent, args, req, info) {

const dois = [];

req.countDOI = args?.dois?.length;

// Normalize request
args.dois.forEach((doi) => {
dois.push(doi.toLowerCase());
Expand Down
10 changes: 4 additions & 6 deletions src/graphql/lib/resolvers/unpaywall.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function flatten(obj) {
return flattened;
}

async function unpaywall(parent, args, req, info) {
const apikey = req.get('X-API-KEY');
async function unpaywall(parent, args, contextValue, info) {
const apikey = contextValue.get('X-API-KEY');

if (!apikey) {
throw Error('Not authorized');
Expand Down Expand Up @@ -80,9 +80,9 @@ async function unpaywall(parent, args, req, info) {
}
}

let index = req?.get('index');
let index = contextValue?.get('index');

const { attributes } = req;
const { attributes } = contextValue;

if (!index) {
index = config.get('elasticsearch.indexAlias');
Expand All @@ -101,8 +101,6 @@ async function unpaywall(parent, args, req, info) {

const dois = [];

req.countDOI = args?.dois?.length;

// Normalize request
args.dois.forEach((doi) => {
dois.push(doi.toLowerCase());
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ezunpaywall-graphql-api",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"author": "felixleo22",
Expand Down
2 changes: 1 addition & 1 deletion src/health/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ezunpaywall-health",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"author": "felixleo22",
Expand Down
2 changes: 1 addition & 1 deletion src/mail/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mail",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/update/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ezunpaywall-update",
"version": "1.3.0",
"version": "1.3.2",
"description": "",
"main": "app.js",
"author": "felixleo22",
Expand Down

0 comments on commit 2080ac3

Please sign in to comment.