| 1 |
""" |
| 2 |
The SSAP core and supporting code. |
| 3 |
|
| 4 |
""" |
| 5 |
|
| 6 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 7 |
#c |
| 8 |
#c This program is free software, covered by the GNU GPL. See the |
| 9 |
#c COPYING file in the source distribution. |
| 10 |
|
| 11 |
|
| 12 |
import itertools |
| 13 |
|
| 14 |
from gavo import base |
| 15 |
from gavo import rsc |
| 16 |
from gavo import rscdef |
| 17 |
from gavo import svcs |
| 18 |
from gavo.protocols import datalink |
| 19 |
from gavo.svcs import outputdef |
| 20 |
|
| 21 |
|
| 22 |
MS = base.makeStruct |
| 23 |
|
| 24 |
|
| 25 |
class SSADescriptor(datalink.ProductDescriptor): |
| 26 |
"""SSA descriptors have ssaRow and limits attributes. |
| 27 |
|
| 28 |
These both reference SSA results. ssaRow is the first result of |
| 29 |
the query, which also provides the accref. limits is a table.Limits |
| 30 |
instance for the total result set. |
| 31 |
|
| 32 |
Warning: limits will be None if this is constructed with fromSSARow. |
| 33 |
""" |
| 34 |
ssaRow = None |
| 35 |
|
| 36 |
@classmethod |
| 37 |
def fromSSARow(cls, ssaRow, paramDict): |
| 38 |
"""returns a descriptor from a row in an ssa table and |
| 39 |
the params of that table. |
| 40 |
|
| 41 |
Don't use this; the limits attribute will be {} for these. |
| 42 |
""" |
| 43 |
paramDict.update(ssaRow) |
| 44 |
ssaRow = paramDict |
| 45 |
res = cls.fromAccref(ssaRow["ssa_pubDID"], ssaRow['accref']) |
| 46 |
res.ssaRow = ssaRow |
| 47 |
res.pubDID = ssaRow["ssa_pubDID"] |
| 48 |
res.limits = {} |
| 49 |
return res |
| 50 |
|
| 51 |
@classmethod |
| 52 |
def fromSSAResult(cls, ssaResult): |
| 53 |
"""returns a descriptor from an SSA query result (an InMemoryTable |
| 54 |
instance). |
| 55 |
""" |
| 56 |
if not ssaResult.rows: |
| 57 |
raise base.NotFoundError("any", "row", "ssa result for SODA block" |
| 58 |
" generation") |
| 59 |
res = cls.fromSSARow(ssaResult.rows[0], ssaResult.getParamDict()) |
| 60 |
res.limits = ssaResult.getLimits() |
| 61 |
res.pubDID = None |
| 62 |
return res |
| 63 |
|
| 64 |
|
| 65 |
def _combineRowIntoOne(ssaRows): |
| 66 |
"""makes a "total row" from ssaRows. |
| 67 |
|
| 68 |
In the resulting row, minima and maxima are representative of the |
| 69 |
whole result set, and enumerated columns are set-valued. |
| 70 |
|
| 71 |
This is useful when generating parameter metadata. |
| 72 |
""" |
| 73 |
# XXX TODO: shouldn't this be made more generic by using something like |
| 74 |
# in user.info? |
| 75 |
if not ssaRows: |
| 76 |
raise base.ReportableError("Datalink meta needs at least one result row") |
| 77 |
|
| 78 |
totalRow = ssaRows[0].copy() |
| 79 |
totalRow["mime"] = set([totalRow["mime"]]) |
| 80 |
calibs = set() |
| 81 |
|
| 82 |
for row in ssaRows[1:]: |
| 83 |
if row["ssa_specstart"]<totalRow["ssa_specstart"]: |
| 84 |
totalRow["ssa_specstart"] = row["ssa_specstart"] |
| 85 |
if row["ssa_specend"]>totalRow["ssa_specend"]: |
| 86 |
totalRow["ssa_specend"] = row["ssa_specend"] |
| 87 |
totalRow["mime"].add(row["mime"]) |
| 88 |
calibs.add(row.get("ssa_fluxcalib", None)) |
| 89 |
|
| 90 |
totalRow["collect_calibs"] = set(c for c in calibs if c is not None) |
| 91 |
return totalRow |
| 92 |
|
| 93 |
|
| 94 |
def getDatalinkCore(dlSvc, ssaTable): |
| 95 |
"""returns a datalink core adapted for ssaTable. |
| 96 |
|
| 97 |
dlSvc is the datalink service, ssaTable a non-empty SSA result table. |
| 98 |
""" |
| 99 |
allowedRendsForStealing = ["dlget"] #noflake: for stealVar downstack |
| 100 |
desc = SSADescriptor.fromSSAResult(ssaTable) |
| 101 |
return dlSvc.core.adaptForDescriptors( |
| 102 |
svcs.getRenderer("dlget"), [desc], None) |
| 103 |
|
| 104 |
|
| 105 |
class SSAPCore(svcs.DBCore): |
| 106 |
"""A core doing SSAP queries. |
| 107 |
|
| 108 |
This core knows about metadata queries, version negotiation, and |
| 109 |
dispatches on REQUEST. Thus, it may return formatted XML data |
| 110 |
under certain circumstances. |
| 111 |
|
| 112 |
Interpreted Properties: |
| 113 |
|
| 114 |
* previews: If set to "auto", the core will automatically add a preview |
| 115 |
column and fill it with the URL of the products-based preview. Other |
| 116 |
values are not defined. |
| 117 |
""" |
| 118 |
name_ = "ssapCore" |
| 119 |
|
| 120 |
outputTableXML = """ |
| 121 |
<outputTable verbLevel="30"> |
| 122 |
<property name="virtual">True</property> |
| 123 |
<FEED source="//ssap#coreOutputAdditionals"/> |
| 124 |
</outputTable>""" |
| 125 |
|
| 126 |
previewColumn = base.parseFromString(svcs.OutputField, |
| 127 |
'<outputField name="preview" type="text"' |
| 128 |
' ucd="meta.ref.url;meta.preview" tablehead="Preview"' |
| 129 |
' description="URL of a preview for the dataset"' |
| 130 |
' select="NULL" displayHint="type=product" verbLevel="15"/>') |
| 131 |
|
| 132 |
|
| 133 |
############### Implementation of the service operations |
| 134 |
|
| 135 |
def onElementComplete(self): |
| 136 |
super().onElementComplete() |
| 137 |
if self.getProperty("previews", default=False)=="auto": |
| 138 |
self.outputTable.columns.append(self.previewColumn) |
| 139 |
|
| 140 |
def _run_getTargetNames(self, service, inputTable, queryMeta): |
| 141 |
with base.getTableConn() as conn: |
| 142 |
table = rsc.TableForDef(self.queriedTable, create=False, |
| 143 |
connection=conn) |
| 144 |
destTD = base.makeStruct(outputdef.OutputTableDef, |
| 145 |
parent_=self.queriedTable.parent, |
| 146 |
id="result", onDisk=False, |
| 147 |
columns=[self.queriedTable.getColumnByName("ssa_targname")]) |
| 148 |
res = table.getTableForQuery(destTD, "", distinct=True) |
| 149 |
res.noPostprocess = True |
| 150 |
return res |
| 151 |
|
| 152 |
def _addPreviewLinks(self, resultTable): |
| 153 |
for row in resultTable: |
| 154 |
if "preview" in row and row["preview"] is None: |
| 155 |
row["preview"] = row["accref"]+"?preview=True" |
| 156 |
|
| 157 |
def _run_queryData(self, service, inputTable, queryMeta): |
| 158 |
limitThroughTOP = inputTable.getParam("TOP") |
| 159 |
if limitThroughTOP and limitThroughTOP<queryMeta["dbLimit"]: |
| 160 |
queryMeta["dbLimit"] = limitThroughTOP |
| 161 |
|
| 162 |
res = svcs.DBCore.run(self, service, inputTable, queryMeta) |
| 163 |
if self.getProperty("previews", default=False)=="auto": |
| 164 |
self._addPreviewLinks(res) |
| 165 |
|
| 166 |
if service.hasProperty("datalink"): |
| 167 |
# backwards compatibility: The datalink property on the service |
| 168 |
# can name a datalink service. We don't want that any more, but |
| 169 |
# for now copy it to the queried table (where it should be) |
| 170 |
# XXX Deprecate around version 1.3 |
| 171 |
try: |
| 172 |
res.getMeta("_associatedDatalinkService", raiseOnFail=True) |
| 173 |
except base.NoMetaKey: |
| 174 |
# No datalink metadata on the table yet, generate one from the property. |
| 175 |
res.addMeta( |
| 176 |
"_associatedDatalinkService.serviceId", |
| 177 |
service.getProperty("datalink")) |
| 178 |
res.addMeta( |
| 179 |
"_associatedDatalinkService.idColumn", |
| 180 |
"ssa_pubDID") |
| 181 |
|
| 182 |
return res |
| 183 |
|
| 184 |
################ the main dispatcher |
| 185 |
|
| 186 |
def run(self, service, inputTable, queryMeta): |
| 187 |
defaultRequest = service.getProperty("defaultRequest", "") |
| 188 |
requestType = (inputTable.getParam("REQUEST") or defaultRequest).upper() |
| 189 |
|
| 190 |
if requestType=="QUERYDATA": |
| 191 |
return self._run_queryData(service, inputTable, queryMeta) |
| 192 |
|
| 193 |
elif requestType=="GETTARGETNAMES": |
| 194 |
return self._run_getTargetNames(service, inputTable, queryMeta) |
| 195 |
|
| 196 |
else: |
| 197 |
raise base.ValidationError("Missing or invalid value for REQUEST.", |
| 198 |
"REQUEST", |
| 199 |
hint="According to SSAP, you can only have queryData here;" |
| 200 |
" as an extension, this service also understands getTargetNames.") |
| 201 |
|
| 202 |
|
| 203 |
_VIEW_COLUMNS_CACHE = [] |
| 204 |
|
| 205 |
def iterViewColumns(context): |
| 206 |
"""returns a list of column objects for building the SSA view |
| 207 |
mixin's columns. |
| 208 |
|
| 209 |
The argument is the DaCHS RD parse context. |
| 210 |
|
| 211 |
This is probably only useful for the //ssap#view mixin. The argument |
| 212 |
is that mixin's context. This could go if we drop the hcd and mixc |
| 213 |
mixins and instead have a normal STREAM with the columns, as it's really |
| 214 |
only necessary to make columns from the stupid params and remove their |
| 215 |
defaults. |
| 216 |
|
| 217 |
This will always return the same column objects -- don't change them. |
| 218 |
""" |
| 219 |
if not _VIEW_COLUMNS_CACHE: |
| 220 |
dontCopyAtts = frozenset(["value", "content_"]) |
| 221 |
protoTable = context.getById("instance") |
| 222 |
|
| 223 |
_VIEW_COLUMNS_CACHE.append([MS(rscdef.Column, |
| 224 |
**c.getCopyableAttributes(ignoreKeys=dontCopyAtts)) |
| 225 |
for c in itertools.chain( |
| 226 |
protoTable.columns, protoTable.params)]) |
| 227 |
|
| 228 |
return _VIEW_COLUMNS_CACHE[0] |