Skip to content

Commit

Permalink
1381 - removed rule input from state as it is calculated value. in ca…
Browse files Browse the repository at this point in the history
…se of any error during cache reading reset the cache.
  • Loading branch information
petmongrels committed May 30, 2024
1 parent 40e87f4 commit fe9253e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/openchs-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"lodash": "4.17.21",
"moment": "2.29.4",
"native-base": "3.4.9",
"openchs-models": "1.31.78",
"openchs-models": "1.31.79",
"prop-types": "15.8.1",
"react": "18.2.0",
"react-native": "0.72.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import ReportCardService from "../../service/customDashboard/ReportCardService";
import General from "../../utility/General";
import DashboardFilterService from "../../service/reports/DashboardFilterService";
import CustomDashboardCacheService from '../../service/CustomDashboardCacheService';
import CryptoUtils from '../../utility/CryptoUtils';
import MessageService from '../../service/MessageService';
import dashboardCacheService from "../../service/DashboardCacheService";

function loadCurrentDashboardInfo(context, newState) {
const dashboardFilterService = context.get(DashboardFilterService);
Expand All @@ -30,7 +30,6 @@ class CustomDashboardActions {
cardToCountResultMap: {},
countUpdateTime: null,
hasFilters: false,
ruleInput: null,
activeDashboardUUID: null,
customDashboardFilters: []
};
Expand All @@ -42,6 +41,9 @@ class CustomDashboardActions {
const dashboards = dashboardService.getDashboards(action.customDashboardType);
newState.dashboards = dashboards;
newState.activeDashboardUUID = _.get(_.head(dashboards), 'uuid');

// context.get(CustomDashboardCacheService).clearAllCache();

return loadCurrentDashboardInfo(context, newState);
}

Expand All @@ -61,16 +63,19 @@ class CustomDashboardActions {
const itemKey = action.reportCardUUID;
const rcUUID = context.get(ReportCardService).getPlainUUIDFromCompositeReportCardUUID(action.reportCardUUID);
const reportCard = context.get(EntityService).findByUUID(rcUUID, ReportCard.schema.name);
const {selectedFilterValues} = context.get(CustomDashboardCacheService).getDashboardCache(state.activeDashboardUUID);
const ruleInputArray = context.get(DashboardFilterService).toRuleInputObjects(state.activeDashboardUUID, selectedFilterValues);

reportCard.itemKey = itemKey;
if (reportCard.isStandardTaskType()) {
action.goToTaskLists(reportCard.standardReportCardType.getTaskTypeType(), state.ruleInput.ruleInputArray);
action.goToTaskLists(reportCard.standardReportCardType.getTaskTypeType(), ruleInputArray);
} else {
const {result, status} = context.get(ReportCardService).getReportCardResult(reportCard, state.ruleInput.ruleInputArray);
const {result, status} = context.get(ReportCardService).getReportCardResult(reportCard, ruleInputArray);
const standardReportCardType = reportCard.standardReportCardType;
const viewName = CustomDashboardActions._getViewName(standardReportCardType);
if (!_.isNil(result)) {
setTimeout(() => action.onCustomRecordCardResults(result, status, viewName,
standardReportCardType && standardReportCardType.getApprovalStatusForType(), state.ruleInput.ruleInputArray, reportCard), 0);
standardReportCardType && standardReportCardType.getApprovalStatusForType(), ruleInputArray, reportCard), 0);
}
}
return newState;
Expand All @@ -94,14 +99,17 @@ class CustomDashboardActions {
}

static refreshCount(state, action, context) {
const {dashboardCache, selectedFilterValues} = context.get(CustomDashboardCacheService).getDashboardCache(state.activeDashboardUUID);

const I18n = context.get(MessageService).getI18n();
const reportCardSectionMappings = state.reportCardSectionMappings;
const newState = {...state};
newState.ruleInput = action.filterApplied ? action.ruleInput : newState.ruleInput;

newState.countUpdateTime = new Date(); //Update this to ensure reportCard count change is reflected
const ruleInputArray = context.get(DashboardFilterService).toRuleInputObjects(state.activeDashboardUUID, selectedFilterValues);
reportCardSectionMappings.forEach(rcm => {
const start = new Date();
const countQueryResponse = context.get(ReportCardService).getReportCardCount(rcm.card, newState.ruleInput.ruleInputArray);
const countQueryResponse = context.get(ReportCardService).getReportCardCount(rcm.card, ruleInputArray);
if (rcm.card.nested) {
if (countQueryResponse && countQueryResponse.length === rcm.card.countOfCards) {
_.forEach(countQueryResponse, (reportCard, index) => {
Expand Down
27 changes: 16 additions & 11 deletions packages/openchs-android/src/action/mydashboard/FiltersActionsV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ArrayUtil, Concept, CustomFilter, ModelGeneral} from 'openchs-models';
import CustomDashboardCacheService from '../../service/CustomDashboardCacheService';

import General from "../../utility/General";
import {JSONStringify} from "../../utility/JsonStringify";

class FiltersActionsV2 {
static getInitialState() {
Expand Down Expand Up @@ -89,27 +90,31 @@ class FiltersActionsV2 {
const {filterConfigs, selectedValues} = state;
const {navigateToDashboardView, setFiltersDataOnDashboardView} = action;
const newState = {...state, filterApplied: true, filterErrors: {}};
const filledFilterValues = _.filter(Object.entries(selectedValues), ([, filterValue]) => !ModelGeneral.isDeepEmpty(filterValue));
//Check if there are errors in filter values specified
filledFilterValues.forEach(([filterUUID, filterValue]) => {
const [success, message] = filterConfigs[filterUUID].validate && filterConfigs[filterUUID].validate(filterValue) || [false, `validate for filterConfig ${filterUUID} not found`];
if (!success)
newState.filterErrors[filterUUID] = message;
const filledFilterValues = {};
Object.keys(selectedValues).forEach((filterUUID) => {
const filterValue = selectedValues[filterUUID];
if (!ModelGeneral.isDeepEmpty(filterValue)) {
filledFilterValues[filterUUID] = filterValue;

const [success, message] = filterConfigs[filterUUID].validate && filterConfigs[filterUUID].validate(filterValue) || [false, `validate for filterConfig ${filterUUID} not found`];
if (!success)
newState.filterErrors[filterUUID] = message;
}
});
//Check if there are errors in filter values specified
if (Object.keys(newState.filterErrors).length > 0) {
newState.filterApplied = false;
newState.loading = false;
return newState;
}
const dashboardFilterService = context.get(DashboardFilterService);
const ruleInputArray = filledFilterValues
.map(([filterUUID, filterValue]) => dashboardFilterService.toRuleInputObject(filterConfigs[filterUUID], filterValue));

const customDashboardCacheService = context.get(CustomDashboardCacheService);
customDashboardCacheService.setSelectedFilterValues(newState.dashboardUUID, selectedValues, true);
customDashboardCacheService.setSelectedFilterValues(newState.dashboardUUID, filledFilterValues, true);

//Invoke callbacks. Used only in test.
// setFiltersDataOnDashboardView(serializableFilterData);
const dashboardFilterService = context.get(DashboardFilterService);
const ruleInputArray = dashboardFilterService.toRuleInputObjects(newState.dashboardUUID, filledFilterValues);
General.logDebugTemp("FiltersActionsV2", `ruleInputArray: ${JSONStringify(ruleInputArray)}`);
navigateToDashboardView(ruleInputArray);
return newState;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/openchs-android/src/service/BaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class BaseService {
return this.getReturnValue(entities);
}

findByFiltered(filter, value, schema = this.getSchema()) {
return this.getReturnValue(this.findAll(schema).filtered(`${filter} = '${value}'`));
}

getReturnValue(entities) {
if (entities.length === 0) return undefined;
if (entities.length === 1) return entities[0];
Expand Down
33 changes: 21 additions & 12 deletions packages/openchs-android/src/service/CustomDashboardCacheService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import Service from "../framework/bean/Service";
import {CustomDashboardCache} from "avni-models";
import _ from "lodash";
import EntityService from "./EntityService";
import {AddressLevel, Concept, CustomFilter, EncounterType, Gender, Individual, Program, SubjectType} from "openchs-models";
import {AddressLevel, Concept, CustomFilter, Dashboard, EncounterType, Gender, Individual, Program, SubjectType} from "openchs-models";
import DashboardFilterService from "./reports/DashboardFilterService";
import General from "../utility/General";

const dataTypeDetails = new Map();
dataTypeDetails.set(Concept.dataType.Coded, {type: Concept, isArray: true});
Expand All @@ -13,11 +14,12 @@ dataTypeDetails.set(CustomFilter.type.Address, {type: AddressLevel, isArray: tru
dataTypeDetails.set(CustomFilter.type.GroupSubject, {type: Individual, isArray: false});

function getDashboardCache(service, dashboardUUID) {
let cache = service.findByKey("dashboard.uuid", dashboardUUID, CustomDashboardCache.schema.name);
let cache = service.findByFiltered("dashboard.uuid", dashboardUUID, CustomDashboardCache.schema.name);
if (_.isNil(cache)) {
cache = service.save(CustomDashboardCache.newInstance(), CustomDashboardCache.schema.name);
const dashboard = service.findByUUID(dashboardUUID, Dashboard.schema.name);
cache = service.save(CustomDashboardCache.newInstance(dashboard), CustomDashboardCache.schema.name);
}
return cache;
return cache.clone();
}

@Service('customDashboardCacheService')
Expand All @@ -30,12 +32,18 @@ class CustomDashboardCacheService extends BaseService {
return CustomDashboardCache.schema.name;
}

clearAllCache() {
this.db.write(() => {
this.db.delete(this.db.objects(CustomDashboardCache.schema.name));
});
}

getDashboardCache(dashboardUUID) {
const entityService = this.getService(EntityService);
const dashboardCache = getDashboardCache(this, dashboardUUID);
try {
const selectedSerialisedValues = dashboardCache.getSelectedValues();
const dashboardFilterService = this.getService(DashboardFilterService);
const entityService = this.getService(EntityService);

const selectedFilterValues = {};
Object.keys(selectedSerialisedValues).forEach((filterUuid) => {
Expand All @@ -60,22 +68,23 @@ class CustomDashboardCacheService extends BaseService {
});
return {selectedFilterValues, dashboardCache};
} catch (e) {
General.logError("CustomDashboardCacheService", e);
dashboardCache.reset();
this.save(dashboardCache, CustomDashboardCache.schema.name);
this.saveOrUpdate(dashboardCache, CustomDashboardCache.schema.name);
return {selectedFilterValues: {}, dashboardCache};
}
}

reset(dashboardUUID) {
const cache = getDashboardCache(this, dashboardUUID);
cache.reset();
this.save(cache);
this.saveOrUpdate(cache);
}

setSelectedFilterValues(dashboardUUID, selectedFilterValues, filterApplied) {
const cachedData = this.findByUUID(dashboardUUID);
cachedData.filterApplied = filterApplied;
cachedData.updatedAt = new Date();
const dashboardCache = getDashboardCache(this, dashboardUUID);
dashboardCache.filterApplied = filterApplied;
dashboardCache.updatedAt = new Date();

const dashboardFilterService = this.getService(DashboardFilterService);
const serialisedSelectedValues = {};
Expand All @@ -102,8 +111,8 @@ class CustomDashboardCacheService extends BaseService {
}
});

cachedData.selectedValuesJSON = JSON.stringify(serialisedSelectedValues);
this.save(cachedData);
dashboardCache.selectedValuesJSON = JSON.stringify(serialisedSelectedValues);
this.saveOrUpdate(dashboardCache);
}

resetCache(dashboardUUID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class CustomDashboardService extends BaseService {
}

getDashboards(customDashboardType) {
General.logDebugTemp("CustomDashboardService", customDashboardType);
switch (customDashboardType) {
case CustomDashboardType.Primary:
return [this.getOnePrimaryDashboard()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class DashboardFilterService extends BaseService {
return filterConfig;
}

toRuleInputObjects(dashboardUUID, selectedFilterValues) {
const filterConfigs = this.getFilterConfigsForDashboard(dashboardUUID);
const thisService = this;
return Object.entries(selectedFilterValues)
.map(([filterUUID, filterValue]) => thisService.toRuleInputObject(filterConfigs[filterUUID], filterValue));
}

toRuleInputObject(filterConfig, filterValue) {
const ruleInput = new DashboardReportFilter();
ruleInput.type = filterConfig.type;
Expand Down

This file was deleted.

0 comments on commit fe9253e

Please sign in to comment.