Skip to content

Commit

Permalink
avniproject/avni-product#1334 | adding rest of the endpoints for HR tab
Browse files Browse the repository at this point in the history
Signed-off-by: ak2502 <akanksha.feb25@gmail.com>
  • Loading branch information
ak2502 committed Aug 3, 2023
1 parent 96bdfa4 commit 81be492
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 17 deletions.
48 changes: 35 additions & 13 deletions src/main/java/org/avniproject/etl/controller/ReportController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import org.avniproject.etl.dto.AggregateReportResult;
import org.avniproject.etl.dto.UserActivityDTO;
import org.avniproject.etl.repository.ReportRepository;
import org.avniproject.etl.service.EtlService;
import org.avniproject.etl.service.ReportService;
import org.avniproject.etl.util.ReportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -28,24 +26,16 @@ public ReportController(ReportService reportService, ReportRepository reportRepo
}

@RequestMapping(value = "reports/hr/userActivity", method = RequestMethod.GET)
public ResponseEntity getUserActivity(@RequestParam(value = "start_date", required = false) String startDate,
public List<UserActivityDTO> getUserActivity(@RequestParam(value = "startDate", required = false) String startDate,
@RequestParam(value = "endDate", required = false) String endDate,
@RequestParam(value = "userIds", required = false, defaultValue = "") List<Long> userIds)
{
try {
List<UserActivityDTO> userActivity = reportRepository.getUserActivity(
@RequestParam(value = "userIds", required = false, defaultValue = "") List<Long> userIds){
return reportRepository.generateUserActivity(
OrgIdentityContextHolder.getDbSchema(),
reportUtil.getDateDynamicWhere(startDate, endDate, "registration_date"),
reportUtil.getDateDynamicWhere(startDate, endDate, "encounter_date_time"),
reportUtil.getDateDynamicWhere(startDate, endDate, "enrolment_date_time"),
reportUtil.getDynamicUserWhere(userIds, "u.id")
);

return ResponseEntity.ok(userActivity);
}
catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@RequestMapping(value = "/report/hr/syncFailures",method = RequestMethod.GET)
Expand Down Expand Up @@ -83,6 +73,38 @@ public List<UserActivityDTO> getUserDetails(@RequestParam(value = "userIds", req
reportUtil.getDynamicUserWhere(userIds, "u.id"));
}

@RequestMapping(value = "/report/hr/championUsers", method = RequestMethod.GET)
public List<AggregateReportResult> getChampionUsers(@RequestParam(value = "startDate", required = false) String startDate,
@RequestParam(value = "endDate", required = false) String endDate,
@RequestParam(value = "userIds", required = false, defaultValue = "") List<Long> userIds) {
return reportRepository.generateCompletedVisitsOnTimeByProportion(
">= 0.8",
OrgIdentityContextHolder.getDbSchema(),
reportUtil.getDateDynamicWhere(startDate, endDate, "encounter_date_time"),
reportUtil.getDynamicUserWhere(userIds, "u.id"));
}

@RequestMapping(value = "/report/hr/nonPerformingUsers", method = RequestMethod.GET)
public List<AggregateReportResult> getNonPerformingUsers(@RequestParam(value = "startDate", required = false) String startDate,
@RequestParam(value = "endDate", required = false) String endDate,
@RequestParam(value = "userIds", required = false, defaultValue = "") List<Long> userIds) {
return reportRepository.generateCompletedVisitsOnTimeByProportion(
"<= 0.5",
OrgIdentityContextHolder.getDbSchema(),
reportUtil.getDateDynamicWhere(startDate, endDate, "encounter_date_time"),
reportUtil.getDynamicUserWhere(userIds, "u.id")
);
}

@RequestMapping(value = "/report/hr/mostCancelled", method = RequestMethod.GET)
public List<AggregateReportResult> getUsersCancellingMostVisits(@RequestParam(value = "startDate", required = false) String startDate,
@RequestParam(value = "endDate", required = false) String endDate,
@RequestParam(value = "userIds", required = false, defaultValue = "") List<Long> userIds) {
return reportRepository.generateUserCancellingMostVisits(
OrgIdentityContextHolder.getDbSchema(),
reportUtil.getDateDynamicWhere(startDate, endDate, "encounter_date_time"),
reportUtil.getDynamicUserWhere(userIds, "u.id"));
}


}
Expand Down
86 changes: 82 additions & 4 deletions src/main/java/org/avniproject/etl/repository/ReportRepository.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.avniproject.etl.repository;

import org.avniproject.etl.dto.AggregateReportResult;
import org.apache.log4j.Logger;
import org.avniproject.etl.dto.UserActivityDTO;
import org.avniproject.etl.repository.rowMappers.reports.AggregateReportMapper;
import org.avniproject.etl.repository.rowMappers.reports.UserActivityMapper;
import org.avniproject.etl.repository.rowMappers.reports.UserCountMapper;
import org.avniproject.etl.repository.rowMappers.reports.UserDetailsMapper;
import org.avniproject.etl.service.EtlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
Expand All @@ -15,13 +17,14 @@
@Component
public class ReportRepository {
private final NamedParameterJdbcTemplate jdbcTemplate;
private static final Logger log = Logger.getLogger(EtlService.class);

@Autowired
public ReportRepository(NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public List<UserActivityDTO> getUserActivity(String orgSchemaName, String subjectWhere, String encounterWhere, String enrolmentWhere, String userWhere) {
public List<UserActivityDTO> generateUserActivity(String orgSchemaName, String subjectWhere, String encounterWhere, String enrolmentWhere, String userWhere) {
String baseQuery = "with registrations as (\n" +
" select last_modified_by_id, count(*) as registration_count\n" +
" from ${schemaName}.individual\n" +
Expand Down Expand Up @@ -75,14 +78,15 @@ public List<UserActivityDTO> getUserActivity(String orgSchemaName, String subjec
.replace("${encounterWhere}", encounterWhere)
.replace("${enrolmentWhere}", enrolmentWhere)
.replace("${userWhere}", userWhere);
log.info(query);
return jdbcTemplate.query(query, new UserActivityMapper());
}

public List<UserActivityDTO> generateUserSyncFailures(String orgSchemaName, String syncTelemetryWhere, String userWhere) {
String baseQuery = "select coalesce(u.name, u.username) as name, \n" +
" count(*) as count\n" +
"from ${schemaName}.sync_telemetry st\n" +
" join ${schemaName}.user u on st.user_id = u.id\n" +
" join ${schemaName}.users u on st.user_id = u.id\n" +
"where sync_status = 'incomplete'\n" +
"and (u.is_voided = false or u.is_voided isnull)\n" +
"and u.organisation_id notnull\n" +
Expand Down Expand Up @@ -122,7 +126,7 @@ public List<AggregateReportResult> generateUserDeviceModels(String orgSchemaName
"from ${schemaName}.users u\n" +
" join\n" +
" (select user_id,\n" +
" coalesce(device_info ->> 'brand', device_name) as device_model,\n" +
" device_name as device_model,\n" +
" row_number() over (partition by user_id order by sync_start_time desc ) as rn\n" +
" from ${schemaName}.sync_telemetry) l on l.user_id = u.id and rn = 1\n" +
"where (u.is_voided = false or u.is_voided isnull) and u.organisation_id notnull \n" +
Expand All @@ -143,7 +147,7 @@ public List<UserActivityDTO> generateUserDetails(String orgSchemaName, String us
" join\n" +
" (select user_id,\n" +
" app_version,\n" +
" coalesce(device_info ->> 'brand', device_name) as device_model,\n" +
" device_name as device_model,\n" +
" sync_start_time,\n" +
" row_number() over (partition by user_id order by sync_start_time desc ) as rn\n" +
" from ${schemaName}.sync_telemetry\n" +
Expand All @@ -157,4 +161,78 @@ public List<UserActivityDTO> generateUserDetails(String orgSchemaName, String us
.replace("${userWhere}", userWhere);
return jdbcTemplate.query(query, new UserDetailsMapper());
}

public List<AggregateReportResult> generateCompletedVisitsOnTimeByProportion(String proportionCondition, String orgSchemaName, String encounterWhere, String userWhere) {
String baseQuery = "with program_enc_data as (\n" +
" select last_modified_by_id,\n" +
" count(*) filter ( where encounter_date_time <= max_visit_date_time ) visits_done_on_time,\n" +
" count(*) filter ( where encounter_date_time notnull and earliest_visit_date_time notnull ) total_scheduled\n" +
" from program_encounter\n" +
" where is_voided = false\n" +
" ${encounterWhere}\n" +
" group by last_modified_by_id\n" +
"),\n" +
" general_enc_data as (\n" +
" select last_modified_by_id,\n" +
" count(*) filter ( where encounter_date_time <= max_visit_date_time ) visits_done_on_time,\n" +
" count(*)\n" +
" filter ( where encounter_date_time notnull and earliest_visit_date_time notnull ) total_scheduled\n" +
" from encounter\n" +
" where is_voided = false\n" +
" ${encounterWhere}\n" +
" group by last_modified_by_id\n" +
" )\n" +
"select coalesce(u.name, u.username) as indicator,\n" +
" coalesce(ged.visits_done_on_time, 0) + coalesce(ped.visits_done_on_time, 0) as count\n" +
"from users u\n" +
" join general_enc_data ged on ged.last_modified_by_id = u.id\n" +
" join program_enc_data ped on ped.last_modified_by_id = u.id\n" +
"where u.organisation_id notnull\n" +
" and is_voided = false\n" +
" and coalesce(ged.visits_done_on_time, 0) + coalesce(ped.visits_done_on_time, 0) > 0\n" +
" ${userWhere}\n" +
" and ((coalesce(ged.visits_done_on_time, 0.0) + coalesce(ped.visits_done_on_time, 0.0)) /\n" +
" nullif((coalesce(ged.total_scheduled, 0) + coalesce(ped.total_scheduled, 0)), 0)) ${proportion_condition}\n";
String query = baseQuery
.replace("${proportion_condition}", proportionCondition)
.replace("${schemaName}", orgSchemaName)
.replace("${encounterWhere}", encounterWhere)
.replace("${userWhere}", userWhere);
return jdbcTemplate.query(query, new AggregateReportMapper());
}

public List<AggregateReportResult> generateUserCancellingMostVisits(String orgSchemaName, String encounterWhere, String userWhere) {
String baseQuery = "with program_enc_data as (\n" +
" select last_modified_by_id,\n" +
" count(*) filter ( where cancel_date_time notnull ) cancelled_visits\n" +
" from program_encounter\n" +
" where is_voided = false\n" +
" ${encounterWhere}\n" +
" group by last_modified_by_id\n" +
"),\n" +
" general_enc_data as (\n" +
" select last_modified_by_id,\n" +
" count(*) filter ( where cancel_date_time notnull ) cancelled_visits\n" +
" from encounter\n" +
" where is_voided = false\n" +
" ${encounterWhere}\n" +
" group by last_modified_by_id\n" +
" )\n" +
"select coalesce(u.name, u.username) as indicator,\n" +
" coalesce(ged.cancelled_visits, 0) + coalesce(ped.cancelled_visits, 0) as count\n" +
"from users u\n" +
" join general_enc_data ged on ged.last_modified_by_id = u.id\n" +
" join program_enc_data ped on ped.last_modified_by_id = u.id\n" +
"where u.organisation_id notnull\n" +
" and is_voided = false\n" +
" and coalesce(ged.cancelled_visits, 0) + coalesce(ped.cancelled_visits, 0) > 0 \n" +
" ${userWhere}\n" +
"order by coalesce(ged.cancelled_visits, 0.0) + coalesce(ped.cancelled_visits, 0.0) desc\n" +
"limit 5;";
String query = baseQuery
.replace("${schemaName}", orgSchemaName)
.replace("${encounterWhere}", encounterWhere)
.replace("${userWhere}", userWhere);
return jdbcTemplate.query(query, new AggregateReportMapper());
}
}

0 comments on commit 81be492

Please sign in to comment.