Skip to content

Commit aac5687

Browse files
committed
Re-factor fetch gorouting code; add support for detail option
1 parent 88d3829 commit aac5687

1 file changed

Lines changed: 52 additions & 31 deletions

File tree

main.go

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func process(query string, jsonout bool, sep string) {
265265
}
266266
var dasrecords []mongo.DASRecord
267267
if len(urls) > 0 {
268-
dasrecords = processURLs(dasquery, urls, maps, dmaps, pkeys)
268+
dasrecords = processURLs(dasquery, urls, maps, &dmaps, pkeys)
269269
} else if len(localApis) > 0 {
270270
dasrecords = processLocalApis(dasquery, localApis, pkeys)
271271
}
@@ -362,16 +362,65 @@ func printRecords(dasrecords []mongo.DASRecord, selectKeys [][]string, jsonout b
362362
}
363363
}
364364

365+
// helper function to get DAS records out of url response
366+
func response2Records(r *utils.ResponseType, dasquery dasql.DASQuery, maps []mongo.DASRecord, dmaps *dasmaps.DASMaps, pkeys []string) []mongo.DASRecord {
367+
var dasrecords []mongo.DASRecord
368+
system := ""
369+
expire := 0
370+
urn := ""
371+
for _, dmap := range maps {
372+
surl := dasmaps.GetString(dmap, "url")
373+
// TMP fix, until we fix Phedex data to use JSON
374+
if strings.Contains(surl, "phedex") {
375+
surl = strings.Replace(surl, "xml", "json", -1)
376+
}
377+
// here we check that request Url match DAS map one either by splitting
378+
// base from parameters or making a match for REST based urls
379+
stm := dasmaps.GetString(dmap, "system")
380+
inst := dasquery.Instance
381+
if inst != "prod/global" && stm == "dbs3" {
382+
surl = strings.Replace(surl, "prod/global", inst, -1)
383+
}
384+
if strings.Split(r.Url, "?")[0] == surl || strings.HasPrefix(r.Url, surl) || r.Url == surl {
385+
urn = dasmaps.GetString(dmap, "urn")
386+
system = dasmaps.GetString(dmap, "system")
387+
expire = dasmaps.GetInt(dmap, "expire")
388+
}
389+
}
390+
// process data records
391+
notations := dmaps.FindNotations(system)
392+
records := services.Unmarshal(system, urn, r.Data, notations)
393+
records = services.AdjustRecords(dasquery, system, urn, records, expire, pkeys)
394+
395+
// add records
396+
for _, rec := range records {
397+
dasrecords = append(dasrecords, rec)
398+
}
399+
return dasrecords
400+
}
401+
365402
// helper function to process given set of URLs associted with dasquery
366-
func processURLs(dasquery dasql.DASQuery, urls map[string]string, maps []mongo.DASRecord, dmaps dasmaps.DASMaps, pkeys []string) []mongo.DASRecord {
403+
func processURLs(dasquery dasql.DASQuery, urls map[string]string, maps []mongo.DASRecord, dmaps *dasmaps.DASMaps, pkeys []string) []mongo.DASRecord {
367404
// defer function will propagate panic message to higher level
368405
// defer utils.ErrPropagate("processUrls")
369406

370407
var dasrecords []mongo.DASRecord
408+
if len(urls) == 1 {
409+
for furl, args := range urls {
410+
if !dasquery.Detail && strings.Contains(furl, "detail=True") {
411+
furl = strings.Replace(furl, "detail=True", "detail=False", -1)
412+
}
413+
resp := utils.FetchResponse(furl, args)
414+
return response2Records(&resp, dasquery, maps, dmaps, pkeys)
415+
}
416+
}
371417
out := make(chan utils.ResponseType)
372418
defer close(out)
373419
umap := map[string]int{}
374420
for furl, args := range urls {
421+
if !dasquery.Detail && strings.Contains(furl, "detail=True") {
422+
furl = strings.Replace(furl, "detail=True", "detail=False", -1)
423+
}
375424
umap[furl] = 1 // keep track of processed urls below
376425
go utils.Fetch(furl, args, out)
377426
}
@@ -381,35 +430,7 @@ func processURLs(dasquery dasql.DASQuery, urls map[string]string, maps []mongo.D
381430
for {
382431
select {
383432
case r := <-out:
384-
system := ""
385-
expire := 0
386-
urn := ""
387-
for _, dmap := range maps {
388-
surl := dasmaps.GetString(dmap, "url")
389-
// TMP fix, until we fix Phedex data to use JSON
390-
if strings.Contains(surl, "phedex") {
391-
surl = strings.Replace(surl, "xml", "json", -1)
392-
}
393-
// here we check that request Url match DAS map one either by splitting
394-
// base from parameters or making a match for REST based urls
395-
stm := dasmaps.GetString(dmap, "system")
396-
inst := dasquery.Instance
397-
if inst != "prod/global" && stm == "dbs3" {
398-
surl = strings.Replace(surl, "prod/global", inst, -1)
399-
}
400-
if strings.Split(r.Url, "?")[0] == surl || strings.HasPrefix(r.Url, surl) || r.Url == surl {
401-
urn = dasmaps.GetString(dmap, "urn")
402-
system = dasmaps.GetString(dmap, "system")
403-
expire = dasmaps.GetInt(dmap, "expire")
404-
}
405-
}
406-
// process data records
407-
notations := dmaps.FindNotations(system)
408-
records := services.Unmarshal(system, urn, r.Data, notations)
409-
records = services.AdjustRecords(dasquery, system, urn, records, expire, pkeys)
410-
411-
// add records
412-
for _, rec := range records {
433+
for _, rec := range response2Records(&r, dasquery, maps, dmaps, pkeys) {
413434
dasrecords = append(dasrecords, rec)
414435
}
415436
// remove from umap, indicate that we processed it

0 commit comments

Comments
 (0)