Skip to content

Commit

Permalink
Update file names for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
HannahMarsh committed Aug 23, 2024
1 parent 22eab68 commit a1ec2b6
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 133 deletions.
134 changes: 134 additions & 0 deletions bin/generateCommitPrompt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/bin/bash

# Constants
MAX_DIFF_LENGTH=10000 # Adjust this value based on your needs

# Function to check if the current directory is a Git repository
check_git_repository() {
git rev-parse --is-inside-work-tree > /dev/null 2>&1
}

# Function to load ignore patterns from .ai-commit.json
load_ignore_patterns() {
local config_path=".ai-commit.json" # Define the path to the configuration file
local ignore_patterns=() # Initialize an empty array to hold ignore patterns

# Check if the configuration file exists
if [ -f "$config_path" ]; then
# Use jq to parse the ignore patterns from the JSON file
ignore_patterns=($(jq -r '.ignore[]' "$config_path"))
fi

# Return the ignore patterns as a space-separated string
echo "${ignore_patterns[@]}"
}

# Function to filter the diff based on ignore patterns
filter_diff() {
local diff="$1"
local ignore_patterns=("${!2}")

local filtered_lines=()
local skip_block=false
IFS=$'\n' read -r -a lines <<< "$diff"

for line in "${lines[@]}"; do
skip_block=false
for pattern in "${ignore_patterns[@]}"; do
if [[ "$line" =~ ${pattern//\*/.*} ]]; then
skip_block=true
break
fi
done

if ! $skip_block; then
filtered_lines+=("$line")
fi

if [[ "$line" =~ ^diff\ --git ]]; then
skip_block=false
fi
done

printf "%s\n" "${filtered_lines[@]}"
}

# Check if the directory is a Git repository
if ! check_git_repository; then
echo "This is not a git repository 🙅‍♂️"
exit 1
fi

# Get the staged diff, ignoring space changes
diff=$(git diff --staged --ignore-space-change)

# Remove empty lines and stars
diff=$(echo "$diff" | sed '/^\+[\s]*$/d' | sed '/^[[:space:]]*$/d' | sed 's/\*//g')

# Truncate the diff if it's too large
if [ ${#diff} -gt $MAX_DIFF_LENGTH ]; then
diff="${diff:0:$MAX_DIFF_LENGTH}\n... [diff truncated]"
fi

# Exit if there's no diff
if [ -z "$diff" ]; then
echo "No changes to commit 🙅"
echo "Maybe you forgot to add the files? Try 'git add .' and then run this script again."
exit 1
fi

# Load ignore patterns from .ai-commit.json if it exists
ignore_patterns=($(load_ignore_patterns))

# Filter the diff based on ignore patterns
filtered_diff=$(filter_diff "$diff" ignore_patterns[@])

# Exit if there's no relevant diff after filtering
if [ -z "$filtered_diff" ]; then
echo "No relevant changes to commit after applying ignore patterns 🙅"
exit 0
fi

# Prepare the prompt for generating a commit message
prompt=$(cat <<EOF
Please act as the author of a git commit message. I will provide you with a git diff, and your task is to convert it into a detailed, informative commit message.
To help you understand the git diff output:
1. File Comparison Line: Shows the files being compared.
2. Index Line: Indicates the blob hashes before and after the change and the file mode.
3. File Change Markers: --- shows the file before the change and +++ shows the file after the change.
4. Hunk Header: Indicates the location and number of lines affected in the files.
Example: @@ -1,5 +1,7 @@ means the changes start at line 1 and cover 5 lines in the original file and start at line 1 and cover 7 lines in the new file.
5. Changes: Lines starting with - are removed lines. Lines starting with + are added lines. Some unchanged lines may be shown for context.
Example:
diff
diff --git file1.txt file1.txt
index e69de29..d95f3ad 100644
--- file1.txt
+++ file1.txt
@@ -0,0 +1,2 @@
-This line was removed.
+This is a new line.
+Another new line.
Here\'s how you can structure your commit message:
Summary: <A concise, one-line sentence in the present tense that summarizes all changes (50 characters or less)>.
Description: <A detailed explanation of all changes in the past tense.>
Important:
1. The summary must be in the present tense, e.g., Fix login issue, edit variables,....
2. The description must be in the past tense, e.g., This change fixed a bug by....
3. Avoid prefacing your response with any additional text.
4. The summary and description should cover ALL changes and focus on the most important ones.
Here is the git diff, which you are to convert into a commit message as described:
$filtered_diff
EOF
)

# Print the prompt
echo "$prompt"

# Copy to clipboard (macOS)
echo -e "$prompt" | pbcopy
4 changes: 2 additions & 2 deletions cmd/bulletin-board/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {

// Start the Bulletin Board's main operations in a new goroutine
go func() {
err := bulletinBoard.StartRuns()
err := bulletinBoard.StartProtocol()
if err != nil {
slog.Error("failed to start runs", err)
config.GlobalCancel()
Expand All @@ -69,7 +69,7 @@ func main() {
http.HandleFunc("/registerRelay", bulletinBoard.HandleRegisterRelay)
http.HandleFunc("/registerClient", bulletinBoard.HandleRegisterClient)
http.HandleFunc("/registerIntentToSend", bulletinBoard.HandleRegisterIntentToSend)
http.HandleFunc("/updateNode", bulletinBoard.HandleUpdateNodeInfo)
http.HandleFunc("/updateRelay", bulletinBoard.HandleUpdateRelayInfo)

// Start the HTTP server
go func() {
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions cmd/metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ func main() {
os.Exit(1)
}

nodePromAddresses := utils.Map(config.GlobalConfig.Relays, func(n config.Relay) string {
relayPromAddresses := utils.Map(config.GlobalConfig.Relays, func(n config.Relay) string {
return fmt.Sprintf("http://%s:%d/metrics", n.Host, n.PrometheusPort)
})

clientPromAddresses := utils.Map(config.GlobalConfig.Clients, func(c config.Client) string {
return fmt.Sprintf("http://%s:%d/metrics", c.Host, c.PrometheusPort)
})

slog.Info("⚡ init visualizer", "nodePromAddresses", nodePromAddresses, "clientPromAddresses", clientPromAddresses)
slog.Info("⚡ init visualizer", "relayPromAddresses", relayPromAddresses, "clientPromAddresses", clientPromAddresses)

scrapeInterval := time.Duration(config.GlobalConfig.ScrapeInterval) * time.Millisecond

// Start the metric collector
for {
nextScrape := time.Now().Add(scrapeInterval)
var wg sync.WaitGroup
wg.Add(len(nodePromAddresses) + len(clientPromAddresses))
wg.Add(len(relayPromAddresses) + len(clientPromAddresses))

for _, address := range nodePromAddresses {
for _, address := range relayPromAddresses {
go func(address string) {
defer wg.Done()
scrapeMetrics(address)
Expand Down
14 changes: 6 additions & 8 deletions cmd/visualizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,17 @@ func main() {
os.Exit(1)
}

cfg := config.GlobalConfig

slog.Info("⚡ init visualizer", "host", cfg.Metrics.Host, "port", cfg.Metrics.Port)
slog.Info("⚡ init visualizer", "address", "http://localhost:8200")

time.Sleep(1 * time.Second)
http.HandleFunc("/data", serveData)
http.Handle("/", http.FileServer(http.Dir("./static")))
//http.Handle("/clients", http.FileServer(http.Dir("./static/client")))
//http.Handle("/client", http.FileServer(http.Dir("./static/client")))
//http.Handle("/nodes", http.FileServer(http.Dir("./static/nodes")))
//http.Handle("/nodes/rounds", http.FileServer(http.Dir("./static/nodes/rounds")))

go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", cfg.Metrics.Port), nil); err != nil {
if err := http.ListenAndServe(fmt.Sprintf(":%d", 8200), nil); err != nil {
if errors.Is(err, http.ErrServerClosed) {
slog.Info("HTTP server closed")
} else {
Expand All @@ -65,7 +63,7 @@ func main() {
}
}()

slog.Info("🌏 start visualizer...", "address", fmt.Sprintf(" %s:%d ", cfg.Metrics.Host, cfg.Metrics.Port))
slog.Info("🌏 start visualizer...", "address", "http://localhost:8200")

quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
Expand Down Expand Up @@ -128,8 +126,8 @@ func serveData(w http.ResponseWriter, r *http.Request) {
}
}

for _, node := range config.GlobalConfig.Relays {
addr := fmt.Sprintf("http://%s:%d/status", node.Host, node.Port)
for _, relay := range config.GlobalConfig.Relays {
addr := fmt.Sprintf("http://%s:%d/status", relay.Host, relay.Port)
resp, err := http.Get(addr)
if err != nil {
slog.Error("failed to get client status", err)
Expand Down
31 changes: 16 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ type Metrics struct {
}

type Config struct {
ServerLoad int `yaml:"x"`
D int `yaml:"d"`
Delta float64 `yaml:"delta"`
L1 int `yaml:"l1"`
L2 int `yaml:"l2"`
Chi float64 `yaml:"chi"`
BulletinBoard BulletinBoard `yaml:"bulletin_board"`
Relays []Relay `yaml:"relays"`
Metrics Metrics `yaml:"visualizer"`
Clients []Client `yaml:"clients"`
Vis bool `yaml:"vis"`
ScrapeInterval int `yaml:"scrapeInterval"`
ServerLoad int `yaml:"x"`
D int `yaml:"d"`
Delta float64 `yaml:"delta"`
L1 int `yaml:"l1"`
L2 int `yaml:"l2"`
Chi float64 `yaml:"chi"`
BulletinBoard BulletinBoard `yaml:"bulletin_board"`
Relays []Relay `yaml:"relays"`
Metrics Metrics `yaml:"visualizer"`
Clients []Client `yaml:"clients"`
Vis bool `yaml:"vis"`
ScrapeInterval int `yaml:"scrapeInterval"`
DropAllOnionsFromClient int `yaml:"dropAllOnionsFromClient"`
}

var GlobalConfig *Config
Expand Down Expand Up @@ -116,9 +117,9 @@ func AddressToName(address string) string {
if name, ok := Names.Load(address); ok {
return name.(string)
}
for _, node := range GlobalConfig.Relays {
if address == node.Address {
name := fmt.Sprintf("%sRelay %d%s", PurpleColor, node.ID, ResetColor)
for _, relay := range GlobalConfig.Relays {
if address == relay.Address {
name := fmt.Sprintf("%sRelay %d%s", PurpleColor, relay.ID, ResetColor)
//name := fmt.Sprintf("Relay %d", relay.ID)
Names.Store(address, name)
return name
Expand Down
3 changes: 2 additions & 1 deletion config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ x: 8 # Server load (x = Ω(polylog λ)) i.e. the expected number of on
tau: 0.8 # (τ < (1 − γ)(1 − X)) Fraction of checkpoints needed to progress local clock
d: 2 # Threshold for number of bruises before an onion is discarded by a gatekeeper
delta: 1e-5 # The probability of differential privacy violation due to the adversary's actions.
chi: 0.3 # Fraction of corrupted relays (which perform no mixing)
chi: 1.0 # Fraction of corrupted relays (which perform no mixing)
vis: true # Visualize the network
scrapeInterval: 1000 # Prometheus scrape interval in milliseconds
dropAllOnionsFromClient: 1 # Client ID to drop all onions from


metrics:
Expand Down
Loading

0 comments on commit a1ec2b6

Please sign in to comment.