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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Added `getRedirectURIs` endpoint
- Added `getDataStreamSummary` endpoint

### Fixed

Expand Down
1,829 changes: 658 additions & 1,171 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/endpoints/dataStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
DataStreamId,
DataStreamsConfiguration,
DataStreamServiceRequest,
DataStreamSummary,
DataStreamSummaryRequest,
Measurement,
MutableDataStreamSequence,
NamespacedId,
Expand All @@ -15,6 +17,7 @@ import {
} from "@/shared";
import Endpoint from "./endpoint";
import CarpDataStreamBatch from "@/shared/models/carpDataStreamBatch";
import { objectKeysFromSnakeToCamel } from "@/shared/utils";

class DataStreams extends Endpoint {
endpoint: string = "/api/data-stream-service";
Expand Down Expand Up @@ -212,6 +215,24 @@ class DataStreams extends Endpoint {

await this.actions.post(this.endpoint, serializedRequest);
}

/**
* Get data stream summary
* @returns The data stream summary
* @param request The data stream summary request
*/
async getDataStreamSummary(
request: DataStreamSummaryRequest,
): Promise<DataStreamSummary> {
const response = await this.actions.get<DataStreamSummary>(
`${this.endpoint}/summary`,
{
params: objectKeysFromSnakeToCamel(request),
},
);

return response.data;
}
}

export default DataStreams;
2 changes: 2 additions & 0 deletions src/shared/coreTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import DataStreamPoint = datadk.cachet.carp.data.application.DataStreamPoint;
import SyncPoint = datadk.cachet.carp.data.application.SyncPoint;
import Measurement = datadk.cachet.carp.data.application.Measurement;
import Geolocation = cdk.cachet.carp.common.application.data.Geolocation;
import CompletedTask = cdk.cachet.carp.common.application.data.CompletedTask;

const { Roles } = cdk.cachet.carp.common.application.users.AssignedTo;
const { EmailAddress } = cdk.cachet.carp.common.application;
Expand Down Expand Up @@ -200,4 +201,5 @@ export {
DeviceConfiguration,
ExpectedParticipantDataCore,
SelectOne,
CompletedTask,
};
73 changes: 73 additions & 0 deletions src/shared/models/dataStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { CompletedTask, Data } from "../coreTypes";

export type DataStreamType =
| "survey"
| "health"
| "cognition"
| "image"
| "audio"
| "video"
| "informed_consent"
| "sensing";
export type DataStreamScope = "study" | "deployment" | "participant";

export interface DataStreamSummaryRequest {
study_id: string;
deployment_id?: string;
participant_id?: string;
scope: DataStreamScope;
type: DataStreamType;
from: string; // ISO8601String;
to: string; // ISO8601String;
}

export interface DateTaskQuantityTriple {
date: string; // ISO8601String;
task: string;
quantity: number;
}

export interface DataStreamSummary {
data: DateTaskQuantityTriple[];
studyId: string;
deploymentId: string;
participantId: string;
scope: DataStreamScope;
type: DataStreamType;
from: string; // ISO8601String;
to: string; // ISO8601String;
}

export class CompletedAppTask extends CompletedTask {
public static dataType = "dk.cachet.carp.completedapptask";

public completedAt: Date;

constructor(
taskName: string,
public taskType: DataStreamType,
private taskDataType: string,
taskData?: Data | null,
) {
super(taskName, taskData as any);
this.completedAt = new Date();
}

public toJSON = () => {
const modifiedTaskData = {
...Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Object.entries(this.taskData).filter(([_, value]) => value),
),
__type: this.taskDataType,
};

return {
__type: CompletedAppTask.dataType,
taskName: this.taskName,
taskType: this.taskType,
taskData: modifiedTaskData,
completedAt: this.completedAt.toISOString(),
};
};
}
1 change: 1 addition & 0 deletions src/shared/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./protocol";
export * from "./studies";
export * from "./carpFile";
export * from "./participantInfo";
export * from "./dataStream";
16 changes: 16 additions & 0 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ export const parseUser = (accessToken: string): User => {
role: [role],
};
};

export const objectKeysFromSnakeToCamel = (
obj: Record<string, any>,
): Record<string, any> =>
Object.fromEntries(
Object.entries(obj).map(([key, value]) => {
const parts = key.split("_");
const camelKey =
parts[0] +
parts
.slice(1)
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join("");
return [camelKey, value];
}),
);
7 changes: 6 additions & 1 deletion src/test/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export const STUDY_PROTOCOL: StudyProtocol = {
tasks: [
{
name: "Monitor movement",
__type: "dk.cachet.carp.common.application.tasks.BackgroundTask",
__type: "dk.cachet.carp.common.application.tasks.Task",
duration: "PT168H",
title: "Monitor movement",
measures: [
{
type: "dk.cachet.carp.geolocation",
Expand Down Expand Up @@ -39,6 +40,10 @@ export const STUDY_PROTOCOL: StudyProtocol = {
"dk.cachet.carp.common.application.sampling.NoOptionsSamplingConfiguration",
},
},
{
type: "dk.cachet.carp.completedapptask",
__type: "dk.cachet.carp.common.application.tasks.Measure.DataStream",
},
],
description: "Track step count and geolocation for one week.",
},
Expand Down
83 changes: 71 additions & 12 deletions src/test/endpoints/dataStreams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { afterAll, beforeAll, expect, test } from "vitest";
import {
StudyStatus,
ParticipantGroupStatus,
getSerializer,
StudyProtocolSnapshot,
DefaultSerializer,
DataStreamsConfiguration,
NamespacedId,
MutableDataStreamSequence,
Expand All @@ -15,6 +12,10 @@ import {
Measurement,
Geolocation,
toList,
CompletedAppTask,
DefaultSerializer,
getSerializer,
StudyProtocolSnapshot,
} from "@/shared";
import { CarpTestClient } from "@/client";
import { STUDY_PROTOCOL } from "../consts";
Expand Down Expand Up @@ -90,13 +91,7 @@ describe("DataStreams", () => {

await testClient.authentication.refresh();

namespaceId = new NamespacedId(
STUDY_PROTOCOL.tasks[0].measures[0].type.replace(
`.${STUDY_PROTOCOL.tasks[0].measures[0].type.split(".").pop()}`,
"",
),
STUDY_PROTOCOL.tasks[0].measures[0].type.split(".").pop(),
);
namespaceId = new NamespacedId("dk.cachet.carp", "completedapptask");
await expect(
testClient.dataStreams.openDataStreams({
studyDeploymentId: participantGroupStatus.id.stringRepresentation,
Expand Down Expand Up @@ -132,7 +127,12 @@ describe("DataStreams", () => {
toLong(1),
null,
namespaceId,
new Geolocation(57, 45, null) as any,
new CompletedAppTask(
"Monitor movement",
"sensing",
"dk.cachet.carp.geolocation",
new Geolocation(57, 45, null) as any,
) as any,
),
]),
);
Expand Down Expand Up @@ -201,7 +201,12 @@ describe("DataStreams", () => {
toLong(1),
null,
namespaceId,
new Geolocation(57, 45, null) as any,
new CompletedAppTask(
"Monitor movement",
"sensing",
"dk.cachet.carp.geolocation",
new Geolocation(57, 45, null) as any,
) as any,
),
]),
);
Expand Down Expand Up @@ -293,6 +298,60 @@ describe("DataStreams", () => {
});
});

test("should be able to get summary for datastreams", async () => {
const batch = new CarpDataStreamBatch();
const sequence = new MutableDataStreamSequence(
new DataStreamId(
participantGroupStatus.id,
STUDY_PROTOCOL.primaryDevices[0].roleName,
namespaceId,
),
toLong(1),
toList([1]),
SyncPoint.Companion.UnixEpoch,
);
sequence.appendMeasurementsList(
toList([
new Measurement(
toLong(1),
null,
namespaceId,
new CompletedAppTask(
"Monitor movement",
"sensing",
"dk.cachet.carp.geolocation",
new Geolocation(57, 45, null) as any,
) as any,
),
]),
);

batch.sequences = [sequence];

await expect(
testClient.dataStreams.appendToDataStreams({
studyDeploymentId: participantGroupStatus.id.stringRepresentation,
batch,
}),
).resolves.not.toThrow();

const response = await testClient.dataStreams.getDataStreamSummary({
study_id: study.studyId.stringRepresentation,
scope: "study",
type: "sensing",
from: new Date(Date.now() - 1000 * 60 * 60 * 24 * 28).toISOString(),
to: new Date().toISOString(),
});

expect(response.studyId).toBe(study.studyId.stringRepresentation);
expect(response.scope).toBe("study");
expect(response.type).toBe("sensing");
response.data.forEach((data) => {
expect(data.quantity).to.be.at.least(1);
expect(data.task).toBe("Monitor movement");
});
});

afterAll(async () => {
if (study) {
if (participantGroupStatus) {
Expand Down