Skip to content

Commit 5c3cce1

Browse files
authored
Merge pull request #1202 from mito-ds/add-code-key
Don't overwrite generated code
2 parents c8b6ab1 + f4e817d commit 5c3cce1

23 files changed

+429
-98
lines changed

.github/workflows/test-mitosheet-frontend.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
timeout-minutes: 60
1717
strategy:
1818
matrix:
19-
python-version: ['3.6', '3.8', '3.10']
19+
python-version: ['3.7', '3.8', '3.10']
2020
fail-fast: false
2121

2222
steps:

mitosheet/mitosheet/api/api.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from time import perf_counter
1212
from typing import Any, Callable, Dict, NoReturn, Optional
1313

14+
from mitosheet.api.get_saved_analysis_code import get_saved_analysis_code
1415
from mitosheet.api.get_all_params_for_step_type import get_all_params_for_step_type
1516
from mitosheet.api.get_ai_completion import get_ai_completion
1617
from mitosheet.api.get_available_snowflake_options_and_defaults import \
@@ -40,6 +41,7 @@
4041
from mitosheet.api.get_unique_value_counts import get_unique_value_counts
4142
from mitosheet.api.get_validate_snowflake_credentials import \
4243
get_validate_snowflake_credentials
44+
from mitosheet.saved_analyses.save_utils import read_analysis
4345
from mitosheet.steps_manager import StepsManager
4446
# AUTOGENERATED LINE: API.PY IMPORT (DO NOT DELETE)
4547
from mitosheet.telemetry.telemetry_utils import log_event_processed
@@ -235,6 +237,8 @@ def handle_api_event(
235237
result = get_parameterizable_params(params, steps_manager)
236238
elif event["type"] == "get_pr_url_of_new_pr":
237239
result = get_pr_url_of_new_pr(params, steps_manager)
240+
elif event["type"] == "get_saved_analysis_code":
241+
result = get_saved_analysis_code(params, steps_manager)
238242
# AUTOGENERATED LINE: API.PY CALL (DO NOT DELETE)
239243
else:
240244
raise Exception(f"Event: {event} is not a valid API call")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Any, Dict, List, Union
2+
from mitosheet.saved_analyses.save_utils import read_analysis
3+
from mitosheet.types import StepsManagerType
4+
5+
def get_saved_analysis_code(params: Dict[str, Any], steps_manager: StepsManagerType) -> Union[List[str], str, None]:
6+
if steps_manager.analysis_to_replay is not None:
7+
analysis = read_analysis(steps_manager.analysis_to_replay)
8+
return analysis.get("code") if analysis is not None else None
9+
return None

mitosheet/mitosheet/saved_analyses/save_utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def read_analysis(analysis_name: str) -> Optional[Dict[str, Any]]:
4242
"""
4343

4444
analysis_path = f'{SAVED_ANALYSIS_FOLDER}/{analysis_name}.json'
45-
4645
if not os.path.exists(analysis_path):
4746
return None
4847

mitosheet/mitosheet/saved_analyses/upgrade.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def upgrade_steps_for_new_format(saved_analysis: Optional[Dict[str, Any]]) -> Op
334334
}
335335

336336
def upgrade_saved_analysis_to_have_public_interface_version(saved_analysis: Optional[Dict[str, Any]], public_interface_version: Optional[int]) -> Optional[Dict[str, Any]]:
337-
337+
338338
if saved_analysis is None:
339339
return None
340340

@@ -358,6 +358,18 @@ def upgrade_saved_analysis_to_have_up_to_date_args(saved_analysis: Optional[Dict
358358

359359
return saved_analysis
360360

361+
def upgrade_saved_analysis_to_have_code(saved_analysis: Optional[Dict[str, Any]], code: Optional[str]) -> Optional[Dict[str, Any]]:
362+
363+
if saved_analysis is None:
364+
return None
365+
366+
if code is not None:
367+
saved_analysis['code'] = code
368+
else:
369+
saved_analysis['code'] = None
370+
371+
return saved_analysis
372+
361373
def upgrade_saved_analysis_to_have_code_options(saved_analysis: Optional[Dict[str, Any]], analysis_name: str, code_options: Optional[CodeOptions]) -> Optional[Dict[str, Any]]:
362374

363375
if saved_analysis is None:
@@ -407,8 +419,12 @@ def upgrade_saved_analysis_to_current_version(saved_analysis: Optional[Dict[str,
407419
saved_analysis_with_public_interface,
408420
args
409421
)
410-
saved_analysis_with_code_options = upgrade_saved_analysis_to_have_code_options(
422+
saved_analysis_with_args_with_code = upgrade_saved_analysis_to_have_code(
411423
saved_analysis_with_args,
424+
saved_analysis.get('code', None) if saved_analysis is not None else None
425+
)
426+
saved_analysis_with_code_options = upgrade_saved_analysis_to_have_code_options(
427+
saved_analysis_with_args_with_code,
412428
analysis_name,
413429
saved_analysis.get('code_options', None) if saved_analysis is not None else None,
414430
)

mitosheet/mitosheet/telemetry/private_params_map.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
# Params that do not need to be anonyimized
14-
LOG_PARAMS_PUBLIC = { 'action', 'analysis_name', 'column_header_index', 'cell_editor_location', 'checklist_id', 'completed_items', 'drop', 'tour_names', 'total_number_of_tour_steps', 'created_non_empty_dataframe', 'destination_sheet_index', 'df_index_type', 'email', 'export_type', 'error', 'error_message', 'error_name', 'error_stack', 'feedback_id', 'field', 'filter_location', 'flatten_column_headers', 'format_type', 'fullscreen', 'function_name', 'graph_id', 'graph_type', 'has_headers', 'has_non_empty_filter', 'height', 'how', 'ignore_index', 'join', 'js_error', 'js_error_info', 'js_taskpane_header', 'jupyterlab_theme', 'keep', 'level', 'log_event', 'message', 'move_to_deprecated_id_algorithm', 'new_column_index', 'new_dtype', 'new_graph_id', 'new_signup_step', 'new_version', 'num_args', 'num_df_args', 'num_str_args', 'num_usages', 'number_rendered_sheets', 'old_dtype', 'old_graph_id', 'old_signup_step', 'old_version', 'operator', 'paper_bgcolor', 'param_filtered', 'path_parts_length', 'plot_bgcolor', 'pro_button_location', 'questions_and_answers', 'safety_filter_turned_on_by_user', 'search_string', 'selected_element', 'sheet_index', 'sheet_index_one', 'sheet_index_two', 'sheet_indexes', 'showlegend', 'skiprows', 'sort', 'sort_direction', 'step_id_to_match', 'step_idx', 'step_type', 'steps_manager_analysis_name', 'title_font_color', 'user_agent', 'user_serch_term', 'view_df', 'visible', 'width', 'row_index', 'type', 'value', 'open_due_to_replay_error', 'num_invalid_imports', 'num_total_imports', 'optional_code_chunk_names', 'code_snippet_name', 'get_code_snippet_error_reason', 'completion', 'edited_completion', 'prompt', 'prompt_version', 'user_input', 'feedback', 'created_dataframe_names', 'deleted_dataframe_names', 'last_line_value', 'aiPrivacyPolicyNotAccepted', 'apiKeyNotDefined', 'feature', 'public_interface_version'}
14+
LOG_PARAMS_PUBLIC = { 'action', 'analysis_name', 'column_header_index', 'cell_editor_location', 'checklist_id', 'completed_items', 'drop', 'tour_names', 'total_number_of_tour_steps', 'created_non_empty_dataframe', 'destination_sheet_index', 'df_index_type', 'email', 'export_type', 'error', 'error_message', 'error_name', 'error_stack', 'feedback_id', 'field', 'filter_location', 'flatten_column_headers', 'format_type', 'fullscreen', 'function_name', 'graph_id', 'graph_type', 'has_headers', 'has_non_empty_filter', 'height', 'how', 'ignore_index', 'join', 'jupyterlab_theme', 'keep', 'level', 'log_event', 'message', 'move_to_deprecated_id_algorithm', 'new_column_index', 'new_dtype', 'new_graph_id', 'new_signup_step', 'new_version', 'num_args', 'num_df_args', 'num_str_args', 'num_usages', 'number_rendered_sheets', 'old_dtype', 'old_graph_id', 'old_signup_step', 'old_version', 'operator', 'paper_bgcolor', 'param_filtered', 'path_parts_length', 'plot_bgcolor', 'pro_button_location', 'questions_and_answers', 'safety_filter_turned_on_by_user', 'search_string', 'selected_element', 'sheet_index', 'sheet_index_one', 'sheet_index_two', 'sheet_indexes', 'showlegend', 'skiprows', 'sort', 'sort_direction', 'step_id_to_match', 'step_idx', 'step_type', 'steps_manager_analysis_name', 'title_font_color', 'user_agent', 'user_serch_term', 'view_df', 'visible', 'width', 'row_index', 'type', 'value', 'open_due_to_replay_error', 'num_invalid_imports', 'num_total_imports', 'optional_code_chunk_names', 'code_snippet_name', 'get_code_snippet_error_reason', 'completion', 'edited_completion', 'prompt', 'prompt_version', 'user_input', 'feedback', 'created_dataframe_names', 'deleted_dataframe_names', 'last_line_value', 'aiPrivacyPolicyNotAccepted', 'apiKeyNotDefined', 'feature', 'public_interface_version', 'length_of_code_with_user_edits', 'length_of_code_without_user_edits', 'js_error', 'js_error_info', 'js_taskpane_header'}
1515

1616
# Params that we want to log the length of the first element
1717
LOG_PARAMS_LENGTH_FIRST_ELEMENT = {'optional_code'}

mitosheet/mitosheet/tests/saved_analyses/test_save_utils.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,23 @@ def test_upgrades_old_analysis_before_replaying_it():
281281
pd.DataFrame({'A': [123], 'B_sum': [123]})
282282
)
283283

284+
def test_save_analysis_has_code():
285+
mito = create_mito_wrapper_with_data([1, 2, 3])
286+
mito.filter(0, 'A', 'And', FC_NUMBER_EXACTLY, 2)
287+
288+
random_name = 'UUID-test_save' + str(random.random())
289+
mito.save_analysis(random_name)
290+
291+
saved_analysis = read_and_upgrade_analysis(random_name, ['df1'])
292+
assert len(saved_analysis['steps_data']) == 1
293+
assert saved_analysis['code'] == [
294+
'from mitosheet.public.v3 import *',
295+
'',
296+
'# Filtered A',
297+
"df1 = df1[df1['A'] == 2]",
298+
''
299+
]
300+
284301
def test_save_analysis_saves_skipped_steps():
285302
mito = create_mito_wrapper_with_data([1, 2, 3])
286303
mito.filter(0, 'A', 'And', FC_NUMBER_EXACTLY, 2)
@@ -331,8 +348,8 @@ def test_save_and_replay_different_interface_version_works():
331348
saved_analysis = read_and_upgrade_analysis(random_name, ['df1'])
332349
assert saved_analysis is not None
333350
assert len(saved_analysis['steps_data']) == 0
334-
print(saved_analysis)
335351
assert saved_analysis['public_interface_version'] == 100
352+
assert saved_analysis['code'] == []
336353

337354
new_mito = create_mito_wrapper_with_data([1, 2, 3])
338355
new_mito.replay_analysis(random_name)

0 commit comments

Comments
 (0)