2020-07-27 18:57:36 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
import json
|
2023-07-18 15:20:02 +00:00
|
|
|
import logging
|
2021-06-22 15:12:25 +00:00
|
|
|
|
|
|
|
import six
|
|
|
|
|
2021-06-28 19:05:37 +00:00
|
|
|
from column import is_visible_column
|
2021-11-03 11:44:28 +00:00
|
|
|
import sort_specs
|
|
|
|
|
2023-07-18 15:20:02 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
ColInfo = namedtuple('ColInfo', ('colId', 'type', 'isFormula', 'formula',
|
|
|
|
'widgetOptions', 'label'))
|
|
|
|
|
|
|
|
|
2022-09-28 23:01:46 +00:00
|
|
|
def make_col_info(col=None, **values):
|
2020-07-27 18:57:36 +00:00
|
|
|
"""Return a ColInfo() with the given fields, optionally copying values from the given column."""
|
|
|
|
for key in ColInfo._fields:
|
|
|
|
values.setdefault(key, getattr(col, key) if col else None)
|
|
|
|
return ColInfo(**values)
|
|
|
|
|
2022-04-05 14:36:22 +00:00
|
|
|
def _make_sum_col_info(col):
|
|
|
|
"""Return a ColInfo() for the sum formula column for column col."""
|
2022-09-28 23:01:46 +00:00
|
|
|
return make_col_info(col=col, isFormula=True,
|
2022-04-05 14:36:22 +00:00
|
|
|
formula='SUM($group.%s)' % col.colId)
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
|
2022-09-28 23:01:46 +00:00
|
|
|
def get_colinfo_dict(col_info, with_id=False):
|
2020-07-27 18:57:36 +00:00
|
|
|
"""Return a dict suitable to use with AddColumn or AddTable (when with_id=True) actions."""
|
2021-06-28 19:05:37 +00:00
|
|
|
col_values = {k: v for k, v in six.iteritems(col_info._asdict())
|
|
|
|
if v is not None and k != 'colId'}
|
2020-07-27 18:57:36 +00:00
|
|
|
if with_id:
|
|
|
|
col_values['id'] = col_info.colId
|
|
|
|
return col_values
|
|
|
|
|
|
|
|
|
2022-04-27 15:53:47 +00:00
|
|
|
def skip_rules_update(col, col_values):
|
|
|
|
"""
|
|
|
|
Rules for summary tables can't be derived from source columns. This function
|
|
|
|
removes (and kips original) rules settings when updating summary tables.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Remove rules from updates.
|
|
|
|
col_values = {k: v for k, v in six.iteritems(col_values) if k != 'rules'}
|
|
|
|
|
|
|
|
try:
|
|
|
|
# New widgetOptions to use.
|
|
|
|
new_widgetOptions = json.loads(col_values.get('widgetOptions', ''))
|
|
|
|
except ValueError:
|
|
|
|
# If we are not updating widgetOptions (or they are
|
|
|
|
# not a valid json string, i.e. in tests), just return the original updates.
|
|
|
|
return col_values
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Original widgetOptions (maybe with styling rules "ruleOptions").
|
|
|
|
widgetOptions = json.loads(col.widgetOptions or '')
|
|
|
|
except ValueError:
|
|
|
|
widgetOptions = {}
|
|
|
|
|
|
|
|
# Keep the original rulesOptions if any, and ignore any new one.
|
|
|
|
new_widgetOptions.pop("rulesOptions", "")
|
|
|
|
rulesOptions = widgetOptions.get('rulesOptions')
|
|
|
|
if rulesOptions:
|
|
|
|
new_widgetOptions['rulesOptions'] = rulesOptions
|
|
|
|
|
|
|
|
col_values['widgetOptions'] = json.dumps(new_widgetOptions)
|
|
|
|
return col_values
|
|
|
|
|
|
|
|
|
2022-03-22 13:41:11 +00:00
|
|
|
def _copy_widget_options(options):
|
|
|
|
"""Copies widgetOptions for a summary group-by column (omitting conditional formatting rules)"""
|
|
|
|
if not options:
|
|
|
|
return options
|
|
|
|
try:
|
|
|
|
options = json.loads(options)
|
|
|
|
except ValueError:
|
|
|
|
# widgetOptions are not always a valid json value (especially in tests)
|
|
|
|
return options
|
|
|
|
return json.dumps({k: v for k, v in options.items() if k != "rulesOptions"})
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
def encode_summary_table_name(source_table_id, groupby_col_ids):
|
2020-07-27 18:57:36 +00:00
|
|
|
"""
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
Create a summary table name based on the source table ID and the groupby column IDs.
|
2020-07-27 18:57:36 +00:00
|
|
|
"""
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
result = source_table_id + '_summary'
|
|
|
|
if groupby_col_ids:
|
|
|
|
result += '_' + '_'.join(sorted(groupby_col_ids))
|
|
|
|
return result
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
def decode_summary_table_name(summary_table_info):
|
2020-07-27 18:57:36 +00:00
|
|
|
"""
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
Extract the name of the source table from the summary table schema info.
|
2020-07-27 18:57:36 +00:00
|
|
|
"""
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
# To generate code, we need to know for each summary table, what its source table is. It would be
|
|
|
|
# easy if we had access to metadata records, but (at least for now) we generate all code based on
|
|
|
|
# schema only. So we use the type of special 'group' column in the summary table.
|
|
|
|
group_col = summary_table_info.columns.get('group')
|
|
|
|
if (
|
|
|
|
group_col
|
|
|
|
and 'getSummarySourceGroup' in group_col.formula
|
|
|
|
and group_col.type.startswith('RefList:')
|
|
|
|
):
|
|
|
|
return group_col.type[8:]
|
2020-07-27 18:57:36 +00:00
|
|
|
return None
|
|
|
|
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
def _group_colinfo(source_table):
|
|
|
|
"""Returns ColInfo() for the 'group' column that must be present in every summary table."""
|
2022-09-28 23:01:46 +00:00
|
|
|
return make_col_info(colId='group', type='RefList:%s' % source_table.tableId,
|
2020-07-27 18:57:36 +00:00
|
|
|
isFormula=True, formula='table.getSummarySourceGroup(rec)')
|
|
|
|
|
|
|
|
|
|
|
|
def _update_sort_spec(sort_spec, old_table, new_table):
|
|
|
|
"""
|
|
|
|
Replace column references in the sort spec (which is a JSON string encoding a list of column
|
|
|
|
refs, negated for descending) with references to the new table. Returns the new JSON string,
|
|
|
|
or empty string in case of a problem.
|
|
|
|
"""
|
|
|
|
old_cols_map = {c.id: c.colId for c in old_table.columns}
|
|
|
|
new_cols_map = {c.colId: c.id for c in new_table.columns}
|
|
|
|
|
|
|
|
# When adjusting, we take a possibly negated old colRef, and produce a new colRef.
|
|
|
|
# If anything is gone, we return 0, which will be excluded from the new sort spec.
|
|
|
|
def adjust(col_spec):
|
2021-11-03 11:44:28 +00:00
|
|
|
old_colref = sort_specs.col_ref(col_spec)
|
|
|
|
new_colref = new_cols_map.get(old_cols_map.get(old_colref), 0)
|
|
|
|
return sort_specs.swap_col_ref(col_spec, new_colref)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
old_sort_spec = json.loads(sort_spec)
|
2021-06-22 15:12:25 +00:00
|
|
|
new_sort_spec = [adjust(col_spec) for col_spec in old_sort_spec]
|
2021-11-03 11:44:28 +00:00
|
|
|
new_sort_spec = [col_spec for col_spec in new_sort_spec if sort_specs.col_ref(col_spec)]
|
2020-07-27 18:57:36 +00:00
|
|
|
return json.dumps(new_sort_spec, separators=(',', ':'))
|
2021-06-22 15:12:25 +00:00
|
|
|
except Exception:
|
2023-07-18 15:20:02 +00:00
|
|
|
log.warning("update_summary_section: can't parse sortColRefs JSON; clearing sortColRefs")
|
2020-07-27 18:57:36 +00:00
|
|
|
return ''
|
|
|
|
|
|
|
|
|
2021-08-11 17:45:24 +00:00
|
|
|
def summary_groupby_col_type(source_type):
|
|
|
|
"""
|
|
|
|
Returns the type of a groupby column in a summary table
|
|
|
|
given the type of the corresponding column in the source table.
|
|
|
|
Most types are returned unchanged.
|
|
|
|
When a source table is grouped by a list-type (RefList/ChoiceList) column
|
|
|
|
the column is 'flattened' into the corresponding non-list type
|
|
|
|
in the summary table.
|
|
|
|
"""
|
|
|
|
if source_type == 'ChoiceList':
|
|
|
|
return 'Choice'
|
|
|
|
else:
|
|
|
|
return source_type.replace('RefList:', 'Ref:')
|
|
|
|
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
class SummaryActions(object):
|
|
|
|
|
|
|
|
def __init__(self, useractions, docmodel):
|
|
|
|
self.useractions = useractions
|
|
|
|
self.docmodel = docmodel
|
|
|
|
|
|
|
|
def _get_or_add_columns(self, table, all_colinfo):
|
|
|
|
"""
|
|
|
|
Given a table record and a list of ColInfo objects, generates a list of corresponding column
|
|
|
|
records in the table, creating appropriate columns if they don't yet exist.
|
|
|
|
"""
|
|
|
|
prior = {c.colId: c for c in table.columns}
|
|
|
|
for ci in all_colinfo:
|
|
|
|
col = prior.get(ci.colId)
|
2023-03-14 16:11:26 +00:00
|
|
|
if col and col.formula == ci.formula:
|
2020-07-27 18:57:36 +00:00
|
|
|
yield col
|
|
|
|
else:
|
|
|
|
result = self.useractions.doAddColumn(table.tableId, ci.colId,
|
2022-09-28 23:01:46 +00:00
|
|
|
get_colinfo_dict(ci, with_id=False))
|
2020-07-27 18:57:36 +00:00
|
|
|
yield self.docmodel.columns.table.get_record(result['colRef'])
|
|
|
|
|
2022-03-22 13:41:11 +00:00
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
def _get_or_create_summary(self, source_table, source_groupby_columns, formula_colinfo):
|
|
|
|
"""
|
|
|
|
Finds a summary table or creates a new one, based on source_table, grouped by the columns
|
|
|
|
in groupby_colinfo, and containing formulas in formula_colinfo. Source_table should be a
|
|
|
|
Record from _grist_Tables, and other arguments should be lists of ColInfo objects.
|
|
|
|
Returns the tuple (summary_table, groupby_columns, formula_columns).
|
|
|
|
"""
|
|
|
|
key = tuple(sorted(int(c) for c in source_groupby_columns))
|
|
|
|
|
2021-07-23 15:29:35 +00:00
|
|
|
groupby_colinfo = [
|
2022-09-28 23:01:46 +00:00
|
|
|
make_col_info(
|
2021-07-23 15:29:35 +00:00
|
|
|
col=c,
|
|
|
|
isFormula=False,
|
|
|
|
formula='',
|
2022-03-22 13:41:11 +00:00
|
|
|
widgetOptions=_copy_widget_options(c.widgetOptions),
|
2021-08-11 17:45:24 +00:00
|
|
|
type=summary_groupby_col_type(c.type)
|
2021-07-23 15:29:35 +00:00
|
|
|
)
|
|
|
|
for c in source_groupby_columns
|
|
|
|
]
|
2020-07-27 18:57:36 +00:00
|
|
|
summary_table = next((t for t in source_table.summaryTables if t.summaryKey == key), None)
|
|
|
|
created = False
|
|
|
|
if not summary_table:
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
groupby_col_ids = [c.colId for c in groupby_colinfo]
|
2020-07-27 18:57:36 +00:00
|
|
|
result = self.useractions.doAddTable(
|
(core) Nice summary table IDs
Summary:
Changes auto-generated summary table IDs from e.g. `GristSummary_6_Table1` to `Table1_summary_A_B` (meaning `Table1` grouped by `A` and `B`). This makes it easier to write formulas involving summary tables, make API requests, understand logs, etc.
Because these don't encode the source table ID as reliably as before, `decode_summary_table_name` now uses the summary table schema info, not just the summary table ID. Specifically, it looks at the type of the `group` column, which is `RefList:<source table id>`.
Renaming a source table renames the summary table as before, and now renaming a groupby column renames the summary table as well.
Conflicting table names are resolved in the usual way by adding a number at the end, e.g. `Table1_summary_A_B2`. These summary tables are not automatically renamed when the disambiguation is no longer needed.
A new migration renames all summary tables to the new scheme, and updates formulas using summary tables with a simple regex.
Test Plan:
Updated many tests to use the new style of name.
Added new Python tests to for resolving conflicts when renaming source tables and groupby columns.
Added a test for the migration, including renames in formulas.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3508
2022-07-11 18:00:25 +00:00
|
|
|
encode_summary_table_name(source_table.tableId, groupby_col_ids),
|
2022-09-28 23:01:46 +00:00
|
|
|
[get_colinfo_dict(ci, with_id=True) for ci in groupby_colinfo + formula_colinfo],
|
2022-07-06 07:41:09 +00:00
|
|
|
summarySourceTableRef=source_table.id,
|
2023-11-20 00:46:32 +00:00
|
|
|
raw_section=True,
|
|
|
|
record_card_section=False)
|
2020-07-27 18:57:36 +00:00
|
|
|
summary_table = self.docmodel.tables.table.get_record(result['id'])
|
|
|
|
created = True
|
|
|
|
# Note that in this case, _get_or_add_columns() below should not add any new columns,
|
|
|
|
# but only return existing ones. (The table may contain extra columns, e.g. 'manualSort',
|
|
|
|
# at least in theory.)
|
|
|
|
|
|
|
|
groupby_columns = list(self._get_or_add_columns(summary_table, groupby_colinfo))
|
|
|
|
formula_columns = list(self._get_or_add_columns(summary_table, formula_colinfo))
|
|
|
|
|
|
|
|
if created:
|
|
|
|
# Set the summarySourceCol field for all the group-by columns in the table.
|
|
|
|
self.docmodel.update(groupby_columns,
|
2021-06-28 19:05:37 +00:00
|
|
|
summarySourceCol=[c.id for c in source_groupby_columns],
|
|
|
|
visibleCol=[c.visibleCol for c in source_groupby_columns])
|
|
|
|
for col in groupby_columns:
|
|
|
|
self.useractions.maybe_copy_display_formula(col.summarySourceCol, col)
|
2020-07-27 18:57:36 +00:00
|
|
|
assert summary_table.summaryKey == key
|
|
|
|
|
|
|
|
return (summary_table, groupby_columns, formula_columns)
|
|
|
|
|
|
|
|
|
|
|
|
def update_summary_section(self, view_section, source_table, source_groupby_columns):
|
|
|
|
source_groupby_colset = set(source_groupby_columns)
|
|
|
|
groupby_colids = {c.colId for c in source_groupby_columns}
|
|
|
|
|
2022-04-26 09:01:47 +00:00
|
|
|
# Go through columns figuring out which ones we'll keep.
|
|
|
|
prev_group_cols, formula_colinfo = [], []
|
|
|
|
for col in view_section.tableRef.columns:
|
|
|
|
srcCol = col.summarySourceCol
|
2020-07-27 18:57:36 +00:00
|
|
|
# Records implement __hash__, so we can look them up in sets.
|
2022-04-05 14:36:22 +00:00
|
|
|
if srcCol in source_groupby_colset:
|
2022-04-26 09:01:47 +00:00
|
|
|
prev_group_cols.append(col)
|
|
|
|
elif col.isFormula and col.colId not in groupby_colids:
|
2022-09-28 23:01:46 +00:00
|
|
|
formula_colinfo.append(make_col_info(col))
|
2020-07-27 18:57:36 +00:00
|
|
|
else:
|
2022-04-05 14:36:22 +00:00
|
|
|
# if user is removing a numeric column from the group by columns we must add it back as a
|
|
|
|
# sum formula column
|
2022-04-26 09:01:47 +00:00
|
|
|
self._append_sister_column_if_any(formula_colinfo, source_table, srcCol)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
2022-04-26 09:01:47 +00:00
|
|
|
# All fields with a column that we don't keep, must be deleted
|
|
|
|
colid_keep_set = set(c.colId for c in prev_group_cols + formula_colinfo)
|
|
|
|
delete_fields = [f for f in view_section.fields if f.colRef.colId not in colid_keep_set]
|
2020-07-27 18:57:36 +00:00
|
|
|
|
2022-04-26 09:01:47 +00:00
|
|
|
have_group_col = any(ci.colId == 'group' for ci in formula_colinfo)
|
2020-07-27 18:57:36 +00:00
|
|
|
if not have_group_col:
|
|
|
|
formula_colinfo.append(_group_colinfo(source_table))
|
|
|
|
|
|
|
|
# Get column records for all the columns we should have in our section.
|
|
|
|
summary_table, groupby_columns, formula_columns = self._get_or_create_summary(
|
|
|
|
source_table, source_groupby_columns, formula_colinfo)
|
|
|
|
|
|
|
|
if not have_group_col:
|
|
|
|
# We've added the "group" column; now restore the lists to match what we want in fields.
|
|
|
|
formula_colinfo.pop()
|
|
|
|
formula_columns.pop()
|
|
|
|
|
|
|
|
# Remember the original table, which we need later to adjust the sort spec (sortColRefs).
|
|
|
|
orig_table = view_section.tableRef
|
|
|
|
|
|
|
|
# This line is a bit hard to explain: we unset viewSection.tableRef before updating all the
|
|
|
|
# fields, and then set it to the correct value. Note how undo will reverse the operations, and
|
|
|
|
# produce the same sequence (unset, update fields, set). Client-side code relies on this to
|
|
|
|
# avoid having to deal with inconsistent view sections while fields are being updated.
|
|
|
|
self.docmodel.update([view_section], tableRef=0)
|
|
|
|
|
|
|
|
# Delete fields no longer relevant.
|
|
|
|
self.docmodel.remove(delete_fields)
|
|
|
|
|
|
|
|
# Update fields for all formula fields and reused group-by fields to point to new columns.
|
2022-04-26 09:01:47 +00:00
|
|
|
colid_to_field_map = {field.colRef.colId: field for field in view_section.fields}
|
|
|
|
prev_group_fields = [
|
|
|
|
colid_to_field_map[col.colId] for col in prev_group_cols
|
|
|
|
if col.colId in colid_to_field_map
|
|
|
|
]
|
2020-07-27 18:57:36 +00:00
|
|
|
source_col_map = dict(zip(source_groupby_columns, groupby_columns))
|
|
|
|
prev_group_columns = [source_col_map[f.colRef.summarySourceCol] for f in prev_group_fields]
|
2022-04-26 09:01:47 +00:00
|
|
|
visible_formula_columns = [c for c in formula_columns if c.colId in colid_to_field_map]
|
|
|
|
formula_fields = [colid_to_field_map[c.colId] for c in visible_formula_columns]
|
2020-07-27 18:57:36 +00:00
|
|
|
self.docmodel.update(formula_fields + prev_group_fields,
|
2022-04-26 09:01:47 +00:00
|
|
|
colRef=[c.id for c in visible_formula_columns + prev_group_columns])
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
# Finally, we need to create fields for newly-added group-by columns. If there were missing
|
|
|
|
# fields for any group-by columns before, they'll be created now.
|
|
|
|
new_group_columns = [c for c in groupby_columns if c not in prev_group_columns]
|
|
|
|
|
|
|
|
# Insert these after the last existing group-by field.
|
|
|
|
insert_pos = prev_group_fields[-1].parentPos if prev_group_fields else None
|
|
|
|
new_group_fields = self.docmodel.insert_after(view_section.fields, insert_pos,
|
|
|
|
colRef=[c.id for c in new_group_columns])
|
|
|
|
|
|
|
|
# Reorder the group-by fields if needed, to match the order requested.
|
|
|
|
group_col_to_field = {f.colRef: f for f in prev_group_fields + new_group_fields}
|
|
|
|
group_fields = [group_col_to_field[c] for c in groupby_columns]
|
|
|
|
group_positions = [field.parentPos for field in group_fields]
|
|
|
|
sorted_positions = sorted(group_positions)
|
|
|
|
if sorted_positions != group_positions:
|
|
|
|
self.docmodel.update(group_fields, parentPos=sorted_positions)
|
|
|
|
|
|
|
|
update_args = {}
|
|
|
|
if view_section.sortColRefs:
|
|
|
|
# Fix the sortSpec to refer to the new columns.
|
|
|
|
update_args['sortColRefs'] = _update_sort_spec(
|
|
|
|
view_section.sortColRefs, orig_table, summary_table)
|
|
|
|
|
|
|
|
# Finally update the section to point to the new table.
|
|
|
|
self.docmodel.update([view_section], tableRef=summary_table.id, **update_args)
|
|
|
|
|
|
|
|
|
|
|
|
def _find_sister_column(self, source_table, col_id):
|
|
|
|
"""Returns a summary formula column for source_table with the given col_id, or None."""
|
|
|
|
for t in source_table.summaryTables:
|
|
|
|
c = self.docmodel.columns.lookupOne(parentId=t.id, colId=col_id, isFormula=True)
|
|
|
|
if c:
|
|
|
|
return c
|
|
|
|
return None
|
|
|
|
|
2022-04-05 14:36:22 +00:00
|
|
|
def _append_sister_column_if_any(self, all_colinfo, source_table, col):
|
|
|
|
"""
|
|
|
|
Appends a col info for one sister column of col (in source_table) if it finds one, else, and if
|
|
|
|
col is of numeric type appends the col info for the sum col, else do nothing.
|
|
|
|
"""
|
|
|
|
c = self._find_sister_column(source_table, col.colId)
|
|
|
|
if c:
|
2022-09-28 23:01:46 +00:00
|
|
|
all_colinfo.append(make_col_info(col=c))
|
2022-04-05 14:36:22 +00:00
|
|
|
elif col.type in ('Int', 'Numeric'):
|
|
|
|
all_colinfo.append(_make_sum_col_info(col))
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
def _create_summary_colinfo(self, source_table, source_groupby_columns):
|
|
|
|
"""Come up automatically with a list of columns to include into a summary table."""
|
|
|
|
# Column 'group' defines the group of records that map to this summary line.
|
|
|
|
all_colinfo = [_group_colinfo(source_table)]
|
|
|
|
|
|
|
|
# For every column in the source data, if there is a same-named formula column in another
|
|
|
|
# summary table, use it here; otherwise if it's a numerical column, automatically add a
|
|
|
|
# same-named column with the sum of the values in the group.
|
|
|
|
groupby_col_ids = {c.colId for c in source_groupby_columns}
|
|
|
|
for col in source_table.columns:
|
2021-06-28 19:05:37 +00:00
|
|
|
if col.colId in groupby_col_ids or col.colId == 'group' or not is_visible_column(col.colId):
|
2020-07-27 18:57:36 +00:00
|
|
|
continue
|
2022-04-05 14:36:22 +00:00
|
|
|
self._append_sister_column_if_any(all_colinfo, source_table, col)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
# Add a default 'count' column for the number of records in the group, unless a different
|
|
|
|
# 'count' was already added (which we would then prefer as presumably more useful). We add the
|
|
|
|
# default 'count' right after 'group', to make it the first of the visible formula columns.
|
|
|
|
if not any(c.colId == 'count' for c in all_colinfo):
|
2022-09-28 23:01:46 +00:00
|
|
|
all_colinfo.insert(1, make_col_info(colId='count', type='Int',
|
2020-07-27 18:57:36 +00:00
|
|
|
isFormula=True, formula='len($group)'))
|
|
|
|
return all_colinfo
|
|
|
|
|
|
|
|
|
|
|
|
def create_new_summary_section(self, source_table, source_groupby_columns, view, section_type):
|
|
|
|
formula_colinfo = list(self._create_summary_colinfo(source_table, source_groupby_columns))
|
|
|
|
summary_table, groupby_columns, formula_columns = self._get_or_create_summary(
|
|
|
|
source_table, source_groupby_columns, formula_colinfo)
|
|
|
|
|
|
|
|
section = self.docmodel.add(view.viewSections, tableRef=summary_table.id,
|
|
|
|
parentKey=section_type)[0]
|
|
|
|
self.docmodel.add(section.fields,
|
|
|
|
colRef=[c.id for c in groupby_columns + formula_columns
|
|
|
|
if c.colId != "group"])
|
|
|
|
return section
|
|
|
|
|
|
|
|
|
|
|
|
def detach_summary_section(self, view_section):
|
|
|
|
"""
|
|
|
|
Create a real table equivalent to the given summary section, and update the section to show
|
|
|
|
the new table instead of the summary.
|
|
|
|
"""
|
|
|
|
source_table_id = view_section.tableRef.summarySourceTable.tableId
|
|
|
|
|
|
|
|
# Get a list of columns that we need for the new table.
|
|
|
|
fields = view_section.fields
|
|
|
|
field_col_recs = [f.colRef for f in fields]
|
|
|
|
|
|
|
|
# Prepare the column info for each column.
|
2022-09-28 23:01:46 +00:00
|
|
|
col_info = [make_col_info(col=c) for c in field_col_recs if c.colId != 'group']
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
# Prepare the 'group' column, which is that one column that's different from the original.
|
2021-08-10 19:26:00 +00:00
|
|
|
group_args = ', '.join(
|
|
|
|
'%s=%s' % (
|
|
|
|
c.summarySourceCol.colId,
|
2022-06-07 14:57:29 +00:00
|
|
|
(
|
|
|
|
'CONTAINS($%s, match_empty="")' if c.summarySourceCol.type == 'ChoiceList' else
|
|
|
|
'CONTAINS($%s, match_empty=0)' if c.summarySourceCol.type.startswith('Reflist') else
|
|
|
|
'$%s'
|
|
|
|
) % c.colId,
|
2021-08-10 19:26:00 +00:00
|
|
|
)
|
|
|
|
for c in field_col_recs if c.summarySourceCol
|
|
|
|
)
|
2022-09-28 23:01:46 +00:00
|
|
|
col_info.append(make_col_info(colId='group', type='RefList:%s' % source_table_id,
|
2020-07-27 18:57:36 +00:00
|
|
|
isFormula=True,
|
|
|
|
formula='%s.lookupRecords(%s)' % (source_table_id, group_args)))
|
|
|
|
|
|
|
|
# Create the new table.
|
2022-09-28 23:01:46 +00:00
|
|
|
res = self.useractions.AddTable(None, [get_colinfo_dict(ci, with_id=True) for ci in col_info])
|
2020-07-27 18:57:36 +00:00
|
|
|
new_table = self.docmodel.tables.table.get_record(res["id"])
|
|
|
|
|
|
|
|
# Remember the original table, which we need later e.g. to adjust the sort spec (sortColRefs).
|
|
|
|
orig_table = view_section.tableRef
|
|
|
|
|
|
|
|
# Populate the new table.
|
|
|
|
old_data = self.useractions._engine.fetch_table(orig_table.tableId, formulas=False)
|
|
|
|
self.useractions.ReplaceTableData(new_table.tableId, old_data.row_ids, old_data.columns)
|
|
|
|
|
|
|
|
# Unset viewSection.tableRef before updating the fields, to avoid having inconsistencies. (See
|
|
|
|
# longer explanation in update_summary_section().)
|
|
|
|
self.docmodel.update([view_section], tableRef=0)
|
|
|
|
|
|
|
|
# Update all fields to point to new columns.
|
|
|
|
new_col_dict = {c.colId: c.id for c in new_table.columns}
|
|
|
|
self.docmodel.update(fields, colRef=[new_col_dict[c.colId] for c in field_col_recs])
|
|
|
|
|
|
|
|
# If the section is sorted, fix the sortSpec to refer to the new columns.
|
|
|
|
update_args = {}
|
|
|
|
if view_section.sortColRefs:
|
|
|
|
update_args['sortColRefs'] = _update_sort_spec(
|
|
|
|
view_section.sortColRefs, orig_table, new_table)
|
|
|
|
|
|
|
|
# Update the section to point to the new table.
|
|
|
|
self.docmodel.update([view_section], tableRef=new_table.id, **update_args)
|