From 61994bafb06049415c4c7e12cd9c49a012577425 Mon Sep 17 00:00:00 2001 From: chaos Date: Sat, 9 Nov 2024 15:36:49 +0000 Subject: [PATCH] update --- tool/journalUtils.jq | 32 +++++++++++++++++++++++++++----- tool/lib/journalLib.jq | 20 +++++++++++++------- tool/lib/numberLib.jq | 3 +++ tool/main.jq | 31 +++++++++---------------------- 4 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 tool/lib/numberLib.jq diff --git a/tool/journalUtils.jq b/tool/journalUtils.jq index 5233e2f..a5adec3 100644 --- a/tool/journalUtils.jq +++ b/tool/journalUtils.jq @@ -2,6 +2,7 @@ include "dropins"; import "lib/utilsLib" as utilsLib; import "lib/stringLib" as stringLib; +import "lib/numberLib" as numberLib; import "lib/journalLib" as journalLib; def formatExperienceTitle: @@ -10,15 +11,21 @@ def formatExperienceTitle: def formatIngestionDose($customUnits): . as $ingestion | - . | journalLib::ingestionDose($customUnits) as $dose | - . | journalLib::ingestionUnit($customUnits) as $unit | + (. | journalLib::ingestionDose($customUnits)) as $dose | + (. | journalLib::ingestionUnit($customUnits)) as $unit | + (. | journalLib::ingestionStandardDeviation($customUnits)) as $standardDeviation | $customUnits | map(select(.id == $ingestion.customUnitId))[0] as $customUnit | if $ingestion.dose == null then "Unknown" elif $customUnit == null then - "\($dose) \($unit)" + if $ingestion.isDoseAnEstimate then "~" else "" end + + "\($dose | numberLib::round(2))" + + if $standardDeviation != null then + "±\($standardDeviation)" + else "" end + + " \($unit)" else - "\($dose) \($unit) (\($ingestion.dose) \($unit) * \($customUnit.dose) \($customUnit.unit))" + "\($dose) \($unit) (\($customUnit.dose) \($unit) * \($ingestion.dose) \($customUnit.unit))" end; def formatIngestionTime: @@ -48,4 +55,19 @@ def formatIngestionROA($customUnits; $substitutions): "\($roaText) (\($customUnit.name))" end; -def formatIngestionROA($customUnits): formatIngestionROA($customUnits; {}); \ No newline at end of file +def formatIngestionROA($customUnits): formatIngestionROA($customUnits; {}); + +def formatIngestionInfo: + . as $ingestionInfo | + if $ingestionInfo.dose == null then + "Unknown \($ingestionInfo.unit)" + else + if $ingestionInfo.isEstimate then "~" else "" end + + "\($ingestionInfo.dose * 100 | round / 100)" + + if $ingestionInfo.standardDeviation != null then + "±\($ingestionInfo.standardDeviation)" + else "" end + + + if $ingestionInfo.isUnknown then "+ Unknown" else "" end + + " \($ingestionInfo.unit)" + end; \ No newline at end of file diff --git a/tool/lib/journalLib.jq b/tool/lib/journalLib.jq index 3aec814..c46aa90 100644 --- a/tool/lib/journalLib.jq +++ b/tool/lib/journalLib.jq @@ -1,4 +1,5 @@ import "utilsLib" as utilsLib; +import "numberLib" as numberLib; import "journalTypes" as journalTypes; def ingestionDose($customUnits): @@ -19,7 +20,7 @@ def addStandardDeviations($expectationX; $standardDeviationX; $expectationY; $st (pow($expectationX; 2) * pow($expectationY; 2)) as $expectations | ($sumX * $sumY - $expectations) as $productVariance | if $productVariance > 0.0000001 then - $productVariance | sqrt + $productVariance | sqrt | numberLib::round(2) else null end; @@ -38,7 +39,7 @@ def ingestionStandardDeviation($customUnits): addStandardDeviations($expectationX; $standardDeviationX; $expectationY; $standardDeviationY) else - .standardDeviation + .estimatedDoseStandardDeviation end; def ingestionUnit($customUnits): @@ -58,15 +59,20 @@ def ingestionConsumerName: def filterIngestions($substanceFilter; $consumerFilter): . as $ingestions | + ($substanceFilter // []) as $substanceFilter | + (($substanceFilter | length) > 0) as $shouldFilterBySubstance | + ($consumerFilter // []) as $consumerFilter | + (($consumerFilter | length) > 0) as $shouldFilterByConsumer | + journalTypes::ensureIngestions | - if (($substanceFilter // [] | length) > 0) then + if $shouldFilterBySubstance or $shouldFilterByConsumer then [ $ingestions[] as $ingestion | if - ([$substanceFilter[] | . == $ingestion.substanceName] | any) + (if $shouldFilterBySubstance then ([$substanceFilter[] | . == $ingestion.substanceName] | any) else true end and - ([$consumerFilter[] | . == ($ingestion | ingestionConsumerName)] | any) + if $shouldFilterByConsumer then ([$consumerFilter[] | . == ($ingestion | ingestionConsumerName)] | any) else true end) then $ingestion else null end ] | map(select(. != null)) @@ -90,9 +96,9 @@ def ingestionsByConsumer: $consumerNames[] as $consumerName | { key: $consumerName, - value: [$ingestions | map(select( + value: $ingestions | map(select( . | ingestionConsumerName == $consumerName - ))], + )), } ] | from_entries; diff --git a/tool/lib/numberLib.jq b/tool/lib/numberLib.jq new file mode 100644 index 0000000..0fd64e5 --- /dev/null +++ b/tool/lib/numberLib.jq @@ -0,0 +1,3 @@ +def round(precision): + . * pow(10; precision) | round / pow(10; precision); + \ No newline at end of file diff --git a/tool/main.jq b/tool/main.jq index bc2999a..fd2c332 100644 --- a/tool/main.jq +++ b/tool/main.jq @@ -16,10 +16,11 @@ def printExperienceStats($stats; $substanceFilter; $consumerFilter; $withTitle): ($consumerFilter // ["default"]) as $consumerFilter | $experience.ingestions | - journalLib::filterIngestions($substanceFilter; $consumerFilter) as $ingestions | - ($ingestions | journalLib::ingestionsByConsumer) as $ingestionsByConsumer | + journalLib::filterIngestions($substanceFilter; $consumerFilter) | + sort_by(.sortDate) as $ingestions | - ($ingestionsByConsumer | keys) as $consumerNames | + ($ingestions | journalLib::ingestionsSubstanceNames) as $substanceNames | + ($ingestions | journalLib::ingestionsByConsumer) as $ingestionsByConsumer | "" as $experienceStatsText | $experienceStatsText | @@ -27,6 +28,7 @@ def printExperienceStats($stats; $substanceFilter; $consumerFilter; $withTitle): . += ($experience | journalUtils::formatExperienceTitle | . + "\n") end | . as $experienceStatsText | + ($ingestionsByConsumer | keys) as $consumerNames | reduce $consumerNames[] as $consumerName ($experienceStatsText; . as $experienceStatsText | $experienceStatsText | @@ -34,8 +36,7 @@ def printExperienceStats($stats; $substanceFilter; $consumerFilter; $withTitle): then . += "Consumer: \($consumerName)\n" end | . as $experienceStatsText | - ($stats.[$consumerName] | keys) as $substanceNames | - + ($ingestionsByConsumer[$consumerName] | journalLib::ingestionsSubstanceNames) as $consumerSubstanceNames | $experienceStatsText | reduce $substanceNames[] as $substanceName (.; . as $experienceStatsText | @@ -51,22 +52,8 @@ def printExperienceStats($stats; $substanceFilter; $consumerFilter; $withTitle): .key as $ingestionMethod | .value as $ingestionInfo | - def formatIngestionInfo: - if $ingestionInfo.dose == null then - "Unknown \($ingestionInfo.unit)" - else - if $ingestionInfo.isEstimate then "~" else "" end + - "\($ingestionInfo.dose * 100 | round / 100)" + - if $ingestionInfo.standardDeviation != null then - "±\($ingestionInfo.standardDeviation)" - else "" end + - - if $ingestionInfo.isUnknown then "+ Unknown" else "" end + - " \($ingestionInfo.unit)" - end; - $experienceStatsText | - . += "Dose (\($ingestionMethod | stringLib::titleCase)): \($ingestionInfo | formatIngestionInfo)\n" | + . += "Dose (\($ingestionMethod | stringLib::titleCase)): \($ingestionInfo | journalUtils::formatIngestionInfo)\n" | . as $experienceStatsText | $experienceStatsText @@ -86,8 +73,8 @@ def printExperienceLog($customUnits; $substanceFilter; $consumerFilter; $pretty; ($consumerFilter // ["default"]) as $consumerFilter | $experience.ingestions | - sort_by(.sortDate) | - journalLib::filterIngestions($substanceFilter; $consumerFilter) as $ingestions | + journalLib::filterIngestions($substanceFilter; $consumerFilter) | + sort_by(.sortDate) as $ingestions | $ingestions | journalLib::ingestionsConsumerNames as $consumerNames |