Skip to content

Commit

Permalink
Merge pull request #6 from ezpaarse-project/ezu-command
Browse files Browse the repository at this point in the history
Ezu command
  • Loading branch information
felixleo22 authored Jun 16, 2021
2 parents 1d4d105 + 02a09ad commit 5affabb
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 95 deletions.
67 changes: 42 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
- [Global options](#Global-options)
- [Command line usage](#Command-line-usage)
- [Commands](#Commands)
- [Test](#Test)
## Prerequisites

The tools you need to let node-ezunpaywall run are :
* npm
* nvm
* node 14.15.2
## Installation

Expand All @@ -33,13 +34,15 @@ Update config to fetch ez-unpyawall.
| --- | --- |
| -g --get | display the configuration |
| -s --set | initialize the configuration file in $HOME/.config |
| --url | ezunpaywall url |
| --port | ezunpaywall port |
| -u --url | ezunpaywall url |
| -p --port | ezunpaywall port |
| -k --apikey | admin apikey |
| -l --list | list of attributes required for configuration |
#### Examples
```bash
$ ezunpaywall config --url http://localhost
$ ezunpaywall config --port 8080
$ ezunpaywall config --apikey admin
```
### ezu ping
Check if service is available.
Expand All @@ -58,12 +61,13 @@ Starts an unpaywall data update process. If you use command update without param
| Name | Description |
| --- | --- |
| -f --file | snapshot's file installed on ezunpaywall |
| -l --list | list of snapshot installed on ezunpaywall |
| -sd --startDate | start date to download and insert updates from unpaywall |
| -ed --endDate | end date to download and insert updates from unpaywall |
| -of --offset | line where processing will start |
| -li --limit | line where processing will end |
| -u --use | use a custom config |
| -s --startDate | start date to download and insert updates from unpaywall |
| -e --endDate | end date to download and insert updates from unpaywall |
| -o --offset | line where processing will start |
| -l --limit | line where processing will end |
| -L --list | list of snapshot installed on ezunpaywall |
| -I --index | name of the index to which the data is inserted |
| -U --use | use a custom config |
#### Examples
```bash
# insert the content of the selected file on list
Expand All @@ -80,28 +84,28 @@ $ ezunpaywall update -sd 2020-04-27
$ ezunpaywall update -sd 2020-04-27 -se 2020-07-01
```

### ezu task
get status of processus in courses.
### ezu status
indicates if there is an update.

#### Parameters
| Name | Description |
| --- | --- |
| -u --use | use a custom config |
| -U --use | use a custom config |
#### Example

```bash
$ ezunpaywall task
$ ezunpaywall status
```
### ezu reports
Displays the contents of latest update report on ezunpaywall.
#### Parameters
| Name | Description |
| --- | --- |
| -f --file | report file installed on ezunpaywall |
| -l --list | list of reports generated by ezunpaywall |
| -la --latest | get the latest report |
| -l --latest | get the latest report |
| -s --status | status of report, success and error only accepted |
| -u --use | use a custom config |
| -L --list | list of reports generated by ezunpaywall |
| -U --use | use a custom config |
#### Examples
```bash
# display the latest report
Expand All @@ -127,13 +131,14 @@ By default, if no attributes is informed, we enriched with all attributes.
| -a --attributes | attributes which must be enriched (separeted by comma). By default, all attributes are added |
| -o --out | name of enriched file. By default, the output file is named: out.jsonl |
| -v --verbose | logs how much lines are enriched |
| -u --use | use a custom config |
| -I --index | name of the index from which the data will be retrieved |
| -U --use | use a custom config |
#### Examples
```bash
# enrich with all attributes
# enrich a file with all unpaywall attributes
$ ezunpaywall enricher -f ./pathOfFile.json
# enrich only with oa_status and best_oa_location.url
$ ezunpaywall enricher -f ./pathOfFile.json -a oa_status best_oa_location.url
# enrich a file with graphql argument { is_oa, best_oa_location { license } }
$ ezunpaywall enricher -f ./pathOfFile.json -a "{ is_oa, best_oa_location { license } }"
```
### ezu enrichCSV
Enriched a CSV file with attributes unpaywall
Expand All @@ -146,13 +151,25 @@ by default, if no attributes is informed, we enriched with all attributes.
| -s --separator | separator of csv out file |
| -o --out | name of enriched file. By default, the output file is named: out.csv |
| -v --verbose | logs how much lines are enriched |
| -u --use | use a custom config |
| -I --index | name of the index from which the data will be retrieved |
| -U --use | use a custom config |
#### Examples
```bash
# enrich with all attributes
# enrich a file with all attributes
$ ezunpaywall enricher -f ./pathOfFile.csv
# enrich only with oa_status and best_oa_location.url
$ ezunpaywall enricher -f ./pathOfFile.csv -a oa_status best_oa_location.url -s ","
# enrich a file with graphql argument { is_oa, best_oa_location { license } }
$ ezunpaywall enricher -f ./pathOfFile.csv -a "{ is_oa, best_oa_location { license } }" -s ","
```

To see all available unpaywall attributes, [click here](https://github.com/ezpaarse-project/ez-unpaywall/tree/master#object-structure).
To see all available unpaywall attributes, [click here](https://github.com/ezpaarse-project/ez-unpaywall/tree/master#object-structure).

## Test

If you want to test all the commands, please install [ezunpaywall](https://github.com/ezpaarse-project/ez-unpaywall) and run it in [test mode](https://github.com/ezpaarse-project/ez-unpaywall#start)


Once ezunpaywall has been launched in test mode, you can run at the root of the node-ezunpaywall repository

```bash
$ npm run test
```
52 changes: 31 additions & 21 deletions bin/cmds/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ const inquirer = require('inquirer');
const { connection } = require('../../lib/axios');
const { getConfig } = require('../../lib/config');

/**
* get list of report in ezunpaywall
* @param {object} axios - axios
* @param {object} config - config
* @returns {array<string>} array of name of report
*/
const getReports = async (axios, config) => {
let res;
try {
res = await axios({
method: 'GET',
url: '/update/snapshot',
});
} catch (err) {
console.error(`service unavailable ${config.url}:${config.port}`);
process.exit(1);
}
return res?.data;
};

/**
* get the content of report
* @param {*} args commander arguments
Expand All @@ -12,7 +32,7 @@ const { getConfig } = require('../../lib/config');
* @param -s --status <status> - status of report, success and error only accepted
* @param -u --use <use> - use a custom config
*/
const getReport = async (args) => {
const report = async (args) => {
const axios = await connection(args.use);
const config = await getConfig(args.use);
// check list and latest, file,
Expand Down Expand Up @@ -40,42 +60,32 @@ const getReport = async (args) => {
}
}

let reports;
let res1;
let url = '';
let query = {};
if (!args.status && !args.latest) query = null;
if (args.status) query.status = args.status;
// if -l --list
if (args.list) {
try {
reports = await axios({
method: 'GET',
url: '/reports',
params: query,
});
} catch (err) {
console.error(`service unavailable ${config.url}:${config.port}`);
process.exit(1);
}
if (!reports?.data?.files?.length) {
const reports = await getReports(axios, config);
if (!reports?.length) {
console.log('no reports available');
process.exit(0);
}
const { files: report } = await inquirer.prompt([{
const oneReport = await inquirer.prompt([{
type: 'list',
pageSize: 10,
pageSize: 5,
name: 'files',
choices: reports.data.files,
choices: reports,
message: 'files',
default: reports.data.files.slice(),
default: reports.slice(),
source: (answersSoFar, input) => new Promise((resolve) => {
const result = reports?.data
.filter((file) => file.toLowerCase().includes(input.toLowerCase()));
resolve(result);
}),
}]);
args.file = report;
args.file = oneReport.files;
}

if (args.file) url += `/${args.file}`;
Expand All @@ -84,7 +94,7 @@ const getReport = async (args) => {
try {
res1 = await axios({
method: 'get',
url: `/reports${url}`,
url: `/update/report${url}`,
params: query,
});
} catch (err) {
Expand All @@ -95,9 +105,9 @@ const getReport = async (args) => {
console.error(`service unavailable ${config.url}:${config.port}`);
process.exit(1);
}
console.log(JSON.stringify(res1.data, null, 2));
console.log(JSON.stringify(res1.data?.report, null, 2));
};

module.exports = {
getReport,
report,
};
7 changes: 6 additions & 1 deletion bin/cmds/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const getStatus = async (args) => {
console.error(`service unavailable ${config.url}:${config.port}`);
process.exit(1);
}
console.log(JSON.stringify(res.data, null, 2));
const status = res?.data?.inUpdate;
if (!status) {
console.log('no update is in progress');
} else {
console.log('an update is being done');
}
};

module.exports = {
Expand Down
8 changes: 7 additions & 1 deletion bin/cmds/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const inquirer = require('inquirer');
const { connection } = require('../../lib/axios');
const { getConfig } = require('../../lib/config');

/**
* get list of snapshot installed in ezunpaywall
* @param {object} axios - axios
* @param {object} config - config
* @returns {array<string>} array of name of snapshot
*/
const getFiles = async (axios, config) => {
let res;
try {
Expand Down Expand Up @@ -143,7 +149,7 @@ const update = async (args) => {
});
} catch (err) {
if (err?.response?.status === 409) {
console.log('process in progress');
console.log('update in progress');
process.exit(1);
}
console.error(`service unavailable ${config.url}:${config.port}`);
Expand Down
44 changes: 22 additions & 22 deletions bin/ezunpaywall
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const pkg = require('../package.json');
const { manageConfig } = require('./cmds/config');
const { ping } = require('./cmds/ping');
const { update } = require('./cmds/update');
const { getReport } = require('./cmds/reports');
const { report } = require('./cmds/reports');
const { getStatus } = require('./cmds/status');
const { enrichJSON, enrichCSV } = require('./cmds/enrich');

Expand All @@ -22,46 +22,46 @@ program
.description('config management command to establish the connection between the command and ezunpaywall')
.option('-g --get', 'display the configuration')
.option('-s --set', 'initialize the configuration file in $HOME/.config')
.option('--url <url>', 'ezunpaywall url')
.option('--port <port>', 'ezunpaywall port')
.option('--apikey <apikey>', 'admin apikey')
.option('-l --list', 'list of attributes required for configuration')
.option('-u --url <url>', 'ezunpaywall url')
.option('-p --port <port>', 'ezunpaywall port')
.option('-k -apikey <apikey>', 'admin apikey')
.option('-L --list', 'list of attributes required for configuration')
.action((args) => manageConfig(args));

program
.command('ping')
.description('Check if service is available')
.option('-u --use <use>', 'use a custom config')
.option('-U --use <use>', 'use a custom config')
.action((args) => ping(args));

program
.command('update')
.description('Starts an unpaywall data update process')
.option('-f --file <file>', 'snapshot\'s file installed on ezunpaywall')
.option('-l --list', 'list of snapshot installed on ezunpaywall')
.option('-sd --startDate <starteDate>', 'start date to download and insert updates from unpaywall')
.option('-ed --endDate <endDate>', 'end date to download and insert updates from unpaywall')
.option('-of --offset <offset>', 'line where processing will start')
.option('-li --limit <limit>', 'line where processing will end')
.option('-i --index <index>', 'name of the index to which the data is inserted')
.option('-u --use <use>', 'use a custom config')
.option('-s --startDate <starteDate>', 'start date to download and insert updates from unpaywall')
.option('-e --endDate <endDate>', 'end date to download and insert updates from unpaywall')
.option('-o --offset <offset>', 'line where processing will start')
.option('-l --limit <limit>', 'line where processing will end')
.option('-L --list', 'list of snapshot installed on ezunpaywall')
.option('-I --index <index>', 'name of the index to which the data is inserted')
.option('-U --use <use>', 'use a custom config')
.action((args) => update(args));

program
.command('status')
.description('Indicates if an update process is running')
.option('-u --use <use>', 'use a custom config')
.option('-U --use <use>', 'use a custom config')
.action((args) => getStatus(args));

program
.command('report')
.description('get the content report')
.option('-f --file <file>', 'report file installed on ezunpaywall')
.option('-l --list', 'list of reports generated by ezunpaywall')
.option('-la --latest [latest]', 'boolean')
.option('-l --latest [latest]', 'boolean')
.option('-s --status <status>', 'status of report, success and error only accepted')
.option('-u --use <use>', 'use a custom config')
.action((args) => getReport(args));
.option('-L --list', 'list of reports generated by ezunpaywall')
.option('-U --use <use>', 'use a custom config')
.action((args) => report(args));

program
.command('enrichJSON')
Expand All @@ -71,8 +71,8 @@ program
.option('-a --attributes <attributes>', 'attributes which must be enriched in graphql format. By default, all attributes are added')
.option('-o --out <out>', 'name of enriched file. By default, the output file is named: out.jsonl')
.option('-v --verbose', 'logs how much lines are enriched')
.option('-u --use <use>', 'use a custom config')
.option('-i --index <index>', 'name of the index from which the data will be retrieved')
.option('-I --index <index>', 'name of the index from which the data will be retrieved')
.option('-U --use <use>', 'use a custom config')
.action((args) => enrichJSON(args));

program
Expand All @@ -84,8 +84,8 @@ program
.option('-s --separator <separator>', 'separator of csv out file')
.option('-o --out <out>', 'name of enriched file')
.option('-v --verbose', 'logs how much lines are enriched')
.option('-u --use <use>', 'use a custom config')
.option('-i --index <index>', 'name of the index from which the data will be retrieved')
.option('-I --index <index>', 'name of the index from which the data will be retrieved')
.option('-U --use <use>', 'use a custom config')
.action((args) => enrichCSV(args));

program.parse(process.argv);
3 changes: 0 additions & 3 deletions test.jsonl

This file was deleted.

Loading

0 comments on commit 5affabb

Please sign in to comment.