Skip to content

Commit

Permalink
C taxonomy (#20)
Browse files Browse the repository at this point in the history
* taxonomy getParents and getParentsMany

* 1.6.0
  • Loading branch information
daviortega committed Dec 4, 2018
1 parent 1b7e28c commit f72772b
Show file tree
Hide file tree
Showing 4 changed files with 1,090 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mist3",
"version": "1.5.5",
"version": "1.6.0",
"description": "Helper package to deal with MiST3 API",
"main": "src/index.js",
"scripts": {
Expand Down
95 changes: 95 additions & 0 deletions src/Taxonomy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict'

const https = require('https')
const bunyan = require('bunyan')

const NodeMist3 = require('./NodeMist3Abstract')

const kDefaults = {
maxRequests: 30
}

module.exports =
class Taxonomy extends NodeMist3 {
constructor(options, logLevel = 'info') {
super(options)
this.log = bunyan.createLogger(
{
name: 'node-mist3-taxonomy',
level: logLevel
}
)
}

getParents(taxid, options = {skipFailed: false}) {
this.log.info(`Getting parents of taxid: ${taxid} with options ${JSON.stringify(options)}`)
this.httpsOptions.method = 'GET'
this.httpsOptions.path = `/v1/taxonomy/${taxid}/parents`
return new Promise((resolve, reject) => {
const request = https.request(this.httpsOptions, (response) => {
if (response.statusCode === 504) {
if (options.skipFailed){
this.log.warn(`taxid ${taxid} ::: ${response.statusCode} - ${response.statusMessage}`)
return resolve([])
}
else {
this.log.error(`taxid ${taxid} ::: ${response.statusCode} - ${response.statusMessage}`)
return reject(response.statusCode)
}
}
const chunks = []
response.on('data', (chunk) => chunks.push(chunk))
response.on('end', () => {
const buffer = Buffer.concat(chunks)
let taxonomy = []
try {
taxonomy = JSON.parse(buffer)
resolve(taxonomy)
}
catch (err) {
if (options.skipFailed === true){
this.log.warn(buffer.toString())
resolve([])
}
else {
console.log('hey')
this.log.error(buffer.toString())
reject(err)
}
}
})
response.on('error', (err) => {
this.log.fatal(err)
reject(err)
})
})
request.end()
})
}

getParentsMany(taxids, options = {skipFailed: true}) {
const tmpTaxids = []
taxids.forEach((taxid) => tmpTaxids.push(taxid))
this.log.info(`getting taxonomy from ${tmpTaxids.length} taxids`)
const self = this
async function asyncGetMany (listOfTaxids) {
const allResponses = []
self.log.debug(`Full list: ${listOfTaxids}`)
while (listOfTaxids.length !== 0) {
const requests = []
const batch = listOfTaxids.splice(0, kDefaults.maxRequests)
self.log.debug(`new Batch: ${batch}`)
batch.forEach((taxid) => requests.push(self.getParents(taxid, options)))
await Promise.all(requests).then((responses) => {
responses.forEach((response) => {
allResponses.push(response)
})
}).catch((err) => {
throw err
})
}
return allResponses
}
return asyncGetMany(tmpTaxids)
}
}
Loading

0 comments on commit f72772b

Please sign in to comment.