Skip to content

Commit 28b5e06

Browse files
author
whipps
committed
Add filtering and sorting parameters to the orders list command
1 parent d99739e commit 28b5e06

File tree

3 files changed

+190
-38
lines changed

3 files changed

+190
-38
lines changed

docs/cli/cli-orders.md

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ then check out our [CLI Concepts](cli-intro.md) guide.
1515

1616
## Core Workflows
1717

18-
### See Recent Orders
18+
### List Orders
1919

20-
You can use the `list` command to show your recent orders:
20+
You can use the `list` command to see details of existing orders. By default they will be sorted
21+
by most recently created.
2122

2223
```sh
2324
planet orders list
@@ -26,24 +27,10 @@ planet orders list
2627
If you’ve not placed any orders with Explorer, the CLI or the API directly, then the results
2728
of this call will be blank, so you may want to try out some of the create order commands below.
2829

29-
Sometimes that list gets too long, so you can put a limit on how many are returned:
30+
Sometimes that list gets too long, so you can put a limit on how many are returned using the
31+
`--limit` parameter.
3032

31-
```sh
32-
planet orders list --limit 5
33-
```
34-
35-
You can also filter orders by their `state`, which can be useful to just see orders in
36-
progress:
37-
38-
```sh
39-
planet orders list --state running
40-
```
41-
42-
The other options are queued, failed, success, partial and cancelled.
43-
44-
Note you can also get a nice list online as well, at https://www.planet.com/account/#/orders
45-
46-
### Recent Orders with Formatting
33+
#### Formatting
4734

4835
You can also print the list of orders more nicely:
4936

@@ -71,10 +58,13 @@ planet orders list | jq -rs '.[] | "\(.id) \(.created_on) \(.state) \(.name)"'
7158

7259
You can customize which fields you want to show by changing the values.
7360

74-
### Number of recent orders
61+
A list of your orders can also be viewed in a UI at https://www.planet.com/account/#/orders
7562

76-
You can use jq to process the output for more insight, like
77-
get a count of how many recent orders you’ve done.
63+
#### Number of existing orders
64+
65+
You can use jq to process the output for more insight, like get a count of how many existing
66+
orders you have. Orders are automatically deleted 90 days after completion, so this number
67+
roughly is equivalent to the number of orders created in the last 90 days.
7868

7969
```sh
8070
planet orders list | jq -s length
@@ -83,6 +73,65 @@ planet orders list | jq -s length
8373
This uses `-s` to collect the output into a single array, and `length` then tells the
8474
length of the array.
8575

76+
#### Filtering
77+
78+
The `list` command supports filtering on a variety of fields:
79+
* `--state`: Filter by one or more states. Options include `queued`, `failed`, `success`, `partial` and `cancelled`.
80+
* `--source-type`: Filter by one or more source types. Options include `scenes`, `basemaps`, or `all`. The default is `all`.
81+
* `--name`: Filter by name.
82+
* `--name-contains`: Only return orders with a name that contains the provided string.
83+
* `--created-on`: Filter on the order creation time or an interval of creation times.
84+
* `--last-modified`: Filter on the order's last modified time or an interval of last modified times.
85+
* `--hosting`: Filter on orders containing a hosting location (e.g. SentinelHub). Accepted values are `true` or `false`.
86+
87+
Datetime args (`--created-on` and `--last-modified`) can either be a date-time or an interval, open or closed. Date and time expressions adhere to RFC 3339. Open intervals are expressed using double-dots.
88+
* A date-time: `2018-02-12T23:20:50Z`
89+
* A closed interval: `2018-02-12T00:00:00Z/2018-03-18T12:31:12Z`
90+
* Open intervals: `2018-02-12T00:00:00Z/..` or `../2018-03-18T12:31:12Z`
91+
92+
To list orders in progress:
93+
```sh
94+
planet orders list --state running
95+
```
96+
97+
To list orders with the `scenes` source type:
98+
```sh
99+
planet orders list --source-type scenes
100+
```
101+
102+
To list orders that were created in 2024:
103+
```sh
104+
planet orders list --created-on 2024-01-01T00:00:00Z/2025-01-01T00:00:00Z
105+
```
106+
107+
To list orders with a hosting location:
108+
```sh
109+
planet orders list --hosting true
110+
```
111+
112+
To list orders with the name `my location xyz`:
113+
```sh
114+
planet orders list --name "my location xyz"
115+
```
116+
117+
To list orders with a name containing `xyz`:
118+
```sh
119+
planet orders list --name-contains xyz
120+
```
121+
122+
#### Sorting
123+
124+
The `list` command also supports sorting the orders on one or more fields: `name`, `created_on`, `state`, and `last_modified`. The sort direction can be specified by appending ` ASC` or ` DESC` to the field name (default is ascending).
125+
126+
When multiple fields are specified, the sort order is applied in the order the fields are listed.
127+
128+
Sorting examples:
129+
* `--sort-by name`: sorts orders by name alphabetically.
130+
* `--sort-by "created_on DESC"`: sorts orders by most recently created.
131+
* `--sort-by "last_modified DESC"`: sorts subscriptions by most recently modified.
132+
* `--sort-by "state ASC"`: sorts orders by state alphabetically.
133+
* `--sort-by "name,last_modified DESC"`: sorts subscriptions by name first, and most recently modified second.
134+
86135
### Info on an Order
87136

88137
For a bit more information about an order, including the location of any downloads, use

planet/cli/orders.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,74 @@ def orders(ctx, base_url):
5454
@translate_exceptions
5555
@coro
5656
@click.option('--state',
57-
help='Filter orders to given state.',
57+
help='Filter by one or more states.',
58+
multiple=True,
5859
type=click.Choice(planet.clients.orders.ORDER_STATE_SEQUENCE,
5960
case_sensitive=False))
61+
@click.option(
62+
'--source-type',
63+
default=["all"],
64+
multiple=True,
65+
help="""Filter by one or more source types. See documentation for all
66+
available types. Default is all.""")
67+
@click.option('--name', help="filter by name")
68+
@click.option(
69+
'--name-contains',
70+
help="only return orders with a name that contains the provided string.")
71+
@click.option(
72+
'--created-on',
73+
help="Filter by creation time or interval. See documentation for examples."
74+
)
75+
@click.option(
76+
'--last-modified',
77+
help="Filter by creation time or interval. See documentation for examples."
78+
)
79+
@click.option(
80+
'--hosting',
81+
type=click.BOOL,
82+
help="Filter by presence of hosting block (e.g. SentinelHub hosting).")
83+
@click.option(
84+
'--sort-by',
85+
help="""fields to sort orders by. Multiple fields can be specified,
86+
separated by commas. The sort direction can be specified by appending
87+
' ASC' or ' DESC' to the field name. The default sort direction is
88+
ascending. When multiple fields are specified, the sort order is applied
89+
in the order the fields are listed.
90+
91+
Supported fields: [name, created_on, state, last_modified].
92+
93+
Example: 'name ASC,created_on DESC'""")
6094
@limit
6195
@pretty
62-
async def list(ctx, state, limit, pretty):
96+
async def list(ctx,
97+
state,
98+
source_type,
99+
name,
100+
name_contains,
101+
created_on,
102+
last_modified,
103+
hosting,
104+
sort_by,
105+
limit,
106+
pretty):
63107
"""List orders
64108
65109
This command prints a sequence of the returned order descriptions,
66110
optionally pretty-printed.
67111
68-
Order descriptions are sorted by creation date with the last created order
112+
By default, order descriptions are sorted by creation date with the last created order
69113
returned first.
70114
"""
71115
async with orders_client(ctx) as cl:
72-
async for o in cl.list_orders(state=state, limit=limit):
116+
async for o in cl.list_orders(state=state,
117+
source_type=source_type,
118+
name=name,
119+
name__contains=name_contains,
120+
created_on=created_on,
121+
last_modified=last_modified,
122+
hosting=hosting,
123+
sort_by=sort_by,
124+
limit=limit):
73125
echo_json(o, pretty)
74126

75127

planet/clients/orders.py

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import asyncio
1717
import logging
1818
import time
19-
from typing import AsyncIterator, Callable, List, Optional
19+
from typing import AsyncIterator, Callable, List, Optional, Sequence
2020
import uuid
2121
import json
2222
import hashlib
@@ -463,11 +463,18 @@ async def wait(self,
463463
return current_state
464464

465465
async def list_orders(self,
466-
state: Optional[str] = None,
466+
state: Optional[Sequence[str]] = None,
467+
source_type: Optional[Sequence[str]] = None,
468+
name: Optional[str] = None,
469+
name__contains: Optional[str] = None,
470+
created_on: Optional[str] = None,
471+
last_modified: Optional[str] = None,
472+
hosting: Optional[bool] = None,
473+
sort_by: Optional[str] = None,
467474
limit: int = 100) -> AsyncIterator[dict]:
468475
"""Iterate over the list of stored orders.
469476
470-
Order descriptions are sorted by creation date with the last created
477+
By default, order descriptions are sorted by creation date with the last created
471478
order returned first.
472479
473480
Note:
@@ -476,10 +483,38 @@ async def list_orders(self,
476483
single result description or return a list of descriptions.
477484
478485
Parameters:
479-
state: Filter orders to given state.
480-
limit: Maximum number of results to return. When set to 0, no
486+
state (Set[str]): include orders with a state in this set.
487+
source_type (Set[str]): include orders with a source type in this set.
488+
name (str): filter by name.
489+
name__contains (str): only include orders with names containing this string.
490+
created_on (str): filter by creation date-time or interval.
491+
last_modified (str): filter by last modified date-time or interval.
492+
hosting (bool): only return orders that contain a hosting block
493+
(e.g. SentinelHub hosting).
494+
sort_by (str): fields to sort orders by. Multiple fields can be specified,
495+
separated by commas. The sort direction can be specified by appending
496+
' ASC' or ' DESC' to the field name. The default sort direction is
497+
ascending. When multiple fields are specified, the sort order is applied
498+
in the order the fields are listed.
499+
500+
Supported fields: name, created_on, state, last_modified
501+
502+
Examples:
503+
* "name"
504+
* "name DESC"
505+
* "name,state DESC,last_modified"
506+
limit (int): maximum number of results to return. When set to 0, no
481507
maximum is applied.
482508
509+
Datetime args (created_on and last_modified) can either be a date-time or an
510+
interval, open or closed. Date and time expressions adhere to RFC 3339. Open
511+
intervals are expressed using double-dots.
512+
513+
Examples:
514+
* A date-time: "2018-02-12T23:20:50Z"
515+
* A closed interval: "2018-02-12T00:00:00Z/2018-03-18T12:31:12Z"
516+
* Open intervals: "2018-02-12T00:00:00Z/.." or "../2018-03-18T12:31:12Z"
517+
483518
Yields:
484519
Description of an order.
485520
@@ -488,14 +523,30 @@ async def list_orders(self,
488523
planet.exceptions.ClientError: If state is not valid.
489524
"""
490525
url = self._orders_url()
491-
params = {"source_type": "all"}
492-
526+
params = {}
527+
if source_type is not None:
528+
params["source_type"] = [val for val in source_type]
529+
else:
530+
params["source_type"] = "all"
531+
if name is not None:
532+
params["name"] = name
533+
if name__contains is not None:
534+
params["name__contains"] = name__contains
535+
if created_on is not None:
536+
params["created_on"] = created_on
537+
if last_modified is not None:
538+
params["last_modified"] = last_modified
539+
if hosting is not None:
540+
params["hosting"] = hosting
541+
if sort_by is not None:
542+
params["sort_by"] = sort_by
493543
if state:
494-
if state not in ORDER_STATE_SEQUENCE:
495-
raise exceptions.ClientError(
496-
f'Order state ({state}) is not a valid state. '
497-
f'Valid states are {ORDER_STATE_SEQUENCE}')
498-
params['state'] = state
544+
for s in state:
545+
if s not in ORDER_STATE_SEQUENCE:
546+
raise exceptions.ClientError(
547+
f'Order state ({s}) is not a valid state. '
548+
f'Valid states are {ORDER_STATE_SEQUENCE}')
549+
params['state'] = [val for val in state]
499550

500551
response = await self._session.request(method='GET',
501552
url=url,

0 commit comments

Comments
 (0)