Mirror of https://github.com/roostorg/osprey
github.com/roostorg/osprey
1#!/bin/bash
2
3# continuously generate and send test actions to the osprey coordinator via gRPC
4# this mimics the Kafka test data generator but sends directly to the coordinator
5
6set -e
7
8COORDINATOR_HOST="${COORDINATOR_HOST:-localhost:19951}"
9
10# Check if grpcurl is installed
11if ! command -v grpcurl &> /dev/null; then
12 echo "Error: grpcurl is not installed."
13 echo "Install it with: brew install grpcurl (macOS) or go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest"
14 exit 1
15fi
16
17# Check if jq is installed
18if ! command -v jq &> /dev/null; then
19 echo "Error: jq is not installed."
20 echo "Install it with: brew install jq (macOS)"
21 exit 1
22fi
23
24# Initialize action_id counter
25action_id=1
26
27# Words to randomly generate post content
28words=(hello the quick brown fox jumps over lazy dog and cat runs fast)
29
30# Get script directory
31SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
32
33# Function to generate random user ID
34generate_random_user_id() {
35 echo "user_$(shuf -i 100-9999 -n 1)"
36}
37
38# Function to generate current timestamp in RFC3339 format
39generate_timestamp() {
40 date -u +"%Y-%m-%dT%H:%M:%S.000000000Z"
41}
42
43# Function to generate random post text
44generate_random_text() {
45 echo "${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]}."
46}
47
48# Function to generate action data from template
49generate_action() {
50 local text=$(generate_random_text)
51 local timestamp=$(generate_timestamp)
52 local user_id=$(generate_random_user_id)
53 local ip_address="192.168.1.$(shuf -i 1-254 -n 1)"
54
55 local sed_commands=()
56 sed_commands+=("s/\$text/$text/g")
57 sed_commands+=("s/\$timestamp/$timestamp/g")
58 sed_commands+=("s/\$user_id/$user_id/g")
59 sed_commands+=("s/\$ip_address/$ip_address/g")
60 sed_commands+=("s/\$action_id/$action_id/g")
61
62 # Apply all sed commands to template.json
63 local cmd="sed"
64 for sed_cmd in "${sed_commands[@]}"; do
65 cmd="$cmd -e '$sed_cmd'"
66 done
67 eval "$cmd" "$SCRIPT_DIR/template.json"
68}
69
70# Function to send a single action
71send_action() {
72 local kafka_format_json=$(generate_action)
73
74 # Extract the data object from the Kafka format and convert to coordinator format
75 local action_data=$(echo "$kafka_format_json" | jq -c '.data')
76 local timestamp=$(echo "$kafka_format_json" | jq -r '.send_time')
77 local action_name=$(echo "$action_data" | jq -r '.action_name')
78 local data_payload=$(echo "$action_data" | jq -c '.data')
79
80 echo "[$action_id] Sending action - Name: $action_name, Timestamp: $timestamp"
81
82 # Build gRPC request format
83 jq -n \
84 --arg action_id "$action_id" \
85 --arg action_name "$action_name" \
86 --argjson data_payload "$data_payload" \
87 --arg timestamp "$timestamp" \
88 '{
89 action_id: ($action_id | tonumber),
90 action_name: $action_name,
91 action_data_json: ($data_payload | tostring),
92 timestamp: $timestamp
93 }' | grpcurl -plaintext -d @ "$COORDINATOR_HOST" \
94 osprey.rpc.osprey_coordinator.sync_action.v1.OspreyCoordinatorSyncActionService/ProcessAction
95
96 # Increment action_id
97 ((action_id++))
98}
99
100# Function to handle cleanup on script termination
101cleanup() {
102 echo
103 echo "Stopping data generation..."
104 exit 0
105}
106
107# Set up signal handlers for graceful shutdown
108trap cleanup SIGINT SIGTERM
109
110# Main execution
111echo "Generating actions every second to Osprey Coordinator at $COORDINATOR_HOST"
112echo "Press Ctrl+C to stop..."
113echo
114
115# Infinite loop to generate and send actions
116while true; do
117 send_action
118 sleep 1
119done