Skip to content

Commit

Permalink
Merge pull request #2754 from Gizra/issue-2740
Browse files Browse the repository at this point in the history
Resolve 'Allowed memory size exhausted' error
  • Loading branch information
anvmn authored Aug 29, 2021
2 parents 8191199 + 8f604c9 commit 6e23414
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 323 deletions.
6 changes: 5 additions & 1 deletion client/src/elm/App/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,11 @@ update msg model =
( [ TryPinCode code ], [] )

Pages.PinCode.Model.Logout ->
( [ SetLoggedIn NotAsked, SetHealthCenter Nothing, SetVillage Nothing ]
( [ SetLoggedIn NotAsked
, MsgIndexedDb Backend.Model.HandleLogout
, SetHealthCenter Nothing
, SetVillage Nothing
]
, [ cachePinCode "", cacheHealthCenter "", cacheVillage "" ]
)

Expand Down
64 changes: 32 additions & 32 deletions client/src/elm/Backend/Dashboard/Decoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Backend.Measurement.Model
)
import Backend.Person.Decoder exposing (decodeGender)
import Dict as LegacyDict
import Gizra.Json exposing (decodeInt)
import Gizra.Json exposing (decodeFloat, decodeInt)
import Gizra.NominalDate exposing (NominalDate, decodeYYYYMMDD)
import Json.Decode exposing (..)
import Json.Decode.Pipeline exposing (..)
Expand Down Expand Up @@ -89,16 +89,16 @@ decodeCaseManagement =
decodeCaseNutrition : Decoder CaseNutrition
decodeCaseNutrition =
succeed CaseNutrition
|> required "stunting" decodeNutritionValueDict
|> required "underweight" decodeNutritionValueDict
|> required "wasting" decodeNutritionValueDict
|> required "muac" decodeNutritionValueDict
|> required "nutrition_signs" decodeNutritionValueDict
|> required "stunting" (decodeNutritionValueDict decodeZScoreNutritionValue)
|> required "underweight" (decodeNutritionValueDict decodeZScoreNutritionValue)
|> required "wasting" (decodeNutritionValueDict decodeZScoreNutritionValue)
|> required "muac" (decodeNutritionValueDict decodeMuacNutritionValue)
|> required "nutrition_signs" (decodeNutritionValueDict decodeZScoreNutritionValue)


decodeNutritionValueDict : Decoder (Dict Int NutritionValue)
decodeNutritionValueDict =
dict (decodeWithFallback (NutritionValue Neutral "X") decodeNutritionValue)
decodeNutritionValueDict : Decoder NutritionValue -> Decoder (Dict Int NutritionValue)
decodeNutritionValueDict decoder =
dict (decodeWithFallback (NutritionValue Neutral "X") decoder)
|> andThen
(\dict ->
LegacyDict.toList dict
Expand All @@ -111,35 +111,35 @@ decodeNutritionValueDict =
)


decodeNutritionValue : Decoder NutritionValue
decodeNutritionValue =
succeed NutritionValue
|> required "c" decodeNutritionStatus
|> required "v" string
decodeZScoreNutritionValue : Decoder NutritionValue
decodeZScoreNutritionValue =
float
|> andThen
(\value ->
if value <= -3 then
succeed <| NutritionValue Severe (String.fromFloat value)

else if value <= -2 then
succeed <| NutritionValue Moderate (String.fromFloat value)

decodeNutritionStatus : Decoder NutritionStatus
decodeNutritionStatus =
string
|> andThen
(\s ->
case s of
"neutral" ->
succeed Neutral
else
succeed <| NutritionValue Good (String.fromFloat value)
)

"good_nutrition" ->
succeed Good

"moderate_nutrition" ->
succeed Moderate
decodeMuacNutritionValue : Decoder NutritionValue
decodeMuacNutritionValue =
decodeFloat
|> andThen
(\value ->
if value <= 11.5 then
succeed <| NutritionValue Severe (String.fromFloat value)

"severe_nutrition" ->
succeed Severe
else if value <= 12.5 then
succeed <| NutritionValue Moderate (String.fromFloat value)

_ ->
fail <|
s
++ " is not a recognized nutrition status."
else
succeed <| NutritionValue Good (String.fromFloat value)
)


Expand Down
45 changes: 16 additions & 29 deletions client/src/elm/Backend/Dashboard/Encoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -73,42 +73,29 @@ encodeCaseManagement caseManagement =

encodeCaseNutrition : CaseNutrition -> List ( String, Value )
encodeCaseNutrition caseNutrition =
[ ( "stunting", dict String.fromInt (encodeNutritionValue >> object) (dictToLegacyDict caseNutrition.stunting) )
, ( "underweight", dict String.fromInt (encodeNutritionValue >> object) (dictToLegacyDict caseNutrition.underweight) )
, ( "wasting", dict String.fromInt (encodeNutritionValue >> object) (dictToLegacyDict caseNutrition.wasting) )
, ( "muac", dict String.fromInt (encodeNutritionValue >> object) (dictToLegacyDict caseNutrition.muac) )
, ( "nutrition_signs", dict String.fromInt (encodeNutritionValue >> object) (dictToLegacyDict caseNutrition.nutritionSigns) )
[ ( "stunting", dict String.fromInt encodeNutritionValue (dictToLegacyDict caseNutrition.stunting) )
, ( "underweight", dict String.fromInt encodeNutritionValue (dictToLegacyDict caseNutrition.underweight) )
, ( "wasting", dict String.fromInt encodeNutritionValue (dictToLegacyDict caseNutrition.wasting) )
, ( "muac", dict String.fromInt encodeNutritionValue (dictToLegacyDict caseNutrition.muac) )
, ( "nutrition_signs", dict String.fromInt encodeNutritionValue (dictToLegacyDict caseNutrition.nutritionSigns) )
]


dictToLegacyDict : Dict comparable v -> LegacyDict.Dict comparable v
dictToLegacyDict dict =
Dict.toList dict
|> LegacyDict.fromList
dictToLegacyDict =
Dict.toList >> LegacyDict.fromList


encodeNutritionValue : NutritionValue -> List ( String, Value )
encodeNutritionValue : NutritionValue -> Value
encodeNutritionValue value =
[ ( "c", encodeNutritionStatus value.class )
, ( "v", string value.value )
]


encodeNutritionStatus : NutritionStatus -> Value
encodeNutritionStatus status =
string <|
case status of
Neutral ->
"neutral"

Good ->
"good_nutrition"

Moderate ->
"moderate_nutrition"

Severe ->
"severe_nutrition"
case value.class of
Neutral ->
null

_ ->
String.toFloat value.value
|> Maybe.map float
|> Maybe.withDefault null


encodeChildrenBeneficiariesData : Dict ProgramType (List ChildrenBeneficiariesStats) -> ( String, Value )
Expand Down
2 changes: 2 additions & 0 deletions client/src/elm/Backend/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ type MsgIndexedDb
| HandlePostedNutritionEncounter IndividualEncounterParticipantId (WebData ( NutritionEncounterId, NutritionEncounter ))
| HandlePostedAcuteIllnessEncounter IndividualEncounterParticipantId (WebData ( AcuteIllnessEncounterId, AcuteIllnessEncounter ))
| HandlePostedHomeVisitEncounter IndividualEncounterParticipantId (WebData ( HomeVisitEncounterId, HomeVisitEncounter ))
-- Operations we may want to perform when logout is clicked.
| HandleLogout
-- Process some revisions we've received from the backend. In some cases,
-- we can update our in-memory structures appropriately. In other cases, we
-- can set them to `NotAsked` and let the "fetch" mechanism re-fetch them.
Expand Down
19 changes: 19 additions & 0 deletions client/src/elm/Backend/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ updateIndexedDb language currentDate currentTime zscores nurseId healthCenterId
( model, Cmd.none, [] )
in
case msg of
HandleLogout ->
let
-- On logout we want to clear the statistics dashboards loaded
-- for health center.
-- We do this to maintain proper loading rutine when switching
-- between nurse and chw.
updatedComputedDashboards =
Maybe.map
(\healthCenterId_ ->
Dict.remove healthCenterId_ model.computedDashboards
)
healthCenterId
|> Maybe.withDefault model.computedDashboards
in
( { model | computedDashboards = updatedComputedDashboards }
, Cmd.none
, []
)

FetchChildMeasurements childId ->
( { model | childMeasurements = Dict.insert childId Loading model.childMeasurements }
, sw.get childMeasurementListEndpoint childId
Expand Down
Loading

0 comments on commit 6e23414

Please sign in to comment.