Skip to content

Commit

Permalink
#546[client] | user year review
Browse files Browse the repository at this point in the history
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
vindeolal committed Dec 23, 2021
1 parent aee5de5 commit 7762c9d
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
.addResourceHandler("/static/**")
.addResourceLocations("file:" + staticPath + "static/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
//this is to serve user review html page in the web app.
registry.addResourceHandler("/userReview")
.addResourceLocations("file:" + staticPath + "userReview/");
registry
.addResourceHandler("/**")
.addResourceLocations("file:" + staticPath);
Expand Down Expand Up @@ -55,6 +58,8 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/analytics/**/{path:[^\\.]*}")
.setViewName("forward:/analytics/index.html");
registry.addViewController("/userReview")
.setViewName("forward:/userReview/index.html");
registry.addViewController("/")
.setViewName("forward:/index.html");
}
Expand Down
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);
}
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;
}
}
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);
}

}
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;
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');

0 comments on commit 7762c9d

Please sign in to comment.