Skip to content

Commit 23ec35e

Browse files
committed
[IMP] util.snippets: add logging of minimal stats in html conversions
Store and return the number of matched and converted record's values that are processed by `convert_html_columns()`. Add a `verbose=False` optional argument in `convert_html_content()` that collects these stats and logs them. Also, do the same in `util.views.records.convert_html_fields()`.
1 parent 0f9ca63 commit 23ec35e

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/util/snippets.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,19 @@ def convert_html_columns(cr, table, columns, converter_callback, where_column="I
278278
update_sql = ", ".join(f'"{column}" = %({column})s' for column in columns)
279279
update_query = f"UPDATE {table} SET {update_sql} WHERE id = %(id)s"
280280

281+
matched_count = 0
282+
converted_count = 0
281283
with ProcessPoolExecutor(max_workers=util.get_max_workers()) as executor:
282284
convert = Convertor(converters, converter_callback)
283285
for query in util.log_progress(split_queries, logger=_logger, qualifier=f"{table} updates"):
284286
cr.execute(query)
285287
for data in executor.map(convert, cr.fetchall()):
288+
matched_count += 1
286289
if "id" in data:
287290
cr.execute(update_query, data)
291+
converted_count += 1
292+
293+
return matched_count, converted_count
288294

289295

290296
def determine_chunk_limit_ids(cr, table, column_arr, where):
@@ -307,6 +313,7 @@ def convert_html_content(
307313
cr,
308314
converter_callback,
309315
where_column="IS NOT NULL",
316+
verbose=False,
310317
**kwargs,
311318
):
312319
"""
@@ -319,17 +326,36 @@ def convert_html_content(
319326
:param str where_column: filtering such as
320327
- "like '%abc%xyz%'"
321328
- "~* '\\yabc.*xyz\\y'"
329+
:param bool verbose: print stats about the conversion
322330
:param dict kwargs: extra keyword arguments to pass to :func:`convert_html_column`
323331
"""
324332

325-
convert_html_columns(
333+
if verbose:
334+
_logger.info("Converting html fields data using %s", repr(converter_callback))
335+
336+
matched_count = 0
337+
converted_count = 0
338+
339+
matched, converted = convert_html_columns(
326340
cr,
327341
"ir_ui_view",
328342
["arch_db"],
329343
converter_callback,
330344
where_column=where_column,
331345
**dict(kwargs, extra_where="type = 'qweb'"),
332346
)
347+
matched_count += matched
348+
converted_count += converted
333349

334350
for table, columns in html_fields(cr):
335-
convert_html_columns(cr, table, columns, converter_callback, where_column=where_column, **kwargs)
351+
matched, converted = convert_html_columns(
352+
cr, table, columns, converter_callback, where_column=where_column, **kwargs
353+
)
354+
matched_count += matched
355+
converted_count += converted
356+
357+
if verbose:
358+
if matched_count:
359+
_logger.info("Converted %d/%d matched html fields values", converted_count, matched_count)
360+
else:
361+
_logger.info("Did not match any html fields values to convert")

src/util/views/convert.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,15 +1081,20 @@ def convert_qweb_views(cr, converter):
10811081
if views_ids:
10821082
convert_views(cr, views_ids, converter)
10831083

1084-
def convert_html_fields(cr, converter):
1084+
def convert_html_fields(cr, converter, verbose=False):
10851085
"""
10861086
Convert all html fields data in the database using the provided converter.
10871087
10881088
:param psycopg2.cursor cr: the database cursor.
10891089
:param EtreeConverter converter: the converter to use.
1090+
:param bool verbose: whether to print stats about the conversion.
10901091
:rtype: None
10911092
"""
1092-
_logger.info("Converting html fields data using %s" % (repr(converter),))
1093+
if verbose:
1094+
_logger.info("Converting html fields data using %s", repr(converter))
1095+
1096+
matched_count = 0
1097+
converted_count = 0
10931098

10941099
html_fields = list(snippets.html_fields(cr))
10951100
for table, columns in misc.log_progress(html_fields, _logger, "tables", log_hundred_percent=True):
@@ -1098,7 +1103,18 @@ def convert_html_fields(cr, converter):
10981103
"(%s)" % converter.build_where_clause(cr, pg.get_value_or_en_translation(cr, table, column))
10991104
for column in columns
11001105
)
1101-
snippets.convert_html_columns(cr, table, columns, converter.convert_callback, extra_where=extra_where)
1106+
# TODO abt: adapt to refactor, maybe make snippets compat w/ converter, instead of adapting?
1107+
matched, converted = snippets.convert_html_columns(
1108+
cr, table, columns, converter.convert_callback, extra_where=extra_where
1109+
)
1110+
matched_count += matched
1111+
converted_count += converted
1112+
1113+
if verbose:
1114+
if matched_count:
1115+
_logger.info("Converted %d/%d matched html fields values", converted_count, matched_count)
1116+
else:
1117+
_logger.info("Did not match any html fields values to convert")
11021118

11031119
else:
11041120

0 commit comments

Comments
 (0)