table_bases
sc_crawler.table_bases
#
Tiny helper classes for the most commonly used fields to be inherited by sc_crawler.tables.
Classes:
| Name | Description |
|---|---|
ScMetaModel |
Custom class factory to auto-update table models. |
ScModel |
Custom extensions to SQLModel objects and tables. |
MetaColumns |
Helper class to add the |
HasPriceFieldsBase |
|
ServerFields |
|
BenchmarkFields |
|
ResourceIdComparator |
Compile server_id/database_id to CASE + typed filter. |
BenchmarkScoreFields |
|
BenchmarkScoreBase |
|
ArchiveRuleFields |
|
ScMetaModel
#
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.
Source code in sc_crawler/table_bases.py
ScModel
#
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.
Methods:
| Name | Description |
|---|---|
__tablename__ |
Override tables names using all-lowercase snake_case. |
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 |
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 |
JSON dump of the primary key columns of a row, as used in hash. |
hash |
Hash the content of the rows. |
Source code in sc_crawler/table_bases.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
__tablename__
#
Override tables names using all-lowercase snake_case.
get_columns
classmethod
#
Return the table's column names in a dict for all, primary keys, and attributes.
Source code in sc_crawler/table_bases.py
get_table_name
classmethod
#
get_validator
classmethod
#
Return the parent Base Pydantic model (without a table definition).
get_scd
classmethod
#
Return the SCD version of the SQLModel table.
Source code in sc_crawler/table_bases.py
get_parent_tables
cached
classmethod
#
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, region, zone and server.
Examples:
Source code in sc_crawler/table_bases.py
get_key_filters
classmethod
#
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:
| Name | Type | Description | Default |
|---|---|---|---|
rule_table_name
|
str
|
The |
required |
keys
|
dict
|
The |
required |
Returns:
| Type | Description |
|---|---|
Optional[dict]
|
Dict of column names and values of the table, or |
Source code in sc_crawler/table_bases.py
get_filters_condition
classmethod
#
SQL condition selecting the rows of the table with the given column values.
Source code in sc_crawler/table_bases.py
matches_rule
classmethod
#
Check if an archive rule archives the record with the given primary key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
ArchiveRule
|
An ArchiveRule. |
required |
primary_key
|
dict
|
Primary key columns of the record with their values. |
required |
Source code in sc_crawler/table_bases.py
is_archived
classmethod
#
Check if any of the archive rules archives the record with the given primary key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary_key
|
dict
|
Primary key columns of the record with their values. |
required |
rules
|
Optional[list]
|
ArchiveRules to check. Defaults to all current rules. |
None
|
Source code in sc_crawler/table_bases.py
archive_condition
classmethod
#
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:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
Optional[list]
|
ArchiveRules to apply. Defaults to all current rules. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[ColumnElement]
|
SQLAlchemy condition, or |
Source code in sc_crawler/table_bases.py
archived_by
classmethod
#
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:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Database connection. |
required |
primary_key
|
dict
|
All primary key columns of the record with their values.
Other keys (e.g. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The matching ArchiveRules, ordered
by table name, |
Source code in sc_crawler/table_bases.py
pk_key
classmethod
#
JSON dump of the primary key columns of a row, as used in hash.
Source code in sc_crawler/table_bases.py
hash
classmethod
#
Hash the content of the rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Database connection to use for object lookups. |
required |
ignored
|
List[str]
|
List of column names to exclude from hashing. |
['observed_at']
|
progress
|
Optional[Progress]
|
Optional progress bar to track the status of the hashing. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary of the row hashes keyed by the JSON dump of primary keys. |
Source code in sc_crawler/table_bases.py
MetaColumns
#
Bases: ScModel
Helper class to add the status and observed_at columns.
Source code in sc_crawler/table_bases.py
HasPriceFieldsBase
#
Bases: ScModel
Source code in sc_crawler/table_bases.py
ServerFields
#
Bases: HasDisplayName, HasApiReferenceObject, HasApiReference, HasName, HasServerIdPK, HasVendorPKFK
Source code in sc_crawler/table_bases.py
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 | |
BenchmarkFields
#
Bases: HasBenchmarkIdPK
Source code in sc_crawler/table_bases.py
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 | |
ResourceIdComparator
#
Bases: Comparator
Compile server_id/database_id to CASE + typed filter.
Source code in sc_crawler/table_bases.py
BenchmarkScoreFields
#
Bases: HasBenchmarkPKFK, HasVendorPKFK
Methods:
| Name | Description |
|---|---|
update_config_to_hashable |
Accept |
Source code in sc_crawler/table_bases.py
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 | |
update_config_to_hashable
#
Accept server_id / database_id keys; hashably sort config.
Source code in sc_crawler/table_bases.py
BenchmarkScoreBase
#
Bases: MetaColumns, BenchmarkScoreFields
Methods:
| Name | Description |
|---|---|
get_parent_tables |
Parent tables of the scores, including the resources they refer to. |
get_key_filters |
Map a rule on a resource onto |
Source code in sc_crawler/table_bases.py
get_parent_tables
cached
classmethod
#
Parent tables of the scores, including the resources they refer to.
get_key_filters
classmethod
#
Map a rule on a resource onto resource_type and resource_id.
Source code in sc_crawler/table_bases.py
ArchiveRuleFields
#
Bases: ScModel