Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

108 snapshots #109

Merged
merged 2 commits into from
Apr 9, 2024
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
2 changes: 2 additions & 0 deletions casbin/casbin_auth_policy.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ p, GUEST, /api/tasks/{taskId},
p, *, /api/studies/{studyId}, GET
p, ADMIN, /api/studies/{studyId}, PATCH
p, ORGANIZATION_MEMBER, /api/studies/{studyId}, PATCH
p, ADMIN, /api/studies/{studyId}/snapshot, PATCH
p, ORGANIZATION_MEMBER, /api/studies/{studyId}/snapshot, PATCH
p, ADMIN, /api/studies/{studyId}, DELETE
p, ORGANIZATION_MEMBER, /api/studies/{studyId}, DELETE
p, ADMIN, /api/studies/{studyId}/studyusers, GET
Expand Down
14 changes: 14 additions & 0 deletions src/controllers/Study.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,17 @@ func (s *StudyController) GetStudyUsersByStudyId(e echo.Context) error {

return common.SendHTTPOkWithBody(e, results)
}

// SnapshotStudyByStudyId
func (s *StudyController) SnapshotStudyByStudyId(e echo.Context) error {
axonlogger.InfoLogger.Println("============= STUDY CONTROLLER: SnapshotStudyByStudyId() =============")

studyParamId := e.Param("studyId")
httpStatus := studyServiceImpl.SnapshotStudyByStudyId(studyParamId)
if !common.HTTPRequestIsSuccessful(httpStatus.Status) {
axonlogger.ErrorLogger.Println("GetStudyUsersByStudyId() failed")
return e.JSON(httpStatus.Status, httpStatus)
}

return common.SendHTTPOk(e)
}
2 changes: 1 addition & 1 deletion src/database/BaseRepository.database.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (b *BaseRepository) GetAllBy(argStructSlice interface{}, query string, args
rowToStructErr := rowsToStructs(rows, argStructSlice)
if rowToStructErr != nil {
axonlogger.ErrorLogger.Println("Attempted Query: " + query)
axonlogger.ErrorLogger.Println("Error scanning rows", err)
axonlogger.ErrorLogger.Println("Error scanning rows", rowToStructErr)
return models.HTTPStatus{Status: http.StatusInternalServerError, Message: http.StatusText(http.StatusInternalServerError)}
}
return models.HTTPStatus{Status: http.StatusOK, Message: http.StatusText(http.StatusOK)}
Expand Down
8 changes: 5 additions & 3 deletions src/database/Study.Database.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *StudyRepository) GetStudyById(studyId uint) (models.Study, models.HTTPS
httpStatus := baseRepositoryImpl.GetOneBy(
&dbStudy,
`
SELECT id, owner_id, created_at, deleted_at, internal_name, external_name, started, can_edit, consent, description, config
SELECT id, owner_id, created_at, deleted_at, internal_name, external_name, started, can_edit, consent, description, config, snapshots
FROM studies
WHERE id = ?
LIMIT 1;
Expand Down Expand Up @@ -148,6 +148,7 @@ func (s *StudyRepository) GetStudyById(studyId uint) (models.Study, models.HTTPS
study.Description = dbStudy.Description
study.Config = dbStudy.Config
study.StudyTasks = studyTasks
study.Snapshots = dbStudy.Snapshots

return study, models.HTTPStatus{Status: http.StatusOK, Message: http.StatusText(http.StatusOK)}
}
Expand All @@ -168,7 +169,7 @@ func (s *StudyRepository) GetStudiesByOrganizationId(organizationId uint) ([]mod
if httpStatus := baseRepositoryImpl.GetAllBy(
&dbStudies,
`
SELECT studies.id, owner_id, studies.created_at, deleted_at, internal_name, external_name, started, can_edit, consent, description, config
SELECT studies.id, owner_id, studies.created_at, deleted_at, internal_name, external_name, started, can_edit, consent, description, config, snapshots
FROM studies JOIN users
ON studies.owner_id = users.id
WHERE users.organization_id = ? AND deleted_at IS NULL ORDER BY created_at DESC;
Expand Down Expand Up @@ -262,7 +263,7 @@ func (s *StudyRepository) UpdateStudyWithoutTaskUpdate(study *models.Study) mode
if _, err := db.Exec(
`
UPDATE studies
SET deleted_at = ?, internal_name = ?, external_name = ?, started = ?, description = ?, can_edit = ?, consent = ?, config = ?
SET deleted_at = ?, internal_name = ?, external_name = ?, started = ?, description = ?, can_edit = ?, consent = ?, config = ?, snapshots = ?
WHERE id = ?;
`,
study.DeletedAt,
Expand All @@ -273,6 +274,7 @@ func (s *StudyRepository) UpdateStudyWithoutTaskUpdate(study *models.Study) mode
study.CanEdit,
study.Consent.ID,
study.Config,
study.Snapshots,
study.ID,
); err != nil {
if err == sql.ErrNoRows {
Expand Down
4 changes: 2 additions & 2 deletions src/models/ParticipantData.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
* This file defines structs that outline the parameters collected for each task that is administered to participants
*/

type SliceMapStringInterface []map[string]interface{}
type SliceMapStringInterface []MapStringInterface

// ParticipantDataSchema defines the SQL table schema for this model
var ParticipantDataSchema = `
Expand Down Expand Up @@ -44,7 +44,7 @@ type ParticipantData struct {
// The data stored in a JSON field is returned as a []uint8
func (s *SliceMapStringInterface) Scan(src interface{}) error {
var source []byte
var tempMapSlice []map[string]interface{}
var tempMapSlice SliceMapStringInterface

switch src := src.(type) {
case []uint8:
Expand Down
3 changes: 3 additions & 0 deletions src/models/Study.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var StudySchema = `
consent INT UNSIGNED DEFAULT(NULL),
description VARCHAR(300),
config JSON NOT NULL DEFAULT (JSON_OBJECT()),
snapshots JSON NOT NULL DEFAULT (JSON_OBJECT()),
FOREIGN KEY (consent) REFERENCES tasks(id),
FOREIGN KEY (owner_id) REFERENCES users(id),
PRIMARY KEY (id)
Expand All @@ -39,6 +40,7 @@ type Study struct {
Description string `json:"description"`
Config MapStringInterface `json:"config"`
StudyTasks []StudyTask `json:"studyTasks"`
Snapshots MapStringInterface `json:"snapshots"`
}

// DBStudy is the database representation of a study
Expand All @@ -54,4 +56,5 @@ type DBStudy struct {
ConsentId uint `json:"consentId"`
Description string `json:"description"`
Config MapStringInterface `json:"config"`
Snapshots MapStringInterface `json:"snapshots"`
}
44 changes: 44 additions & 0 deletions src/services/Study.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,47 @@ func (s *StudyService) ArchiveStudyById(studyId string, loggedInUserId string, l

return studyRepositoryImpl.UpdateStudyWithoutTaskUpdate(&study)
}

func (s *StudyService) SnapshotStudyByStudyId(studyId string) models.HTTPStatus {
axonlogger.InfoLogger.Println("STUDY SERVICE: SnapshotStudyByStudyId()")

parsedStudyId, parsedStudyIdErr := convertStringToUint8(studyId)
if parsedStudyIdErr != nil {
axonlogger.WarningLogger.Println("Could not convert id to uint", parsedStudyIdErr)
return models.HTTPStatus{Status: http.StatusInternalServerError, Message: http.StatusText(http.StatusInternalServerError)}
}

retrievedStudy, getStudyHttpStatus := studyRepositoryImpl.GetStudyById(parsedStudyId)
if !common.HTTPRequestIsSuccessful(getStudyHttpStatus.Status) {
return getStudyHttpStatus
}

updatedSnapshot := retrievedStudy.Snapshots
snapshotList := make(models.SliceMapStringInterface, 0)
currTime := time.Now().UTC().Format(time.RFC3339)

for _, studyTask := range retrievedStudy.StudyTasks {
taskSnapshot := make(models.MapStringInterface)
taskSnapshot["taskOrder"] = studyTask.TaskOrder
taskSnapshot["fromPlatform"] = studyTask.Task.FromPlatform
taskSnapshot["description"] = studyTask.Task.Description
taskSnapshot["externalURL"] = studyTask.Task.ExternalURL
taskSnapshot["taskType"] = studyTask.Task.TaskType
taskSnapshot["name"] = studyTask.Task.Name
taskSnapshot["id"] = studyTask.Task.ID

if len(studyTask.Config) == 0 {
taskSnapshot["config"] = studyTask.Task.Config
} else {
taskSnapshot["config"] = studyTask.Config
}

snapshotList = append(snapshotList, taskSnapshot)
}
updatedSnapshot[currTime] = snapshotList
studyRepositoryImpl.UpdateStudyWithoutTaskUpdate(&retrievedStudy)
return models.HTTPStatus{
Status: http.StatusOK,
Message: http.StatusText(http.StatusOK),
}
}
1 change: 1 addition & 0 deletions src/setup/Router.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func setUpStudyRoutes(group *echo.Group) {
studies.GET("/:studyId", studiesControllerImpl.GetStudyById)
studies.GET("/:studyId/crowdsourcedusers", studiesControllerImpl.GetCrowdSourcedUsersByStudyId)
studies.GET("/:studyId/studyusers", studiesControllerImpl.GetStudyUsersByStudyId)
studies.PATCH("/:studyId/snapshot", studiesControllerImpl.SnapshotStudyByStudyId)
studies.PATCH("/:studyId", studiesControllerImpl.UpdateStudy) // takes a query param (updateTasks)
studies.DELETE("/:studyId", studiesControllerImpl.ArchiveStudyById)
}
Expand Down
Loading