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

Added namespace ids and label #2670

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -16,6 +16,7 @@ import {
isRoleValid,
getDefaultRole,
} from "shared/permissions";
import uniqid from "uniqid";
import {
UpdateSdkWebhookProps,
deleteLegacySdkWebhookById,
Expand Down Expand Up @@ -843,13 +844,13 @@ export async function getNamespaces(req: AuthRequest, res: Response) {

export async function postNamespaces(
req: AuthRequest<{
name: string;
label: string;
description: string;
status: "active" | "inactive";
}>,
res: Response
) {
const { name, description, status } = req.body;
const { label, description, status } = req.body;
const context = getContextFromReq(req);

if (!context.permissions.canCreateNamespace()) {
Expand All @@ -861,14 +862,18 @@ export async function postNamespaces(
const namespaces = org.settings?.namespaces || [];

// Namespace with the same name already exists
if (namespaces.filter((n) => n.name === name).length > 0) {
throw new Error("Namespace names must be unique.");
if (namespaces.filter((n) => n.label === label).length > 0) {
throw new Error("A namespace with this name already exists.");
}

// Create a unique id for this new namespace - We might want to clean this
// up later, but for now, 'name' is the unique identifier, and 'label' is
// the display name.
const name = uniqid("ns-");
await updateOrganization(org.id, {
settings: {
...org.settings,
namespaces: [...namespaces, { name, description, status }],
namespaces: [...namespaces, { name, label, description, status }],
},
});

Expand Down Expand Up @@ -896,16 +901,17 @@ export async function postNamespaces(
export async function putNamespaces(
req: AuthRequest<
{
name: string;
label: string;
description: string;
status: "active" | "inactive";
},
{ name: string }
>,
res: Response
) {
const { name, description, status } = req.body;
const originalName = req.params.name;
const { label, description, status } = req.body;
const { name } = req.params;

const context = getContextFromReq(req);

if (!context.permissions.canUpdateNamespace()) {
Expand All @@ -916,13 +922,15 @@ export async function putNamespaces(

const namespaces = org.settings?.namespaces || [];

// Namespace with the same name already exists
if (namespaces.filter((n) => n.name === originalName).length === 0) {
// Make sure this namespace exists
if (namespaces.filter((n) => n.name === name).length === 0) {
throw new Error("Namespace not found.");
}

const updatedNamespaces = namespaces.map((n) => {
if (n.name === originalName) {
return { name, description, status };
if (n.name === name) {
// cannot update the 'name' (id) of a namespace
return { label, name: n.name, description, status };
}
return n;
});
Expand Down
8 changes: 8 additions & 0 deletions packages/back-end/src/util/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ export function upgradeOrganizationDoc(
}
});

// Make sure namespaces have labels- if it's missing, use the name
if (org?.settings?.namespaces?.length) {
org.settings.namespaces = org.settings.namespaces.map((ns) => ({
...ns,
label: ns.label || ns.name,
}));
}

return org;
}

Expand Down
1 change: 1 addition & 0 deletions packages/back-end/types/organization.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export interface MetricDefaults {

export interface Namespaces {
name: string;
label: string;
description: string;
status: "active" | "inactive";
}
Expand Down
4 changes: 2 additions & 2 deletions packages/front-end/components/Experiment/NamespaceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function NamespaceModal({
const existingNamespace = existing?.namespace;
const form = useForm<Partial<Namespaces>>({
defaultValues: {
name: existingNamespace?.name || "",
label: existingNamespace?.label || existingNamespace?.name || "",
description: existingNamespace?.description || "",
status: existingNamespace?.status || "active",
},
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function NamespaceModal({
maxLength={60}
disabled={!!existing?.experiments}
required
{...form.register("name")}
{...form.register("label")}
/>
<Field label="Description" textarea {...form.register("description")} />
</Modal>
Expand Down
19 changes: 16 additions & 3 deletions packages/front-end/components/Features/ExperimentRefSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, { ReactElement } from "react";
import { includeExperimentInPayload } from "shared/util";
import { getVariationColor } from "@/services/features";
import ValidateValue from "@/components/Features/ValidateValue";
import useOrgSettings from "@/hooks/useOrgSettings";
import ValueDisplay from "./ValueDisplay";
import ExperimentSplitVisual from "./ExperimentSplitVisual";
import ConditionDisplay from "./ConditionDisplay";
Expand Down Expand Up @@ -66,6 +67,8 @@ export default function ExperimentRefSummary({
const { variations } = rule;
const type = feature.valueType;

const { namespaces } = useOrgSettings();

if (!experiment) {
return (
<ExperimentSkipped
Expand Down Expand Up @@ -183,9 +186,19 @@ export default function ExperimentRefSummary({
<>
{" "}
<span>in the namespace </span>
<span className="mr-1 border px-2 py-1 bg-light rounded">
{phase.namespace.name}
</span>
<Link href={`/namespaces`}>
<span className="mr-1 border px-2 py-1 bg-light rounded">
{namespaces?.find((n) => n.name === phase.namespace.name)
?.label || (
<span
className="italic text-danger"
title="this namespace is not found"
>
phase.namespace.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap this in curly braces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

</span>
)}
</span>
</Link>
</>
)}
</div>
Expand Down
5 changes: 4 additions & 1 deletion packages/front-end/components/Features/ExperimentSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import ValidateValue from "@/components/Features/ValidateValue";
import NewExperimentForm from "@/components/Experiment/NewExperimentForm";
import Modal from "@/components/Modal";
import useOrgSettings from "@/hooks/useOrgSettings";
import ValueDisplay from "./ValueDisplay";
import ExperimentSplitVisual from "./ExperimentSplitVisual";

Expand All @@ -29,6 +30,7 @@ export default function ExperimentSummary({
}) {
const { namespace, coverage, values, hashAttribute, trackingKey } = rule;
const type = feature.valueType;
const { namespaces: allNamespaces } = useOrgSettings();

const { datasources, metrics } = useDefinitions();
const [newExpModal, setNewExpModal] = useState(false);
Expand Down Expand Up @@ -105,7 +107,8 @@ export default function ExperimentSummary({
{" "}
<span>in the namespace </span>
<span className="mr-1 border px-2 py-1 bg-light rounded">
{namespace.name}
{allNamespaces?.find((n) => n.name === namespace.name)?.label ||
namespace.name}
</span>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function NamespaceSelector({
.filter((n) => {
return n?.status !== "inactive";
})
.map((n) => ({ value: n.name, label: n.name }))}
.map((n) => ({ value: n.name, label: n.label }))}
/>
{namespace &&
namespaces.filter((n) => n.name === namespace).length > 0 && (
Expand Down
7 changes: 5 additions & 2 deletions packages/front-end/components/Features/Rule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ export const Rule = forwardRef<HTMLDivElement, RuleProps>(
Experiment:{" "}
<strong className="mr-3">{linkedExperiment.name}</strong>{" "}
<Link href={`/experiment/${linkedExperiment.id}`}>
View Experiment
<FaExternalLinkAlt />
View Experiment{" "}
<FaExternalLinkAlt
className="small ml-1 position-relative"
style={{ top: "-1px" }}
/>
</Link>
</div>
) : (
Expand Down
7 changes: 5 additions & 2 deletions packages/front-end/components/Settings/NamespaceTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function NamespaceTableRow({
style={{ cursor: "pointer" }}
>
<td onClick={expandRow}>
{namespace.name}
{namespace.label}
{status === "inactive" && (
<div
className={`badge badge-secondary ml-2`}
Expand All @@ -61,6 +61,9 @@ export default function NamespaceTableRow({
</div>
)}
</td>
<td onClick={expandRow} className="text-muted small">
{namespace.name}
</td>
<td onClick={expandRow}>{namespace.description}</td>
<td onClick={expandRow}>{experiments.length}</td>
<td onClick={expandRow}>
Expand Down Expand Up @@ -117,7 +120,7 @@ export default function NamespaceTableRow({
}}
>
<td
colSpan={5}
colSpan={6}
className="px-4 bg-light"
style={{
boxShadow: "rgba(0, 0, 0, 0.06) 0px 2px 4px 0px inset",
Expand Down
5 changes: 5 additions & 0 deletions packages/front-end/pages/namespaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useUser } from "@/services/UserContext";
import NamespaceTableRow from "@/components/Settings/NamespaceTableRow";
import { useAuth } from "@/services/auth";
import usePermissionsUtil from "@/hooks/usePermissionsUtils";
import Tooltip from "@/components/Tooltip/Tooltip";

export type NamespaceApiResponse = {
namespaces: NamespaceUsage;
Expand Down Expand Up @@ -85,6 +86,10 @@ const NamespacesPage: FC = () => {
<thead>
<tr>
<th>Namespace</th>
<th>
Namespace ID{" "}
<Tooltip body="This id is used as the namespace hash key and cannot be changed" />
</th>
<th>Description</th>
<th>Active experiments</th>
<th>Percent available</th>
Expand Down
Loading