Skip to content

Commit caad09f

Browse files
authored
Merge pull request #1215 from david-roper/add-group-column
Add group column, remove group from subject id
2 parents 50e4dc7 + d7cf820 commit caad09f

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

apps/api/src/instrument-records/instrument-records.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ type ExpandDataType =
3838
success: false;
3939
};
4040

41+
function afterFirstDollar(str: string) {
42+
if (!str) return str;
43+
const match = /\$(.*)/.exec(str);
44+
if (!match) return str;
45+
if (!match[1]) return str;
46+
return match[1];
47+
}
48+
4149
@Injectable()
4250
export class InstrumentRecordsService {
4351
constructor(
@@ -180,7 +188,7 @@ export class InstrumentRecordsService {
180188
sessionId: record.session.id,
181189
sessionType: record.session.type,
182190
subjectAge: record.subject.dateOfBirth ? yearsPassed(record.subject.dateOfBirth) : null,
183-
subjectId: record.subject.id,
191+
subjectId: afterFirstDollar(record.subject.id),
184192
subjectSex: record.subject.sex,
185193
timestamp: record.date.toISOString(),
186194
value: measureValue
@@ -203,7 +211,7 @@ export class InstrumentRecordsService {
203211
sessionId: record.session.id,
204212
sessionType: record.session.type,
205213
subjectAge: record.subject.dateOfBirth ? yearsPassed(record.subject.dateOfBirth) : null,
206-
subjectId: record.subject.id,
214+
subjectId: afterFirstDollar(record.subject.id),
207215
subjectSex: record.subject.sex,
208216
timestamp: record.date.toISOString(),
209217
value: arrayEntry.measureValue

apps/web/src/hooks/__tests__/useInstrumentVisualization.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ describe('useInstrumentVisualization', () => {
7272
const [filename, getContentFn] = mockDownloadFn.mock.calls[0] ?? [];
7373
expect(filename).toContain('.csv');
7474
const csvContents = getContentFn();
75-
expect(csvContents).toMatch(`subjectId,Date,someValue\r\ntestId,${toBasicISOString(FIXED_TEST_DATE)},abc`);
75+
expect(csvContents).toMatch(
76+
`GroupID,subjectId,Date,someValue\r\ntestGroupId,testId,${toBasicISOString(FIXED_TEST_DATE)},abc`
77+
);
7678
});
7779
});
7880
describe('TSV', () => {
@@ -85,7 +87,9 @@ describe('useInstrumentVisualization', () => {
8587
const [filename, getContentFn] = mockDownloadFn.mock.calls[0] ?? [];
8688
expect(filename).toContain('.tsv');
8789
const tsvContents = getContentFn();
88-
expect(tsvContents).toMatch(`subjectId\tDate\tsomeValue\r\ntestId\t${toBasicISOString(FIXED_TEST_DATE)}\tabc`);
90+
expect(tsvContents).toMatch(
91+
`GroupID\tsubjectId\tDate\tsomeValue\r\ntestGroupId\ttestId\t${toBasicISOString(FIXED_TEST_DATE)}\tabc`
92+
);
8993
});
9094
});
9195
describe('CSV Long', () => {
@@ -100,7 +104,7 @@ describe('useInstrumentVisualization', () => {
100104
expect(filename).toContain('.csv');
101105
const csvLongContents = getContentFn();
102106
expect(csvLongContents).toMatch(
103-
`Date,SubjectID,Value,Variable\r\n${toBasicISOString(FIXED_TEST_DATE)},testId,abc,someValue`
107+
`GroupID,Date,SubjectID,Value,Variable\r\ntestGroupId,${toBasicISOString(FIXED_TEST_DATE)},testId,abc,someValue`
104108
);
105109
});
106110
});
@@ -116,7 +120,7 @@ describe('useInstrumentVisualization', () => {
116120
expect(filename).toMatch('.tsv');
117121
const tsvLongContents = getContentFn();
118122
expect(tsvLongContents).toMatch(
119-
`Date\tSubjectID\tValue\tVariable\r\n${toBasicISOString(FIXED_TEST_DATE)}\ttestId\tabc\tsomeValue`
123+
`GroupID\tDate\tSubjectID\tValue\tVariable\r\ntestGroupId\t${toBasicISOString(FIXED_TEST_DATE)}\ttestId\tabc\tsomeValue`
120124
);
121125
});
122126
});

apps/web/src/hooks/useInstrumentVisualization.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
3636
const [minDate, setMinDate] = useState<Date | null>(null);
3737
const [instrumentId, setInstrumentId] = useState<null | string>(null);
3838

39+
function afterFirstDollar(str: string) {
40+
if (!str) return str;
41+
const match = /\$(.*)/.exec(str);
42+
if (!match) return str;
43+
if (!match[1]) return str;
44+
return match[1];
45+
}
46+
3947
const instrument = useInstrument(instrumentId) as AnyUnilingualScalarInstrument;
4048

4149
const instrumentInfoQuery = useInstrumentInfoQuery({
@@ -70,7 +78,10 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
7078
const makeWideRows = () => {
7179
const columnNames = Object.keys(exportRecords[0]!);
7280
return exportRecords.map((item) => {
73-
const obj: { [key: string]: any } = { subjectId: params.subjectId };
81+
const obj: { [key: string]: any } = {
82+
GroupID: currentGroup ? currentGroup.id : 'root',
83+
subjectId: afterFirstDollar(params.subjectId)
84+
};
7485
for (const key of columnNames) {
7586
const val = item[key];
7687
if (key === '__date__') {
@@ -99,8 +110,10 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
99110
objVal.forEach((arrayItem) => {
100111
Object.entries(arrayItem as object).forEach(([arrKey, arrItem]) => {
101112
longRecord.push({
113+
GroupID: currentGroup ? currentGroup.id : 'root',
114+
// eslint-disable-next-line perfectionist/sort-objects
102115
Date: toBasicISOString(date),
103-
SubjectID: params.subjectId,
116+
SubjectID: afterFirstDollar(params.subjectId),
104117
Variable: `${objKey}-${arrKey}`,
105118
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, perfectionist/sort-objects
106119
Value: arrItem
@@ -109,8 +122,10 @@ export function useInstrumentVisualization({ params }: UseInstrumentVisualizatio
109122
});
110123
} else {
111124
longRecord.push({
125+
GroupID: currentGroup ? currentGroup.id : 'root',
126+
// eslint-disable-next-line perfectionist/sort-objects
112127
Date: toBasicISOString(date),
113-
SubjectID: params.subjectId,
128+
SubjectID: afterFirstDollar(params.subjectId),
114129
Value: objVal,
115130
Variable: objKey
116131
});

0 commit comments

Comments
 (0)