avniproject / avni-models

Avni data model to be used by front end clients
https://avniproject.org
GNU Affero General Public License v3.0
1 stars 4 forks source link

Add support in rules-server to handle question group concepts - both repeatable and non-repeatable #43

Closed mahalakshme closed 1 year ago

mahalakshme commented 1 year ago

Issue: For question group concepts, model functions doesn't execute as expected in message rule

Ticket: https://avni.freshdesk.com/a/tickets/2518 - workaround for this issue was done by using the model function in decision rule here

Steps to reproduce:

Expectation: let dateTime = programEncounter.getObservationValue("Date of Attendance", "Attendance Day"); and

const groupedObservations = programEncounter.findGroupedObservation("Attendance Day");
const groupObservations = _.isEmpty(groupedObservations) ? [] : groupedObservations[0].groupObservations;
  const dateOfAttendance =  _.find(groupObservations, (observation) => {
  return (observation.concept.name === "Date of Attendance");
  });

in message rule should return date as expected.

Analysis: Printing the observations in decision rule vs in message rule:

decision_rule.png

message_rule.png

looks like observations when sent from server to rule-server to execute message rule is not handled for group observations but in client since the data is accessible it works in decision rule.

vinayvenu commented 1 year ago

@mahalakshme can you add the Freshdesk ticket related to this?

himeshr commented 1 year ago

During testing, found that, during messageRule execution, we are now capable of extracting observation from a repeatableQuestionGroup. But, in-order to test ability to fetch QuestionGroup observation during MessageRule execution, i created a new Encounter with messageRule and Schedule rule. Here, i'm unable to get the ScheduleRule to execute successfully.

Avni-server error log:

2023-08-11 14:09:27.955 ERROR 2382 --- [http-nio-8021-exec-12] o.a.messaging.service.MessagingService : Missing Schedule dateTime in message rule template Inter_att_single

Schedule rule:

'use strict'; ({params, imports}) => { const programEncounter = params.entity; var scheduledDate = programEncounter.encounterDateTime; console.log('enrolment====>',programEncounter.enrolment); return {scheduledDateTime: scheduledDate} };

Rules_server error observed during encounter save:

0|rules-server | Headers from req: maha@lahi eyJraWQiOiJqQlZHUU9sTzVKaGIyUWJ1R3NvZlZwTWlEdjFmTlh3bmFscU1xTXo2ZitnPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiJhZWY4MTA0NC04YjY4LTQyMjItODAxOS00NGJlYWYzMGE0OTgiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLmFwLXNvdXRoLTEuYW1hem9uYXdzLmNvbVwvYXAtc291dGgtMV9EVTI3QUhKdloiLCJwaG9uZV9udW1iZXJfdmVyaWZpZWQiOnRydWUsImNvZ25pdG86dXNlcm5hbWUiOiJtYWhhQGxhaGkiLCJjdXN0b206dXNlclVVSUQiOiIwZTY4ZmVjNS0zNGM0LTQ4ZTYtOWEzMC05NDgyNTJlNmViZmUiLCJhdWQiOiIxZDZyZ3ZpdGpzZm9vbmxrYm0wN3VpdmdtZyIsImV2ZW50X2lkIjoiMTI0MjU3ODgtMjg3MC00MmJlLTk3NDQtODVlNTZlYjc4M2E4IiwidG9rZW5fdXNlIjoiaWQiLCJhdXRoX3RpbWUiOjE2OTE3NTczMTAsInBob25lX251bWJlciI6Iis5MTk3OTEyMTcwNzgiLCJleHAiOjE2OTE3NjQ2NDUsImlhdCI6MTY5MTc2MTA0NSwiZW1haWwiOiJtYWhhQG1haWwuY29tIn0.eens6-ys7rqtd0mJo-VfR1043ZFM5GhKdLvi9tKLTeSW14xCdjOqDZEca11ABUzh-dnHzTrcKoM5WqILe7gcestl3chq-xWcz9NwFbseGTcL4JUByJQ4yLF0p5PBUvojd8YRIDcwsUtljAhW4CZrCVNsu2PGS1Z9WVR_Qks72esSzmstBsGTQPfb3aG6hFyO9j8f9DNH_qeSPiv3GmK3eA3GaCvhe8pydhkKZcZnuofs_UzL9xt4rvM8H1bAY9n7AQtUfhUJvAtPc4ttyRirPUJI60lv_6tXcWYLpwXAagcTBtLi4C594fZkHoAr31oHa2B7BXY-KHyJX6Vnqrd06Q 28dac619-c6b5-4e57-9bf9-58acd7da18e0 0|rules-server | TypeError: Cannot read property 'name' of undefined 0|rules-server | at Function.fromResource (/opt/rules-server/node_modules/openchs-models/dist/Concept.js:121:36) 0|rules-server | at Function.constructConceptModel (/opt/rules-server/node_modules/openchs-models/dist/observation/QuestionGroup.js:63:38) 0|rules-server | at concept.answers._lodash.default.map.answer (/opt/rules-server/node_modules/openchs-models/dist/observation/QuestionGroup.js:71:47) 0|rules-server | at arrayMap (/opt/rules-server/node_modules/lodash/lodash.js:639:23) 0|rules-server | at Function.map (/opt/rules-server/node_modules/lodash/lodash.js:9554:14) 0|rules-server | at Function.constructConceptModel (/opt/rules-server/node_modules/openchs-models/dist/observation/QuestionGroup.js:68:41) 0|rules-server | at _lodash.default.map (/opt/rules-server/node_modules/openchs-models/dist/observation/QuestionGroup.js:52:43) 0|rules-server | at arrayMap (/opt/rules-server/node_modules/lodash/lodash.js:639:23) 0|rules-server | at Function.map (/opt/rules-server/node_modules/lodash/lodash.js:9554:14)

Testing org details

Org : Lend a Hand India Env: pre-release Encounters: Single Question Group: https://prerelease.avniproject.org/#/appDesigner/encounterType/2118/show Repeatable Question Group: https://prerelease.avniproject.org/#/appDesigner/encounterType/1814/show

himeshr commented 1 year ago

Second Error observed while executing message rule is:

Rules-server logs:

0|rules-server | Headers from req: saeed@lahi undefined 28dac619-c6b5-4e57-9bf9-58acd7da18e0 0|rules-server | HR>>>>>>dateOfAttendance 2023-04-25T18:30:00.000Z 0|rules-server | HR>>>>>>result { parameters: 0|rules-server | [ '**test', 0|rules-server | 'Internship LAHI', 0|rules-server | 'Apr 26, 2023', 0|rules-server | 'Karigar - School of Applied Learning' ] }

Avni-server error:

2023-08-11 14:19:52.238 ERROR 2382 --- [pool-5-thread-1] o.a.messaging.external.GlificRestClient : Error while calling Glific API: /api 2023-08-11 14:19:52.238 ERROR 2382 --- [pool-5-thread-1] o.a.messaging.external.GlificRestClient : Request is {"query":"mutation sendHsmMessage($templateId: ID!, $receiverId: ID!, $parameters: [String]) {sendHsmMessage(templateId: $templateId, receiverId: $receiverId, parameters: $parameters) {message{id body isHsm } errors {key message } } }","variables":{"templateId":61881,"receiverId":907895,"parameters":[]}} 2023-08-11 14:19:52.238 ERROR 2382 --- [pool-5-thread-1] o.a.messaging.external.GlificRestClient : Response is GlificResponse{data=org.avni.messaging.contract.glific.GlificMessageResponse@413e5868, errors=[GlificError{locations=[GlificErrorLocation{column=85, row=0}], message='Please provide the right number of parameters for the template.'}]} 2023-08-11 14:19:52.238 ERROR 2382 --- [pool-5-thread-1] o.a.messaging.service.MessagingService : Could not send message for message request id: 167

org.avni.messaging.domain.exception.GlificConnectException: Please provide the right number of parameters for the template. at org.avni.messaging.external.GlificRestClient.makeCall(GlificRestClient.java:78) at org.avni.messaging.external.GlificRestClient.callAPI(GlificRestClient.java:62) at org.avni.messaging.external.GlificRestClient.callAPI(GlificRestClient.java:51) at org.avni.messaging.repository.GlificMessageRepository.sendMessageToContact(GlificMessageRepository.java:23) at org.avni.messaging.repository.GlificMessageRepository$$FastClassBySpringCGLIB$$2dad3d0f.invoke() at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) at org.avni.messaging.repository.GlificMessageRepository$$EnhancerBySpringCGLIB$$2ef15fc6.sendMessageToContact() at org.avni.messaging.service.IndividualMessagingService.ensureExternalIdPresenceAndSendMessage(IndividualMessagingService.java:42) at org.avni.messaging.service.IndividualMessagingService.sendAutomatedMessage(IndividualMessagingService.java:49) "/var/log/openchs/chs.log" [readonly] 26846L, 2695912B 19993,1 74% at org.keycloak.adapters.tomcat.AbstractKeycloakAuthenticatorValve.invoke(AbstractKeycloakAuthenticatorValve.java:181) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) at org.keycloak.adapters.tomcat.AbstractKeycloakAuthenticatorValve.invoke(AbstractKeycloakAuthenticatorValve.java:181) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:677) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:750)

mahalakshme commented 1 year ago

@himeshr Single Question group had an issue with message rule: we were doing getValue on the value returned by getObservationValue. After removing getValue() it started working. Removed it since getObservationValue will already do getValue() internally.

repeatable question group is working without me making any changes. I just deleted all the requests whose status is anything other than 'Sent' to clear up the logs from unnecessary exceptions.

mahalakshme commented 1 year ago

@himeshr tested the code with schedule rule having multi-line JS - the code that was there before - it is working.

himeshr commented 1 year ago

Moved the card back to QA Failed, as we are still experiencing the error:

2023-08-17 04:49:14.711 ERROR 19538 --- [http-nio-8021-exec-4] o.a.messaging.service.MessagingService : Missing Schedule dateTime in message rule template Inter_att_single

Due to which Msg isnt getting scheduled for being sent out.

mahalakshme commented 1 year ago

Need to review this commit alone in avni-server: 3400d2 cc: @himeshr

mahalakshme commented 1 year ago

cherry-picked the commit from master to 4.0 branch in avni-server

mahalakshme commented 1 year ago

issue raised by himesh was happening since single/multi select element values were not suppoerted in QG/RQG

himeshr commented 1 year ago

During testing, i'm unable to save messageRequests due to following exception for both repeatable and non-repeatable question groups:

saveMessageRequest.txt

Tested on pre-release environment with 4.1 server and client.

mahalakshme commented 1 year ago

Need to review this commit alone: da74685dce2a8f146b4d059033de0286312426b7 in server.

The issue was because coded concepts computed via decision rule, are saved in an array. But normally they are not saved as array when singleselect form element used. So made the check in the above commit specific to QuestionGroup, so that if any other concept has array values, it will not get affected.

himeshr commented 1 year ago

Validated that it works for I. Program-encounter with a. repeatable question-group with single code-concept b. non-repeatable question-group with single code-concept II. General encounter with a. non-repeatable question-group with Single as well as Multi code-concepts

mahalakshme commented 1 year ago

fix and testing were done in 4.1 by mistake and hence this ticket: cherrypick done to 4.0 branch.