Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sampleform #18

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions Dockerfile

This file was deleted.

4 changes: 3 additions & 1 deletion database/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ CREATE TABLE xas_standard_data (
COMMENT ON TABLE xas_standard_data IS 'Data file storing the standard data';

CREATE TYPE review_status_enum AS ENUM('pending', 'approved', 'rejected');
CREATE TYPE licence_enum AS ENUM('cc_by', 'cc_0', 'logged_in_only');
CREATE TYPE licence_enum AS ENUM('cc_by', 'cc_0');
CREATE TYPE sample_form_enum AS ENUM('other', 'foil', 'pellet');

CREATE Table xas_standard (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
Expand All @@ -241,6 +242,7 @@ CREATE Table xas_standard (
sample_name TEXT,
sample_prep TEXT,
sample_comp TEXT,
sample_form sample_form_enum,
beamline_id INTEGER,
mono_name TEXT,
mono_dspacing TEXT,
Expand Down
23 changes: 8 additions & 15 deletions xas-standards-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
# This file is for use as a devcontainer and a runtime container
#
# The devcontainer should use the build target and run as root with podman
# or docker with user namespaces.
#
#build api
#copy to runtime
FROM python:3.11 as build

ARG PIP_OPTIONS=.

# Add any system dependencies for the developer/build environment here e.g.
# RUN apt-get update && apt-get upgrade -y && \
# apt-get install -y --no-install-recommends \
# desired-packages \
# && rm -rf /var/lib/apt/lists/*

# set up a virtual environment and put it in PATH
RUN python -m venv /venv
ENV PATH=/venv/bin:$PATH

# Copy any required context for the pip install over
COPY . /context
WORKDIR /context
WORKDIR /api
COPY . .

# install python package into /venv
RUN pip install ${PIP_OPTIONS}
Expand All @@ -32,6 +22,9 @@ FROM python:3.11-slim as runtime
COPY --from=build /venv/ /venv/
ENV PATH=/venv/bin:$PATH

RUN apt-get update && apt-get install libpq5 -y

EXPOSE 5000

# change this entrypoint if it is not the same as the repo
ENTRYPOINT ["xas-standards-api"]
CMD ["--version"]
29 changes: 1 addition & 28 deletions xas-standards-api/src/xas_standards_api/app.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
import os

from fastapi import (
FastAPI,
)
from fastapi.staticfiles import StaticFiles
from fastapi import FastAPI
from fastapi_pagination import add_pagination
from starlette.responses import RedirectResponse

from .routers import admin, open, protected

dev = False

env_value = os.environ.get("FASTAPI_APP_ENV")

if env_value and env_value == "development":
print("RUNNING IN DEV MODE")
dev = True


build_dir = os.environ.get("FRONTEND_BUILD_DIR")

app = FastAPI()

app.include_router(open.router)
app.include_router(protected.router)
app.include_router(admin.router)

add_pagination(app)


@app.get("/login", response_class=RedirectResponse)
async def redirect_home():
# proxy handles log in so if you reach here go home
return "/"


if build_dir:
app.mount("/", StaticFiles(directory="/client/dist", html=True), name="site")
12 changes: 12 additions & 0 deletions xas-standards-api/src/xas_standards_api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Person,
PersonInput,
ReviewStatus,
SampleForm,
XASStandard,
XASStandardAdminReviewInput,
XASStandardData,
Expand Down Expand Up @@ -82,6 +83,7 @@ def get_metadata(session):
output["edges"] = select_all(session, Edge)
output["beamlines"] = select_all(session, Beamline)
output["licences"] = list(LicenceType)
output["sample_forms"] = list(SampleForm)

return output

Expand Down Expand Up @@ -260,3 +262,13 @@ def is_admin_user(session: Session, user_id: str):
raise HTTPException(status_code=401, detail=f"User {user_id} not admin")

return True


def get_registered_elements(session: Session):
statement = (
select(Element)
.where(XASStandard.element_z == Element.z)
.where(XASStandard.review_status == ReviewStatus.approved)
)
results = session.exec(statement)
return results.unique().all()
8 changes: 7 additions & 1 deletion xas-standards-api/src/xas_standards_api/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ class ReviewStatus(enum.Enum):
class LicenceType(enum.Enum):
cc_by = "cc_by"
cc_0 = "cc_0"
logged_in_only = "logged_in_only"


class SampleForm(enum.Enum):
other = "other"
foil = "foil"
pellet = "pellet"


class PersonInput(SQLModel):
Expand Down Expand Up @@ -102,6 +107,7 @@ class XASStandardInput(SQLModel):
sample_name: str
sample_prep: Optional[str]
sample_comp: Optional[str]
sample_form: SampleForm = Field(sa_column=Column(Enum(SampleForm)))
beamline_id: int = Field(foreign_key="beamline.id")
licence: LicenceType = Field(sa_column=Column(Enum(LicenceType)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ class MetadataResponse(SQLModel):
elements: List[Element]
edges: List[Edge]
licences: List[str]
sample_forms: List[str]
14 changes: 13 additions & 1 deletion xas-standards-api/src/xas_standards_api/routers/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from fastapi_pagination.cursor import CursorPage
from sqlmodel import Session

from ..crud import get_data, get_file, get_metadata, get_standard, read_standards_page
from ..crud import (
get_data,
get_file,
get_metadata,
get_registered_elements,
get_standard,
read_standards_page,
)
from ..database import get_session
from ..models.models import ReviewStatus
from ..models.response_models import (
Expand Down Expand Up @@ -48,3 +55,8 @@ async def read_data(
return get_file(session, id)

return get_data(session, id)


@router.get("/api/elements/metrics")
async def read_elements(session: Session = Depends(get_session)):
return get_registered_elements(session)
2 changes: 2 additions & 0 deletions xas-standards-api/src/xas_standards_api/routers/protected.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def add_standard_file(
beamline_id: Annotated[int, Form()],
sample_name: Annotated[str, Form()],
sample_prep: Annotated[str, Form()],
sample_form: Annotated[str, Form()],
doi: Annotated[str, Form()],
citation: Annotated[str, Form()],
comments: Annotated[str, Form()],
Expand All @@ -62,6 +63,7 @@ def add_standard_file(
edge_id=edge_id,
sample_name=sample_name,
sample_prep=sample_prep,
sample_form=sample_form,
submitter_comments=comments,
citation=citation,
licence=licence,
Expand Down
1 change: 1 addition & 0 deletions xas-standards-api/tests/test_protected_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def get_admin_user():
"beamline_id": 1,
"sample_name": unique_sample_name,
"sample_prep": "test",
"sample_form": "foil",
"doi": "doi",
"citation": "citation",
"comments": "comments",
Expand Down
2 changes: 2 additions & 0 deletions xas-standards-api/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def build_test_database(session: Session):
sample_name="sample",
sample_prep="pellet",
sample_comp="H",
sample_form="foil",
beamline_id=1,
licence=LicenceType.cc_0,
id=1,
Expand All @@ -108,6 +109,7 @@ def build_test_database(session: Session):
sample_name="sample",
sample_prep="pellet",
sample_comp="He",
sample_form="pellet",
beamline_id=1,
licence=LicenceType.cc_0,
id=2,
Expand Down
25 changes: 25 additions & 0 deletions xas-standards-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#build client
#build api
#copy to runtime

FROM node:18-bullseye-slim as build-web

WORKDIR /client

RUN yes | npm install -g pnpm

RUN apt update

COPY . .

RUN yes | pnpm install

RUN pnpm vite build

From nginx as host

COPY --from=build-web /client/dist/ /usr/share/nginx/html
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf

# change this entrypoint if it is not the same as the repo
ENTRYPOINT ["nginx","-g", "daemon off;"]
46 changes: 46 additions & 0 deletions xas-standards-client/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
server {
listen 80;
server_name localhost;

#access_log /var/log/nginx/host.access.log main;


location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html =404;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

4 changes: 3 additions & 1 deletion xas-standards-client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { useState, useMemo } from "react";
import { Stack } from "@mui/material";
import { createTheme, ThemeProvider } from "@mui/material/styles";

import ReviewPage from "./components/ReviewPage.tsx";
import ReviewPage from "./components/review/ReviewPage.tsx";
import Terms from "./components/Terms.tsx";

function App() {
const prefersDarkMode = useMediaQuery("(prefers-color-scheme: dark)");
Expand Down Expand Up @@ -56,6 +57,7 @@ function App() {
<Routes>
<Route path="/" element={<WelcomePage />} />
<Route path="/view" element={<StandardViewerMui />} />
<Route path="/terms" element={<Terms />} />
<Route
path="/submit"
element={
Expand Down
Loading
Loading