Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Update edsc_extension to work with new edsc instance (#66)
Browse files Browse the repository at this point in the history
* add url parsers from earthdata src code

* add attribution comments

* clean up

* create cmr query url from edsc code, pass to maap-py

* start granule/collection search

* exclude working! showing correct params. list comment w search

* collection and granule search working

* cleanup, adding sources
  • Loading branch information
mayadebellis authored Aug 11, 2020
1 parent 009efe9 commit 4ad4a90
Show file tree
Hide file tree
Showing 9 changed files with 1,469 additions and 52 deletions.
21 changes: 7 additions & 14 deletions edsc_extension/edsc_extension/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
import nbformat
from notebook.base.handlers import IPythonHandler
import subprocess
sys.path.append('./edsc_extension/maap-py')
print(subprocess.check_output('pwd',shell=True))
#from maap import maap.MAAP as MAAP
import maap
from maap.maap import MAAP

# In local Config
#PATH_TO_MAAP_CFG = './maap-py/maap.cfg'

# In Docker Image Che Config
PATH_TO_MAAP_CFG = '/edsc_extension/maap-py/maap.cfg'

class GetGranulesHandler(IPythonHandler):
def printUrls(self, granules):
Expand All @@ -27,11 +19,11 @@ def printUrls(self, granules):
def get(self):

maap = MAAP()
json_obj = self.get_argument('json_obj', '')
cmr_query = self.get_argument('cmr_query', '')
limit = str(self.get_argument('limit', ''))
print("json obj", json_obj)
print("cmr_query", cmr_query)

query_string = maap.getCallFromEarthdataQuery(json_obj, limit=limit)
query_string = maap.getCallFromCmrUri(cmr_query, limit=limit)
granules = eval(query_string)
query_result = self.printUrls(granules)
try:
Expand All @@ -44,11 +36,12 @@ def get(self):
class GetQueryHandler(IPythonHandler):
def get(self):
maap = MAAP()
json_obj = self.get_argument('json_obj', '')
cmr_query = self.get_argument('cmr_query', '')
limit = str(self.get_argument('limit', ''))
print("json obj", json_obj)
query_type = self.get_argument('query_type', 'granule')
print("cmr_query", cmr_query)

query_string = maap.getCallFromEarthdataQuery(json_obj, limit=limit)
query_string = maap.getCallFromCmrUri(cmr_query, limit=limit, search=query_type)
print("Response is: ", query_string)
self.finish({"query_string": query_string})

Expand Down
3 changes: 2 additions & 1 deletion edsc_extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"@types/jquery": "^3.5.0",
"@types/node": "^14.0.14",
"jquery": "^3.5.1",
"jupyterlab_toastify": "^3.0.0"
"jupyterlab_toastify": "^3.0.0",
"qs": "^6.9.4"
},
"devDependencies": {
"rimraf": "^2.6.1",
Expand Down
159 changes: 159 additions & 0 deletions edsc_extension/src/buildCmrQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { stringify } from 'qs';
import { granuleParams, collectionParams } from "./globals";

export const buildCmrQuery = (urlParams, nonIndexedKeys, permittedCmrKeys, granule=true) => {
return buildParams({
body: camelCaseKeysToUnderscore(urlParams),
nonIndexedKeys,
permittedCmrKeys,
granule
});
}

/**
* Apapted from source: https://github.com/nasa/earthdata-search/blob/f09ff3bfd40420322f005654bc349374aab1fe57/serverless/src/util/cmr/buildParams.js
* Builds a URL used to perform a search request
* @param {object} paramObj Parameters needed to build a search request URL
*/
export const buildParams = (paramObj) => {
const {
body,
nonIndexedKeys,
permittedCmrKeys,
granule,
stringifyResult = true
} = paramObj;


const obj = pick(body, permittedCmrKeys)
granule ? granuleParams = obj : collectionParams = obj;

// console.log("unfiltered", body);
// console.log("filtered", obj)

// For JSON requests we want dont want to stringify the params returned
if (stringifyResult) {
// Transform the query string hash to an encoded url string
const queryParams = prepKeysForCmr(obj, nonIndexedKeys)
return queryParams
}

return obj
}

/**
* Adapted from source https://github.com/nasa/earthdata-search/blob/f09ff3bfd40420322f005654bc349374aab1fe57/serverless/src/util/pick.js
* Select only desired keys from a provided object.
* @param {object} providedObj - An object containing any keys.
* @param {array} keys - An array of strings that represent the keys to be picked.
* @return {obj} An object containing only the desired keys.
*/
export const pick = (providedObj = {}, keys) => {
let obj = null

// if `null` is provided the default parameter will not be
// set so we'll handle it manually
if (providedObj == null) {
obj = {}
} else {
obj = providedObj
}

let filteredObj = {};
keys.forEach((key) => {
let val;
if (key === 'exclude') {
val = getObject(obj, "excluded_granule_ids");
} else {
val = getObject(obj, key);
}
if (val) {
filteredObj[key] = val;
}
})
return filteredObj
}

/*
* Adapted from
* https://stackoverflow.com/questions/15523514/find-by-key-deep-in-a-nested-array
* */
function getObject(theObject, key) {
var result = null;
if(theObject instanceof Array) {
for(var i = 0; i < theObject.length; i++) {
result = getObject(theObject[i], key);
if (result) {
break;
}
}
}
else
{
for(var prop in theObject) {
if(prop == key) {
if(theObject[prop]) {
return theObject[prop];
}
}
if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
result = getObject(theObject[prop], key);
if (result) {
break;
}
}
}
}
return result;
}

/**
* Adapted from source https://github.com/nasa/earthdata-search/blob/f09ff3bfd40420322f005654bc349374aab1fe57/sharedUtils/prepKeysForCmr.js
* Create a query string containing both indexed and non-indexed keys.
* @param {object} queryParams - An object containing all queryParams.
* @param {array} nonIndexedKeys - An array of strings that represent the keys which should not be indexed.
* @return {string} A query string containing both indexed and non-indexed keys.
*/
export const prepKeysForCmr = (queryParams, nonIndexedKeys = []) => {
const nonIndexedAttrs = {}
const indexedAttrs = { ...queryParams }

nonIndexedKeys.forEach((key) => {
nonIndexedAttrs[key] = indexedAttrs[key]
delete indexedAttrs[key]
})

return [
stringify(indexedAttrs),
stringify(nonIndexedAttrs, { indices: false, arrayFormat: 'brackets' })
].filter(Boolean).join('&')
}

/*
* Source: https://stackoverflow.com/questions/30970286/convert-javascript-object-camelcase-keys-to-underscore-case
* */
function camelCaseKeysToUnderscore(obj){
if (typeof(obj) != "object") return obj;

for(let oldName in obj){

// Camel to underscore
let newName = oldName.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});

// Only process if names are different
if (newName != oldName) {
// Check for the old property name to avoid a ReferenceError in strict mode.
if (obj.hasOwnProperty(oldName)) {
obj[newName] = obj[oldName];
delete obj[oldName];
}
}

// Recursion
if (typeof(obj[newName]) == "object") {
obj[newName] = camelCaseKeysToUnderscore(obj[newName]);
}

}
return obj;
}
Loading

0 comments on commit 4ad4a90

Please sign in to comment.