Skip to content

Commit

Permalink
Add --json flag for asynq stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
mdibaiee authored Jan 2, 2022
1 parent c1f0810 commit 2d01705
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- The `asynq stats` command now supports a `--json` option, making its output a JSON object

## [0.20.0] - 2021-12-19

### Added
Expand Down
45 changes: 36 additions & 9 deletions tools/asynq/cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -40,8 +41,11 @@ Example: watch -n 3 asynq stats -> Shows current state of tasks every three seco
Run: stats,
}

var jsonFlag bool

func init() {
rootCmd.AddCommand(statsCmd)
statsCmd.Flags().BoolVar(&jsonFlag, "json", false, "Output stats in JSON format.")

// Here you will define your flags and configuration settings.

Expand All @@ -55,15 +59,21 @@ func init() {
}

type AggregateStats struct {
Active int
Pending int
Scheduled int
Retry int
Archived int
Completed int
Processed int
Failed int
Timestamp time.Time
Active int `json:"active"`
Pending int `json:"pending"`
Scheduled int `json:"scheduled"`
Retry int `json:"retry"`
Archived int `json:"archived"`
Completed int `json:"completed"`
Processed int `json:"processed"`
Failed int `json:"failed"`
Timestamp time.Time `json:"timestamp"`
}

type FullStats struct {
Aggregate AggregateStats `json:"aggregate"`
QueueStats []*rdb.Stats `json:"queues"`
RedisInfo map[string]string `json:"redis"`
}

func stats(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -104,6 +114,23 @@ func stats(cmd *cobra.Command, args []string) {
fmt.Println(err)
os.Exit(1)
}

if jsonFlag {
statsJSON, err := json.Marshal(FullStats{
Aggregate: aggStats,
QueueStats: stats,
RedisInfo: info,
})

if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(string(statsJSON))
return
}

bold := color.New(color.Bold)
bold.Println("Task Count by State")
printStatsByState(&aggStats)
Expand Down

0 comments on commit 2d01705

Please sign in to comment.