6103cfb97b45dafba47eced8573b21179823a63e hiram Fri Apr 5 15:49:35 2019 -0700 better html output refs #18869 diff --git src/hg/hubApi/hubApi.c src/hg/hubApi/hubApi.c index 867efe9..f754cdf 100644 --- src/hg/hubApi/hubApi.c +++ src/hg/hubApi/hubApi.c @@ -1,1333 +1,1359 @@ /* hubApi - access mechanism to hub data resources. */ #include "dataApi.h" #include "botDelay.h" #include "jsHelper.h" /* +------------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+------------------+------+-----+---------+-------+ | hubUrl | longblob | NO | PRI | NULL | | | shortLabel | varchar(255) | NO | | NULL | | | longLabel | varchar(255) | NO | | NULL | | | registrationTime | varchar(255) | NO | | NULL | | | dbCount | int(10) unsigned | NO | | NULL | | | dbList | blob | YES | | NULL | | | descriptionUrl | longblob | YES | | NULL | | +------------------+------------------+------+-----+---------+-------+ */ /* Global Variables for all modules */ int maxItemsOutput = 1000; /* can be set in URL maxItemsOutput=N */ static int maxItemLimit = 1000000; /* maximum of 1,000,000 items returned */ /* for debugging purpose, current bot delay value */ int botDelay = 0; boolean debug = FALSE; /* can be set in URL debug=1, to turn off: debug=0 */ #define delayFraction 0.03 /* Global only to this one source file */ static struct cart *cart; /* CGI and other variables */ static struct hash *oldVars = NULL; static struct hash *trackCounter = NULL; static long totalTracks = 0; static boolean measureTiming = FALSE; /* set by CGI parameters */ static boolean allTrackSettings = FALSE; /* checkbox setting */ static char **shortLabels = NULL; /* public hub short labels in array */ // struct hubPublic *publicHubList = NULL; static int publicHubCount = 0; static char *defaultHub = "Plants"; // static char *defaultHubUrl = " http://genome-test.gi.ucsc.edu/~hiram/hubs/Plants/hub.txt"; static char *defaultDb = "ce11"; static long enteredMainTime = 0; /* will become = clock1000() on entry */ /* to allow calculation of when to bail out, taking too long */ static long timeOutSeconds = 100; static boolean timedOut = FALSE; static char *urlPrefix = ""; /* initalized to support self references */ static struct slName *supportedTypes = NULL; /* will be initialized to a known supported set */ static void initSupportedTypes() /* initalize the list of supported track types */ { struct slName *el = newSlName("bed"); slAddHead(&supportedTypes, el); el = newSlName("wig"); slAddHead(&supportedTypes, el); el = newSlName("broadPeak"); slAddHead(&supportedTypes, el); el = newSlName("narrowPeak"); slAddHead(&supportedTypes, el); el = newSlName("bigBed"); slAddHead(&supportedTypes, el); el = newSlName("bigWig"); slAddHead(&supportedTypes, el); el = newSlName("bigNarrowPeak"); slAddHead(&supportedTypes, el); el = newSlName("bigGenePred"); slAddHead(&supportedTypes, el); el = newSlName("genePred"); slAddHead(&supportedTypes, el); // el = newSlName("bigPsl"); // slAddHead(&supportedTypes, el); // el = newSlName("bigBarChart"); // slAddHead(&supportedTypes, el); // el = newSlName("bigInteract"); // slAddHead(&supportedTypes, el); // el = newSlName("bigMaf"); // slAddHead(&supportedTypes, el); // el = newSlName("bigChain"); // slAddHead(&supportedTypes, el); slNameSort(&supportedTypes); } static boolean isSupportedType(char *type) /* is given type in the supportedTypes list ? */ { boolean ret = FALSE; if (startsWith("wigMaf", type)) /* not wigMaf at this time */ return ret; struct slName *el; for (el = supportedTypes; el; el = el->next) { if (startsWith(el->name, type)) { ret = TRUE; break; } } return ret; } static int publicHubCmpCase(const void *va, const void *vb) /* Compare two shortLabels, ignore case. */ { const struct hubPublic *a = *((struct hubPublic **)va); const struct hubPublic *b = *((struct hubPublic **)vb); return strcasecmp(a->shortLabel, b->shortLabel); } static void publicHubSortCase(struct hubPublic **pList) /* Sort slName list, ignore case. */ { slSort(pList, publicHubCmpCase); } struct hubPublic *hubPublicDbLoadAll() /* read entire hubPublic table in hgcentral and return resulting list */ { char query[1024]; struct hubPublic *list = NULL; struct sqlConnection *conn = hConnectCentral(); sqlSafef(query, sizeof(query), "select * from %s", hubPublicTableName()); struct sqlResult *sr = sqlGetResult(conn, query); char **row; while ((row = sqlNextRow(sr)) != NULL) { struct hubPublic *el = hubPublicLoad(row); slAddHead(&list, el); } sqlFreeResult(&sr); hDisconnectCentral(&conn); publicHubSortCase(&list); int listSize = slCount(list); AllocArray(shortLabels, listSize); struct hubPublic *el = list; int i = 0; for ( ; el != NULL; el = el->next ) { shortLabels[i++] = el->shortLabel; ++publicHubCount; } return list; } static boolean timeOutReached() /* see if the timeout has been reached to determine if an exit * is appropriate at this time */ { long nowTime = clock1000(); timedOut = FALSE; if ((nowTime - enteredMainTime) > (1000 * timeOutSeconds)) timedOut= TRUE; return timedOut; } static void hashCountTrack(struct trackDb *tdb, struct hash *countTracks) /* this is counting up track types into the hash countTracks */ { char *stripType = cloneString(tdb->type); if (startsWith("chain ", tdb->type)) stripType = cloneString("chain"); else if (startsWith("netAlign ", tdb->type)) stripType = cloneString("netAlign"); else if (startsWith("genePred ", tdb->type)) stripType = cloneString("genePred"); else if (startsWith("bigWig ", tdb->type)) stripType = cloneString("bigWig"); else if (startsWith("wigMaf ", tdb->type)) stripType = cloneString("wigMaf"); else if (startsWith("wig ", tdb->type)) stripType = cloneString("wig"); else stripType = cloneString(tdb->type); // char *compositeTrack = trackDbLocalSetting(tdb, "compositeTrack"); boolean compositeContainer = tdbIsComposite(tdb); boolean compositeView = tdbIsCompositeView(tdb); // char *superTrack = trackDbLocalSetting(tdb, "superTrack"); boolean superChild = tdbIsSuperTrackChild(tdb); if (compositeContainer) hashIncInt(countTracks, "composite container"); else if (compositeView) hashIncInt(countTracks, "composite view"); else if (superChild) { hashIncInt(countTracks, "superTrack child"); hashIncInt(countTracks, stripType); hashIncInt(countTracks, "track count"); } else if (isEmpty(tdb->type)) hashIncInt(countTracks, "no type specified"); else { hashIncInt(countTracks, stripType); hashIncInt(countTracks, "track count"); } freeMem(stripType); } static void sampleUrl(struct trackHub *hub, char *db, struct trackDb *tdb, char *chrom, unsigned chromSize, char *errorString) /* print out a sample getData URL */ { char errorPrint[2048]; errorPrint[0] = 0; if (isNotEmpty(errorString)) { safef(errorPrint, sizeof(errorPrint), " ERROR: %s", errorString); } unsigned start = chromSize / 4; unsigned end = start + 10000; if (end > chromSize) end = chromSize; char *genome = NULL; if (hub) genome = hub->genomeList->name; if (db) { if (hub) { char urlReference[2048]; safef(urlReference, sizeof(urlReference), " (sample getData)%s\n", urlPrefix, hub->url, genome, tdb->track, chrom, start, end, errorPrint); if (tdb->parent) - hPrintf("
\n"); cartDump(cart); hPrintf("\n"); } static void sendJsonHogMessage(char *hogHost) { apiErrAbort("Your host, %s, has been sending too many requests lately and is " "unfairly loading our site, impacting performance for other users. " "Please contact genome@soe.ucsc.edu to ask that your site " "be reenabled. Also, please consider downloading sequence and/or " "annotations in bulk -- see http://genome.ucsc.edu/downloads.html.", hogHost); } static void sendHogMessage(char *hogHost) { hPrintf("\n"); hPrintf("\n"); hPrintf("\n"); hPrintf("\n"); hPrintf("
\n"); hPrintf("Your host, %s, has been sending too many requests lately and is " "unfairly loading our site, impacting performance for other users. " "Please contact genome@soe.ucsc.edu to ask that your site " "be reenabled. Also, please consider downloading sequence and/or " "annotations in bulk -- see http://genome.ucsc.edu/downloads.html.", hogHost); hPrintf("
\n"); exit(0); } static void hogExit() /* bottleneck server requests exit */ { char *hogHost = getenv("REMOTE_ADDR"); char *pathInfo = getenv("PATH_INFO"); /* nothing on incoming path, then display the WEB page instead */ if (sameOk("/",pathInfo)) pathInfo = NULL; if (isNotEmpty(pathInfo)) { puts("Content-Type:application/json"); puts("Status: 459 Too Many Requests"); /* maybe a Retry-After: 3600 statement here ? */ puts("\n"); sendJsonHogMessage(hogHost); } else { puts("Content-Type:text/html"); puts("Status: 459 Too Many Requests"); /* maybe a Retry-After: 3600 statement here ? */ puts("\n"); sendHogMessage(hogHost); } } static void doMiddle(struct cart *theCart) /* Set up globals and make web page */ { cart = theCart; measureTiming = hPrintStatus() && isNotEmpty(cartOptionalString(cart, "measureTiming")); measureTiming = TRUE; char *database = NULL; char *genome = NULL; cgiVarSet("ignoreCookie", "1"); getDbAndGenome(cart, &database, &genome, oldVars); initGenbankTableNames(database); initSupportedTypes(); initUrlPrefix(); /* global variable for all workers to honor this limit */ maxItemsOutput = cartUsualInt(cart, "maxItemsOutput", maxItemsOutput); if (maxItemsOutput > maxItemLimit) /* safety check */ maxItemsOutput = maxItemLimit; if (maxItemsOutput < 1) /* safety check */ maxItemsOutput = 1; debug = cartUsualBoolean(cart, "debug", debug); int timeout = cartUsualInt(cart, "udcTimeout", 300); if (udcCacheTimeout() < timeout) udcSetCacheTimeout(timeout); knetUdcInstall(); char *pathInfo = getenv("PATH_INFO"); /* nothing on incoming path, then display the WEB page instead */ if (sameOk("/",pathInfo)) pathInfo = NULL; boolean commandError = FALSE; /*expect no more than MAX_PATH_INFO number of words*/ char *words[MAX_PATH_INFO]; if (isNotEmpty(pathInfo)) { setupFunctionHash(); struct hashEl *hel = parsePathInfo(pathInfo, words); /* verify valid API command */ if (hel) /* have valid command */ { hPrintDisable(); /* could check botDelay here to see if 459 status is advised */ puts("Content-Type:application/json"); puts("\n"); void (*apiFunction)(char **) = hel->val; (*apiFunction)(words); return; } else commandError = TRUE; } /* could check botDelay here to see if 459 status is advised */ // puts("Content-Type:text/html"); // puts("\n"); (void) hubPublicDbLoadAll(); struct dbDb *dbList = ucscDbDb(); char **ucscDbList = NULL; int listSize = slCount(dbList); AllocArray(ucscDbList, listSize); struct dbDb *el = dbList; int ucscDataBaseCount = 0; int maxDbNameWidth = 0; for ( ; el != NULL; el = el->next ) { ucscDbList[ucscDataBaseCount++] = el->name; if (strlen(el->name) > maxDbNameWidth) maxDbNameWidth = strlen(el->name); } maxDbNameWidth += 1; webStartJWest(cart, database, "UCSC JSON API interface"); hPrintf("\n"); if (sameWord("go", goUcscDb)) /* requested UCSC db track list */ { tracksForUcscDb(ucscDb); } else { - hPrintf("