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

[instruments] Bulk load SessionID and VisitLabel #8855

Merged
merged 2 commits into from
Nov 13, 2023
Merged
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
61 changes: 43 additions & 18 deletions php/libraries/NDB_BVL_Instrument.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1409,37 +1409,44 @@ abstract class NDB_BVL_Instrument extends NDB_Page
return $allValues[$fieldName];
}

private ?SessionID $sessionID;
/**
* Gets the sessionID for the timepoint to which this instrument pertains
*
* @return ?SessionID The sessionID
*/
function getSessionID(): ?SessionID
{

$query = "SELECT SessionID FROM flag WHERE CommentID = :CID";
$result = \NDB_Factory::singleton()->database()->pselectOne(
$query,
[
'CID' => $this->getCommentID(),
]
);

if (empty($result)) {
return null;
if (empty($this->sessionID)) {
$query = "SELECT SessionID FROM flag WHERE CommentID = :CID";
$result = \NDB_Factory::singleton()->database()->pselectOne(
$query,
[
'CID' => $this->getCommentID(),
]
);
if (empty($result)) {
return null;
}
$this->sessionID = new SessionID($result);
}

return (new SessionID($result));
return $this->sessionID;
}

private ?string $visitLabel = null;
/**
* Gets the VisitLabel for the timepoint for this instrument.
*
* @return string The visit label
*/
function getVisitLabel(): string
{
return \TimePoint::singleton($this->getSessionID())->getVisitLabel();
if ($this->visitLabel === null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just curious, why is this done in a lazy way instead of being instantiated in the factory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I didn't want to change the behaviour so I left if the way it was and just added caching.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense :)

$this->visitLabel = \TimePoint::singleton(
$this->getSessionID()
)->getVisitLabel();
}
return $this->visitLabel;
}

/**
Expand Down Expand Up @@ -2208,7 +2215,13 @@ abstract class NDB_BVL_Instrument extends NDB_Page

if ($this->jsonData) {
$jsondata = $db->pselect(
"SELECT CommentID, Data FROM flag WHERE CommentID IN ("
"SELECT SessionID,
CommentID,
session.Visit_Label as VisitLabel,
Data
FROM flag
JOIN session ON (session.ID=flag.SessionID)
WHERE CommentID IN ("
. join(',', $prepBindings) . ')',
$prepValues,
);
Expand All @@ -2217,8 +2230,10 @@ abstract class NDB_BVL_Instrument extends NDB_Page
$newinst = clone $this;

$newinst->commentID = $row['CommentID'];
$newinst->visitLabel = $row['VisitLabel'];
$newinst->sessionID = new SessionID($row['SessionID']);
$newinst->instanceData = json_decode(
$row['Data'],
$row['Data'] ?? '{}',
true,
) ?? $this->defaultInstanceData();

Expand All @@ -2228,15 +2243,25 @@ abstract class NDB_BVL_Instrument extends NDB_Page
);
} else {
$defaults = $db->pselect(
"SELECT * FROM $this->table WHERE CommentID IN ("
"SELECT flag.CommentID as CommentID,
session.Visit_Label as VisitLabel,
session.ID as SessionID, t.*
FROM $this->table t
JOIN flag ON (t.CommentID=flag.CommentID)
JOIN session ON (flag.SessionID=session.ID)
WHERE t.CommentID IN ("
. join(',', $prepBindings) . ')',
$prepValues,
);
return array_map(
function ($row) {
$newinst = clone $this;

$newinst->commentID = $row['CommentID'];
$newinst->commentID = $row['CommentID'];
$newinst->visitLabel = $row['VisitLabel'];
$newinst->sessionID = new SessionID($row['SessionID']);
unset($row['CommentID'], $row['VisitLabel'], $row['SessionID']);

$newinst->instanceData = $row;

return $newinst;
Expand Down