-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added table to prepopulate the user matrix api to fetch the matrix for a user script to populate the user matrix added static resource handler to serve from webapp
- Loading branch information
Showing
6 changed files
with
262 additions
and
0 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
9 changes: 9 additions & 0 deletions
9
avni-server-api/src/main/java/org/avni/dao/UserReviewMatrixRepository.java
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,9 @@ | ||
package org.avni.dao; | ||
|
||
import org.avni.domain.User; | ||
import org.avni.domain.UserReviewMatrix; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserReviewMatrixRepository extends JpaRepository<UserReviewMatrix, Long> { | ||
UserReviewMatrix findFirstByUserAndOrganisationId(User user, Long orgId); | ||
} |
71 changes: 71 additions & 0 deletions
71
avni-server-api/src/main/java/org/avni/domain/UserReviewMatrix.java
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,71 @@ | ||
package org.avni.domain; | ||
|
||
import org.hibernate.annotations.Type; | ||
|
||
import javax.persistence.*; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@Entity | ||
@Table(name = "user_review_matrix") | ||
public class UserReviewMatrix { | ||
|
||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", updatable = false, nullable = false) | ||
@Id | ||
private Long id; | ||
|
||
@NotNull | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@Column | ||
private Long organisationId; | ||
|
||
@Column | ||
private Long reviewYear; | ||
|
||
@Column | ||
@Type(type = "observations") | ||
private ObservationCollection matrix; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
|
||
public Long getReviewYear() { | ||
return reviewYear; | ||
} | ||
|
||
public void setReviewYear(Long reviewYear) { | ||
this.reviewYear = reviewYear; | ||
} | ||
|
||
public ObservationCollection getMatrix() { | ||
return matrix; | ||
} | ||
|
||
public void setMatrix(ObservationCollection matrix) { | ||
this.matrix = matrix; | ||
} | ||
|
||
public Long getOrganisationId() { | ||
return organisationId; | ||
} | ||
|
||
public void setOrganisationId(Long organisationId) { | ||
this.organisationId = organisationId; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
avni-server-api/src/main/java/org/avni/web/UserReviewMatrixController.java
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,32 @@ | ||
package org.avni.web; | ||
|
||
import org.avni.dao.UserReviewMatrixRepository; | ||
import org.avni.domain.UserContext; | ||
import org.avni.domain.UserReviewMatrix; | ||
import org.avni.framework.security.UserContextHolder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class UserReviewMatrixController { | ||
|
||
private final UserReviewMatrixRepository userReviewMatrixRepository; | ||
|
||
@Autowired | ||
public UserReviewMatrixController(UserReviewMatrixRepository userReviewMatrixRepository) { | ||
this.userReviewMatrixRepository = userReviewMatrixRepository; | ||
} | ||
|
||
@RequestMapping(value = "/userMatrix", method = RequestMethod.GET) | ||
@PreAuthorize(value = "hasAnyAuthority('user')") | ||
public ResponseEntity<UserReviewMatrix> getUserReview() { | ||
UserContext userContext = UserContextHolder.getUserContext(); | ||
UserReviewMatrix userMatrix = userReviewMatrixRepository.findFirstByUserAndOrganisationId(userContext.getUser(), userContext.getOrganisation().getId()); | ||
return ResponseEntity.ok().body(userMatrix); | ||
} | ||
|
||
} |
132 changes: 132 additions & 0 deletions
132
avni-server-api/src/main/resources/database/populateUserReviewMatrix.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,132 @@ | ||
with total_data(user_id, org_id, ind_id, subj_type_id, program_id, month, count) as ( | ||
select i.last_modified_by_id, | ||
i.organisation_id, | ||
i.id, | ||
st.id, | ||
null, | ||
to_char(i.last_modified_date_time, 'Mon'), | ||
count(*) | ||
from individual i | ||
join subject_type st on i.subject_type_id = st.id and st.is_voided = false | ||
where extract(year from i.last_modified_date_time) = 2021 | ||
group by 1, 2, 3, 4, i.last_modified_date_time | ||
|
||
union all | ||
|
||
select enl.last_modified_by_id, | ||
enl.organisation_id, | ||
individual_id, | ||
s.id, | ||
p.id, | ||
to_char(enl.last_modified_date_time, 'Mon'), | ||
count(*) | ||
from program_enrolment enl | ||
join program p on p.id = enl.program_id and p.is_voided = false | ||
join individual i2 on enl.individual_id = i2.id | ||
join subject_type s on i2.subject_type_id = s.id and s.is_voided = false | ||
where extract(year from enl.last_modified_date_time) = 2021 | ||
group by 1, 2, 3, 4, 5, enl.last_modified_date_time | ||
|
||
union all | ||
|
||
select enc.last_modified_by_id, | ||
enc.organisation_id, | ||
individual_id, | ||
s.id, | ||
null, | ||
to_char(enc.last_modified_date_time, 'Mon'), | ||
count(*) | ||
from encounter enc | ||
join individual i2 on enc.individual_id = i2.id | ||
join subject_type s on i2.subject_type_id = s.id and s.is_voided = false | ||
where extract(year from enc.last_modified_date_time) = 2021 | ||
group by 1, 2, 3, 4, enc.last_modified_date_time | ||
|
||
union all | ||
|
||
select penc.last_modified_by_id, | ||
penc.organisation_id, | ||
individual_id, | ||
s.id, | ||
p.id, | ||
to_char(penc.last_modified_date_time, 'Mon'), | ||
count(*) | ||
from program_encounter penc | ||
join program_enrolment enl on penc.program_enrolment_id = enl.id | ||
join program p on p.id = enl.program_id and p.is_voided = false | ||
join individual i2 on enl.individual_id = i2.id | ||
join subject_type s on i2.subject_type_id = s.id and s.is_voided = false | ||
where extract(year from penc.last_modified_date_time) = 2021 | ||
group by 1, 2, 3, 4, 5, penc.last_modified_date_time | ||
), | ||
by_subject_type as ( | ||
select user_id, org_id, jsonb_agg(matrix) matrix | ||
from (select r.user_id, | ||
r.org_id, | ||
r.subj_type_id, | ||
jsonb_build_object('name', t.name, | ||
'count', count(distinct ind_id) | ||
) matrix | ||
from total_data r | ||
left join subject_type t on r.subj_type_id = t.id | ||
where r.subj_type_id notnull | ||
group by 1, 2, 3, t.name) s | ||
group by 1, 2 | ||
), | ||
by_program as ( | ||
select user_id, org_id, jsonb_agg(matrix) matrix | ||
from ( | ||
select r.user_id, | ||
r.org_id, | ||
r.program_id, | ||
jsonb_build_object('name', t.name, | ||
'count', count(distinct ind_id) | ||
) matrix | ||
from total_data r | ||
left join program t on r.program_id = t.id | ||
where r.program_id notnull | ||
group by 1, 2, 3, t.name) p | ||
group by 1, 2 | ||
), | ||
by_month as ( | ||
select user_id, | ||
org_id, | ||
jsonb_agg(activities_by_month) activities_by_month, | ||
jsonb_agg(reach_by_month) reach_by_month | ||
from (select r.user_id, | ||
r.org_id, | ||
jsonb_build_object('month', r.month, | ||
'count', sum(r.count) | ||
) activities_by_month, | ||
jsonb_build_object('month', r.month, | ||
'count', count(distinct ind_id) | ||
) reach_by_month | ||
from total_data r | ||
group by 1, 2, r.month) s | ||
group by 1, 2 | ||
), | ||
total_agg as ( | ||
select r.user_id, | ||
r.org_id, | ||
sum(r.count) form_filled, | ||
count(distinct ind_id) reach | ||
from total_data r | ||
group by 1, 2 | ||
) | ||
insert into user_review_matrix(user_id, organisation_id, matrix, review_year) | ||
select u.id, | ||
t.org_id, | ||
jsonb_build_object( | ||
'activities', t.form_filled, | ||
'activitiesByMonth', coalesce(activities_by_month, '[]'::jsonb), | ||
'reach', t.reach, | ||
'reachByMonth', coalesce(reach_by_month, '[]'::jsonb), | ||
'reachBySubjectType', coalesce(st.matrix, '[]'::jsonb), | ||
'reachByProgram', coalesce(p.matrix, '[]'::jsonb) | ||
), | ||
2021 | ||
from users u | ||
join total_agg t on u.id = t.user_id | ||
left join by_subject_type st on st.user_id = u.id and st.org_id = t.org_id | ||
left join by_program p on p.user_id = u.id and p.org_id = t.org_id | ||
left join by_month m on m.user_id = u.id and m.org_id = t.org_id; |
13 changes: 13 additions & 0 deletions
13
avni-server-api/src/main/resources/db/migration/V1_199__CreateYearReviewTable.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,13 @@ | ||
create table user_review_matrix | ||
( | ||
id serial primary key, | ||
user_id integer references users (id), | ||
organisation_id integer references organisation (id), | ||
review_year integer not null default 2021, | ||
matrix jsonb not null | ||
); | ||
|
||
alter table user_review_matrix add unique (user_id, organisation_id, review_year); | ||
|
||
select enable_rls_on_tx_table('user_review_matrix'); | ||
|