Skip to content

Commit

Permalink
Merge pull request #2 from daltondiaz/feat/list/equal-greater-than
Browse files Browse the repository at this point in the history
Feat/list/equal greater than
  • Loading branch information
daltondiaz authored Mar 27, 2024
2 parents 6ed131e + 20f0e60 commit 445a5aa
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 39 deletions.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,67 @@ Result:
| TOTAL OF CONNECTIONS | 451 | | |
+---------------------------------------+--------------+---------------------+---------+
```

#### Equals or Greater than X

If you want to purge or list only queues with total mesages equals or greater than X it's possible to use the flag `--greater`.


#### Example

You have the following list

```
+--------------------------------+----------+
| QUEUE | MESSAGES |
+--------------------------------+----------+
| something_test1 | 5 |
| queue_test1234 | 5 |
| queue_test1 | 5 |
| test_something | 3 |
| something_queue | 1 |
+--------------------------------+----------+
| TOTAL QUEUES | 5 |
| TOTAL MESSAGES | 19|
+--------------------------------+----------+
```
Using our flag:

```
./rabbit-tools list all --greater=5
```

Result:

```
+--------------------------------+----------+
| QUEUE | MESSAGES |
+--------------------------------+----------+
| something_test1 | 5 |
| queue_test1234 | 5 |
| queue_test1 | 5 |
+--------------------------------+----------+
| TOTAL QUEUES | 3 |
| TOTAL MESSAGES | 15|
+--------------------------------+----------+
```

You can use this flag on purge too

```
./rabbit-tools purge all --greater=5
```

Result:

```
+--------------------------------+----------+
|PURGE QUEUE | MESSAGES |
+--------------------------------+----------+
| something_test1 | 5 |
| queue_test1234 | 5 |
| queue_test1 | 5 |
+--------------------------------+----------+
| TOTAL PURGED MESSAGES | 15|
+--------------------------------+----------+
```
18 changes: 15 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"log"
"strconv"

"github.com/daltondiaz/rabbit-tools/internal"
"github.com/jedib0t/go-pretty/v6/table"
Expand All @@ -21,12 +22,22 @@ var listCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}

result := internal.GetQueues(filter, env)

greaterStr, err := cmd.Flags().GetString("greater")
if err != nil {
log.Fatal(err)
}

if _,err := strconv.Atoi(greaterStr); err != nil {
log.Fatal(err)
}

greater, _ := strconv.Atoi(greaterStr)
result := internal.GetQueues(filter, env, greater)
var prettyResult internal.PrettyModelResult
prettyResult.Title = "List of Queues"
prettyResult.Header = table.Row{"Queue", "Messages"}
totalItems := 0.0
totalItems := 0
for queueName, value := range result {
row := table.Row{queueName, value}
totalItems += value
Expand All @@ -42,4 +53,5 @@ func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().String("env", "", "Prefix of environment in config.env")
listCmd.PersistentFlags().String("filter", "", "Filter queues")
listCmd.PersistentFlags().String("greater", "", "Filter queues with equal or greater than N messages")
}
15 changes: 14 additions & 1 deletion cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"log"
"strconv"

"github.com/daltondiaz/rabbit-tools/internal"
"github.com/spf13/cobra"
Expand All @@ -19,12 +20,24 @@ var purgeCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
internal.PurgeQueue(filter, env)
greaterStr, err := cmd.Flags().GetString("greater")
greater :=0
if err != nil {
log.Fatal(err)
}

if value,err := strconv.Atoi(greaterStr); err != nil {
log.Fatal(err)
} else {
greater = value
}
internal.PurgeQueue(filter, env, greater)
},
}

func init(){
rootCmd.AddCommand(purgeCmd)
rootCmd.PersistentFlags().String("env","", "Prefix of environment in config.env")
rootCmd.PersistentFlags().String("filter","", "Filter queue to be purge")
rootCmd.PersistentFlags().String("greater","", "Filter queues with equal or greater than N messages")
}
72 changes: 41 additions & 31 deletions internal/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,47 @@ import (
"strings"
)

func GetQueues(queue string, env string, greater int) map[string]int {

func GetQueues(queue string, env string) map[string]float64 {
params := LoadEnvVariables(env)
client := &http.Client{}
req, err := http.NewRequest("GET", params.Url, nil)
req.SetBasicAuth(params.User, params.Pass)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
var body []map[string]any
var showQueue bool
if len(queue) > 0 && queue != "all" {
showQueue = true
} else {
showQueue = false
}
json.Unmarshal(bodyText, &body)
result := make(map[string]int)
for _, el := range body {
queueName := el["name"].(string)
if showQueue != false {
if strings.Contains(queueName, queue) {
floatMsgRd := el["messages_ready"].(float64)
result[queueName] = int(floatMsgRd)
}
} else {
floatMsgRd := el["messages_ready"].(float64)
result[queueName] = int(floatMsgRd)
}
}
if greater > 0 {
filterResult := make(map[string]int)
for key, value := range result {
if value >= greater {
filterResult[key] = value
}
}
return filterResult
}

params := LoadEnvVariables(env)
client := &http.Client{}
req, err := http.NewRequest("GET", params.Url, nil)
req.SetBasicAuth(params.User, params.Pass)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
var body []map[string]any
var showQueue bool
if len(queue) > 0 && queue != "all" {
showQueue = true
} else {
showQueue = false
}
json.Unmarshal(bodyText, &body)
result := make(map[string]float64)
for _, el := range body {
queueName := el["name"].(string)
if showQueue != false {
if(strings.Contains(queueName, queue)){
result[queueName] = el["messages_ready"].(float64)
}
} else {
result[queueName] = el["messages_ready"].(float64)
}
}
return result
return result
}

8 changes: 4 additions & 4 deletions internal/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

// Do purge of the queues found from queue argument
func PurgeQueue(queue string, env string) {
func PurgeQueue(queue string, env string, greater int) {
params := LoadEnvVariables(env)
client := &http.Client{}
queues := GetQueues(queue, env)
sum := 0.0
queues := GetQueues(queue, env, greater)
sum := 0
for queueName, value := range queues {
url := (params.Url + "/" + queueName + "/contents")
sum += value
Expand All @@ -31,7 +31,7 @@ func PurgeQueue(queue string, env string) {
var prettyResult PrettyModelResult
prettyResult.Title = "Purged Queues"
prettyResult.Header = table.Row{"Purge Queue", "Messages"}
totalItems := 0.0
totalItems := 0
for queueName, value := range queues {
row := table.Row{queueName, value}
totalItems += value
Expand Down

0 comments on commit 445a5aa

Please sign in to comment.