Skip to content

Commit

Permalink
sort scan results by rack, board, and device (#193)
Browse files Browse the repository at this point in the history
* sort scan results by rack, board, and device

* merge sorting, update docs
  • Loading branch information
edaniszewski committed Jul 16, 2018
1 parent 371e56a commit a1096a6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
57 changes: 42 additions & 15 deletions docs/api/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,39 +449,64 @@ response = requests.get('http://host:5000/synse/v2/scan')
"id": "vec",
"devices": [
{
"id": "eb100067acb0c054cf877759db376b03",
"info": "Synse Temperature Sensor 1",
"id": "0fe8f06229aa9a01ef6032d1ddaf18a5",
"info": "Synse Temperature Sensor 3",
"type": "temperature"
},
{
"id": "83cc1efe7e596e4ab6769e0c6e3edf88",
"id": "12835beffd3e6c603aa4dd92127707b5",
"info": "Synse Fan",
"type": "fan"
},
{
"id": "12ea5644d052c6bf1bca3c9864fd8a44",
"info": "Synse LED",
"type": "led"
},
{
"id": "34c226b1afadaae5f172a4e1763fd1a6",
"info": "Synse Humidity Sensor",
"type": "humidity"
},
{
"id": "3ee84834c79c5a124d858e237e81e186",
"info": "Synse Temperature Sensor 2",
"type": "temperature"
},
{
"id": "329a91c6781ce92370a3c38ba9bf35b2",
"info": "Synse Temperature Sensor 4",
"id": "45ffe8f7f7a2b0ae970b687abd06f9e6",
"info": "Synse Temperature Sensor 1",
"type": "temperature"
},
{
"id": "f97f284037b04badb6bb7aacd9654a4e",
"id": "8f7ac60be5c8a3815ce89753de138edf",
"info": "Synse Temperature Sensor 5",
"type": "temperature"
},
{
"id": "eb9a56f95b5bd6d9b51996ccd0f2329c",
"info": "Synse Fan",
"type": "fan"
"id": "bcf0618c50bff9121cb10d141d66f46f",
"info": "Synse backup LED",
"type": "led"
},
{
"id": "f52d29fecf05a195af13f14c7306cfed",
"info": "Synse LED",
"type": "led"
"id": "df6a06d6e28da8aab0c25ee41688fd1c",
"info": "Synse Airflow Sensor",
"type": "airflow"
},
{
"id": "d29e0bd113a484dc48fd55bd3abad6bb",
"info": "Synse Backup LED",
"type": "led"
"id": "e385de0e2b5d16af5e34167d479fc766",
"info": "Synse Pressure Sensor 1",
"type": "pressure"
},
{
"id": "f441d97b2f6545ef3001a688489e820a",
"info": "Synse Temperature Sensor 4",
"type": "temperature"
},
{
"id":" f838b2d6afceb01e7a2634893f6f935c",
"info": "Synse Pressure Sensor 2",
"type": "pressure"
}
]
}
Expand All @@ -503,6 +528,8 @@ By default, `scan` will enumerate all devices on all boards on all racks. The `r
parameters, defined below, can be used to refine the scan to return devices only within the scope of
the given rack or board.

The scan results are sorted by rack id, board id, and plugin.

### HTTP Request

`GET http://host:5000/synse/v2/scan[/{rack}[/{board}]]`
Expand Down
34 changes: 20 additions & 14 deletions synse/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,24 +589,30 @@ def _build_scan_cache(device_info):
# Add the root 'racks' field to the scan data
scan_cache['racks'] = []

# Populate the rack info and add it to the scan data racks list
for ref in _tracked.values():
ref['rack']['boards'] = list(ref['boards'].values())
scan_cache['racks'].append(ref['rack'])

# Sort the scan cache by racks['id']
scan_cache['racks'] = sorted(scan_cache['racks'], key=lambda rck: rck['id'])

# Sort each rack by plugin and sort_ordinal.
for rack in scan_cache['racks']:
for board in rack['boards']:
board['devices'] = \
sorted(board['devices'], key=lambda o: (o['plugin'], o['sort_ordinal']))
# Delete the plugin and sort_ordinal keys after sorting.
# Sort the racks in the _tracked dictionary by rack id
sorted_rack_ids = sorted(_tracked.keys())
for rid in sorted_rack_ids:
rack_ref = _tracked[rid]
rack = rack_ref['rack']

# Sort each board on each rack by board id
sorted_board_ids = sorted(rack_ref['boards'].keys())
for bid in sorted_board_ids:
board = rack_ref['boards'][bid]

# Sort all devices on each board by plugin and sort ordinal
board['devices'] = sorted(
board['devices'],
key=lambda d: (d['plugin'], d['sort_ordinal'])
)

# Delete the plugin and sort_ordinal from the scan result after sorting.
for device in board['devices']:
del device['plugin']
del device['sort_ordinal']

rack['boards'].append(board)
scan_cache['racks'].append(rack)
return scan_cache


Expand Down

0 comments on commit a1096a6

Please sign in to comment.