From 9f9feb2a93db8e1e09b1fb2906d2dfd0a5ea656b Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Fri, 7 Feb 2020 13:51:32 +0100 Subject: [PATCH] Orgas: export jq filtering outside gen.sh code (simplification) --- orgas/TODO | 1 - orgas/commons.sh | 6 ++++++ orgas/filters/libraries | 6 ++++++ orgas/filters/organisations | 5 +++++ orgas/filters/patron_types | 5 +++++ orgas/filters/users | 9 +++++++++ orgas/gen.sh | 38 ++++++++----------------------------- 7 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 orgas/filters/libraries create mode 100644 orgas/filters/organisations create mode 100644 orgas/filters/patron_types create mode 100644 orgas/filters/users diff --git a/orgas/TODO b/orgas/TODO index 36cc5bd..af0a8e2 100644 --- a/orgas/TODO +++ b/orgas/TODO @@ -1,6 +1,5 @@ # TODO list * item -> item_types -> patron_types -> circulation policies + organisations - * patron_types on organisations.svg * do it in Python * use http://localhost:9200/patrons/_search?size=50 and similar to create all graphs? => problem: you need to launch docker-compose and load data (setup), which takes time diff --git a/orgas/commons.sh b/orgas/commons.sh index ea9a9d1..03ac147 100644 --- a/orgas/commons.sh +++ b/orgas/commons.sh @@ -40,6 +40,12 @@ save() { rendered_template="$(render $1)" send "${rendered_template}" } +# parse_json: read JSON file (filename given in $1) and applying filter (same name as $1) +parse_json() { + filepath="${SRC_DIR}/$1.json" + filter="filters/$1" + cat "$filepath" | jq -cf "$filter" +} # Output file initialization echo "" > "$outputfile" # flush output file diff --git a/orgas/filters/libraries b/orgas/filters/libraries new file mode 100644 index 0000000..7c952a3 --- /dev/null +++ b/orgas/filters/libraries @@ -0,0 +1,6 @@ +.[] | { + name, + pid, + code, + organisation: .organisation."$ref" +} diff --git a/orgas/filters/organisations b/orgas/filters/organisations new file mode 100644 index 0000000..b7f88e2 --- /dev/null +++ b/orgas/filters/organisations @@ -0,0 +1,5 @@ +.[] | { + name, + pid, + code +} diff --git a/orgas/filters/patron_types b/orgas/filters/patron_types new file mode 100644 index 0000000..a8413d3 --- /dev/null +++ b/orgas/filters/patron_types @@ -0,0 +1,5 @@ +.[] | { + pid, + name, + organisation: .organisation."$ref" +} diff --git a/orgas/filters/users b/orgas/filters/users new file mode 100644 index 0000000..3f50791 --- /dev/null +++ b/orgas/filters/users @@ -0,0 +1,9 @@ +.[] | { + email, + first_name, + last_name, + barcode, + roles, + library: .library."$ref", + pt: .patron_type."$ref" +} diff --git a/orgas/gen.sh b/orgas/gen.sh index f19d0cf..8c2dbe3 100755 --- a/orgas/gen.sh +++ b/orgas/gen.sh @@ -33,13 +33,9 @@ title="Link between organisations, libraries and users." save "templates/header.tmpl" # ORGANISATIONS -orga_file="${SRC_DIR}organisations.json" shape="box" color="${ORGA_COLOR}" -cat "${orga_file}"|jq -c '.[] | { - name, - pid, - code }'| while read orga +while read -r orga do # take important info pid=$(echo $orga|jq -r .pid) @@ -49,17 +45,12 @@ do identifier="Orga${pid}" label="$(render ${tmpl_label})" save "${tmpl_gizmo}" -done +done <<< $(parse_json "organisations") # LIBRARIES -lib_file="${SRC_DIR}libraries.json" shape="house" color="${LIB_COLOR}" -cat "${lib_file}"|jq -c '.[] | { - name, - pid, - code, - organisation: .organisation."$ref"}'|while read lib +while read -r lib do pid=$(echo $lib|jq -r .pid) name=$(echo $lib|jq -r .name) @@ -72,17 +63,13 @@ do save "${tmpl_gizmo}" relation="Orga${orga_pid}" save "${tmpl_link}" -done +done <<< $(parse_json "libraries") # PATRON_TYPES -pt_file="${SRC_DIR}patron_types.json" shape="polygon" color="${PT_COLOR}" additionals="sides=7" -cat "${pt_file}" |jq -c '.[] | { - pid, - name, - organisation: .organisation."$ref"}'|while read pt +while read pt do pid=$(echo $pt|jq -r .pid) name=$(echo $pt|jq -r .name) @@ -98,21 +85,13 @@ do relation="Orga${orga_pid}" save "${tmpl_link}" fi -done +done <<< $(parse_json "patron_types") # USERS -user_file="${SRC_DIR}users.json" shape="ellipse" additionals='' color="${USER_COLOR}" -cat "${user_file}"|jq -c '.[] | { - email, - first_name, - last_name, - barcode, - roles, - library: .library."$ref", - pt: .patron_type."$ref"}'|while read user +while read user do email=$(echo $user|jq -r .email) first_name=$(echo $user|jq -r .first_name) @@ -157,11 +136,10 @@ do relation="Type${pt_id}" save "${tmpl_link}" fi -done +done <<< $(parse_json "users") # Graph footer save "templates/footer.tmpl" - # END of program exit 0