(cmd *cobra.Command)
| 119 | } |
| 120 | |
| 121 | func getQueryTime(cmd *cobra.Command) (int64, int64, error) { |
| 122 | since, _ := cmd.Flags().GetString("since") |
| 123 | from, _ := cmd.Flags().GetString("from") |
| 124 | to, _ := cmd.Flags().GetString("to") |
| 125 | var fromTime, toTime time.Time |
| 126 | var err error |
| 127 | // priority: from&to > since |
| 128 | if from != "" { |
| 129 | fromTime, err = time.Parse(time.RFC3339, from) |
| 130 | if err != nil { |
| 131 | return 0, 0, err |
| 132 | } |
| 133 | } |
| 134 | if to != "" { |
| 135 | toTime, err = time.Parse(time.RFC3339, to) |
| 136 | if err != nil { |
| 137 | return 0, 0, err |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // when and only when from&to are null, use --since time |
| 142 | if toTime.IsZero() && fromTime.IsZero() { |
| 143 | if since == "" { |
| 144 | since = "1h" |
| 145 | } |
| 146 | sinceDuration, err := time.ParseDuration(since) |
| 147 | if err != nil { |
| 148 | return 0, 0, err |
| 149 | } |
| 150 | fromTime = time.Now().Add(-sinceDuration) |
| 151 | toTime = time.Now() |
| 152 | } |
| 153 | |
| 154 | if toTime.IsZero() { |
| 155 | toTime = time.Now() |
| 156 | } |
| 157 | |
| 158 | if fromTime.IsZero() { |
| 159 | fromTime = toTime.Add(-1 * time.Hour) |
| 160 | } |
| 161 | |
| 162 | if fromTime.After(toTime) { |
| 163 | return 0, 0, fmt.Errorf("query time from: %d should not greater than to: %d", fromTime.Unix(), toTime.Unix()) |
| 164 | } |
| 165 | |
| 166 | return fromTime.Unix(), toTime.Unix(), nil |
| 167 | } |
| 168 | |
| 169 | func statsQuery(cmd *cobra.Command, from, to int64, metric string, targetLabels, appLabels []string) error { |
| 170 | server := common.GetServerInfo(cmd) |
no test coverage detected