# `sc_crawler.vendor_helpers`

> Helpers shared by the vendor modules of sc_crawler.vendors, e.g. fetching the servers of all regions in parallel.

# `sc_crawler.vendor_helpers`

Helpers shared by the vendor modules of [sc_crawler.vendors](https://docs.sparecores.com/crawler/reference/vendors.md), e.g. fetching the servers of all regions in parallel.

**Functions:**

- [**server_status_from_availability_categories**](#server_status_from_availability_categories) – Map vendor availability categories to a single Server/Database status.
- [**retirement_date_from_rules**](#retirement_date_from_rules) – Retirement date of the first `(pattern, date)` rule fully matching the SKU `name`.
- [**status_from_retirement_date**](#status_from_retirement_date) – Server/Database status from a retirement date.
- [**fetch_servers**](#fetch_servers) – Fetch servers of a region/zone.
- [**parallel_fetch_servers**](#parallel_fetch_servers) – Fetch servers of all regions/zones in parallel on 8 threads.
- [**preprocess_servers**](#preprocess_servers) – Preprocess servers before inserting into the database.
- [**add_vendor_id**](#add_vendor_id) – Adds `vendor_id` field to a dict.
- [**get_region_by_id**](#get_region_by_id) – Get a [region](https://docs.sparecores.com/crawler/reference/tables.md#region) by its ID or alias.
- [**disk_speed_totals**](#disk_speed_totals) – Server `storage_*` speed columns summed over the disks with known values.
- [**split_disk_speed**](#split_disk_speed) – Speed of one of `disks` identical disks from a published server total.

## func <code class="doc-name">server_status_from_availability_categories</code> [vendor_helpers.py:13–36](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L13-L36)

```python
server_status_from_availability_categories(categories, category_to_status, *, missing_status=Status.INACTIVE)
```

Map vendor availability categories to a single Server/Database status.

Each category is mapped via ``category_to_status``. If more than one
status results, ``Status.best`` selects the best. Empty or unmapped
``categories`` return ``missing_status``.

## func <code class="doc-name">retirement_date_from_rules</code> [vendor_helpers.py:39–50](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L39-L50)

```python
retirement_date_from_rules(name, rules)
```

Retirement date of the first `(pattern, date)` rule fully matching the SKU `name`.

The rules are the vendor-documented retirement dates, e.g.
`_AZURE_RETIREMENT_RULES`. Returns `None` if no rule matches.

## func <code class="doc-name">status_from_retirement_date</code> [vendor_helpers.py:53–65](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L53-L65)

```python
status_from_retirement_date(retirement_date, as_of=None)
```

Server/Database status from a retirement date.

`RETIRED` from the retirement date (inclusive), `PLANNED_FOR_RETIREMENT`
before, and `ACTIVE` without a retirement date.

## func <code class="doc-name">fetch_servers</code> [vendor_helpers.py:68–86](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L68-L86)

```python
fetch_servers(fn, where, vendor)
```

Fetch servers of a region/zone.

**Parameters:**

- **fn** ([`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)) – A function that takes the region or zone id as its first and only argument.
The returning list must conform with the Server object, or need to
be in a format that [preprocess_servers](#preprocess_servers)'s
`fn` can manage.
- **where** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – A [Region](https://docs.sparecores.com/crawler/reference/tables.md#region) or [Zone](https://docs.sparecores.com/crawler/reference/tables.md#zone)
`api_reference` or similar that is passed to `fn`.
- **vendor** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)`]`) – Optional [Vendor](https://docs.sparecores.com/crawler/reference/tables.md#vendor) instance used for
logging and progress bar updates.

## func <code class="doc-name">parallel_fetch_servers</code> [vendor_helpers.py:89–121](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L89-L121)

```python
parallel_fetch_servers(vendor, fn, id_col, by)
```

Fetch servers of all regions/zones in parallel on 8 threads.

**Parameters:**

- **vendor** ([`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)) – Required [Vendor](https://docs.sparecores.com/crawler/reference/tables.md#vendor) instance used for
the regions lookup, logging and progress bar updates.
- **fn** ([`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)) – A function to be passed to [fetch_servers](#fetch_servers).
- **id_col** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – Field name to be used to deduplicate the list of server dicts.
- **by** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)`['regions'`, `'zones']`) – What objects of the `vendor` to iterate on.

## func <code class="doc-name">preprocess_servers</code> [vendor_helpers.py:124–144](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L124-L144)

```python
preprocess_servers(servers, vendor, fn)
```

Preprocess servers before inserting into the database.

Takes a list of dicts and tranform to a list of dicts that
follows the [Server](https://docs.sparecores.com/crawler/reference/tables.md#server) schema.

**Parameters:**

- **servers** ([`List`](https://docs.python.org/3/library/typing.html#typing.List)`[`[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)`]`) – To be passed to `fn`.
- **vendor** ([`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)) – The related [Vendor](https://docs.sparecores.com/crawler/reference/tables.md#vendor) instance used
for database connection, logging and progress bar updates.
- **fn** ([`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)) – A function that takes a server from `servers` (one-by-one) and the `vendor`.

## func <code class="doc-name">add_vendor_id</code> [vendor_helpers.py:147–150](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L147-L150)

```python
add_vendor_id(obj, vendor)
```

Adds `vendor_id` field to a dict.

## func <code class="doc-name">get_region_by_id</code> [vendor_helpers.py:153–170](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L153-L170)

```python
get_region_by_id(region_id, vendor)
```

Get a [region](https://docs.sparecores.com/crawler/reference/tables.md#region) by its ID or alias.

**Parameters:**

- **region_id** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – The ID or alias of the region to get.
- **vendor** ([`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)) – The [vendor](https://docs.sparecores.com/crawler/reference/tables.md#vendor) to get the region from.

**Returns:**

- [`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Region`](https://docs.sparecores.com/crawler/reference/tables.md#region)`]` – The [region](https://docs.sparecores.com/crawler/reference/tables.md#region) if found, otherwise None.

## func <code class="doc-name">disk_speed_totals</code> [vendor_helpers.py:173–181](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L173-L181)

```python
disk_speed_totals(storages)
```

Server `storage_*` speed columns summed over the disks with known values.

## func <code class="doc-name">split_disk_speed</code> [vendor_helpers.py:184–186](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/vendor_helpers.py#L184-L186)

```python
split_disk_speed(total, disks)
```

Speed of one of `disks` identical disks from a published server total.
