diff --git a/parts/03-query-utils.sh b/parts/03-query-utils.sh index aca74ed..52eb3dc 100644 --- a/parts/03-query-utils.sh +++ b/parts/03-query-utils.sh @@ -159,3 +159,75 @@ summary_statistics() { get_user_filter() { echo "(galaxy_user.email = '$1' or galaxy_user.username = '$1' or galaxy_user.id = CAST(REGEXP_REPLACE('$1', '.*\D+.*', '-1') AS INTEGER))" } + +function create_sql_parameters_select() { + local strSelect = "" + + if [[ -n $arg_arrSelect1 ]]; then + if [[ $(echo "$arg_arrSelect1" | cut -d';' -f1) == "when" ]]; then + strSelect+=" WHEN" $(echo "$arg_arrSelect1" | cut -d';' -f2) "THEN" $(echo "$arg_arrSelect1" | cut -d';' -f3) + else + strSelect+="ELSE" $(echo "$arg_arrSelect1" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrSelect2" | cut -d';' -f1) == "when" ]]; then + strSelect+=" WHEN" $(echo "$arg_arrSelect2" | cut -d';' -f2) "THEN" $(echo "$arg_arrSelect2" | cut -d';' -f3) + else + strSelect+=" ELSE" $(echo "$arg_arrSelect2" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrSelect3" | cut -d';' -f1) == "when" ]]; then + strSelect+=" WHEN" $(echo "$arg_arrSelect3" | cut -d';' -f2) "THEN" $(echo "$arg_arrSelect3" | cut -d';' -f3) + else + strSelect+=" ELSE" $(echo "$arg_arrSelect3" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrSelect4" | cut -d';' -f1) == "when" ]]; then + strSelect+=" WHEN" $(echo "$arg_arrSelect4" | cut -d';' -f2) "THEN" $(echo "$arg_arrSelect4" | cut -d';' -f3) + else + strSelect+=" ELSE" $(echo "$arg_arrSelect4" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrSelect5" | cut -d';' -f1) == "when" ]]; then + strSelect+=" WHEN" $(echo "$arg_arrSelect5" | cut -d';' -f2) "THEN" $(echo "$arg_arrSelect5" | cut -d';' -f3) + else + strSelect+=" ELSE" $(echo "$arg_arrSelect5" | cut -d';' -f3) + fi + + strSelect="CASE" $strSelect "END" + fi + + return $strSelect; +} + +function create_sql_parameters_where() { + local strWhere = "" + + if [[ -n $arg_arrWhere1 ]]; then + if [[ $(echo "$arg_arrWhere1" | cut -d';' -f1) == "when" ]]; then + strWhere+=" WHEN" $(echo "$arg_arrWhere1" | cut -d';' -f2) "THEN" $(echo "$arg_arrWhere1" | cut -d';' -f3) + else + strWhere+="ELSE" $(echo "$arg_arrWhere1" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrWhere2" | cut -d';' -f1) == "when" ]]; then + strWhere+=" WHEN" $(echo "$arg_arrWhere2" | cut -d';' -f2) "THEN" $(echo "$arg_arrWhere2" | cut -d';' -f3) + else + strWhere+=" ELSE" $(echo "$arg_arrWhere2" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrWhere3" | cut -d';' -f1) == "when" ]]; then + strWhere+=" WHEN" $(echo "$arg_arrWhere3" | cut -d';' -f2) "THEN" $(echo "$arg_arrWhere3" | cut -d';' -f3) + else + strWhere+=" ELSE" $(echo "$arg_arrWhere3" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrWhere4" | cut -d';' -f1) == "when" ]]; then + strWhere+=" WHEN" $(echo "$arg_arrWhere4" | cut -d';' -f2) "THEN" $(echo "$arg_arrWhere4" | cut -d';' -f3) + else + strWhere+=" ELSE" $(echo "$arg_arrWhere4" | cut -d';' -f3) + fi + if [[ $(echo "$arg_arrWhere5" | cut -d';' -f1) == "when" ]]; then + strWhere+=" WHEN" $(echo "$arg_arrWhere5" | cut -d';' -f2) "THEN" $(echo "$arg_arrWhere5" | cut -d';' -f3) + else + strWhere+=" ELSE" $(echo "$arg_arrWhere5" | cut -d';' -f3) + fi + + strWhere="CASE" $strWhere "END" + fi + + return $strWhere; +} diff --git a/parts/22-query.sh b/parts/22-query.sh index 6938815..044e1fb 100644 --- a/parts/22-query.sh +++ b/parts/22-query.sh @@ -4723,7 +4723,17 @@ query_potentially-duplicated-reclaimable-space() { ##? EOF } -query_tpt-tool-cpu() { ##? [--startyear=] [--endyear=] [--formula=avg]: Start year is required. Formula returns sum if blank. +query_tpt-tool-cpu() { ##? [--startyear=] [--endyear=] [--formula=avg] [--arrSelect1={type};{hypothesis};{conclusion}] [--arrSelect2=...] [--arrWhere1={hypothesis};{conclusion}] [--arrWhere2=...]: Start year is required. Formula returns sum if blank. arrSelect maximum inputs is 5. arrWhere maximum inputs is 5. + +# Example arguments: +# --startyear=2022 +# --endyear=2024 +# --formula=sum +# --arrSelect1="when;destination_id LIKE 'stampede%';metric_value" +# --arrSelect2="else; null;metric_value / 1000000000" +# --arrWhere1="destination_id LIKE 'stampede%';metric_name = 'runtime_seconds'" +# --arrWhere2="metric_name = 'cpuacct.usage';null" + meta <<-EOF AUTHORS: hujambo-dunia ADDED: 20 @@ -4760,16 +4770,24 @@ query_tpt-tool-cpu() { ##? [--startyear=] [--endyear=] [--formula=av sql_formula="SUM" fi + if [[ -n $arg_arrSelect1 ]]; then + sql_select=$(create_sql_parameters_select "$arg_arrSelect1" "$arg_arrSelect2" "$arg_arrSelect3" "$arg_arrSelect4" "$arg_arrSelect5") + fi + + if [[ -n $arg_arrWhere1 ]]; then + sql_where=$(create_sql_parameters_where "$arg_arrWhere1" "$arg_arrWhere2" "$arg_arrWhere3" "$arg_arrWhere4" "$arg_arrWhere5") + fi + read -r -d '' QUERY <<-EOF WITH cpu_usage AS ( SELECT DISTINCT job_id, destination_id, - metric_value / 1000000000 AS cpu_usage_seconds + $sql_select cpu_usage_seconds FROM job_metric_numeric WHERE - metric_name = 'cpuacct.usage' + $sql_where ) SELECT job.tool_id,