journal/tool/journalUtils.jq

126 lines
3.9 KiB
Plaintext
Raw Normal View History

2024-11-07 08:58:38 +00:00
include "utils";
def formatExperienceTitle:
. as $experience |
"\"\(.title)\": \(.creationDate / 1000 | strftime("%d-%m-%Y"))";
def calculateIngestionDose($customUnits):
. as $ingestion |
if .customUnitId != null then
($customUnits | map(select(.id == $ingestion.customUnitId))[0]) as $customUnit |
.dose * $customUnit.dose | . as $dose |
$dose * 100 | round / 100
else
.dose
end;
def ingestionUnit($customUnits):
. as $ingestion |
if .customUnitId != null then
($customUnits | map(select(.id == $ingestion.customUnitId))[0]) as $customUnit |
$customUnit.originalUnit
else
.units
end;
def formatIngestionDose($customUnits):
. as $ingestion |
. | calculateIngestionDose($customUnits) as $dose |
. | ingestionUnit($customUnits) as $unit |
$customUnits | map(select(.id == $ingestion.customUnitId))[0] as $customUnit |
if $ingestion.dose == null then
"Unknown"
elif $customUnit == null then
"\($dose) \($unit)"
else
"\($dose) \($unit) (\($ingestion.dose) \($unit) * \($customUnit.dose) \($customUnit.unit))"
end;
def formatIngestionTime:
. as $ingestion |
$ingestion.time / 1000 | strftime("%a %I:%M %p");
def formatIngestionROA($customUnits; $substitutions):
. as $ingestion |
$ingestion.administrationRoute as $roa |
(if
$substitutions | has($roa)
then
$substitutions.[$roa]
else
$roa | titleCase
end) as $roaText |
$ingestion.customUnitId as $customUnitId |
if
$customUnitId == null
then
$roaText
else
$customUnits | map(select(.id == $customUnitId))[0] as $customUnit |
"\($roaText) (\($customUnit.name))"
end;
def formatIngestionROA($customUnits): formatIngestionROA($customUnits; {});
def filterIngestions($substanceFilter; $consumerFilter):
. as $ingestions |
$ingestions | if (($substanceFilter // [] | length) > 0) then
(reduce .[] as $ingestion ([];
if
([$substanceFilter[] | . == $ingestion.substanceName] | any)
and
([$consumerFilter[] | . == ifNullDefault($ingestion.consumerName; "default")] | any)
then . += [$ingestion]
else . end)
)
end;
def ingestionsByConsumer:
. as $ingestions |
(reduce $ingestions[] as $ingestion ({};
ifNullDefault($ingestion.consumerName; "default") as $consumerName |
if .[$consumerName] == null then .[$consumerName] |= [] end |
.[$consumerName] += [$ingestion]
));
def ingestionsSubstanceNames:
. as $ingestions |
[$ingestions[].substanceName] | orderedUnique;
def ingestionsConsumerNames:
. as $ingestions |
[$ingestions[] as $ingestion | ifNullDefault($ingestion.consumerName; "default")] | orderedUnique;
def experienceStats($customUnits):
. as $experience |
$experience.ingestions as $ingestions |
(reduce $ingestions[] as $ingestion ({}; . as $stats |
$ingestion |
.substanceName as $name |
.administrationRoute as $administrationRoute |
. | calculateIngestionDose($customUnits) as $dose |
. | ingestionUnit($customUnits) as $unit |
(.consumerName // "default") as $consumerName |
$stats |
.[$consumerName].[$name].[$administrationRoute]|=
($stats.[$consumerName].[$name].[$administrationRoute] // {
unit: "",
# null because null+null = null for ingestions with unknown dose which .dose is null
dose: null
}) |
.[$consumerName].[$name].[$administrationRoute].unit |= $unit |
.[$consumerName].[$name].[$administrationRoute].dose += $dose
));
def calculateCombinedDose($substanceName; $consumerName):
. as $stats |
($stats.[$consumerName].[$substanceName] | [to_entries[] | .value.dose] | add)| . as $combinedDose |
($stats.[$consumerName].[$substanceName] | to_entries[0] | .value.unit) as $combinedDoseUnit |
{dose: $combinedDose, unit: $combinedDoseUnit};
def experienceByTitle($name):
assert((. | type) == "array"; "experienceByTitle takes a array of experiences as input") |
map(select(.title == $name))[0];