This is the repository for the NSLS-II Facility API codebase.
In order to develop locally you will need to have a local MongoDB running.
This can be installed using your preferred method, a native install
or running a container work perfectly fine.
Once you have MongoDB up and running you then need to 'seed' the facility and beamline information that does not get pulled from any other source.
The files for the collections can be found within the /nsls2/software/dssi/nsls2core/nsls2core-development.tgz
-
Copy and unpack the archive into a directory (e.g.
nsls2core-development) to your development machine -
Import facility information into the local mongodb```
mongorestore --uri="mongodb://localhost:27017" --nsFrom=nsls2core-development.facilities --nsTo=nsls2core-development.facilities ./nsls2core-development/facilities.bson -
Import beamline information into the local mongodb
mongorestore --uri="mongodb://localhost:27017" --nsFrom=nsls2core-development.beamlines --nsTo=nsls2core-development.beamlines ./nsls2core-development/beamlines.bson
You will then need to create a .env file that contains the configuration (an example can also be found in the same
directory as the json files).
- Copy
/nsls2/software/dssi/nsls2core/.env.developmentto your local machine - Rename to
.envand place in thesrc/nsls2apidirectory in your cloned repo (in the same folder asmain.py) - Ensure that you have the
bnlroot.crtfile (which is deployed to all BNL managed machines) in the location specified within the.envfile.
These admin-only endpoints let an external job create Slack channels while
nsls2api supplies the naming rules and stores the resulting IDs. Neither new
endpoint calls Slack. Send an admin API key as the raw Authorization header
value, without a Bearer prefix. Do not put the key in a URL or request body.
GET /v1/proposal/{proposal_id}/slack-channels/spec returns a list: the general
channel first, followed by the beamline channels. For example, one list entry
is:
{
"proposal_id": "314159",
"channel_name": "pass-314159",
"channel_id": null,
"topic": "Discussion channel for proposal 314159 - PI: Unknown Unknown - Title: Test Proposal",
"beamlines": ["ZZZ"],
"beamline_members": {
"ZZZ": {
"slack_channel_managers": [],
"slack_beamline_bot_user_id": null
}
},
"user_emails": ["member@example.org"]
}channel_id is the stored ID matching that exact channel name, or null.
Each channel's beamline_members map contains only its beamlines, with raw
stored manager and bot user IDs. Proposal e-mails are trimmed, deduplicated,
and omit blank values. Missing Slack metadata yields empty manager lists and
null bot IDs. The spec includes all proposal beamlines, regardless of
slack_autocreate_channels; filtering enabled beamlines and truncating long
topics are the client's responsibility. GET never updates the proposal.
PUT /v1/proposal/{proposal_id}/slack-channels accepts a bare list:
[
{"channel_name": "pass-314159", "channel_id": "C_EXAMPLE"}
]It atomically merges by name and returns the complete stored channel list
with those two fields. Submitted names must be generated for this proposal;
omitted entries, including historic names, are preserved. Registering an
existing name replaces its ID; repeating a request does not duplicate entries.
Concurrent registrations for different names are preserved, while the last
database write wins for the same name. A nonempty registration updates
last_updated without changing unrelated proposal fields. An empty list is
a no-op, including its timestamp; it does not clear channels.
Missing/invalid/non-admin keys return 401; a missing proposal returns 404. Malformed bodies, extra fields, duplicate or unknown names, non-string values, and empty or whitespace-padded names/IDs return 422 without a write. Slack IDs are recorded without live Slack validation.
The existing GET /v1/proposal/{proposal_id}/slack-channels still returns live
Slack details with conversation_id, name, and is_private. For private
channels registered by a different bot, that GET still requires the API's bot
to be a member. The old POST at the same path is deprecated, but remains
available with its existing admin guard and behavior. Existing beamline Slack
readers are unchanged.
When started with uvicorn_log_config.yml (as the container is), application (uvicorn.error) logs can also be pushed to
Grafana Loki with loki-logger-handler.
Access logs are not currently pushed to Loki.
This is off unless both LOKI_URL and LOKI_PUSH_SECRET are set (in the environment or the .env file); leave them
unset for local development.
| Variable | Default | Description |
|---|---|---|
LOKI_URL |
(empty) | Loki push URL |
LOKI_PUSH_SECRET |
(empty) | Value of the loki-push-secret header |
LOKI_ENVIRONMENT |
(empty) | deployment_environment_name label, e.g. production or development |
LOKI_HOST |
the fully qualified hostname | host label |
Streams also carry service_name="nsls2api", which is set in the code. The level is a field in each JSON line, not a
label; Loki detects it, so query it with {service_name="nsls2api"} | detected_level="error".
This project uses ruff to format the code - in order to run the formatter simply type:
uvx ruff formatThis project uses ruff to check the code - in order to run the linter simply type:
uvx ruff checkThe project uses uv pip compile to manage the requirements.txt and requirements-dev.txt files.
In order to upgrade the packages versions you will need to simply run
uv pip compile requirements-dev.in --upgrade -o requirements-dev.txt --exclude-newer "1 week"
uv pip compile requirements.in --upgrade -o requirements.txt --exclude-newer "1 week"
Then in order to actually upgrade the packages
uv pip install -r requirements-dev.txt
uv pip install -r requirements.txt
Of course, you can drop the uv from these last commands if you want to wait longer.