Skip to content

Commit

Permalink
Merge pull request #3 from lsst-sqre/tickets/DM-30119
Browse files Browse the repository at this point in the history
DM-30119: Sort by date rather than upload time and allow time window queries
  • Loading branch information
SimonKrughoff committed May 10, 2021
2 parents 914cf97 + ed3f1b2 commit 3d1d973
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion manifests/base/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Kustomization

images:
- name: lsstsqre/rubintv
newTag: 0.0.5
newTag: 0.0.6

resources:
- configmap.yaml
Expand Down
65 changes: 57 additions & 8 deletions src/rubintv/handlers/external/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"speccurrent",
]

from datetime import datetime
from typing import List, Optional

from aiohttp import web
Expand All @@ -26,8 +27,18 @@ async def get_table(request: web.Request) -> web.Response:
num = int(request.query["num"])
else:
num = 10
if "beg_date" in request.query:
beg_date = datetime.fromisoformat(request.query["beg_date"])
else:
beg_date = None # type: ignore[assignment]
if "end_date" in request.query:
end_date = datetime.fromisoformat(request.query["end_date"])
else:
end_date = None # type: ignore[assignment]
bucket = request.config_dict["rubintv/gcs_bucket"]
page = get_formatted_table("table.html", bucket, num=num)
page = get_formatted_table(
"table.html", bucket, num=num, beg_date=beg_date, end_date=end_date
)
return web.Response(text=page, content_type="text/html")


Expand Down Expand Up @@ -68,9 +79,23 @@ def get_formatted_table(
template: str,
bucket: Bucket,
num: int = None,
beg_date: datetime = None,
end_date: datetime = None,
) -> str:
imimgs = timeSort(bucket, "summit_imexam", num)
specimgs = timeSort(bucket, "summit_specexam", num)
if beg_date or end_date:
imimgs = timeWindowSort(
bucket, "summit_imexam", num, beg_date=beg_date, end_date=end_date
)
specimgs = timeWindowSort(
bucket,
"summit_specexam",
num,
beg_date=beg_date,
end_date=end_date,
)
else:
imimgs = timeSort(bucket, "summit_imexam", num)
specimgs = timeSort(bucket, "summit_specexam", num)
trimmed_specims = []
for img in imimgs:
match = False
Expand Down Expand Up @@ -147,11 +172,35 @@ def timeSort(
bucket: Bucket, prefix: str, num: Optional[int] = None
) -> List[Image]:
blobs = bucket.list_blobs(prefix=prefix)
sblobs = sorted(blobs, key=lambda x: x.time_created, reverse=True)
clean_URLs = [
el.public_url for el in sblobs if el.public_url.endswith(".png")
imgs = [
Image(el.public_url) for el in blobs if el.public_url.endswith(".png")
]
simgs = sorted(imgs, key=lambda x: (x.date, x.seq), reverse=True)

if num:
return simgs[:num]
return simgs


def timeWindowSort(
bucket: Bucket,
prefix: str,
num: Optional[int] = None,
beg_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
) -> List[Image]:
imgs = timeSort(bucket, prefix)
if beg_date and end_date:
simgs = [
el for el in imgs if beg_date < el.date and end_date > el.date
]
elif beg_date:
simgs = [el for el in imgs if beg_date < el.date]
elif end_date:
simgs = [el for el in imgs if end_date > el.date]
else:
raise RuntimeError(f"Something went wrong: {beg_date} and {end_date}")

if num:
return [Image(p) for p in clean_URLs][:num]
return [Image(p) for p in clean_URLs][:num]
return simgs[:num]
return simgs
7 changes: 6 additions & 1 deletion src/rubintv/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@
<div class="codehilite"><pre><span></span><code>https://roundtable.lsst.codes/rubintv/imevents/&lt;date&gt;/&lt;seq&gt;
<div class="codehilite"><pre><span></span><code>https://roundtable.lsst.codes/rubintv/specevents/&lt;date&gt;/&lt;seq&gt;
</code></pre></div>

<p>where the date format is <code>%Y-%m%-%d</code>.</p>

<p>To enlarge the table, use</p>
<div class="codehilite"><pre><span></span><code> https://roundtable.lsst.codes/rubintv/?num=&lt;num&gt;
</code></pre></div>

<p>To search by time window, use</p>
<div class="codehilite"><pre><span></span><code> https://roundtable.lsst.codes/rubintv/?beg_date=&lt;ISO date&gt;&amp;end_date=&lt;ISO date&gt;
</code></pre></div>
<p>The beg_date and end_date parameters must be ISO 8601 compliant time strings: e.g 2021-05-15. If either is missing they will be treated as lower or upper date limits respectively.</p>

<p>To get the most recent events, use</p>
<div class="codehilite"><pre><span></span><code><a href="https://roundtable.lsst.codes/rubintv/im_current">https://roundtable.lsst.codes/rubintv/im_current</a>
<div class="codehilite"><pre><span></span><code><a href="https://roundtable.lsst.codes/rubintv/spec_current">https://roundtable.lsst.codes/rubintv/spec_current</a>
Expand Down

0 comments on commit 3d1d973

Please sign in to comment.