mirror of
https://github.com/Novartis/cellxgene-gateway.git
synced 2026-09-15 20:57:59 +08:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f2b372d32 | ||
|
|
25755d3f69 | ||
|
|
893092f199 | ||
|
|
d3ee04b6e5 | ||
|
|
f1f4b0c0ca | ||
|
|
01dccef7db | ||
|
|
9c17e2ff32 | ||
|
|
fbc18fb636 | ||
|
|
8c5a635de9 | ||
|
|
94062c2d64 | ||
|
|
068e8f7633 | ||
|
|
16b54f9409 | ||
|
|
942410bb44 | ||
|
|
0d084a405e | ||
|
|
49d679e779 | ||
|
|
5e6faa4b02 | ||
|
|
9fe846c786 | ||
|
|
1c7907aabd | ||
|
|
30d2b07b1a | ||
|
|
21ff56ea8b | ||
|
|
0b866a46ec | ||
|
|
d5246d4b4b | ||
|
|
465373ca7e | ||
|
|
fc83523c84 | ||
|
|
4e367b0ec9 | ||
|
|
ce3eb0eedf | ||
|
|
9219336356 | ||
|
|
b689357f29 |
49
.github/workflows/pr-checks.yaml
vendored
Normal file
49
.github/workflows/pr-checks.yaml
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
# Tests that run on every PR
|
||||
|
||||
name: Pull Request Checks
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
black:
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
name: Checkout repository
|
||||
|
||||
- uses: actions/setup-python@v2
|
||||
name: Setup Python
|
||||
with:
|
||||
python-version: 3.9
|
||||
|
||||
- name: Install black
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install black
|
||||
- name: Run black
|
||||
run: |
|
||||
black -l 79 . --check
|
||||
# This job is copied over from `deploy.yaml`
|
||||
run-tests:
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
# See: https://github.com/marketplace/actions/setup-conda
|
||||
- uses: s-weigand/setup-conda@v1
|
||||
with:
|
||||
conda-channels: "conda-forge"
|
||||
|
||||
- name: Build environment
|
||||
run: |
|
||||
conda env create -f environment.yml
|
||||
eval "$(conda shell.bash hook)"
|
||||
conda activate cellxgene-gateway
|
||||
python setup.py install
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
eval "$(conda shell.bash hook)"
|
||||
conda activate cellxgene-gateway
|
||||
python -m unittest discover tests
|
||||
bash <(curl -s https://codecov.io/bash)
|
||||
@@ -7,7 +7,7 @@
|
||||
language: python
|
||||
matrix:
|
||||
include:
|
||||
- python: 3.5 # we don't actually use this
|
||||
- python: 3.5 # we don't actually use this
|
||||
env: PYTHON_VERSION=3.7
|
||||
|
||||
install:
|
||||
@@ -26,7 +26,7 @@ install:
|
||||
|
||||
# Install Python, py.test, and required packages.
|
||||
- conda env create -f environment.yml
|
||||
- source activate cellxgene-dev
|
||||
- source activate cellxgene-gateway
|
||||
- python setup.py install
|
||||
|
||||
script:
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
# 0.2.3
|
||||
|
||||
* Added support for ProxyFix
|
||||
|
||||
# 0.2.2
|
||||
|
||||
* Fixed bug with annotations (missing annotation.js asset)
|
||||
|
||||
# 0.2.1
|
||||
|
||||
* Minor fixes to enable cellxgene 0.16.0
|
||||
|
||||
18
README.md
18
README.md
@@ -60,6 +60,7 @@ Here's what the environment variables mean:
|
||||
|
||||
* `CELLXGENE_LOCATION` - the location of the cellxgene executable, e.g. `~/anaconda2/envs/cellxgene/bin/cellxgene`
|
||||
* `CELLXGENE_DATA` - a directory that can contain subdirectories with `.h5ad` data files, *without* trailing slash, e.g. `/mnt/cellxgene_data`
|
||||
|
||||
Optional environment variables:
|
||||
* `CELLXGENE_ARGS` - catch-all variable that can be used to pass additional command line args to cellxgene server
|
||||
* `EXTERNAL_HOST` - the hostname and port from the perspective of the web browser, typically `localhost:5005` if running locally. Defaults to "localhost:{GATEWAY_PORT}"
|
||||
@@ -69,7 +70,14 @@ Optional environment variables:
|
||||
* `GATEWAY_EXTRA_SCRIPTS` - JSON array of script paths, will be embedded into each page and forwarded with `--scripts` to cellxgene server
|
||||
* `GATEWAY_ENABLE_UPLOAD` - Set to `true` or `1` to enable HTTP uploads. This is not recommended for a public server.
|
||||
* `GATEWAY_ENABLE_ANNOTATIONS` - Set to `true` or to `1` to enable cellxgene annotations.
|
||||
* `GATEWAY_ENABLE_BACKED_MODE` - Set to `true` or to `1` to load AnnData in file-backed mode. This saves memory and speeds up launch time but may reduce overall performance.
|
||||
* `GATEWAY_ENABLE_BACKED_MODE` - Set to `true` or to `1` to load AnnData in file-backed mode. This saves memory and speeds up launch time but may reduce overall performance.
|
||||
|
||||
If any of the following optional variables are set, [ProxyFix](https://werkzeug.palletsprojects.com/en/1.0.x/middleware/proxy_fix/) will be used.
|
||||
* `PROXY_FIX_FOR` - Number of upstream proxies setting X-Forwarded-For
|
||||
* `PROXY_FIX_PROTO` - Number of upstream proxies setting X-Forwarded-Proto
|
||||
* `PROXY_FIX_HOST` - Number of upstream proxies setting X-Forwarded-Host
|
||||
* `PROXY_FIX_PORT` - Number of upstream proxies setting X-Forwarded-Port
|
||||
* `PROXY_FIX_PREFIX` - Number of upstream proxies setting X-Forwarded-Prefix
|
||||
|
||||
The defaults should be fine if you set up a venv and cellxgene_data folder as above.
|
||||
|
||||
@@ -113,6 +121,14 @@ python setup.py develop
|
||||
|
||||
For convenience, the code repo includes a `run.sh.example` shell script to run the gateway.
|
||||
|
||||
4. Install pre-commit hooks
|
||||
|
||||
```bash
|
||||
conda install -c conda-forge pre-commit
|
||||
pre-commit install
|
||||
```
|
||||
|
||||
|
||||
## Running Tests
|
||||
|
||||
[](https://travis-ci.org/Novartis/cellxgene-gateway)
|
||||
|
||||
2
cellxgene_gateway/__init__.py
Executable file → Normal file
2
cellxgene_gateway/__init__.py
Executable file → Normal file
@@ -7,4 +7,4 @@
|
||||
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing permissions and limitations under the License.
|
||||
|
||||
__version__ = "0.2.1"
|
||||
__version__ = "0.2.3"
|
||||
|
||||
@@ -13,7 +13,7 @@ from threading import Thread
|
||||
from flask_api import status
|
||||
|
||||
from cellxgene_gateway import env
|
||||
from cellxgene_gateway.cache_entry import CacheEntry
|
||||
from cellxgene_gateway.cache_entry import CacheEntry, CacheEntryStatus
|
||||
from cellxgene_gateway.cellxgene_exception import CellxgeneException
|
||||
from cellxgene_gateway.subprocess_backend import SubprocessBackend
|
||||
|
||||
@@ -42,7 +42,7 @@ class BackendCache:
|
||||
for c in contents
|
||||
if c.key.dataset == key.dataset
|
||||
and c.key.annotation_file == key.annotation_file
|
||||
and c.status != "terminated"
|
||||
and c.status != CacheEntryStatus.terminated
|
||||
]
|
||||
|
||||
if len(matches) == 0:
|
||||
|
||||
@@ -6,18 +6,28 @@
|
||||
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
|
||||
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing permissions and limitations under the License.
|
||||
import psutil
|
||||
import logging
|
||||
import datetime
|
||||
import logging
|
||||
from flask.helpers import url_for
|
||||
from flask.wrappers import Response
|
||||
|
||||
from flask import make_response, request, render_template
|
||||
import psutil
|
||||
from enum import Enum
|
||||
from flask import make_response, render_template, request
|
||||
from requests import get, post, put
|
||||
import re
|
||||
|
||||
from cellxgene_gateway import env
|
||||
from cellxgene_gateway.cellxgene_exception import CellxgeneException
|
||||
from cellxgene_gateway.util import current_time_stamp
|
||||
from cellxgene_gateway.flask_util import querystring
|
||||
from cellxgene_gateway.util import current_time_stamp
|
||||
|
||||
|
||||
class CacheEntryStatus(Enum):
|
||||
loaded = "loaded"
|
||||
loading = "loading"
|
||||
error = "error"
|
||||
terminated = "terminated"
|
||||
|
||||
|
||||
class CacheEntry:
|
||||
@@ -28,7 +38,7 @@ class CacheEntry:
|
||||
port,
|
||||
launchtime,
|
||||
timestamp,
|
||||
status,
|
||||
status: CacheEntryStatus,
|
||||
message,
|
||||
all_output,
|
||||
stderr,
|
||||
@@ -54,7 +64,7 @@ class CacheEntry:
|
||||
port,
|
||||
current_time_stamp(),
|
||||
current_time_stamp(),
|
||||
"loading",
|
||||
CacheEntryStatus.loading,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
@@ -63,13 +73,13 @@ class CacheEntry:
|
||||
|
||||
def set_loaded(self, pid):
|
||||
self.pid = pid
|
||||
self.status = "loaded"
|
||||
self.status = CacheEntryStatus.loaded
|
||||
|
||||
def set_error(self, message, stderr, http_status):
|
||||
self.message = message
|
||||
self.stderr = stderr
|
||||
self.http_status = http_status
|
||||
self.status = "error"
|
||||
self.status = CacheEntryStatus.error
|
||||
|
||||
def append_output(self, output):
|
||||
if self.all_output == None:
|
||||
@@ -79,7 +89,7 @@ class CacheEntry:
|
||||
|
||||
def terminate(self):
|
||||
pid = self.pid
|
||||
if pid != None and self.status != "terminated":
|
||||
if pid != None and self.status != CacheEntryStatus.terminated:
|
||||
terminated = []
|
||||
|
||||
def on_terminate(p):
|
||||
@@ -96,7 +106,7 @@ class CacheEntry:
|
||||
logging.getLogger("cellxgene_gateway").info(
|
||||
f"terminated {terminated}"
|
||||
)
|
||||
self.status = "terminated"
|
||||
self.status = CacheEntryStatus.terminated
|
||||
|
||||
def rewrite_text_content(self, cellxgene_content):
|
||||
# for v0.16.0 compatibility, see issue #24
|
||||
@@ -112,7 +122,7 @@ class CacheEntry:
|
||||
return gateway_content
|
||||
|
||||
def gateway_basepath(self):
|
||||
return f"{env.external_protocol}://{env.external_host}/view/{self.key.pathpart}/"
|
||||
return url_for("do_view", path=self.key.pathpart) + "/"
|
||||
|
||||
def cellxgene_basepath(self):
|
||||
return f"http://127.0.0.1:{self.port}"
|
||||
@@ -122,10 +132,10 @@ class CacheEntry:
|
||||
subpath = path[len(self.key.pathpart) :] # noqa: E203
|
||||
|
||||
if len(subpath) == 0:
|
||||
r = make_response(f"Redirect to {gateway_basepath}\n", 301)
|
||||
r = make_response(f"Redirect to {gateway_basepath}\n", 302)
|
||||
r.headers["location"] = gateway_basepath + querystring()
|
||||
return r
|
||||
elif self.status == "loading":
|
||||
elif self.status == CacheEntryStatus.loading:
|
||||
launch_time = datetime.datetime.fromtimestamp(self.launchtime)
|
||||
return render_template(
|
||||
"loading.html",
|
||||
@@ -161,11 +171,15 @@ class CacheEntry:
|
||||
cellxgene_response = get(full_path, headers=headers)
|
||||
elif request.method == "PUT":
|
||||
cellxgene_response = put(
|
||||
full_path, headers=headers, data=request.data,
|
||||
full_path,
|
||||
headers=headers,
|
||||
data=request.data,
|
||||
)
|
||||
elif request.method == "POST":
|
||||
cellxgene_response = post(
|
||||
full_path, headers=headers, data=request.data,
|
||||
full_path,
|
||||
headers=headers,
|
||||
data=request.data,
|
||||
)
|
||||
else:
|
||||
raise CellxgeneException(
|
||||
@@ -185,7 +199,9 @@ class CacheEntry:
|
||||
resp_headers[h] = cellxgene_response.headers[h]
|
||||
|
||||
gateway_response = make_response(
|
||||
gateway_content, cellxgene_response.status_code, resp_headers,
|
||||
gateway_content,
|
||||
cellxgene_response.status_code,
|
||||
resp_headers,
|
||||
)
|
||||
|
||||
return gateway_response
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
# There are three kinds of CacheKey:
|
||||
# 1) somedir/dataset.h5ad: a dataset
|
||||
# in this case, pathpart == dataset == 'somedir/dataset.h5ad'
|
||||
# 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotaitons file.
|
||||
# in this case, pathpart == 'dataset_annotations/saldaal1-T5HMVBNV.csv', dataset == 'somedir/dataset.h5ad'
|
||||
# 2) somedir/dataset_annotations/my_annotations.csv : an actual annotaitons file.
|
||||
# in this case, pathpart == 'dataset_annotations/my_annotations.csv', dataset == 'somedir/dataset.h5ad'
|
||||
# 3) somedir/dataset_annotations: an annotation directory. The corresponding h5ad must exist, but the directory may not.
|
||||
# in this case, pathpart == 'dataset_annotations', dataset == 'somedir/dataset.h5ad'
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing permissions and limitations under the License.
|
||||
|
||||
import os
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
|
||||
cellxgene_location = os.environ.get("CELLXGENE_LOCATION")
|
||||
@@ -31,16 +31,28 @@ enable_upload = os.environ.get("GATEWAY_ENABLE_UPLOAD", "").lower() in [
|
||||
]
|
||||
enable_annotations = os.environ.get(
|
||||
"GATEWAY_ENABLE_ANNOTATIONS", ""
|
||||
).lower() in ["true", "1"]
|
||||
).lower() in [
|
||||
"true",
|
||||
"1",
|
||||
]
|
||||
enable_backed_mode = os.environ.get(
|
||||
"GATEWAY_ENABLE_BACKED_MODE", ""
|
||||
).lower() in ["true", "1"]
|
||||
).lower() in [
|
||||
"true",
|
||||
"1",
|
||||
]
|
||||
|
||||
env_vars = {
|
||||
"CELLXGENE_LOCATION": cellxgene_location,
|
||||
"CELLXGENE_DATA": cellxgene_data,
|
||||
}
|
||||
|
||||
proxy_fix_for = int(os.environ.get("PROXY_FIX_FOR", "0"))
|
||||
proxy_fix_proto = int(os.environ.get("PROXY_FIX_PROTO", "0"))
|
||||
proxy_fix_host = int(os.environ.get("PROXY_FIX_HOST", "0"))
|
||||
proxy_fix_port = int(os.environ.get("PROXY_FIX_PORT", "0"))
|
||||
proxy_fix_prefix = int(os.environ.get("PROXY_FIX_PREFIX", "0"))
|
||||
|
||||
optional_env_vars = {
|
||||
"EXTERNAL_HOST": external_host,
|
||||
"EXTERNAL_PROTOCOL": external_protocol,
|
||||
@@ -52,6 +64,11 @@ optional_env_vars = {
|
||||
"GATEWAY_ENABLE_ANNOTATIONS": enable_annotations,
|
||||
"GATEWAY_ENABLE_BACKED_MODE": enable_backed_mode,
|
||||
"CELLXGENE_ARGS": cellxgene_args,
|
||||
"PROXY_FIX_FOR": proxy_fix_for,
|
||||
"PROXY_FIX_PROTO": proxy_fix_proto,
|
||||
"PROXY_FIX_HOST": proxy_fix_host,
|
||||
"PROXY_FIX_PORT": proxy_fix_port,
|
||||
"PROXY_FIX_PREFIX": proxy_fix_prefix,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,9 +7,10 @@
|
||||
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing permissions and limitations under the License.
|
||||
|
||||
from cellxgene_gateway import env
|
||||
from json import loads
|
||||
|
||||
from cellxgene_gateway import env
|
||||
|
||||
|
||||
def get_extra_scripts():
|
||||
# can be array of script tags to inject on every page, e.g. for google analytics could be
|
||||
|
||||
@@ -14,6 +14,7 @@ from cellxgene_gateway.dir_util import (
|
||||
make_annotations,
|
||||
annotations_suffix,
|
||||
)
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def recurse_dir(path):
|
||||
@@ -91,7 +92,7 @@ def render_entries(entries):
|
||||
|
||||
|
||||
def get_url(entry):
|
||||
return f"/view/{ entry['path'].lstrip('/') }"
|
||||
return url_for("do_view", path=entry["path"].lstrip("/") + "/")
|
||||
|
||||
|
||||
def get_class(entry):
|
||||
|
||||
@@ -7,16 +7,17 @@
|
||||
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing permissions and limitations under the License.
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
# import BaseHTTPServer
|
||||
import os
|
||||
import logging
|
||||
from threading import Thread, Lock
|
||||
import json
|
||||
from threading import Lock, Thread
|
||||
|
||||
from flask import (
|
||||
Flask,
|
||||
redirect,
|
||||
make_response,
|
||||
redirect,
|
||||
render_template,
|
||||
request,
|
||||
send_from_directory,
|
||||
@@ -24,17 +25,18 @@ from flask import (
|
||||
)
|
||||
from flask_api import status
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
from cellxgene_gateway import env
|
||||
from cellxgene_gateway.backend_cache import BackendCache
|
||||
from cellxgene_gateway.cache_entry import CacheEntryStatus
|
||||
from cellxgene_gateway.cellxgene_exception import CellxgeneException
|
||||
from cellxgene_gateway.dir_util import create_dir, is_subdir
|
||||
from cellxgene_gateway.filecrawl import recurse_dir, render_entries
|
||||
from cellxgene_gateway.extra_scripts import get_extra_scripts
|
||||
from cellxgene_gateway.filecrawl import recurse_dir, render_entries
|
||||
from cellxgene_gateway.path_util import get_key
|
||||
from cellxgene_gateway.process_exception import ProcessException
|
||||
from cellxgene_gateway.prune_process_cache import PruneProcessCache
|
||||
from cellxgene_gateway.util import current_time_stamp
|
||||
from cellxgene_gateway.path_util import get_key
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -48,9 +50,23 @@ def _force_https(app):
|
||||
|
||||
|
||||
app.wsgi_app = _force_https(app.wsgi_app)
|
||||
if (
|
||||
env.proxy_fix_for > 0
|
||||
or env.proxy_fix_proto > 0
|
||||
or env.proxy_fix_host > 0
|
||||
or env.proxy_fix_port > 0
|
||||
or env.proxy_fix_prefix > 0
|
||||
):
|
||||
app.wsgi_app = ProxyFix(
|
||||
app.wsgi_app,
|
||||
x_for=env.proxy_fix_for,
|
||||
x_proto=env.proxy_fix_proto,
|
||||
x_host=env.proxy_fix_host,
|
||||
x_port=env.proxy_fix_port,
|
||||
x_prefix=env.proxy_fix_prefix,
|
||||
)
|
||||
|
||||
cache = BackendCache()
|
||||
location = f"{env.external_protocol}://{env.external_host}"
|
||||
|
||||
|
||||
@app.errorhandler(CellxgeneException)
|
||||
@@ -124,7 +140,7 @@ def make_user():
|
||||
|
||||
create_dir(env.cellxgene_data, dir_name)
|
||||
|
||||
return redirect(location, code=302)
|
||||
return redirect(url_for("index"), code=302)
|
||||
|
||||
|
||||
def make_subdir():
|
||||
@@ -133,7 +149,7 @@ def make_subdir():
|
||||
|
||||
create_dir(parent_path, dir_name)
|
||||
|
||||
return redirect(location, code=302)
|
||||
return redirect(url_for("index"), code=302)
|
||||
|
||||
|
||||
def upload_file():
|
||||
@@ -152,7 +168,7 @@ def upload_file():
|
||||
full_upload_path, secure_filename(f.filename)
|
||||
)
|
||||
)
|
||||
return redirect("/filecrawl.html", code=302)
|
||||
return redirect(url_for("filecrawl"), code=302)
|
||||
else:
|
||||
raise CellxgeneException(
|
||||
"Uploaded file must be in anndata (.h5ad) format.",
|
||||
@@ -168,7 +184,7 @@ def upload_file():
|
||||
"Invalid directory.", status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
return redirect(location, code=302)
|
||||
return redirect(url_for("index"), code=302)
|
||||
|
||||
|
||||
if env.enable_upload:
|
||||
@@ -238,9 +254,12 @@ def do_view(path):
|
||||
|
||||
match.timestamp = current_time_stamp()
|
||||
|
||||
if match.status == "loaded" or match.status == "loading":
|
||||
if (
|
||||
match.status == CacheEntryStatus.loaded
|
||||
or match.status == CacheEntryStatus.loading
|
||||
):
|
||||
return match.serve_content(path)
|
||||
elif match.status == "error":
|
||||
elif match.status == CacheEntryStatus.error:
|
||||
raise ProcessException.from_cache_entry(match)
|
||||
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ import os
|
||||
from flask_api import status
|
||||
|
||||
from cellxgene_gateway import env
|
||||
from cellxgene_gateway.cache_key import CacheKey
|
||||
from cellxgene_gateway.cellxgene_exception import CellxgeneException
|
||||
from cellxgene_gateway.dir_util import make_h5ad
|
||||
from cellxgene_gateway.cache_key import CacheKey
|
||||
|
||||
|
||||
def get_key(path):
|
||||
@@ -31,7 +31,7 @@ def get_key(path):
|
||||
return CacheKey(trimmed, trimmed, None)
|
||||
elif trimmed.endswith(".csv"):
|
||||
|
||||
# 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotations file.
|
||||
# 2) somedir/dataset_annotations/my_annotations.csv : an actual annotations file.
|
||||
annotations_dir = os.path.split(trimmed)[0]
|
||||
dataset = make_h5ad(annotations_dir)
|
||||
if data_file_exists(dataset):
|
||||
|
||||
@@ -7,17 +7,19 @@
|
||||
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing permissions and limitations under the License.
|
||||
|
||||
import time
|
||||
import logging
|
||||
import time
|
||||
|
||||
from cellxgene_gateway.util import current_time_stamp
|
||||
from cellxgene_gateway.env import ttl
|
||||
from cellxgene_gateway import env
|
||||
from cellxgene_gateway import util
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PruneProcessCache:
|
||||
def __init__(self, cache):
|
||||
self.cache = cache
|
||||
self.expire_seconds = 3600 if ttl is None else int(ttl)
|
||||
self.expire_seconds = 3600 if env.ttl is None else int(env.ttl)
|
||||
|
||||
def __call__(self):
|
||||
while True:
|
||||
@@ -25,7 +27,7 @@ class PruneProcessCache:
|
||||
self.prune()
|
||||
|
||||
def prune(self):
|
||||
timestamp = current_time_stamp()
|
||||
timestamp = util.current_time_stamp()
|
||||
cutoff = timestamp - self.expire_seconds
|
||||
processes_to_delete = [
|
||||
p for p in self.cache.entry_list if p.timestamp < cutoff
|
||||
@@ -33,9 +35,9 @@ class PruneProcessCache:
|
||||
processes_to_keep = [
|
||||
p for p in self.cache.entry_list if not p.timestamp < cutoff
|
||||
]
|
||||
logger = logging.getLogger("cellxgene_gateway")
|
||||
|
||||
logger.debug(
|
||||
f"Cutoff {cutoff} = timestamp {timestamp} - expire seconds {self.expire_seconds} , keeping {processes_to_keep}"
|
||||
f"Cutoff {cutoff} = timestamp {timestamp} - expire seconds {self.expire_seconds} , keeping {processes_to_keep}, pruning {processes_to_delete}"
|
||||
)
|
||||
|
||||
for process in processes_to_delete:
|
||||
|
||||
@@ -11,14 +11,15 @@ import logging
|
||||
import subprocess
|
||||
|
||||
from flask_api import status
|
||||
from cellxgene_gateway.cache_entry import CacheEntryStatus
|
||||
from cellxgene_gateway.dir_util import make_annotations
|
||||
from cellxgene_gateway.path_util import get_annotation_file_path, get_file_path
|
||||
from cellxgene_gateway.env import (
|
||||
enable_annotations,
|
||||
enable_backed_mode,
|
||||
cellxgene_args,
|
||||
)
|
||||
from cellxgene_gateway.process_exception import ProcessException
|
||||
from cellxgene_gateway.dir_util import make_annotations
|
||||
from cellxgene_gateway.path_util import get_file_path, get_annotation_file_path
|
||||
|
||||
|
||||
class SubprocessBackend:
|
||||
@@ -85,7 +86,7 @@ class SubprocessBackend:
|
||||
message = "Cellxgene failed to launch dataset."
|
||||
http_status = status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
|
||||
cache_entry.status = "error"
|
||||
cache_entry.status = CacheEntryStatus.error
|
||||
cache_entry.set_error(message, stderr, http_status)
|
||||
|
||||
raise ProcessException.from_cache_entry(cache_entry)
|
||||
|
||||
@@ -48,11 +48,11 @@
|
||||
<td>{{ entry.port }}</td>
|
||||
<td class="timestamp">{{ entry.launchtime }}</td>
|
||||
<td class="timestamp">{{ entry.timestamp }}</td>
|
||||
<td>{{ entry.status }}</td>
|
||||
<td>{{ entry.status.name }}</td>
|
||||
<td>{{ entry.message }}</td>
|
||||
<td>{{ entry.http_status }}</td>
|
||||
<td>
|
||||
{% if entry.status == 'loaded' %}
|
||||
{% if entry.status.name == 'loaded' %}
|
||||
<a href="{{ url_for('do_terminate', path=entry.key.pathpart) }}"> terminate </a>
|
||||
{% endif %}
|
||||
</td>
|
||||
|
||||
@@ -28,11 +28,11 @@
|
||||
|
||||
<h4>{{ message }}</h4>
|
||||
|
||||
<a href="/filecrawl.html">
|
||||
<a href="{{ url_for('filecrawl') }}">
|
||||
Please click here to be redirected to the file directory.
|
||||
</a>
|
||||
<br>
|
||||
<a href="/">
|
||||
<a href="{{ url_for('index') }}">
|
||||
Please click here to return to the homepage.
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -36,10 +36,10 @@
|
||||
Navigation:
|
||||
<ul>
|
||||
{% if path %}
|
||||
<li><a href="/filecrawl.html">top level</a></li>
|
||||
<li><a href="{{ url_for('filecrawl') }}">top level</a></li>
|
||||
{% else %}
|
||||
{% endif %}
|
||||
<li><a href="/">homepage</a></li>
|
||||
<li><a href="{{ url_for('index') }}">homepage</a></li>
|
||||
</ul>
|
||||
</p>
|
||||
<script>
|
||||
|
||||
@@ -35,12 +35,12 @@
|
||||
Links:
|
||||
</h1>
|
||||
<div class="list-group" style="width:50%;padding-left:65px">
|
||||
<a href="/filecrawl.html" class="list-group-item list-group-item-action">
|
||||
<a href="{{ url_for('filecrawl') }}" class="list-group-item list-group-item-action">
|
||||
<u>File Crawler: Allows you to view all uploaded data.</u></a>
|
||||
|
||||
</div>
|
||||
<div class="list-group" style="width:50%;padding-left:65px">
|
||||
<a href="/cache_status" class="list-group-item list-group-item-action">
|
||||
<a href="{{ url_for('do_GET_status') }}" class="list-group-item list-group-item-action">
|
||||
<u>Cache Status: view status of launched cellxgene servers.</u></a>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -35,11 +35,11 @@
|
||||
The page will refresh shortly.
|
||||
</p>
|
||||
|
||||
<a href="/filecrawl.html">
|
||||
<a href="{{ url_for('filecrawl') }}">
|
||||
Please click here to be redirected to the file directory.
|
||||
</a>
|
||||
<br>
|
||||
<a href="/">
|
||||
<a href="{{ url_for('index') }}">
|
||||
Please click here to return to the homepage.
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -39,10 +39,10 @@
|
||||
<li><a href="{{url_for('do_relaunch', path=dataset)}}">
|
||||
Attempt to relaunch the cellxgene server.
|
||||
</a></li>
|
||||
<li><a href="/filecrawl.html">
|
||||
<li><a href="{{ url_for('filecrawl') }}">
|
||||
Return to the file directory.
|
||||
</a></li>
|
||||
<li><a href="/">
|
||||
<li><a href="{{ url_for('index') }}">
|
||||
Return to the homepage.
|
||||
</a></li>
|
||||
</ul>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
name: cellxgene-dev
|
||||
name: cellxgene-gateway
|
||||
channels:
|
||||
- conda-forge
|
||||
dependencies:
|
||||
|
||||
1
setup.py
1
setup.py
@@ -53,6 +53,7 @@ setup(
|
||||
package_data={
|
||||
"cellxgene_gateway": [
|
||||
"static/css/homepagestyle.css",
|
||||
"static/js/annotation.js",
|
||||
"static/nibr.ico",
|
||||
"templates/*.html",
|
||||
]
|
||||
|
||||
@@ -1,23 +1,37 @@
|
||||
import unittest
|
||||
from cellxgene_gateway.cache_entry import CacheEntry
|
||||
from flask import Flask
|
||||
from cellxgene_gateway.cache_entry import CacheEntry, CacheEntryStatus
|
||||
from cellxgene_gateway.cache_key import CacheKey
|
||||
from cellxgene_gateway.gateway import app
|
||||
|
||||
key = CacheKey("czi/pbmc3k.h5ad", "pbmc3k.h5ad", "tmp.csv")
|
||||
|
||||
|
||||
class TestRenderEntry(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = app
|
||||
self.app_context = self.app.test_request_context()
|
||||
self.app_context.push()
|
||||
self.client = self.app.test_client()
|
||||
|
||||
def test_GIVEN_key_and_port_THEN_returns_loading_CacheEntry(self):
|
||||
entry = CacheEntry.for_key("some-key", 1)
|
||||
self.assertEqual(entry.status, CacheEntryStatus.loading)
|
||||
|
||||
def test_GIVEN_absolute_static_url_THEN_include_path(self):
|
||||
actual = CacheEntry.for_key(key, 8000).rewrite_text_content(
|
||||
"src:url(/static/assets/"
|
||||
)
|
||||
expected = (
|
||||
"src:url(http://localhost:5005/view/czi/pbmc3k.h5ad/static/assets/"
|
||||
)
|
||||
expected = "src:url(/view/czi/pbmc3k.h5ad/static/assets/"
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_GIVEN_absolute_src_THEN_include_path(self):
|
||||
actual = CacheEntry.for_key(key, 8000).rewrite_text_content(
|
||||
'<link rel="shortcut icon" href="/static/assets/favicon.ico">'
|
||||
)
|
||||
expected = '<link rel="shortcut icon" href="http://localhost:5005/view/czi/pbmc3k.h5ad/static/assets/favicon.ico">'
|
||||
expected = '<link rel="shortcut icon" href="/view/czi/pbmc3k.h5ad/static/assets/favicon.ico">'
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from cellxgene_gateway.extra_scripts import get_extra_scripts
|
||||
|
||||
|
||||
|
||||
46
tests/test_filecrawl.py
Normal file
46
tests/test_filecrawl.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from cellxgene_gateway.filecrawl import render_entry
|
||||
|
||||
|
||||
class TestRenderEntry(unittest.TestCase):
|
||||
def test_GIVEN_path_both_slash_THEN_view_has_single_slash(self):
|
||||
entry = {
|
||||
"path": "/somepath/",
|
||||
"name": "entry",
|
||||
"type": "file",
|
||||
"annotations": [],
|
||||
}
|
||||
rendered = render_entry(entry)
|
||||
self.assertIn("view/somepath", rendered)
|
||||
|
||||
def test_GIVEN_path_starts_slash_THEN_view_has_single_slash(self):
|
||||
entry = {
|
||||
"path": "/somepath",
|
||||
"name": "entry",
|
||||
"type": "file",
|
||||
"annotations": [],
|
||||
}
|
||||
rendered = render_entry(entry)
|
||||
self.assertIn("view/somepath", rendered)
|
||||
|
||||
def test_GIVEN_path_ends_slash_THEN_view_has_single_slash(self):
|
||||
entry = {
|
||||
"path": "somepath/",
|
||||
"name": "entry",
|
||||
"type": "file",
|
||||
"annotations": [],
|
||||
}
|
||||
rendered = render_entry(entry)
|
||||
self.assertIn("view/somepath", rendered)
|
||||
|
||||
def test_GIVEN_path_no_slash_THEN_view_has_single_slash(self):
|
||||
entry = {
|
||||
"path": "somepath",
|
||||
"name": "entry",
|
||||
"type": "file",
|
||||
"annotations": [],
|
||||
}
|
||||
rendered = render_entry(entry)
|
||||
self.assertIn("view/somepath", rendered)
|
||||
@@ -1,7 +1,8 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
from cellxgene_gateway.cache_entry import CacheEntry
|
||||
|
||||
from cellxgene_gateway.backend_cache import BackendCache
|
||||
from cellxgene_gateway.cache_entry import CacheEntry
|
||||
|
||||
|
||||
class TestPruneProcessCache(unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user