2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00

Add --json flag for asynq stats command

This commit is contained in:
Mahdi Dibaiee 2022-01-02 15:24:29 +00:00 committed by GitHub
parent c1f08106da
commit 2d0170541c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 9 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- The `asynq stats` command now supports a `--json` option, making its output a JSON object
## [0.20.0] - 2021-12-19 ## [0.20.0] - 2021-12-19
### Added ### Added

View File

@ -5,6 +5,7 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"math" "math"
@ -40,8 +41,11 @@ Example: watch -n 3 asynq stats -> Shows current state of tasks every three seco
Run: stats, Run: stats,
} }
var jsonFlag bool
func init() { func init() {
rootCmd.AddCommand(statsCmd) rootCmd.AddCommand(statsCmd)
statsCmd.Flags().BoolVar(&jsonFlag, "json", false, "Output stats in JSON format.")
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
@ -55,15 +59,21 @@ func init() {
} }
type AggregateStats struct { type AggregateStats struct {
Active int Active int `json:"active"`
Pending int Pending int `json:"pending"`
Scheduled int Scheduled int `json:"scheduled"`
Retry int Retry int `json:"retry"`
Archived int Archived int `json:"archived"`
Completed int Completed int `json:"completed"`
Processed int Processed int `json:"processed"`
Failed int Failed int `json:"failed"`
Timestamp time.Time 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) { func stats(cmd *cobra.Command, args []string) {
@ -104,6 +114,23 @@ func stats(cmd *cobra.Command, args []string) {
fmt.Println(err) fmt.Println(err)
os.Exit(1) 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 := color.New(color.Bold)
bold.Println("Task Count by State") bold.Println("Task Count by State")
printStatsByState(&aggStats) printStatsByState(&aggStats)