Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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

This file was deleted.

38 changes: 37 additions & 1 deletion src/frontend/src/lib/services/attach-detach/attach.services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setSegment } from '$lib/api/console.api';
import { attachWithMissionControl } from '$lib/services/attach-detach/_attach.mission-control.services';
import { setOrbiter, setSatellite } from '$lib/api/mission-control.api';
import { loadSegments } from '$lib/services/segments.services';
import { i18n } from '$lib/stores/app/i18n.store';
import { toasts } from '$lib/stores/app/toasts.store';
Expand All @@ -21,6 +21,12 @@ interface AttachParams {
segmentId: Principal;
}

interface AttachWithMissionControlParams {
missionControlId: MissionControlId;
canisterId: Principal;
identity: OptionIdentity;
}

export const attachSegment = async ({
identity,
missionControlId,
Expand Down Expand Up @@ -129,3 +135,33 @@ const attachWithConsole = async ({
}
});
};

const attachWithMissionControl = async ({
segment,
segmentId,
...rest
}: {
missionControlId: MissionControlId;
identity: Identity;
segment: 'satellite' | 'orbiter';
segmentId: Principal;
}) => {
const attachOrbiter = async ({ canisterId, ...rest }: AttachWithMissionControlParams) => {
await setOrbiter({ ...rest, orbiterId: canisterId });
};

const attachSatellite = async ({
canisterId,
missionControlId,
identity
}: AttachWithMissionControlParams) => {
await setSatellite({ missionControlId, satelliteId: canisterId, identity });
};

const fn = segment === 'orbiter' ? attachOrbiter : attachSatellite;

await fn({
...rest,
canisterId: segmentId
});
};
93 changes: 61 additions & 32 deletions src/frontend/src/lib/services/attach-detach/out-of-sync.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { mctrlOrbiters } from '$lib/derived/mission-control/mission-control-orbi
import { mctrlSatellites } from '$lib/derived/mission-control/mission-control-satellites.derived';
import { outOfSyncOrbiters, outOfSyncSatellites } from '$lib/derived/out-of-sync.derived';
import { execute } from '$lib/services/_progress.services';
import { attachWithMissionControl as attachWithMissionControlService } from '$lib/services/attach-detach/_attach.mission-control.services';
import {
setMissionControlAsControllerAndAttachOrbiter,
setMissionControlAsControllerAndAttachSatellite
} from '$lib/services/factory/_factory.attach.services';
import { loadSegments } from '$lib/services/segments.services';
import { i18n } from '$lib/stores/app/i18n.store';
import { toasts } from '$lib/stores/app/toasts.store';
import type { OptionIdentity } from '$lib/types/itentity';
import type { Metadata } from '$lib/types/metadata';
import type { MissionControlId } from '$lib/types/mission-control';
import type { Orbiter } from '$lib/types/orbiter';
import { type OutOfSyncProgress, OutOfSyncProgressStep } from '$lib/types/progress-out-of-sync';
import type { Satellite } from '$lib/types/satellite';
import type { Option } from '$lib/types/utils';
import { isNullish, toNullable } from '@dfinity/utils';
import type { Identity } from '@icp-sdk/core/agent';
Expand Down Expand Up @@ -121,14 +125,27 @@ const reconcileSatellites = async ({
) === undefined
);

await attachWithMissionControl({
missionControlId,
identity,
type SegmentWithoutId = Omit<Satellite, 'satellite_id'>;

const attachFn: AttachFn<SegmentWithoutId> = async ({ segment: { segmentId, ...rest } }) => {
await setMissionControlAsControllerAndAttachSatellite({
missionControlId,
identity,
satellite: {
satellite_id: segmentId,
...rest
}
});
};

await attachWithMissionControl<SegmentWithoutId>({
onTextProgress,
segmentType: 'satellite',
segments: attachMctrlSatellites.map(({ satellite_id: segmentId }) => ({
segments: attachMctrlSatellites.map(({ satellite_id: segmentId, ...rest }) => ({
...rest,
segmentId
}))
})),
attachFn
});

await attachWithConsole({
Expand Down Expand Up @@ -162,15 +179,27 @@ const reconcileOrbiters = async ({
undefined
);

await attachWithMissionControl({
missionControlId,
identity,
type SegmentWithoutId = Omit<Orbiter, 'orbiter_id'>;

const attachFn: AttachFn<SegmentWithoutId> = async ({ segment: { segmentId, ...rest } }) => {
await setMissionControlAsControllerAndAttachOrbiter({
missionControlId,
identity,
orbiter: {
orbiter_id: segmentId,
...rest
}
});
};

await attachWithMissionControl<SegmentWithoutId>({
onTextProgress,
segmentType: 'satellite',
segments: attachMctrlOrbiters.map(({ orbiter_id: segmentId }) => ({
segmentId,
segment: 'orbiter'
}))
segments: attachMctrlOrbiters.map(({ orbiter_id: segmentId, ...rest }) => ({
...rest,
segmentId
})),
attachFn
});

await attachWithConsole({
Expand All @@ -184,25 +213,30 @@ const reconcileOrbiters = async ({
});
};

interface AttachSegment {
type SegmentWithoutId = Omit<Satellite, 'satellite_id'> | Omit<Orbiter, 'orbiter_id'>;

type AttachSegment<T extends SegmentWithoutId> = {
segmentId: Principal;
metadata: Metadata;
}
} & T;

interface AttachWithMissionControlProgressStats {
index: number;
total: number;
}

const attachWithMissionControl = async ({
identity,
missionControlId,
type AttachFn<T extends SegmentWithoutId> = (params: {
segment: AttachSegment<T>;
}) => Promise<void>;

const attachWithMissionControl = async <T extends SegmentWithoutId>({
onTextProgress,
segments,
segmentType
}: ReconcileParams & {
segments: Omit<AttachSegment, 'metadata'>[];
segmentType,
attachFn
}: Pick<ReconcileParams, 'onTextProgress'> & {
segments: AttachSegment<T>[];
segmentType: 'satellite' | 'orbiter';
attachFn: AttachFn<T>;
}) => {
// We do the check for the lengths here. Not really graceful but,
// avoid to duplicate the assertions for both Satellites and Orbiters
Expand All @@ -229,25 +263,20 @@ const attachWithMissionControl = async ({
onTextProgress(text.replace('{0}', `${progress.index} / ${progress.total}`));
};

for (const { segmentId } of segments) {
for (const segment of segments) {
incrementProgress();

await attachWithMissionControlService({
segment: segmentType,
segmentId,
missionControlId,
identity
});
await attachFn({ segment });
}
};

const attachWithConsole = async ({
const attachWithConsole = async <T extends SegmentWithoutId>({
identity,
onTextProgress,
segments,
segmentType
}: {
segments: Omit<AttachSegment, 'segment'>[];
segments: Pick<AttachSegment<T>, 'segmentId' | 'metadata'>[];
segmentType: 'satellite' | 'orbiter';
} & Pick<ReconcileParams, 'identity' | 'onTextProgress'>) => {
// We do the check for the lengths here. Not really graceful but,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const attachSatellites = async ({
for (const satellite of satellites) {
incrementProgress();

const result = await attachSatellite({
const result = await setMissionControlAsControllerAndAttachSatellite({
satellite,
missionControlId,
identity
Expand Down Expand Up @@ -201,7 +201,7 @@ const attachOrbiters = async ({
for (const orbiter of orbiters) {
incrementProgress();

const result = await attachOrbiter({
const result = await setMissionControlAsControllerAndAttachOrbiter({
orbiter,
missionControlId,
identity
Expand All @@ -217,7 +217,7 @@ const attachOrbiters = async ({
return canisterIds;
};

const attachSatellite = async ({
export const setMissionControlAsControllerAndAttachSatellite = async ({
satellite,
missionControlId,
identity
Expand Down Expand Up @@ -255,7 +255,7 @@ const attachSatellite = async ({
});
};

const attachOrbiter = async ({
export const setMissionControlAsControllerAndAttachOrbiter = async ({
orbiter,
missionControlId,
identity
Expand Down
Loading