Skip to content

Commit

Permalink
fix: inconsistent server updates, don't run get after post, fix tags, (
Browse files Browse the repository at this point in the history
…#151)

* fix fetching, tag creation ,etc.

* style

* change to id
  • Loading branch information
cabreraalex authored Sep 6, 2023
1 parent 04a06b3 commit e1a1b19
Show file tree
Hide file tree
Showing 23 changed files with 317 additions and 186 deletions.
47 changes: 35 additions & 12 deletions backend/zeno_backend/database/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,83 +316,106 @@ def system(
db.commit()


def folder(project: str, name: str):
def folder(project: str, name: str) -> int | None:
"""Adding a folder to an existing project.
Args:
project (str): the project the user is currently working with.
name (str): name of the folder to be added.
Returns:
int | None: the id of the newly created folder.
"""
db = Database()
db.connect_execute(
"INSERT INTO folders (name, project_uuid) VALUES (%s,%s);",
id = db.connect_execute_return(
"INSERT INTO folders (name, project_uuid) VALUES (%s,%s) RETURNING id;",
[name, project],
)
if id is not None and len(id) > 0:
return id[0][0]


def slice(project: str, req: Slice):
def slice(project: str, req: Slice) -> int | None:
"""Add a slice to an existing project.
Args:
project (str): the project the user is currently working with.
req (Slice): the slice to be added to the project.
Returns:
int | None: the id of the newly created slice.
"""
db = Database()
db.connect_execute(
ids = db.connect_execute_return(
"INSERT INTO slices (name, folder_id, filter, project_uuid) "
"VALUES (%s,%s,%s,%s);",
"VALUES (%s,%s,%s,%s) RETURNING id;",
[
req.slice_name,
req.folder_id,
json.dumps(req.filter_predicates, cls=PredicatesEncoder),
project,
],
)
if ids is not None:
return ids[0][0]
else:
return None


def chart(project: str, chart: Chart):
def chart(project: str, chart: Chart) -> int | None:
"""Add a chart to an existing project.
Args:
project (str): the project the user is currently working with.
chart (Chart): the chart to be added to the project.
Returns:
int | None: the id of the newly created chart.
"""
db = Database()
db.connect_execute(
id = db.connect_execute_return(
"INSERT INTO charts (name, type, parameters, project_uuid) "
"VALUES (%s,%s,%s,%s);",
"VALUES (%s,%s,%s,%s) RETURNING id;",
[
chart.name,
chart.type,
json.dumps(chart.parameters, cls=ParametersEncoder),
project,
],
)
if id is not None and len(id) > 0:
return id[0][0]


def tag(project: str, tag: Tag):
def tag(project: str, tag: Tag) -> int | None:
"""Add a tag to an existing project.
Args:
project (str): the project the user is currently working with.
tag (Tag): the tag to be added to the project.
Returns:
int | None: the id of the newly created tag.
"""
with Database() as db:
id = db.execute_return(
"INSERT INTO tags (name, folder_id, project_uuid) VALUES (%s,%s,%s) "
"RETURNING id;",
[tag.tag_name, tag.folder_id, project],
)
if id is None:
if id is None or len(id) == 0:
return
for datapoint in tag.data_ids:
db.execute(
sql.SQL("INSERT INTO {} (tag_id, data_id) VALUES (%s,%s);").format(
sql.Identifier(f"{project}_tags_datapoints")
),
[id[0], datapoint],
[id[0][0], datapoint],
)
db.commit()
return id[0][0]


def user(user: User):
Expand Down
2 changes: 1 addition & 1 deletion backend/zeno_backend/database/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ def tags(project: str) -> list[Tag]:
folder_id=tag_result[2],
data_ids=[]
if len(data_results) == 0
else list(map(lambda d: d[0], data_results[0])),
else [d[0] for d in data_results],
)
)
return tags
Expand Down
2 changes: 1 addition & 1 deletion backend/zeno_backend/database/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def tag(tag: Tag, project: str):
tag.id,
],
)
if len(data_ids_result) == 0:
if data_ids_result is None:
return

existing_data = set(map(lambda d: d[0], data_ids_result))
Expand Down
62 changes: 53 additions & 9 deletions backend/zeno_backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def get_filtered_table(req: TableRequest, project_uuid: str, request: Request):
# Prepend the DATA_URL to the data column if it exists
project = select.project_from_uuid(project_uuid)
if project and project.data_url:
filt_df["data_id"] = project.data_url + filt_df["data_id"]
filt_df["data"] = project.data_url + filt_df["data"]

if req.diff_column_1 and req.diff_column_2:
filt_df = generate_diff_cols(
Expand Down Expand Up @@ -455,21 +455,65 @@ def login(name: str):
else:
return fetched_user

@api_app.post("/folder/{project}", tags=["zeno"], dependencies=[Depends(auth)])
@api_app.post(
"/folder/{project}",
response_model=int,
tags=["zeno"],
dependencies=[Depends(auth)],
)
def add_folder(project: str, name: str):
insert.folder(project, name)
id = insert.folder(project, name)
if id is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to insert folder",
)
return id

@api_app.post("/slice/{project}", tags=["zeno"], dependencies=[Depends(auth)])
@api_app.post(
"/slice/{project}",
response_model=int,
tags=["zeno"],
dependencies=[Depends(auth)],
)
def add_slice(project: str, req: Slice):
insert.slice(project, req)
id = insert.slice(project, req)
if id is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to insert slice",
)
return id

@api_app.post("/chart/{project}", tags=["zeno"], dependencies=[Depends(auth)])
@api_app.post(
"/chart/{project}",
response_model=int,
tags=["zeno"],
dependencies=[Depends(auth)],
)
def add_chart(project: str, chart: Chart):
insert.chart(project, chart)
id = insert.chart(project, chart)
if id is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to insert chart",
)
return id

@api_app.post("/tag/{project}", tags=["zeno"], dependencies=[Depends(auth)])
@api_app.post(
"/tag/{project}",
response_model=int,
tags=["zeno"],
dependencies=[Depends(auth)],
)
def add_tag(tag: Tag, project: str):
insert.tag(project, tag)
id = insert.tag(project, tag)
if id is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to insert tag",
)
return id

@api_app.post("/add-organization", tags=["zeno"], dependencies=[Depends(auth)])
def add_organization(user: User, organization: Organization):
Expand Down
22 changes: 14 additions & 8 deletions frontend/src/lib/components/chart/ChartHomeBlock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
mdiViewGrid
} from '@mdi/js';
import { goto } from '$app/navigation';
import { goto, invalidateAll } from '$app/navigation';
import { page } from '$app/stores';
import { charts, project } from '$lib/stores';
import { clickOutside } from '$lib/util/clickOutside';
Expand Down Expand Up @@ -69,10 +69,17 @@
name: 'Copy of ' + chart.name,
type: chart.type,
parameters: chart.parameters
}).then(() => {
ZenoService.getCharts($project ? $project.uuid : '').then((fetchedCharts) =>
charts.set(fetchedCharts)
);
}).then((res) => {
invalidateAll();
charts.update((c) => {
c.push({
id: res,
name: 'Copy of ' + chart.name,
type: chart.type,
parameters: chart.parameters
});
return c;
});
});
}}
>
Expand All @@ -86,9 +93,8 @@
e.stopPropagation();
showOptions = false;
ZenoService.deleteChart(chart).then(() => {
ZenoService.getCharts($project ? $project.uuid : '').then((fetchedCharts) =>
charts.set(fetchedCharts)
);
invalidateAll();
charts.update((c) => c.filter((c) => c.id != chart.id));
});
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
updateTable();
}
comparisonColumn.subscribe((_) => {
comparisonColumn.subscribe(() => {
table = undefined;
compareSort.set([undefined, true]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
onMount(() => {
if ($project === undefined || $project.view === '') {
selected = 'table';
return;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export let entry: Record<string, number | string | boolean>;
export let modelColumn: string;
$: audioURL = entry['data_id'] as string;
$: audioURL = entry['data'] as string;
</script>

<div class="p-4 border border-grey-lighter max-w-[450px] min-w-[400px] rounded">
Expand Down
6 changes: 0 additions & 6 deletions frontend/src/lib/components/metadata/MetadataHeader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
let comparisonColumnOptions: ZenoColumn[] = [];
onMount(() => {
if ($model === undefined && $models.length > 0) {
model.set($models[0]);
}
if ($metric === undefined && $metrics.length > 0) {
metric.set($metrics[0]);
}
comparisonColumnOptions = $columns.filter((c) => c.model === $model);
comparisonColumn.set(comparisonColumnOptions[0]);
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/components/metadata/Slices.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{#each $folders as folder}
<FolderCell {folder} />
{/each}
{#each $slices.filter((s) => s.folderId === null && s.sliceName !== 'All Instances') as s (s.sliceName)}
{#each $slices.filter((s) => s.folderId === null || s.folderId === undefined) as s (s.sliceName)}
<SliceCell compare={$page.url.href.includes('compare')} slice={s} />
{/each}
</div>
37 changes: 23 additions & 14 deletions frontend/src/lib/components/metadata/Tags.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { invalidateAll } from '$app/navigation';
import { page } from '$app/stores';
import { editTag, editedIds, project, selectionIds, selections, tagIds, tags } from '$lib/stores';
import { ZenoService, type Tag } from '$lib/zenoapi';
Expand All @@ -13,20 +14,26 @@
function saveChanges() {
if ($editTag === undefined || $project === undefined) return;
ZenoService.updateTag($project.uuid, { ...$editTag, dataIds: $editedIds }).then(() => {
ZenoService.updateTag($project.uuid, {
...$editTag,
dataIds: Array.from(new Set([...$editTag.dataIds, ...$editedIds]))
}).then(() => {
invalidateAll();
tags.update((t) => {
const index = t.findIndex((tag) => tag.id === $editTag?.id);
if (index !== -1 && $editTag !== undefined) {
t[index] = { ...$editTag, dataIds: $editedIds };
}
return t;
});
let s = new Set<string>();
$selections.tags.forEach((tagId) => {
const tag: Tag | undefined = $tags.find((cur) => cur.id === tagId);
if (tag !== undefined) tag.dataIds.forEach((item) => s.add(item));
tagIds.set([...s]);
});
editTag.set(undefined);
editedIds.set([]);
if ($project !== undefined) {
ZenoService.getTags($project.uuid).then((fetchedTags) => {
tags.set(fetchedTags);
let s = new Set<string>();
$selections.tags.forEach((tagId) => {
const tag: Tag | undefined = $tags.find((cur) => cur.id === tagId);
if (tag !== undefined) tag.dataIds.forEach((item) => s.add(item));
tagIds.set([...s]);
});
});
}
});
}
</script>
Expand Down Expand Up @@ -79,13 +86,15 @@
{#each [...$tags.values()] as t}
{#if $editTag !== undefined && $editTag.id === t.id}
<div style="display: flex; align-items: center">
<div class="mr-2">
<div class="mr-2 w-full">
<TagCell tag={t} />
</div>
<Button
style="background-color: var(--N1); margin-top: 5px; color: white; "
on:click={saveChanges}>Done</Button
on:click={saveChanges}
>
Done
</Button>
</div>
{:else}
<TagCell tag={t} />
Expand Down
Loading

0 comments on commit e1a1b19

Please sign in to comment.