Skip to main content

sc_crawler.table_bases

Tiny helper classes for the most commonly used fields to be inherited by sc_crawler.tables.

Classes:

class ScMetaModel table_bases.py:57–129⁠ (opens in a new tab)​

Bases: __class__

Custom class factory to auto-update table models.

  • Reuse description of the table and its fields as SQL comment.

    Checking if the table and its fields have explicit comment set to be shown in the CREATE TABLE statements, and if not, reuse the optional table and field descriptions. Table docstrings are truncated to first line.

  • Reuse description of the fields to dynamically append to the docstring in the Attributes section.

  • Set __validator__ to the parent Pydantic model without table=True, which is useful for running validations. The Pydantic model is found by the parent class' name ending in "Base".

  • Auto-generate SCD table docs from the non-SCD table docs.

class ScModel table_bases.py:132–398⁠ (opens in a new tab)​

Bases: SQLModel

Custom extensions to SQLModel objects and tables.

Extra features:

  • auto-generated table names using snake_case,
  • support for hashing table rows,
  • reuse description field of tables/columns as SQL comment,
  • reuse description field of columns to extend the Attributes section of the docstring.

Functions:

  • get_columns – Return the table's column names in a dict for all, primary keys, and attributes.
  • get_table_name – Return the SQLModel object's table name.
  • get_validator – Return the parent Base Pydantic model (without a table definition).
  • get_scd – Return the SCD version of the SQLModel table.
  • get_parent_tables – Names of the tables that the records of this table belong to.
  • get_key_filters – Translate the match or successor of an archive rule into column filters on the table.
  • get_filters_condition – SQL condition selecting the rows of the table with the given column values.
  • matches_rule – Check if an archive rule archives the record with the given primary key.
  • is_archived – Check if any of the archive rules archives the record with the given primary key.
  • archive_condition – SQL condition selecting the rows of the table archived by the rules.
  • archived_by – List the archive rules archiving a record, including the rules on its parent tables.
  • pk_key – JSONJSON: JavaScript Object NotationA text format for structured data, built from objects (key-value pairs), arrays, strings, numbers, booleans, and null. Most languages and tools read and write it, which makes it a common format for API responses and benchmark output.JSONGlossary dump of the primary key columns of a row, as used in hash.
  • hash – Hash the content of the rows.

meth ScModel.get_columns table_bases.py:148–154⁠ (opens in a new tab)​

get_columns()

Return the table's column names in a dict for all, primary keys, and attributes.

meth ScModel.get_table_name table_bases.py:156–159⁠ (opens in a new tab)​

get_table_name()

Return the SQLModel object's table name.

meth ScModel.get_validator table_bases.py:161–166⁠ (opens in a new tab)​

get_validator()

Return the parent Base Pydantic model (without a table definition).

meth ScModel.get_scd table_bases.py:168–179⁠ (opens in a new tab)​

get_scd()

Return the SCD version of the SQLModel table.

meth ScModel.get_parent_tables table_bases.py:181–205⁠ (opens in a new tab)​

get_parent_tables()

Names of the tables that the records of this table belong to.

These are the tables referenced by a foreign key made up of primary key columns of this table, and (recursively) the parents of those tables. So a record cannot exist without its parent records, e.g. a server price without its vendor, regionregionA geographic area where a cloud vendor runs its data centers, such as Frankfurt or Northern Virginia, made up of one or more availability zones. A server type is offered in some regions only, and its price can differ by region; a region's ID, such as eu-central-1, is unique only within its vendor.Glossary, zone and server.

Examples:

>>> from sc_crawler.tables import Zone
>>> sorted(Zone.get_parent_tables())
['region', 'vendor']

meth ScModel.get_key_filters table_bases.py:207–228⁠ (opens in a new tab)​

get_key_filters(rule_table_name, keys)

Translate the match or successor of an archive rule into column filters on the table.

This defines which records an ArchiveRule refers to, for both the SQL conditions and matching primary keys in Python. Tables referencing a parent without a foreign key override it.

Parameters:

Returns:

meth ScModel.get_filters_condition table_bases.py:230–235⁠ (opens in a new tab)​

get_filters_condition(filters)

SQL condition selecting the rows of the table with the given column values.

meth ScModel.matches_rule table_bases.py:237–251⁠ (opens in a new tab)​

matches_rule(rule, primary_key)

Check if an archive rule archives the record with the given primary key.

Parameters:

meth ScModel.is_archived table_bases.py:253–263⁠ (opens in a new tab)​

is_archived(primary_key, rules=None)

Check if any of the archive rules archives the record with the given primary key.

Parameters:

meth ScModel.archive_condition table_bases.py:265–286⁠ (opens in a new tab)​

archive_condition(rules=None)

SQL condition selecting the rows of the table archived by the rules.

Child records are selected by the key columns of the rules instead of looking up their parents, so it also finds the child records of parents that are not in the database.

Parameters:

Returns:

meth ScModel.archived_by table_bases.py:288–335⁠ (opens in a new tab)​

archived_by(session, primary_key)

List the archive rules archiving a record, including the rules on its parent tables.

Reads the rules from the archive_rule table of the database, so it also returns the rules that were removed from the code since (stored with INACTIVE status), and uses the same matching as pull and sync. It does not look up the record itself.

Parameters:

  • session (Session) – Database connection.
  • primary_key (dict⁠ (opens in a new tab)) – All primary key columns of the record with their values. Other keys (e.g. observed_at of an SCD record) are ignored.

Returns:

meth ScModel.pk_key table_bases.py:337–341⁠ (opens in a new tab)​

pk_key(values)

JSON dump of the primary key columns of a row, as used in hash.

meth ScModel.hash table_bases.py:343–398⁠ (opens in a new tab)​

hash(session, ignored=['observed_at'], progress=None)

Hash the content of the rows.

Parameters:

Returns:

class MetaColumns table_bases.py:401–422⁠ (opens in a new tab)​

Bases: ScModel

Helper class to add the status and observed_at columns.

class HasPriceFieldsBase table_bases.py:579–612⁠ (opens in a new tab)​

Bases: ScModel

class ServerFields table_bases.py:819–1129⁠ (opens in a new tab)​

Bases: HasDisplayName, HasApiReferenceObject, HasApiReference, HasName, HasServerIdPK, HasVendorPKFK

class BenchmarkFields table_bases.py:1460–1535⁠ (opens in a new tab)​

Bases: HasBenchmarkIdPK

class ResourceIdComparator table_bases.py:1658–1676⁠ (opens in a new tab)​

Bases: Comparator⁠ (opens in a new tab)

Compile server_id/database_id to CASE + typed filter.

class BenchmarkScoreFields table_bases.py:1679–1789⁠ (opens in a new tab)​

Bases: HasBenchmarkPKFK, HasVendorPKFK

Functions:

meth BenchmarkScoreFields.update_config_to_hashable table_bases.py:1694–1708⁠ (opens in a new tab)​

update_config_to_hashable(values)

Accept server_id / database_id keys; hashably sort config.

class BenchmarkScoreBase table_bases.py:1792–1816⁠ (opens in a new tab)​

Bases: MetaColumns, BenchmarkScoreFields

Functions:

  • get_parent_tables – Parent tables of the scores, including the resources they refer to.
  • get_key_filters – Map a rule on a resource onto resource_type and resource_id.

meth BenchmarkScoreBase.get_parent_tables table_bases.py:1796–1800⁠ (opens in a new tab)​

get_parent_tables()

Parent tables of the scores, including the resources they refer to.

meth BenchmarkScoreBase.get_key_filters table_bases.py:1802–1816⁠ (opens in a new tab)​

get_key_filters(rule_table_name, keys)

Map a rule on a resource onto resource_type and resource_id.

class ArchiveRuleFields table_bases.py:1819–1859⁠ (opens in a new tab)​

Bases: ScModel