Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / scrape-job-cost.sh
1 #! /bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2020 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 # This script will be run on the Nexus Server for each project.  Typically each
13 # project will have multiple nexus silos. This script will find all the job cost
14 # files (job_name/job_num/cost.csv) in each silo and append them to the annual
15 # cost file (~nexus/cost/$silo.YYYY.csv~nexus/bin . It will then delete all the
16 # job cost files.
17
18 # Because this file meant to be run by cron, I have restricted normal info
19 # logging messages on a single line. Error may be multi-line.
20 #
21 # Each cost file contains one or more CSV records in the following format:
22 #
23 #   JobName , BuildNumber , Date , InstanceType , Uptime , Cost , StackCost
24 #
25 #   Date format: '%Y-%m-%d %H:%M:%S'
26 #   Cost format: '%.2f' (1.29)
27 #
28 ##############################################################################
29 #
30 #  Testing/Validation
31 #
32 #  You can validate this script by running as yourself on the Nexus server. You
33 #  should not have write permission anywhere in the Silo Directory. The
34 #  Silo Cost File from your test will be created/updated in:
35 #      ~/cost/$silo-$year.csv. If you run multiple times, duplicate records
36 #      will be created.
37 #
38 #  To enable debug set envionment variable DEBUG=true. If this is done on the
39 #  command-line, you  do not have to edit this file.
40 debug=${DEBUG:-false}
41 $debug && echo "DEBUG Enabled"
42 #
43 ##############################################################################
44
45 set -eufo pipefail
46
47 get-year-list () {
48     # Grab the years for each cost record use sort | uniq to get the
49     # list of unique years found
50     local list
51     list=$(awk -F',' '{print $3}' "$cost_file_records" \
52                | awk -F'-' '{print $1}' | sort | uniq)
53     echo "$list"
54 }
55
56 ###########  End of Function Definitions  ######################################
57
58 if [[ $# != 2 ]]; then
59     echo "usage: $(basename "$0") silo silo_dir"
60     exit 1
61 fi
62
63 # The Silo Dir is top-level directory that will contain the job directories
64 # which will contain the cost files (cost.csv)
65 silo=$1
66 silo_dir=$2
67
68 cost_file_records=/tmp/cost-file-records$$
69 cost_file_list=/tmp/cost-file-list$$
70 # The directory where the annual cost file will be located
71 cost_dir=~/cost
72 [[ -d $cost_dir ]] || mkdir $cost_dir
73
74 # The Silo Directory for sandbox will get deleted periodically, so
75 # gracefully handle that
76 if [[ -d $silo_dir ]]; then
77     cd "$silo_dir"
78 else
79     echo  "$(date +'%Y-%m-%d %H:%M') No Silo Directory, nothing to do"
80     exit 0
81 fi
82
83 find . -maxdepth 3 -name cost.csv > $cost_file_list
84 xargs cat < $cost_file_list | \
85     sort --field-separator=',' --key=3  > $cost_file_records
86 num_of_records=$(wc -l < $cost_file_records)
87 echo -n "$(date +'%Y-%m-%d %H:%M') Records: $num_of_records "
88
89 if [[ $num_of_records == 0 ]]; then
90     echo "Nothing to do"
91     set +f
92     rm -rf /tmp/cost-file-* || true
93     exit 0
94 fi
95
96 # Append each entry to the silo cost file based on date
97 year_list=$(get-year-list)
98 for year in $year_list; do
99     echo -n "cost-$year.csv: $(grep -Fc ",$year-" $cost_file_records) "
100     grep -F ",$year-" $cost_file_records >>    \
101          "$cost_dir/$silo-$year.csv"
102 done
103
104 # Rename the job cost files (make them hidden)
105 while read -r p; do
106     job_dir=$(dirname "$p")
107     $debug || (cd "$job_dir" ; mv cost.csv .cost.csv)
108 done < $cost_file_list
109
110 rm -r $cost_file_list $cost_file_records
111
112 echo -n "Sorting: "
113 # Sort the silo cost file by 'date' (column 3)
114 for year in $year_list; do
115     echo -n "cost-$year.csv "
116     sort --field-separator=',' --key=3  \
117          -o "$cost_dir/$silo-$year.csv" \
118          "$cost_dir/$silo-$year.csv"
119 done
120
121 set +f
122 rm -rf /tmp/cost-file-* || true
123
124 # Keep track of time initally
125 echo "Complete $SECONDS Secs"