Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
84 changes: 83 additions & 1 deletion src/murfey/server/api/session_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from pathlib import Path
from typing import Dict, List, Optional

import requests
from fastapi import APIRouter, Depends, Request
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel
from sqlmodel import select

Expand Down Expand Up @@ -276,6 +277,87 @@ def update_current_gain_ref(
db.commit()


# Endpoints for turning alerts on and off


@router.get("/silences/{instrument_name}")
def get_silences(instrument_name: MurfeyInstrumentName):
all_configs = get_machine_config()
valid_instrument_names = list(all_configs.keys())
if instrument_name in valid_instrument_names:
index = valid_instrument_names.index(instrument_name)
query_params = f"filter=microscope={sanitise(valid_instrument_names[index])}"
else:
return None
Comment thread
stephen-riggs marked this conversation as resolved.
Outdated
machine_config = all_configs[instrument_name]
alertmanager_url = machine_config.alertmanager_url
if alertmanager_url == "":
Comment thread
stephen-riggs marked this conversation as resolved.
Outdated
logger.warning(f"alertmanager_url not set for {sanitise(instrument_name)}")
return None
response = requests.get(f"{alertmanager_url}/api/v2/silences?{query_params}")
if response.status_code != 200:
logger.warning(
f"Tried to get silences for {sanitise(instrument_name)}, but received status {response.status_code} from alertmanager API"
)
active_silences = []
for silence in response.json():
if silence["status"]["state"] == "active":
active_silences.append(silence)
return active_silences


@router.post("/silences/{instrument_name}")
def create_silence(instrument_name: MurfeyInstrumentName, end_time: datetime):
machine_config = machine_info_by_instrument(instrument_name)
alertmanager_url = machine_config.alertmanager_url
if alertmanager_url == "":
logger.warning(f"alertmanager_url not set for {sanitise(instrument_name)}")
return None
start_time = datetime.now().astimezone().isoformat()
end_time_str = end_time.astimezone().isoformat()
silence_json = {
"matchers": [
{"name": "microscope", "value": instrument_name, "isRegex": False}
],
"createdBy": "murfey",
"annotations": {"description": f"Silence for alerts on {instrument_name}"},
"comment": "silence created from murfey",
"status": {"state": "active"},
"startsAt": str(start_time),
"endsAt": str(end_time_str),
}
response = requests.post(f"{alertmanager_url}/api/v2/silences", json=silence_json)
if response.status_code != 200:
logger.warning(
f"Tried to create silence for {sanitise(instrument_name)}, but received status {response.status_code} from alertmanager API"
)
return JSONResponse(
status_code=response.status_code, content=response.json()
) # return a response with same data and code as from alertmanager


@router.delete(
"/silences/{instrument_name}"
) # delete all silences for given microscope
def delete_silences(instrument_name: MurfeyInstrumentName):
machine_config = machine_info_by_instrument(instrument_name)
alertmanager_url = machine_config.alertmanager_url
if alertmanager_url == "":
logger.warning(f"alertmanager_url not set for {sanitise(instrument_name)}")
return None
silences = get_silences(instrument_name)
if len(silences) == 0:
return None
for silence in silences:
id = silence["id"]
response = requests.delete(f"{alertmanager_url}/api/v2/silence/{id}")
Comment thread
stephen-riggs marked this conversation as resolved.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
if response.status_code != 200:
logger.warning(
f"Tried to delete silence {id} for {sanitise(instrument_name)}, but received status {response.status_code} from alertmanager API"
)
return response # returns final response in loop


spa_router = APIRouter(
prefix="/session_info/spa",
dependencies=[Depends(validate_token)],
Expand Down
3 changes: 3 additions & 0 deletions src/murfey/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class MachineConfig(BaseModel): # type: ignore
# Pydantic BaseModel settings
model_config = ConfigDict(extra="allow")

# Alertmanager URL
alertmanager_url: str = "https://murfey-alertmanager.diamond.ac.uk"
Comment thread
stephen-riggs marked this conversation as resolved.
Outdated

@field_validator("calibrations", mode="before")
@classmethod
def validate_calibration_data(
Expand Down
21 changes: 21 additions & 0 deletions src/murfey/util/route_manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,27 @@ murfey.server.api.session_info.router:
type: int
methods:
- PUT
- path: /session_info/silences/{instrument_name}
function: get_silences
path_params:
- name: instrument_name
type: str
methods:
- GET
- path: /session_info/silences/{instrument_name}
function: create_silence
path_params:
- name: instrument_name
type: str
methods:
- POST
- path: /session_info/silences/{instrument_name}
function: delete_silences
path_params:
- name: instrument_name
type: str
methods:
- DELETE
murfey.server.api.session_info.spa_router:
- path: /session_info/spa/sessions/{session_id}/spa_processing_parameters
function: get_spa_proc_param_details
Expand Down