Skip to content

Commit ebce590

Browse files
committed
Add stored procedure GetResourceActivityPErResource<ajorVersion
1 parent 66df1d0 commit ebce590

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

WebAPI/LearningHub.Nhs.Database/LearningHub.Nhs.Database.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@
515515
<None Include="Scripts\Post-Deploy\Scripts\TD-2929_ActivityStatusUpdates.sql" />
516516
<Build Include="Stored Procedures\Resources\BlockCollectionFileSearch.sql" />
517517
<None Include="Scripts\Post-Deploy\Scripts\TD-4031_AMStoMKIOMigration.sql" />
518+
<Build Include="Stored Procedures\Activity\GetResourceActivityPerResourceMajorVersion.sql" />
518519
</ItemGroup>
519520
<ItemGroup>
520521
<None Include="Scripts\Pre-Deploy\Scripts\Card5766_AuthorTableChanges.PreDeployment.sql" />
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
2+
-------------------------------------------------------------------------------
3+
-- Author Phil T
4+
-- Created 04-07-24
5+
-- Purpose Return resource activity for each major version for user
6+
7+
8+
-- Description
9+
/*
10+
This procedure returns a single entry per resource Id, selecting the most important one for that major version.
11+
This is so users can still have a resourceActivity history following a majorVersion change
12+
13+
UserIds is nullable so that general resource activity can be searched for
14+
ResourceIds is nullable so that all a users history can be searched for
15+
16+
When determining the resourceActivity statusDescription in the front end resourceTypeId is also required for changing completed statuses to resourceType specific ones
17+
18+
Currently if multiple rows meet the case criteria we retrieve the one with the highest Id which is also expected to be the ActivityEnd part of the activityStatus pair.
19+
20+
*/
21+
-- Future Considerations
22+
/*
23+
Because the activityResource should come in pairs one with ActivityStart populated and one with ActivityEnd populated
24+
it could be desireable to join via LaunchResourceActivityId and coalesce the data in future.
25+
Or/And coalesce where the case returns multiple rows.
26+
27+
*/
28+
-- Notes
29+
-- resourceId is used not originalResourceId
30+
31+
32+
-------------------------------------------------------------------------------
33+
34+
35+
36+
-- Split the comma-separated list into a table of integers
37+
DECLARE @ResourceIdTable TABLE (ResourceId INT);
38+
39+
IF @ResourceIds IS NOT NULL AND @ResourceIds <> ''
40+
BEGIN
41+
INSERT INTO @ResourceIdTable (ResourceId)
42+
SELECT CAST(value AS INT)
43+
FROM STRING_SPLIT(@ResourceIds, ',');
44+
END;
45+
46+
-- Split the comma-separated list of UserIds into a table
47+
DECLARE @UserIdTable TABLE (UserId INT);
48+
49+
IF @UserIds IS NOT NULL AND @UserIds <> ''
50+
BEGIN
51+
INSERT INTO @UserIdTable (UserId)
52+
SELECT CAST(value AS INT)
53+
FROM STRING_SPLIT(@UserIds, ',');
54+
END;
55+
56+
WITH FilteredResourceActivities AS (
57+
SELECT
58+
ars.[Id],
59+
ars.[UserId],
60+
ars.[LaunchResourceActivityId],
61+
ars.[ResourceId],
62+
ars.[ResourceVersionId],
63+
ars.[MajorVersion],
64+
ars.[MinorVersion],
65+
ars.[NodePathId],
66+
ars.[ActivityStatusId],
67+
ars.[ActivityStart],
68+
ars.[ActivityEnd],
69+
ars.[DurationSeconds],
70+
ars.[Score],
71+
ars.[Deleted],
72+
ars.[CreateUserID],
73+
ars.[CreateDate],
74+
ars.[AmendUserID],
75+
ars.[AmendDate]
76+
FROM
77+
[activity].[resourceactivity] ars
78+
WHERE
79+
(@UserIds IS NULL OR ars.userId IN (SELECT UserId FROM @UserIdTable) OR NOT EXISTS (SELECT 1 FROM @UserIdTable))
80+
AND (@ResourceIds IS NULL OR @ResourceIds = '' OR ars.resourceId IN (SELECT ResourceId FROM @ResourceIdTable) OR NOT EXISTS (SELECT 1 FROM @ResourceIdTable))
81+
AND ars.Deleted = 0
82+
AND ars.ActivityStatusId NOT IN (1, 6, 2) -- These Ids are not in use - Launched, Downloaded, In Progress (stored as completed and incomplete then renamed in the application)
83+
),
84+
RankedActivities AS (
85+
SELECT
86+
ra.[Id],
87+
ra.[UserId],
88+
ra.[LaunchResourceActivityId],
89+
ra.[ResourceId],
90+
ra.[ResourceVersionId],
91+
ra.[MajorVersion],
92+
ra.[MinorVersion],
93+
ra.[NodePathId],
94+
ra.[ActivityStatusId],
95+
ra.[ActivityStart],
96+
ra.[ActivityEnd],
97+
ra.[DurationSeconds],
98+
ra.[Score],
99+
ra.[Deleted],
100+
ra.[CreateUserID],
101+
ra.[CreateDate],
102+
ra.[AmendUserID],
103+
ra.[AmendDate],
104+
ROW_NUMBER() OVER (
105+
PARTITION BY resourceId, userId, MajorVersion
106+
ORDER BY
107+
CASE
108+
WHEN ActivityStatusId = 5 THEN 1 -- Passed
109+
WHEN ActivityStatusId = 3 THEN 2 -- Completed
110+
WHEN ActivityStatusId = 4 THEN 3 -- Failed
111+
WHEN ActivityStatusId = 7 THEN 4 -- Incomplete
112+
ELSE 5 -- shouldn't be any
113+
END,
114+
Id DESC -- we have two entries per interacting with a resource the start and the end, we are just returning the last entry made
115+
-- there is the option of instead coalescing LaunchResourceActivityId, ActivityStart,ActivityEnd potentially via joining LaunchResourceActivityId and UserId
116+
) AS RowNum
117+
FROM
118+
FilteredResourceActivities ra
119+
)
120+
SELECT
121+
ra.[Id],
122+
ra.[UserId],
123+
ra.[LaunchResourceActivityId],
124+
ra.[ResourceId],
125+
ra.[ResourceVersionId],
126+
ra.[MajorVersion],
127+
ra.[MinorVersion],
128+
ra.[NodePathId],
129+
ra.[ActivityStatusId],
130+
ra.[ActivityStart],
131+
ra.[ActivityEnd],
132+
ra.[DurationSeconds],
133+
ra.[Score],
134+
ra.[Deleted],
135+
ra.[CreateUserID],
136+
ra.[CreateDate],
137+
ra.[AmendUserID],
138+
ra.[AmendDate]
139+
FROM
140+
RankedActivities ra
141+
WHERE
142+
RowNum = 1
143+
order by MajorVersion desc;
144+
END;
145+
GO
146+
147+

0 commit comments

Comments
 (0)