Skip to content

Commit

Permalink
Added get mode for update-pages-comment.sh
Browse files Browse the repository at this point in the history
* corrected page search by timeout
  • Loading branch information
Nikita-Smirnov-Exactpro committed Feb 4, 2025
1 parent c1447c5 commit 54bcb8c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
11 changes: 6 additions & 5 deletions cradle-admin-tool-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,22 @@ spec:
```
Help:
Description: this script provide ability to update comment for pages covered by time rage
Description: this script provide ability to update comment for pages covered by time rage where page.started is included and page.ended is excluded
Required utils: jq curl paste grep head
Arguments:
--cradle-admin-tool-url (required) - cradle admin tool URL
--book (required) - th2 book for searching and updating pages
--start-timestamp (required) - start timestamp for searching page to add --comment comment
--end-timestamp (required) - end timestamp for searching page to add --comment comment
--comment (required) - comment for adding to pages found from --start-timestamp to --end-timestamp
--mode (optional) - work mode. Default value is 'append'
--start-timestamp (optional) - start timestamp for searching page to add --comment comment. Default is min timestamp
--end-timestamp (optional) - end timestamp for searching page to add --comment comment. Default is max timestamp
--comment (conditional) - comment for adding to pages found from --start-timestamp to --end-timestamp. Required for ['append','set'] modes
--mode (optional) - work mode. Default value is 'get'
* append - appends existed pages' comment by text specified using --comment. 'auto-page' default page comment is removed
Final comment has JSON string array format, for example: '["<existed comment>","<specified comment>"]'
* set - sets text specified using --comment as pages' comment
Final comment has JSON string array format, for example: '["<specified comment>"]'
* reset - resets pages' comment to 'auto-page' default page comment
Final comment is 'auto-page'
* get - prints pages and their comments
```

## Release notes
Expand Down
90 changes: 75 additions & 15 deletions cradle-admin-tool-http/scripts/update-pages-comment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ARG_MODE='--mode'
MODE_APPEND='append'
MODE_SET='set'
MODE_RESET='reset'
MODE_GET='get'

CRADLE_ADMIN_GET_ALL_BOOKS_PATH='get-all-books'
CRADLE_ADMIN_GET_BOOK_INFO_PATH='get-book-info'
Expand All @@ -49,21 +50,22 @@ REQUIRED_UTILS=('jq' 'curl' 'paste' 'grep' 'head')

print_help() {
echo 'Help:'
echo ' Description: this script provide ability to update comment for pages covered by time rage'
echo ' Description: this script provide ability to update comment for pages covered by time rage where page.started is included and page.ended is excluded'
echo " Required utils: ${REQUIRED_UTILS[*]}"
echo ' Arguments:'
echo " ${ARG_CRADLE_ADMIN_TOOL_URL} (required) - cradle admin tool URL"
echo " ${ARG_BOOK} (required) - th2 book for searching and updating pages"
echo " ${ARG_START_TIMESTAMP} (required) - start timestamp for searching page to add ${ARG_COMMENT} comment"
echo " ${ARG_END_TIMESTAMP} (required) - end timestamp for searching page to add ${ARG_COMMENT} comment"
echo " ${ARG_COMMENT} (required) - comment for adding to pages found from ${ARG_START_TIMESTAMP} to ${ARG_END_TIMESTAMP}"
echo " ${ARG_MODE} (optional) - work mode. Default value is '${MODE_APPEND}'"
echo " ${ARG_START_TIMESTAMP} (optional) - start timestamp for searching page to add ${ARG_COMMENT} comment. Default is min timestamp"
echo " ${ARG_END_TIMESTAMP} (optional) - end timestamp for searching page to add ${ARG_COMMENT} comment. Default is max timestamp"
echo " ${ARG_COMMENT} (conditional) - comment for adding to pages found from ${ARG_START_TIMESTAMP} to ${ARG_END_TIMESTAMP}. Required for ['${MODE_APPEND}','${MODE_SET}'] modes"
echo " ${ARG_MODE} (optional) - work mode. Default value is '${MODE_GET}'"
echo " * ${MODE_APPEND} - appends existed pages' comment by text specified using ${ARG_COMMENT}. '$CRADLE_ADMIN_DEFAULT_COMMENT' default page comment is removed"
echo " Final comment has JSON string array format, for example: '[\"<existed comment>\",\"<specified comment>\"]'"
echo " * ${MODE_SET} - sets text specified using ${ARG_COMMENT} as pages' comment"
echo " Final comment has JSON string array format, for example: '[\"<specified comment>\"]'"
echo " * ${MODE_RESET} - resets pages' comment to '$CRADLE_ADMIN_DEFAULT_COMMENT' default page comment"
echo " Final comment is '$CRADLE_ADMIN_DEFAULT_COMMENT'"
echo " * ${MODE_GET} - prints pages and their comments"
}

parse_args() {
Expand All @@ -72,7 +74,7 @@ parse_args() {
START_TIMESTAMP=''
END_TIMESTAMP=''
COMMENT=''
MODE="${MODE_APPEND}"
MODE="${MODE_GET}"

while [[ "$#" -gt 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -104,6 +106,9 @@ parse_args() {
MODE="$2"
shift 2
;;
*)
echo " ERROR: unknown '${1}' argument"
exit 1
esac
done

Expand Down Expand Up @@ -139,11 +144,16 @@ verify_utils() {

verify_args() {
echo 'Check arguments:'
verify_mode
verify_url
verify_book
verify_timestamps
verify_comment
verify_mode

case "${MODE}" in
"${MODE_SET}"|"${MODE_APPEND}")
verify_comment
;;
esac
}

verify_url() {
Expand Down Expand Up @@ -179,20 +189,24 @@ verify_book() {
}

verify_timestamps() {
if [[ "${START_TIMESTAMP}" =~ ${TIMESTAMP_REGEX} ]]; then
if [[ "${START_TIMESTAMP}" == '' || "${START_TIMESTAMP}" =~ ${TIMESTAMP_REGEX} ]]; then
echo " INFO: '${START_TIMESTAMP}' start timestamp has correct format"
else
echo " ERROR: '${START_TIMESTAMP}' start timestamp has invalid format. Ensure it is in the format 'YYYY-MM-DDTHH:MM:SSZ'"
exit 4
fi

if [[ "${END_TIMESTAMP}" =~ ${TIMESTAMP_REGEX} ]]; then
if [[ "${END_TIMESTAMP}" == '' || "${END_TIMESTAMP}" =~ ${TIMESTAMP_REGEX} ]]; then
echo " INFO: '${END_TIMESTAMP}' end timestamp has correct format"
else
echo " ERROR: '${END_TIMESTAMP}' end timestamp has invalid format. Ensure it is in the format 'YYYY-MM-DDTHH:MM:SSZ'"
exit 4
fi

if [[ "${START_TIMESTAMP}" == '' || "${END_TIMESTAMP}" == '' ]]; then
return
fi

local start_epoch
local end_epoch
start_epoch=$(date --date "${START_TIMESTAMP}" +%s)
Expand All @@ -214,8 +228,8 @@ verify_comment() {
}

verify_mode() {
if [[ "${MODE}" != "${MODE_APPEND}" && "${MODE}" != "${MODE_SET}" && "${MODE}" != "${MODE_RESET}" ]]; then
echo " ERROR: '${MODE}' mode isn't either of values ['${MODE_APPEND}','${MODE_SET}','${MODE_RESET}']"
if [[ "${MODE}" != "${MODE_APPEND}" && "${MODE}" != "${MODE_SET}" && "${MODE}" != "${MODE_RESET}" && "${MODE}" != "${MODE_GET}" ]]; then
echo " ERROR: '${MODE}' mode isn't either of values ['${MODE_APPEND}','${MODE_SET}','${MODE_RESET}','${MODE_GET}']"
exit 6
fi
}
Expand Down Expand Up @@ -333,15 +347,34 @@ prepare_comment_test() {
exit 0
}

generate_jq_query() {
echo ".[] | .[\"${CRADLE_ADMIN_PAGES_KEY}\"][] |
select(
(
.[\"${CRADLE_ADMIN_ENDED_KEY}\"] == ${CRADLE_ADMIN_NULL_VALUE}
or
\"${START_TIMESTAMP}\" == \"\"
or
.[\"${CRADLE_ADMIN_ENDED_KEY}\"] > \"${START_TIMESTAMP}\"
) and (
\"${END_TIMESTAMP}\" == \"\"
or
.[\"${CRADLE_ADMIN_STARTED_KEY}\"] <= \"${END_TIMESTAMP}\"
)
)"
}

update_page_comments() {
echo 'Update page comments:'
echo "Update pages' comments:"
local books_info_json
local url
local jq_query
url="${CRADLE_ADMIN_TOOL_URL}/${CRADLE_ADMIN_GET_BOOK_INFO_PATH}?${CRADLE_ADMIN_BOOK_ID_HTTP_ARG}=${BOOK}"
echo " INFO: GET - $url"
books_info_json=$(curl --silent "${url}")

echo "${books_info_json}" | jq -r ".[] | .[\"${CRADLE_ADMIN_PAGES_KEY}\"][] | select((.[\"${CRADLE_ADMIN_ENDED_KEY}\"] == ${CRADLE_ADMIN_NULL_VALUE} or .[\"${CRADLE_ADMIN_ENDED_KEY}\"] >= \"${START_TIMESTAMP}\") and (.[\"${CRADLE_ADMIN_STARTED_KEY}\"] <= \"${END_TIMESTAMP}\")) | \"\(.[\"${CRADLE_ADMIN_PAGE_ID_KEY}\"]) \(.[\"${CRADLE_ADMIN_COMMENT_KEY}\"])\"" | \
jq_query="$(generate_jq_query) | \"\(.[\"${CRADLE_ADMIN_PAGE_ID_KEY}\"]) \(.[\"${CRADLE_ADMIN_COMMENT_KEY}\"])\""
echo "${books_info_json}" | jq -r "${jq_query}" | \
while read -r page_id comment; do
local new_comment

Expand All @@ -355,6 +388,10 @@ update_page_comments() {
"${MODE_RESET}")
new_comment="${CRADLE_ADMIN_DEFAULT_COMMENT}"
;;
*)
echo " ERROR: incorrect '${MODE}' mode for update pages' comment"
exit 20
;;
esac

if [ -z "${new_comment}" ]; then
Expand All @@ -373,6 +410,29 @@ update_page_comments() {
done
}

get_page_comments() {
echo "Get pages' comments:"
local books_info_json
local url
local jq_query
url="${CRADLE_ADMIN_TOOL_URL}/${CRADLE_ADMIN_GET_BOOK_INFO_PATH}?${CRADLE_ADMIN_BOOK_ID_HTTP_ARG}=${BOOK}"
echo " INFO: GET - $url"
books_info_json=$(curl --silent "${url}")

jq_query="$(generate_jq_query) | \"\(.[\"${CRADLE_ADMIN_PAGE_ID_KEY}\"]) \(.[\"${CRADLE_ADMIN_STARTED_KEY}\"]) \(.[\"${CRADLE_ADMIN_ENDED_KEY}\"]) \(.[\"${CRADLE_ADMIN_COMMENT_KEY}\"]) \""
echo "${books_info_json}" | jq -r "${jq_query}" | \
while read -r page_id started ended comment; do
echo " '${BOOK}.${page_id}' [${started} - ${ended}) - '${comment}'"
done
}

verify_utils
parse_args "$@"
update_page_comments
case "${MODE}" in
"${MODE_GET}")
get_page_comments
;;
*)
update_page_comments
;;
esac

0 comments on commit 54bcb8c

Please sign in to comment.