""" The SSAP core and supporting code. """ #c Copyright 2008-2025, the GAVO project #c #c This program is free software, covered by the GNU GPL. See the #c COPYING file in the source distribution. import itertools from gavo import base from gavo import rsc from gavo import rscdef from gavo import svcs from gavo.protocols import datalink from gavo.svcs import outputdef MS = base.makeStruct class SSADescriptor(datalink.ProductDescriptor): """SSA descriptors have ssaRow and limits attributes. These both reference SSA results. ssaRow is the first result of the query, which also provides the accref. limits is a table.Limits instance for the total result set. Warning: limits will be None if this is constructed with fromSSARow. """ ssaRow = None @classmethod def fromSSARow(cls, ssaRow, paramDict): """returns a descriptor from a row in an ssa table and the params of that table. Don't use this; the limits attribute will be {} for these. """ paramDict.update(ssaRow) ssaRow = paramDict res = cls.fromAccref(ssaRow["ssa_pubDID"], ssaRow['accref']) res.ssaRow = ssaRow res.pubDID = ssaRow["ssa_pubDID"] res.limits = {} return res @classmethod def fromSSAResult(cls, ssaResult): """returns a descriptor from an SSA query result (an InMemoryTable instance). """ if not ssaResult.rows: raise base.NotFoundError("any", "row", "ssa result for SODA block" " generation") res = cls.fromSSARow(ssaResult.rows[0], ssaResult.getParamDict()) res.limits = ssaResult.getLimits() res.pubDID = None return res def _combineRowIntoOne(ssaRows): """makes a "total row" from ssaRows. In the resulting row, minima and maxima are representative of the whole result set, and enumerated columns are set-valued. This is useful when generating parameter metadata. """ # XXX TODO: shouldn't this be made more generic by using something like # in user.info? if not ssaRows: raise base.ReportableError("Datalink meta needs at least one result row") totalRow = ssaRows[0].copy() totalRow["mime"] = set([totalRow["mime"]]) calibs = set() for row in ssaRows[1:]: if row["ssa_specstart"]totalRow["ssa_specend"]: totalRow["ssa_specend"] = row["ssa_specend"] totalRow["mime"].add(row["mime"]) calibs.add(row.get("ssa_fluxcalib", None)) totalRow["collect_calibs"] = set(c for c in calibs if c is not None) return totalRow def getDatalinkCore(dlSvc, ssaTable): """returns a datalink core adapted for ssaTable. dlSvc is the datalink service, ssaTable a non-empty SSA result table. """ allowedRendsForStealing = ["dlget"] #noflake: for stealVar downstack desc = SSADescriptor.fromSSAResult(ssaTable) return dlSvc.core.adaptForDescriptors( svcs.getRenderer("dlget"), [desc], None) class SSAPCore(svcs.DBCore): """A core doing SSAP queries. This core knows about metadata queries, version negotiation, and dispatches on REQUEST. Thus, it may return formatted XML data under certain circumstances. Interpreted Properties: * previews: If set to "auto", the core will automatically add a preview column and fill it with the URL of the products-based preview. Other values are not defined. """ name_ = "ssapCore" outputTableXML = """ True """ previewColumn = base.parseFromString(svcs.OutputField, '') ############### Implementation of the service operations def onElementComplete(self): super().onElementComplete() if self.getProperty("previews", default=False)=="auto": self.outputTable.columns.append(self.previewColumn) def _run_getTargetNames(self, service, inputTable, queryMeta): with base.getTableConn() as conn: table = rsc.TableForDef(self.queriedTable, create=False, connection=conn) destTD = base.makeStruct(outputdef.OutputTableDef, parent_=self.queriedTable.parent, id="result", onDisk=False, columns=[self.queriedTable.getColumnByName("ssa_targname")]) res = table.getTableForQuery(destTD, "", distinct=True) res.noPostprocess = True return res def _addPreviewLinks(self, resultTable): for row in resultTable: if "preview" in row and row["preview"] is None: row["preview"] = row["accref"]+"?preview=True" def _run_queryData(self, service, inputTable, queryMeta): limitThroughTOP = inputTable.getParam("TOP") if limitThroughTOP and limitThroughTOP