Skip to content

Commit

Permalink
Add front end for caching new collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctslater committed Oct 22, 2024
1 parent 1b4a809 commit 699da76
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 20 deletions.
25 changes: 25 additions & 0 deletions app/_components/AddCollectionSelectServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

'use server'

export async function pollJob(jobId) {

console.log(`pollJob ${jobId}`)
let res = await fetch(`http://production-tools/plot-navigator/cache/job/${jobId}`)
let data = await res.json()
console.log(res.json())
return data
}

export async function putCollection(repo, collectionName) {

let res = await fetch("http://production-tools/plot-navigator/cache/",
{method: "PUT",
body: JSON.stringify({repo: repo, collection: collectionName}),
headers: {"Content-Type": "application/json"}}
)
let data = await res.json()
console.log(`putCollection ${JSON.stringify(data)}`)

return data.jobId

}
67 changes: 67 additions & 0 deletions app/_components/addCollectionSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use client'

import React from 'react';
import { useState, useEffect } from 'react'

import {putCollection, pollJob} from './AddCollectionSelectServer.js'

export default function AddCollectionSelect({repos}) {

const [repo, setRepo] = useState(repos[0]);
const [collectionName, setCollectionName] = useState("");
const [jobId, setJobId] = useState("");
const [statusMessage, setStatusMessage] = useState("");
const [statusClasses, setStatusClasses] = useState("");
const [resultMessage, setResultMessage] = useState("");

const pollUpdates = async () => {
if(jobId == "") {
return
}
await new Promise(r => setTimeout(r, 5000));
console.log(`Polling ${jobId}`)
const res = await pollJob(jobId)
setStatusMessage(res.status.replace("_", " "))
bounceText()
setStatusClasses("bg-sky-500/10")
if(res.status == "in_progress") {
await pollUpdates()
} else if(res.status == "complete") {
setResultMessage(res.result)
}
}
useEffect(() => {pollUpdates(jobId)}, [jobId])

const submitForm = async () => {
const jobId = await putCollection(repo, collectionName)
setJobId(jobId)
setStatusMessage("Started")

}

const bounceText = () => {
setStatusClasses("bg-sky-500/10")
}

useEffect(() => {setStatusClasses("duration-1000")}, [statusClasses])

return (
<div className="p-2">
<select className="p-1 m-2" value={repo} onChange={(e) => {
setRepo(e.target.value);
}}>
{repos.map(
(repo) => <option value={repo} key={repo}>{repo}</option>
)}
</select>
<input className="m-2 border-2" type="text" size={40} value={collectionName} onChange={(e) => setCollectionName(e.target.value)} />
<button className="p-2 px-4 m-2 rounded-md text-white bg-sky-600" onClick={submitForm}>Add</button>
<div className={`m-2 p-2 px-4 inline-block ${statusClasses}`}>{statusMessage}</div>
<div className={`m-2 p-2 px-4 inline-block `}>{resultMessage}</div>
{ /*
<button className="p-2 px-4 m-2 rounded-md text-white bg-sky-600" onClick={bounceText}>Bounce</button>
*/ }
</div>
)
}

23 changes: 23 additions & 0 deletions app/addcollection/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

export const dynamic = 'force-dynamic'

import Link from 'next/link'

import AddCollectionSelect from '@/components/addCollectionSelect'

export default async function AddCollection() {


const repoUrls = JSON.parse(process.env.REPO_URLS ?? "[]")

/* REPO_URLS */
return (
<div>
<div className="text-m m-5"><Link href={"/"}>&lt;- Back to collections</Link></div>
<div className="text-2xl m-5">Add a Collection:</div>
<AddCollectionSelect repos={Object.keys(repoUrls)} />
</div>

)

}
5 changes: 3 additions & 2 deletions app/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export default function RootLayout({ children }) {
<div className="bg-black text-rubin-blue text-3xl p-2">Rubin Plot Navigator</div>
<ul className="flex flex-row p-2">
<li className="p-2 px-3 text-xl"><a href="/plot-navigator">Plots</a></li>
<li className="p-2 px-3 text-xl"><a href="/plot-navigator/metrics">Metrics</a></li>
<li className="p-2 px-3 text-xl"><a href="/plot-navigator/bokeh">Bokeh</a></li>
{(process.env.PAGE_LINKS ?? "").split(',').map((page) =>
<li className="p-2 px-3 text-xl" key={page}><a href={`/plot-navigator/${page.toLowerCase()}`}>{page}</a></li>
)}
</ul>
</div>
{children}
Expand Down
46 changes: 28 additions & 18 deletions app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,34 @@ export default async function Collections() {

return (
<div>
<h1 className="text-2xl m-5">Official Collections</h1>
<div className="m-5 border-2 rounded px-2 inline-block my-0" >
<table className="divide-y">
<thead>
<tr>
<td className={cellClassNames}>Collection</td>
<td className={cellClassNames}>Repo</td>
<td className={`text-right ${cellClassNames}`}>Last Updated</td>
</tr>
</thead>
<tbody>
{officialSummaryRefs.map((summary, n) =>
(<tr key={n}><td className={cellClassNames}><Link href={`/collection/${encodeURIComponent(summary.repo)}/${encodeURIComponent(summary.collection)}`}>{summary.collection}</Link></td>
<td className={`${cellClassNames}`}>{summary.repo}</td>
<td className={`text-right ${cellClassNames}`}>{summary.lastModified.toDateString()}</td>
</tr>))}
</tbody>
</table>
<div className="m-5 inline-block">
<div className="flex flex-row items-end">
<div className="">
<h1 className="text-2xl py-4">Official Collections</h1>
</div>
<div className="grow text-right">
<div className="py-4"><Link className="p-2 px-4 m-2 rounded-md text-white bg-sky-600" href="/addcollection">Add Collection</Link></div>
</div>
</div>
<div className="clear-both"></div>
<div className="border-2 rounded px-2 inline-block my-0 " >
<table className="divide-y">
<thead>
<tr>
<td className={cellClassNames}>Collection</td>
<td className={cellClassNames}>Repo</td>
<td className={`text-right ${cellClassNames}`}>Last Updated</td>
</tr>
</thead>
<tbody>
{officialSummaryRefs.map((summary, n) =>
(<tr key={n}><td className={cellClassNames}><Link href={`/collection/${encodeURIComponent(summary.repo)}/${encodeURIComponent(summary.collection)}`}>{summary.collection}</Link></td>
<td className={`${cellClassNames}`}>{summary.repo}</td>
<td className={`text-right ${cellClassNames}`}>{summary.lastModified.toDateString()}</td>
</tr>))}
</tbody>
</table>
</div>
</div>

<h1 className="text-2xl m-5">User Collections</h1>
Expand Down
1 change: 1 addition & 0 deletions env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ BASE_URL=
BUTLER_URL=
BUCKET_NAME=
BUCKET_URL=
PAGE_LINKS=
S3_KEY=
S3_SECRET=
4 changes: 4 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module.exports = {
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
],
safelist: [
'duration-1000',
'bg-sky-500/10',
],
theme: {
extend: {
colors: {
Expand Down

0 comments on commit 699da76

Please sign in to comment.