sc_crawler.table_bases
Tiny helper classes for the most commonly used fields to be inherited by sc_crawler.tables.
Classes:
- ScMetaModel – Custom class factory to auto-update table models.
- ScModel – Custom extensions to SQLModel objects and tables.
- MetaColumns – Helper class to add the
statusandobserved_atcolumns. - HasPriceFieldsBase –
- ServerFields –
- BenchmarkFields –
- ResourceIdComparator – Compile server_id/database_id to CASE + typed filter.
- BenchmarkScoreFields –
- BenchmarkScoreBase –
- ArchiveRuleFields –
class ScMetaModel table_bases.py:57–129
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 TABLEstatements, 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 withouttable=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
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
Attributessection 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
matchorsuccessorof 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
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
get_table_name()
Return the SQLModel object's table name.
meth ScModel.get_validator table_bases.py:161–166
get_validator()
Return the parent Base Pydantic model (without a table definition).
meth ScModel.get_scd table_bases.py:168–179
get_scd()
Return the SCD version of the SQLModel table.
meth ScModel.get_parent_tables table_bases.py:181–205
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
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:
- rule_table_name (
str) – Thetable_nameof the rule. - keys (
dict) – Thematchorsuccessorof the rule.
Returns:
Optional[dict]– Dict of column names and values of the table, orNoneif the rule does not apply to the table.
meth ScModel.get_filters_condition table_bases.py:230–235
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
matches_rule(rule, primary_key)
Check if an archive rule archives the record with the given primary key.
Parameters:
- rule (
ArchiveRule) – An ArchiveRule. - primary_key (
dict) – Primary key columns of the record with their values.
meth ScModel.is_archived table_bases.py:253–263
is_archived(primary_key, rules=None)
Check if any of the archive rules archives the record with the given primary key.
Parameters:
- primary_key (
dict) – Primary key columns of the record with their values. - rules (
Optional[list]) – ArchiveRules to check. Defaults to all current rules.
meth ScModel.archive_condition table_bases.py:265–286
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:
- rules (
Optional[list]) – ArchiveRules to apply. Defaults to all current rules.
Returns:
Optional[ColumnElement]– SQLAlchemy condition, orNoneif no rule applies to the table.
meth ScModel.archived_by table_bases.py:288–335
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) – All primary key columns of the record with their values. Other keys (e.g.observed_atof an SCD record) are ignored.
Returns:
list – The matching ArchiveRules, ordered by table name,matchandsuccessor.
meth ScModel.pk_key table_bases.py:337–341
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
hash(session, ignored=['observed_at'], progress=None)
Hash the content of the rows.
Parameters:
- session (
Session) – Database connection to use for object lookups. - ignored (
List[str]) – List of column names to exclude from hashing. - progress (
Optional[Progress]) – Optional progress bar to track the status of the hashing.
Returns:
dict – Dictionary of the row hashes keyed by the JSON dump of primary keys.
class MetaColumns table_bases.py:401–422
Bases: ScModel
Helper class to add the status and observed_at columns.
class HasPriceFieldsBase table_bases.py:579–612
Bases: ScModel
class ServerFields table_bases.py:819–1129
Bases: HasDisplayName, HasApiReferenceObject, HasApiReference, HasName, HasServerIdPK, HasVendorPKFK
class BenchmarkFields table_bases.py:1460–1535
Bases: HasBenchmarkIdPK
class ResourceIdComparator table_bases.py:1658–1676
Bases: Comparator
Compile server_id/database_id to CASE + typed filter.
class BenchmarkScoreFields table_bases.py:1679–1789
Bases: HasBenchmarkPKFK, HasVendorPKFK
Functions:
- update_config_to_hashable – Accept
server_id/database_idkeys; hashably sort config.
meth BenchmarkScoreFields.update_config_to_hashable table_bases.py:1694–1708
update_config_to_hashable(values)
Accept server_id / database_id keys; hashably sort config.
class BenchmarkScoreBase table_bases.py:1792–1816
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_typeandresource_id.
meth BenchmarkScoreBase.get_parent_tables table_bases.py:1796–1800
get_parent_tables()
Parent tables of the scores, including the resources they refer to.
meth BenchmarkScoreBase.get_key_filters table_bases.py:1802–1816
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
Bases: ScModel