-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: add first and last correctness counts to course-wide analyti…
…cs and extend database
- Loading branch information
1 parent
ca56973
commit c546071
Showing
6 changed files
with
220 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 90 additions & 28 deletions
118
apps/analytics/src/modules/participant_analytics/save_participant_analytics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,92 @@ | ||
from datetime import datetime | ||
|
||
|
||
def save_participant_analytics(db, df_analytics, timestamp, analytics_type="DAILY"): | ||
# Create daily analytics entries for all participants | ||
for _, row in df_analytics.iterrows(): | ||
db.participantanalytics.upsert( | ||
where={ | ||
"type_courseId_participantId_timestamp": { | ||
"type": analytics_type, | ||
"courseId": row["courseId"], | ||
"participantId": row["participantId"], | ||
"timestamp": timestamp, | ||
} | ||
}, | ||
data={ | ||
"create": { | ||
"type": analytics_type, | ||
"timestamp": timestamp, | ||
"trialsCount": row["trialsCount"], | ||
"responseCount": row["responseCount"], | ||
"totalScore": row["totalScore"], | ||
"totalPoints": row["totalPoints"], | ||
"totalXp": row["totalXp"], | ||
"meanCorrectCount": row["meanCorrectCount"], | ||
"meanPartialCorrectCount": row["meanPartialCount"], | ||
"meanWrongCount": row["meanWrongCount"], | ||
"participant": {"connect": {"id": row["participantId"]}}, | ||
"course": {"connect": {"id": row["courseId"]}}, | ||
computedAt = datetime.now().strftime("%Y-%m-%d") + "T00:00:00.000Z" | ||
|
||
# Create daily / weekly / monthly analytics entries for all participants | ||
if analytics_type in ["DAILY", "WEEKLY", "MONTHLY"]: | ||
for _, row in df_analytics.iterrows(): | ||
db.participantanalytics.upsert( | ||
where={ | ||
"type_courseId_participantId_timestamp": { | ||
"type": analytics_type, | ||
"courseId": row["courseId"], | ||
"participantId": row["participantId"], | ||
"timestamp": timestamp, | ||
} | ||
}, | ||
"update": {}, | ||
}, | ||
) | ||
data={ | ||
"create": { | ||
"type": analytics_type, | ||
"timestamp": timestamp, | ||
"computedAt": computedAt, | ||
"trialsCount": row["trialsCount"], | ||
"responseCount": row["responseCount"], | ||
"totalScore": row["totalScore"], | ||
"totalPoints": row["totalPoints"], | ||
"totalXp": row["totalXp"], | ||
"meanCorrectCount": row["meanCorrectCount"], | ||
"meanPartialCorrectCount": row["meanPartialCount"], | ||
"meanWrongCount": row["meanWrongCount"], | ||
"participant": {"connect": {"id": row["participantId"]}}, | ||
"course": {"connect": {"id": row["courseId"]}}, | ||
}, | ||
"update": {}, | ||
}, | ||
) | ||
|
||
# Create or update course-wide analytics entries (should be unique for participant / course combination) | ||
elif analytics_type == "COURSE": | ||
timestamp_const = "1970-01-01T00:00:00.000Z" | ||
for _, row in df_analytics.iterrows(): | ||
db.participantanalytics.upsert( | ||
where={ | ||
"type_courseId_participantId_timestamp": { | ||
"type": analytics_type, | ||
"courseId": row["courseId"], | ||
"participantId": row["participantId"], | ||
"timestamp": timestamp_const, | ||
} | ||
}, | ||
data={ | ||
"create": { | ||
"type": "COURSE", | ||
"timestamp": timestamp_const, | ||
"computedAt": computedAt, | ||
"trialsCount": row["trialsCount"], | ||
"responseCount": row["responseCount"], | ||
"totalScore": row["totalScore"], | ||
"totalPoints": row["totalPoints"], | ||
"totalXp": row["totalXp"], | ||
"meanCorrectCount": row["meanCorrectCount"], | ||
"meanPartialCorrectCount": row["meanPartialCount"], | ||
"meanWrongCount": row["meanWrongCount"], | ||
"firstCorrectCount": row["firstCorrectCount"], | ||
"firstWrongCount": row["firstWrongCount"], | ||
"lastCorrectCount": row["lastCorrectCount"], | ||
"lastWrongCount": row["lastWrongCount"], | ||
"participant": {"connect": {"id": row["participantId"]}}, | ||
"course": {"connect": {"id": row["courseId"]}}, | ||
}, | ||
"update": { | ||
"timestamp": timestamp_const, | ||
"computedAt": computedAt, | ||
"trialsCount": row["trialsCount"], | ||
"responseCount": row["responseCount"], | ||
"totalScore": row["totalScore"], | ||
"totalPoints": row["totalPoints"], | ||
"totalXp": row["totalXp"], | ||
"meanCorrectCount": row["meanCorrectCount"], | ||
"meanPartialCorrectCount": row["meanPartialCount"], | ||
"meanWrongCount": row["meanWrongCount"], | ||
"firstCorrectCount": row["firstCorrectCount"], | ||
"firstWrongCount": row["firstWrongCount"], | ||
"lastCorrectCount": row["lastCorrectCount"], | ||
"lastWrongCount": row["lastWrongCount"], | ||
}, | ||
}, | ||
) | ||
|
||
else: | ||
raise ValueError("Unknown analytics type: {}".format(analytics_type)) |
36 changes: 36 additions & 0 deletions
36
packages/prisma/src/prisma/migrations/20240827152536_computed_at_analytics/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `updatedAt` to the `AggregatedAnalytics` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `updatedAt` to the `AggregatedCompetencyAnalytics` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `updatedAt` to the `AggregatedCourseAnalytics` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `updatedAt` to the `CompetencyAnalytics` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `updatedAt` to the `ParticipantAnalytics` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `updatedAt` to the `ParticipantCourseAnalytics` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "AggregatedAnalytics" ADD COLUMN "computedAt" DATE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "AggregatedCompetencyAnalytics" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "AggregatedCourseAnalytics" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "CompetencyAnalytics" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "ParticipantAnalytics" ADD COLUMN "computedAt" DATE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "ParticipantCourseAnalytics" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters