Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Handle sidebar groups config update #5191

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ import {
useRecoilStateLoadable,
useRecoilValueLoadable,
} from "recoil";
import { collapseFields, getCurrentEnvironment } from "../utils";
import * as atoms from "./atoms";
import { getBrowserStorageEffectForKey } from "./customEffects";
import { collapseFields, getCurrentEnvironment } from "../../utils";
import * as atoms from "../atoms";
import { getBrowserStorageEffectForKey } from "../customEffects";
import {
active3dSlices,
active3dSlicesToSampleMap,
activeModalSidebarSample,
pinned3DSampleSlice,
} from "./groups";
import { isLargeVideo } from "./options";
import { cumulativeValues, values } from "./pathData";
} from "../groups";
import { isLargeVideo } from "../options";
import { cumulativeValues, values } from "../pathData";
import {
buildSchema,
field,
Expand All @@ -53,23 +53,24 @@ import {
filterPaths,
isOfDocumentFieldList,
pathIsShown,
} from "./schema";
import { isFieldVisibilityActive } from "./schemaSettings.atoms";
} from "../schema";
import { isFieldVisibilityActive } from "../schemaSettings.atoms";
import {
datasetName,
disableFrameFiltering,
isVideoDataset,
stateSubscription,
} from "./selectors";
import { State } from "./types";
} from "../selectors";
import { State } from "../types";
import {
fieldsMatcher,
groupFilter,
labelsMatcher,
primitivesMatcher,
unsupportedMatcher,
} from "./utils";
import * as viewAtoms from "./view";
} from "../utils";
import * as viewAtoms from "../view";
import { mergeGroups } from "./sidebar-utils";

export enum EntryKind {
EMPTY = "EMPTY",
Expand Down Expand Up @@ -210,6 +211,10 @@ export const resolveGroups = (
? DEFAULT_VIDEO_GROUPS
: DEFAULT_IMAGE_GROUPS;

if (currentGroups.length && configGroups.length) {
groups = mergeGroups(groups, configGroups);
}

const expanded = configGroups.reduce((map, { name, expanded }) => {
map[name] = expanded;
return map;
Expand Down
37 changes: 37 additions & 0 deletions app/packages/state/src/recoil/sidebar/sidebar-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it, vi } from "vitest";

vi.mock("recoil");
vi.mock("recoil-relay");

import { merge, mergeGroups } from "./sidebar-utils";

describe("test sidebar groups resolution", () => {
it("test list merge", () => {
expect(merge([], ["one", "two"])).toStrictEqual(["one", "two"]);
});
benjaminpkane marked this conversation as resolved.
Show resolved Hide resolved

it("merges current and config groups", () => {
expect(
mergeGroups(
[
{ name: "one", paths: ["one.one", "one.three"] },
{ name: "three", paths: [] },
],

[
{ name: "zero", paths: [] },
{
name: "one",
paths: ["one.zero", "one.one", "one.two"],
},
{ name: "two", paths: [] },
]
)
).toStrictEqual([
{ name: "zero", paths: [] },
{ name: "one", paths: ["one.zero", "one.one", "one.two", "one.three"] },
{ name: "two", paths: [] },
{ name: "three", paths: [] },
]);
});
});
benjaminpkane marked this conversation as resolved.
Show resolved Hide resolved
89 changes: 89 additions & 0 deletions app/packages/state/src/recoil/sidebar/sidebar-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { State } from "../types";

const hasNeighbor = (sink: string[], source: string[], key: string) => {
const index = source.indexOf(key);
const before = source[index - 1];
const after = source[index + 1];

return sink.includes(before) || sink.includes(after);
};

const insertFromNeighbor = (sink: string[], source: string[], key: string) => {
if (sink.includes(key)) {
return;
}

const sourceIndex = source.indexOf(key);
const before = source[sourceIndex - 1];
const beforeIndex = sink.indexOf(before);

if (beforeIndex >= 0) {
sink.splice(beforeIndex + 1, 0, key);
return;
}

const after = source[sourceIndex + 1];
const afterIndex = sink.indexOf(after);

if (afterIndex >= 0) {
sink.splice(afterIndex, 0, key);
return;
}

sink.push(key);
return;
};
benjaminpkane marked this conversation as resolved.
Show resolved Hide resolved

export const merge = (sink: string[], source: string[]) => {
const missing = new Set(source.filter((key) => !sink.includes(key)));

while (missing.size) {
const force = [...missing].every(
(name) => !hasNeighbor(sink, source, name)
);
for (const name of missing) {
if (!force && !hasNeighbor(sink, source, name)) {
continue;
}

insertFromNeighbor(sink, source, name);

missing.delete(name);
}
}

return sink;
};
benjaminpkane marked this conversation as resolved.
Show resolved Hide resolved

export const mergeGroups = (
sink: State.SidebarGroup[],
source: State.SidebarGroup[]
) => {
// make copies, assume readonly
const mapping = Object.fromEntries(sink.map((g) => [g.name, { ...g }]));
const configMapping = Object.fromEntries(
source.map((g) => [g.name, { ...g }])
);

const sinkKeys = sink.map(({ name }) => name);
const sourceKeys = source.map(({ name }) => name);

merge(sinkKeys, sourceKeys);

for (const key of sinkKeys) {
mapping[key] = mapping[key] ?? configMapping[key];
}
const resolved = sinkKeys.map((g) => mapping[g] ?? configMapping[g]);
for (const { name } of resolved) {
const i = sourceKeys.indexOf(name);
if (i < 0) {
continue;
}

// make copies, assume readonly
mapping[name].paths = [...(mapping[name].paths ?? [])];
merge(mapping[name].paths, [...source[i].paths]);
}

return resolved;
};
5 changes: 4 additions & 1 deletion fiftyone/core/singletons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""

from collections import defaultdict
import weakref

Expand Down Expand Up @@ -37,7 +38,9 @@ def __call__(cls, name=None, _create=True, *args, **kwargs):
name = instance.name # `__init__` may have changed `name`
else:
try:
instance._update_last_loaded_at()
instance._update_last_loaded_at(
force=kwargs.get("_force_load", False)
)
except ValueError:
instance._deleted = True
return cls.__call__(
Expand Down
4 changes: 3 additions & 1 deletion fiftyone/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def serialize(self, reflective=True):

if self.dataset is not None:
d["dataset"] = self.dataset.name
collection = self.dataset
collection = fo.Dataset(
self.dataset.name, _create=False, _force_load=True
)
if self.view is not None:
collection = self.view

Expand Down
6 changes: 1 addition & 5 deletions fiftyone/server/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,7 @@ def run():
if not fod.dataset_exists(dataset_name):
return None

dataset = fod.load_dataset(dataset_name)

if update_last_loaded_at:
dataset._update_last_loaded_at(force=True)

dataset = fo.Dataset(dataset_name, _create=False, _force_load=True)
dataset.reload()
view_name = None
try:
Expand Down
Loading