Skip to content

Commit cd276ba

Browse files
authored
Merge pull request #932 from PAIR-code/fixes
Correct cohort deletion re-routing + subsequent cohort creation
2 parents cf61fa9 + 104dbe7 commit cd276ba

File tree

3 files changed

+76
-65
lines changed

3 files changed

+76
-65
lines changed

frontend/src/components/experiment_dashboard/cohort_list.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export class Component extends MobxLitElement {
134134
}
135135

136136
private renderHeader() {
137-
const getCohortName = (length: number) => {
138-
return this.experimentManager.getNextCohortName(length);
137+
const getCohortName = (offset: number) => {
138+
return this.experimentManager.getNextCohortName(offset);
139139
};
140140

141141
return html`
@@ -155,9 +155,7 @@ export class Component extends MobxLitElement {
155155
?loading=${this.experimentManager.isWritingCohort}
156156
@click=${() => {
157157
this.analyticsService.trackButtonClick(ButtonClick.COHORT_ADD);
158-
const name = getCohortName(
159-
this.experimentManager.cohortList.length,
160-
);
158+
const name = getCohortName(0);
161159
this.experimentManager.createCohort({}, name);
162160
}}
163161
>
@@ -170,12 +168,11 @@ export class Component extends MobxLitElement {
170168
variant="tonal"
171169
?loading=${this.experimentManager.isWritingCohort}
172170
@click=${() => {
173-
const numCohorts = this.experimentManager.cohortList.length;
174171
[...Array(5).keys()].forEach((key) => {
175172
this.analyticsService.trackButtonClick(
176173
ButtonClick.COHORT_ADD,
177174
);
178-
const name = getCohortName(numCohorts + key);
175+
const name = getCohortName(key);
179176
this.experimentManager.createCohort({}, name);
180177
});
181178
}}
@@ -189,12 +186,11 @@ export class Component extends MobxLitElement {
189186
variant="tonal"
190187
?loading=${this.experimentManager.isWritingCohort}
191188
@click=${() => {
192-
const numCohorts = this.experimentManager.cohortList.length;
193189
[...Array(10).keys()].forEach((key) => {
194190
this.analyticsService.trackButtonClick(
195191
ButtonClick.COHORT_ADD,
196192
);
197-
const name = getCohortName(numCohorts + key);
193+
const name = getCohortName(key);
198194
this.experimentManager.createCohort({}, name);
199195
});
200196
}}

frontend/src/components/experiment_dashboard/cohort_settings_dialog.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ export class CohortSettingsDialog extends MobxLitElement {
5151
<pr-button
5252
color="error"
5353
variant="tonal"
54-
@click=${() => {
54+
@click=${async () => {
5555
this.analyticsService.trackButtonClick(ButtonClick.COHORT_DELETE);
56-
this.experimentManager.deleteCohort(
56+
await this.experimentManager.deleteCohort(
5757
this.experimentManager.cohortEditing!.id,
5858
);
59+
this.experimentManager.setCurrentCohortId(undefined);
60+
this.experimentManager.setShowCohortList(true, true);
5961
}}
6062
>
6163
Delete cohort

frontend/src/services/experiment.manager.ts

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,27 @@ export class ExperimentManager extends Service {
369369
}
370370

371371
// Return name for next cohort based on number of cohorts
372-
getNextCohortName(numCohorts = this.cohortList.length) {
372+
getNextCohortName(offset = 0) {
373373
const hasTransfer = this.sp.experimentService.stages.find(
374374
(stage) => stage.kind === StageKind.TRANSFER,
375375
);
376-
if (numCohorts > 0 || !hasTransfer) {
377-
return `Cohort ${String(numCohorts).padStart(2, '0')}`;
376+
377+
if (this.cohortList.length === 0 && hasTransfer) {
378+
return 'Lobby';
378379
}
379-
return 'Lobby';
380+
381+
let maxNum = -1;
382+
this.cohortList.forEach((cohort) => {
383+
const match = cohort.metadata.name.match(/^Cohort (\d+)$/);
384+
if (match) {
385+
const num = parseInt(match[1], 10);
386+
if (!isNaN(num) && num > maxNum) {
387+
maxNum = num;
388+
}
389+
}
390+
});
391+
392+
return `Cohort ${String(maxNum + 1 + offset).padStart(2, '0')}`;
380393
}
381394

382395
isFullCohort(cohort: CohortConfig) {
@@ -464,14 +477,13 @@ export class ExperimentManager extends Service {
464477
'alerts',
465478
),
466479
(snapshot) => {
467-
let changedDocs = snapshot.docChanges().map((change) => change.doc);
468-
if (changedDocs.length === 0) {
469-
changedDocs = snapshot.docs;
470-
}
471-
472-
changedDocs.forEach((doc) => {
473-
const data = doc.data() as AlertMessage;
474-
this.alertMap[data.id] = data;
480+
snapshot.docChanges().forEach((change) => {
481+
if (change.type === 'removed') {
482+
delete this.alertMap[change.doc.id];
483+
} else {
484+
const data = change.doc.data() as AlertMessage;
485+
this.alertMap[data.id] = data;
486+
}
475487
});
476488
},
477489
),
@@ -487,21 +499,25 @@ export class ExperimentManager extends Service {
487499
'cohorts',
488500
),
489501
(snapshot) => {
490-
let changedDocs = snapshot.docChanges().map((change) => change.doc);
491-
if (changedDocs.length === 0) {
492-
changedDocs = snapshot.docs;
493-
}
494-
495-
changedDocs.forEach((doc) => {
496-
const data = doc.data() as CohortConfig;
497-
this.cohortMap[doc.id] = data;
498-
this.currentCohortId = doc.id;
502+
snapshot.docChanges().forEach((change) => {
503+
if (change.type === 'removed') {
504+
delete this.cohortMap[change.doc.id];
505+
if (this.currentCohortId === change.doc.id) {
506+
this.currentCohortId = undefined;
507+
}
508+
} else {
509+
const data = change.doc.data() as CohortConfig;
510+
this.cohortMap[change.doc.id] = data;
511+
if (!this.currentCohortId) {
512+
this.currentCohortId = change.doc.id;
513+
}
514+
}
499515
});
500516

501517
// If multiple cohorts, show cohort list
502-
if (changedDocs.length > 1) {
518+
if (Object.keys(this.cohortMap).length > 1) {
503519
this.setShowCohortList(true, true);
504-
} else if (changedDocs.length === 0) {
520+
} else if (Object.keys(this.cohortMap).length === 0) {
505521
this.setShowCohortEditor(true, true);
506522
}
507523

@@ -523,17 +539,16 @@ export class ExperimentManager extends Service {
523539
where('currentStatus', '!=', ParticipantStatus.DELETED),
524540
),
525541
(snapshot) => {
526-
let changedDocs = snapshot.docChanges().map((change) => change.doc);
527-
if (changedDocs.length === 0) {
528-
changedDocs = snapshot.docs;
529-
}
530-
531-
changedDocs.forEach((doc) => {
532-
const data = {
533-
agentConfig: null,
534-
...doc.data(),
535-
} as ParticipantProfileExtended;
536-
this.participantMap[doc.id] = data;
542+
snapshot.docChanges().forEach((change) => {
543+
if (change.type === 'removed') {
544+
delete this.participantMap[change.doc.id];
545+
} else {
546+
const data = {
547+
agentConfig: null,
548+
...change.doc.data(),
549+
} as ParticipantProfileExtended;
550+
this.participantMap[change.doc.id] = data;
551+
}
537552
});
538553

539554
this.isParticipantsLoading = false;
@@ -554,17 +569,16 @@ export class ExperimentManager extends Service {
554569
where('currentStatus', '!=', ParticipantStatus.DELETED),
555570
),
556571
(snapshot) => {
557-
let changedDocs = snapshot.docChanges().map((change) => change.doc);
558-
if (changedDocs.length === 0) {
559-
changedDocs = snapshot.docs;
560-
}
561-
562-
changedDocs.forEach((doc) => {
563-
const data = {
564-
agentConfig: null,
565-
...doc.data(),
566-
} as MediatorProfileExtended;
567-
this.mediatorMap[doc.id] = data;
572+
snapshot.docChanges().forEach((change) => {
573+
if (change.type === 'removed') {
574+
delete this.mediatorMap[change.doc.id];
575+
} else {
576+
const data = {
577+
agentConfig: null,
578+
...change.doc.data(),
579+
} as MediatorProfileExtended;
580+
this.mediatorMap[change.doc.id] = data;
581+
}
568582
});
569583

570584
this.isMediatorsLoading = false;
@@ -586,14 +600,13 @@ export class ExperimentManager extends Service {
586600
),
587601
),
588602
(snapshot) => {
589-
let changedDocs = snapshot.docChanges().map((change) => change.doc);
590-
if (changedDocs.length === 0) {
591-
changedDocs = snapshot.docs;
592-
}
593-
594-
changedDocs.forEach((doc) => {
595-
const data = doc.data() as AgentPersonaConfig;
596-
this.agentPersonaMap[doc.id] = data;
603+
snapshot.docChanges().forEach((change) => {
604+
if (change.type === 'removed') {
605+
delete this.agentPersonaMap[change.doc.id];
606+
} else {
607+
const data = change.doc.data() as AgentPersonaConfig;
608+
this.agentPersonaMap[change.doc.id] = data;
609+
}
597610
});
598611

599612
this.isAgentsLoading = false;
@@ -710,7 +723,7 @@ export class ExperimentManager extends Service {
710723
cohortId,
711724
},
712725
);
713-
this.loadExperimentData(this.experimentId);
726+
714727
this.cohortEditing = undefined;
715728
return response;
716729
}

0 commit comments

Comments
 (0)