Skip to content

Commit 963b4b3

Browse files
authored
Merge pull request #26 from doneill/task/jdo-45-reduce-patrols-data
Update patrols data
2 parents b841446 + 2e4a390 commit 963b4b3

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

api/apipatrols.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Patrol struct {
3333
}
3434

3535
type PatrolSegment struct {
36-
ID string `json:"id"`
36+
ID string `json:"id"`
3737
Leader *struct {
3838
Name string `json:"name"`
3939
} `json:"leader"`
@@ -68,6 +68,7 @@ type DateRangeFilter struct {
6868
func (c *Client) Patrols(days int, status string) (*PatrolsResponse, error) {
6969
params := url.Values{}
7070
params.Add("exclude_empty_patrols", "true")
71+
params.Add("page_size", "200")
7172

7273
if status != "" {
7374
params.Add("status", status)

api/ersvc.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package api
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
7+
"net"
68
"net/http"
9+
"os"
10+
"strings"
711
"time"
812
)
913

1014
// ----------------------------------------------
1115
// const endpoints
1216
// ----------------------------------------------
17+
1318
const DOMAIN = ".pamdas.org"
1419

1520
const API_V1 = "/api/v1.0"
@@ -85,7 +90,7 @@ func (c *Client) newRequest(method, endpoint string, isAuth bool) (*http.Request
8590
func (c *Client) doRequest(req *http.Request, v interface{}) error {
8691
resp, err := c.httpClient.Do(req)
8792
if err != nil {
88-
return fmt.Errorf("failed to make request: %w", err)
93+
return c.handleRequestError(err)
8994
}
9095
defer resp.Body.Close()
9196

@@ -100,6 +105,36 @@ func (c *Client) doRequest(req *http.Request, v interface{}) error {
100105
return nil
101106
}
102107

108+
func (c *Client) handleRequestError(err error) error {
109+
// Check for timeout errors
110+
if os.IsTimeout(err) {
111+
return fmt.Errorf("request timed out - try reducing the requested data and/or check your network connection")
112+
}
113+
114+
// Check for context deadline exceeded (another form of timeout)
115+
if err == context.DeadlineExceeded || strings.Contains(err.Error(), "context deadline exceeded") {
116+
return fmt.Errorf("request timed out - try reducing the date range with --days or check your network connection")
117+
}
118+
119+
// Check for network timeout specifically
120+
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
121+
return fmt.Errorf("network timeout - try reducing the date range with --days or check your network connection")
122+
}
123+
124+
// Check for DNS resolution errors
125+
if strings.Contains(err.Error(), "no such host") {
126+
return fmt.Errorf("unable to connect to EarthRanger server - check your network connection and site name")
127+
}
128+
129+
// Check for connection refused
130+
if strings.Contains(err.Error(), "connection refused") {
131+
return fmt.Errorf("connection refused - check your network connection and EarthRanger server status")
132+
}
133+
134+
// Default error message for other request failures
135+
return fmt.Errorf("failed to connect to EarthRanger server: %v", err)
136+
}
137+
103138
// ----------------------------------------------
104139
// Helper functions
105140
// ----------------------------------------------

cmd/patrols.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ var patrolsCmd = &cobra.Command{
3737
return fmt.Errorf("invalid status value: %s\nValid status values are: active, done, cancelled", status)
3838
}
3939
}
40+
if days > 30 {
41+
return fmt.Errorf("days value cannot exceed 30 (got %d)", days)
42+
}
43+
if days < 0 {
44+
return fmt.Errorf("days value must be positive (got %d)", days)
45+
}
4046
return nil
4147
},
4248
Run: func(cmd *cobra.Command, args []string) {
@@ -126,13 +132,13 @@ func formatPatrolData(patrol *api.Patrol) []string {
126132
fmt.Sprintf("%d", patrol.SerialNumber),
127133
patrol.State,
128134
patrol.ID,
129-
segmentID,
130135
title,
131136
leader,
132137
startLocation,
133138
endLocation,
134139
startTime,
135140
endTime,
141+
segmentID,
136142
}
137143
}
138144

@@ -142,13 +148,13 @@ func configurePatrolsTable() *tablewriter.Table {
142148
"Serial",
143149
"State",
144150
"ID",
145-
"Segment ID",
146151
"Title",
147152
"Leader",
148153
"Start Location",
149154
"End Location",
150155
"Start Time",
151156
"End Time",
157+
"Segment ID",
152158
})
153159
table.SetBorders(tablewriter.Border{
154160
Left: true,

0 commit comments

Comments
 (0)