Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba13c281d2 | ||
|
|
9184d1f7bd |
No files matched your search
@@ -1,10 +1,10 @@
|
|||||||
from ctypes import Union
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
from src.api.types import ListData, ListParams, UpdateTags
|
from src.api.types import ListData, ListParams, UpdateTags
|
||||||
from src.automations.errors import FailedDeletion
|
from src.automations.errors import FailedDeletion
|
||||||
from src.automations.manager import AutomationManager
|
from src.automations.manager import AutomationManager
|
||||||
from src.automations.types import Automation, ExtenededAutomation
|
from src.automations.types import Automation, ExtenededAutomation
|
||||||
|
from src.errors import ErrorSet
|
||||||
|
|
||||||
|
|
||||||
def make_automation_router(automations: AutomationManager) -> APIRouter:
|
def make_automation_router(automations: AutomationManager) -> APIRouter:
|
||||||
@@ -12,15 +12,18 @@ def make_automation_router(automations: AutomationManager) -> APIRouter:
|
|||||||
|
|
||||||
@router.post("/list")
|
@router.post("/list")
|
||||||
def list_autos(body: ListParams) -> ListData[ExtenededAutomation]:
|
def list_autos(body: ListParams) -> ListData[ExtenededAutomation]:
|
||||||
automations.reload()
|
try:
|
||||||
return ListData(
|
automations.reload()
|
||||||
params=body,
|
return ListData(
|
||||||
totalItems=automations.count(),
|
params=body,
|
||||||
data=automations.list(
|
totalItems=automations.count(),
|
||||||
offset=body.offset,
|
data=automations.list(
|
||||||
limit=body.limit,
|
offset=body.offset,
|
||||||
),
|
limit=body.limit,
|
||||||
)
|
),
|
||||||
|
)
|
||||||
|
except ErrorSet as err:
|
||||||
|
raise HTTPException(status_code=400, detail=err.gen_message())
|
||||||
|
|
||||||
@router.post("/item")
|
@router.post("/item")
|
||||||
def insert_auto(body: Automation):
|
def insert_auto(body: Automation):
|
||||||
|
|||||||
+20
-17
@@ -7,7 +7,7 @@ from typing import Iterable, Iterator, List, Optional
|
|||||||
|
|
||||||
from src.logger import get_logger
|
from src.logger import get_logger
|
||||||
|
|
||||||
from .errors import DBDataError, DBError, DBNoAutomationFound
|
from .errors import DBDataError, DBError, DBNoAutomationFound, RepeatedAutomationId
|
||||||
from .types import ExtenededAutomation
|
from .types import ExtenededAutomation
|
||||||
|
|
||||||
logger = get_logger(__file__)
|
logger = get_logger(__file__)
|
||||||
@@ -47,8 +47,6 @@ class AutomationDBConnection:
|
|||||||
try:
|
try:
|
||||||
yield cur
|
yield cur
|
||||||
conn.commit()
|
conn.commit()
|
||||||
except sqlite3.IntegrityError as err:
|
|
||||||
raise DBDataError(*err.args) from err
|
|
||||||
except sqlite3.DatabaseError as err:
|
except sqlite3.DatabaseError as err:
|
||||||
raise DBError(*err.args) from err
|
raise DBError(*err.args) from err
|
||||||
finally:
|
finally:
|
||||||
@@ -75,20 +73,25 @@ class AutomationDBConnection:
|
|||||||
def insert_automations(self, automations: List[ExtenededAutomation]):
|
def insert_automations(self, automations: List[ExtenededAutomation]):
|
||||||
with self.get_cur() as cur:
|
with self.get_cur() as cur:
|
||||||
for automation in automations:
|
for automation in automations:
|
||||||
cur.execute(
|
try:
|
||||||
f"""
|
cur.execute(
|
||||||
INSERT INTO {automations_tbl} VALUES (
|
f"""
|
||||||
"{automation.id}",
|
INSERT INTO {automations_tbl} VALUES (
|
||||||
"{automation.alias}",
|
"{automation.id}",
|
||||||
"{automation.description}",
|
"{automation.alias}",
|
||||||
"{automation.mode}",
|
"{automation.description}",
|
||||||
"{automation.source_file}",
|
"{automation.mode}",
|
||||||
"{automation.source_file_type}",
|
"{automation.source_file}",
|
||||||
"{automation.configuration_key}",
|
"{automation.source_file_type}",
|
||||||
"{encode_auto(automation)}"
|
"{automation.configuration_key}",
|
||||||
);
|
"{encode_auto(automation)}"
|
||||||
""",
|
);
|
||||||
)
|
""",
|
||||||
|
)
|
||||||
|
except sqlite3.IntegrityError as err:
|
||||||
|
raise RepeatedAutomationId(
|
||||||
|
f"automation id '{automation.id}' in '{automation.source_file}' was already seen before. Please check for duplicate automation ids in your files."
|
||||||
|
) from err
|
||||||
|
|
||||||
def get_automation(self, automation_id: str) -> ExtenededAutomation:
|
def get_automation(self, automation_id: str) -> ExtenededAutomation:
|
||||||
with self.get_cur() as cur:
|
with self.get_cur() as cur:
|
||||||
|
|||||||
@@ -25,5 +25,9 @@ class DBDataError(DBError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RepeatedAutomationId(DBDataError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DBNoAutomationFound(DBError):
|
class DBNoAutomationFound(DBError):
|
||||||
pass
|
pass
|
||||||
@@ -60,7 +60,7 @@ class db_tests(TestWithDB):
|
|||||||
configuration_key="automation",
|
configuration_key="automation",
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
with self.assertRaises(DBDataError):
|
with self.assertRaises(DBDataError) as err:
|
||||||
self.db.insert_automations(originals)
|
self.db.insert_automations(originals)
|
||||||
autos = list(self.db.list_automations(0, 10))
|
autos = list(self.db.list_automations(0, 10))
|
||||||
self.assertEqual(len(autos), 0)
|
self.assertEqual(len(autos), 0)
|
||||||
@@ -25,6 +25,11 @@
|
|||||||
|
|
||||||
.automation-manager--metadatabox .metadatabox--title {
|
.automation-manager--metadatabox .metadatabox--title {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
max-width: calc(80% - 5em);
|
||||||
|
}
|
||||||
|
|
||||||
|
.automation-manager--metadatabox .metadatabox--switch {
|
||||||
|
width: 5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.automation-manager--metadatabox .metadatabox--title .title {
|
.automation-manager--metadatabox .metadatabox--title .title {
|
||||||
@@ -94,4 +99,5 @@
|
|||||||
.automation-manager--metadatabox .metadatabox--title--source .MuiChip-root {
|
.automation-manager--metadatabox .metadatabox--title--source .MuiChip-root {
|
||||||
font-size: 0.85em;
|
font-size: 0.85em;
|
||||||
height: 2em;
|
height: 2em;
|
||||||
|
max-width: 60%;
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ import { ButtonIcon } from "components/Icons/ButtonIcons";
|
|||||||
import { AutomationManagerAuto } from "../types";
|
import { AutomationManagerAuto } from "../types";
|
||||||
import { Tags } from "./Tags";
|
import { Tags } from "./Tags";
|
||||||
import { TagDB } from "../TagDB";
|
import { TagDB } from "../TagDB";
|
||||||
import { Chip } from "@mui/material";
|
import { Chip, Tooltip } from "@mui/material";
|
||||||
|
|
||||||
export type MetadataBoxProps = AutomationManagerAuto & {
|
export type MetadataBoxProps = AutomationManagerAuto & {
|
||||||
onDelete: () => void;
|
onDelete: () => void;
|
||||||
@@ -55,7 +55,14 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
|||||||
title={`ID=${props.id}, EntityID=${props.entityId}`}
|
title={`ID=${props.id}, EntityID=${props.entityId}`}
|
||||||
>
|
>
|
||||||
<span className="metadatabox--title--source">
|
<span className="metadatabox--title--source">
|
||||||
<Chip label={`@${props.source_file} (${props.source_file_type})`} />
|
<Tooltip
|
||||||
|
title={`This automation is located in the file ${props.source_file} and is set up in your configuration.yaml file under the key ${props.configuration_key}.`}
|
||||||
|
>
|
||||||
|
<Chip
|
||||||
|
label={`@${props.source_file} (${props.source_file_type})`}
|
||||||
|
className="text-ellipsis"
|
||||||
|
/>
|
||||||
|
</Tooltip>
|
||||||
</span>
|
</span>
|
||||||
<InputTextView
|
<InputTextView
|
||||||
className="title"
|
className="title"
|
||||||
|
|||||||
@@ -10,6 +10,10 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.automation-manager.error>code {
|
||||||
|
padding: 1.5em;
|
||||||
|
}
|
||||||
|
|
||||||
.automation-manager.loading .automation-manager--sidebar.loading {
|
.automation-manager.loading .automation-manager--sidebar.loading {
|
||||||
width: 25vh;
|
width: 25vh;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
@@ -31,6 +35,8 @@
|
|||||||
max-height: 50vh;
|
max-height: 50vh;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
padding: 0.5em;
|
||||||
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.invalid-automation-modal-list li {
|
.invalid-automation-modal-list li {
|
||||||
|
|||||||
@@ -195,13 +195,20 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!api.state.automations.ok) {
|
if (!api.state.automations.ok) {
|
||||||
|
let error = api.state.automations.error;
|
||||||
|
try {
|
||||||
|
const errorObj = JSON.parse(api.state.automations.error);
|
||||||
|
if ("detail" in errorObj) {
|
||||||
|
error = errorObj["detail"];
|
||||||
|
}
|
||||||
|
} catch (_: any) {}
|
||||||
return (
|
return (
|
||||||
<div className="automation-manager error">
|
<div className="automation-manager error">
|
||||||
<Alert color="error">
|
<Alert color="error">
|
||||||
Failed to load automations from <code>/config</code>
|
Failed to load automations from <code>/config</code>
|
||||||
</Alert>{" "}
|
</Alert>{" "}
|
||||||
<br />
|
<br />
|
||||||
<code>{api.state.automations.error}</code>
|
<code>{error}</code>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user