-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import Logs for Deps Import #186
Changes from all commits
f6df1ec
372c628
d0ad337
7825b9e
b79c74c
97779c6
601e3cb
fb78432
57a4ed4
19dfa7d
151e397
ffbe1f5
372e5ab
737c79d
da072df
e2f4cef
5bc9075
c6a4697
f667558
1c2e3c1
0e98a8a
d23f0cd
7dd6145
f1a31f1
505fabc
e85f6b2
b1f39ca
555a72c
675c37a
84ba2e9
6dd28a9
aa96f62
51e76e5
6cbcb7d
6c54fdc
1da8a7d
9aefa9f
173d31a
5dac65e
ef99211
771c939
0d688ac
53d90b1
766beb3
8393600
7a6d4fa
30b7c50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from apps.fyle.models import DependentFieldSetting | ||
from apps.sage300.models import CostCategory | ||
from apps.fyle.helpers import connect_to_platform | ||
from apps.mappings.models import ImportLog | ||
from apps.mappings.exceptions import handle_import_exceptions | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.level = logging.INFO | ||
|
@@ -68,7 +70,8 @@ def create_dependent_custom_field_in_fyle(workspace_id: int, fyle_attribute_type | |
return platform.expense_custom_fields.post(expense_custom_field_payload) | ||
|
||
|
||
def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict, is_enabled: bool = True) -> List[str]: | ||
@handle_import_exceptions | ||
def post_dependent_cost_code(import_log: ImportLog, dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict, is_enabled: bool = True) -> List[str]: | ||
projects = CostCategory.objects.filter(**filters).values('job_name').annotate(cost_codes=ArrayAgg('cost_code_name', distinct=True)) | ||
projects_from_categories = [project['job_name'] for project in projects] | ||
posted_cost_codes = [] | ||
|
@@ -103,10 +106,14 @@ def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, pla | |
logger.error(f'Exception while posting dependent cost code | Error: {exception} | Payload: {payload}') | ||
raise | ||
|
||
import_log.status = 'COMPLETE' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we also store batches count while posting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. store batch count in import log? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
import_log.error_log = [] | ||
import_log.save() | ||
return posted_cost_codes | ||
|
||
|
||
def post_dependent_cost_type(dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict): | ||
@handle_import_exceptions | ||
def post_dependent_cost_type(import_log: ImportLog, dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict): | ||
cost_categories = CostCategory.objects.filter(is_imported=False, **filters).values('cost_code_name').annotate(cost_categories=ArrayAgg('name', distinct=True)) | ||
|
||
for category in cost_categories: | ||
|
@@ -129,6 +136,10 @@ def post_dependent_cost_type(dependent_field_setting: DependentFieldSetting, pla | |
logger.error(f'Exception while posting dependent cost type | Error: {exception} | Payload: {payload}') | ||
raise | ||
|
||
import_log.status = 'COMPLETE' | ||
import_log.error_log = [] | ||
import_log.save() | ||
|
||
|
||
def post_dependent_expense_field_values(workspace_id: int, dependent_field_setting: DependentFieldSetting, platform: PlatformConnector = None): | ||
if not platform: | ||
|
@@ -141,10 +152,20 @@ def post_dependent_expense_field_values(workspace_id: int, dependent_field_setti | |
if dependent_field_setting.last_successful_import_at: | ||
filters['updated_at__gte'] = dependent_field_setting.last_successful_import_at | ||
|
||
posted_cost_types = post_dependent_cost_code(dependent_field_setting, platform, filters) | ||
cost_code_import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='COST_CODE').first() | ||
cost_category_import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='COST_CATEGORY').first() | ||
|
||
posted_cost_types = post_dependent_cost_code(cost_code_import_log, dependent_field_setting, platform, filters) | ||
if posted_cost_types: | ||
filters['cost_code_name__in'] = posted_cost_types | ||
post_dependent_cost_type(dependent_field_setting, platform, filters) | ||
|
||
if cost_code_import_log.status in ['FAILED', 'FATAL']: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't there be exception incase of errors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both post_cost_code and post_cost_type method are decoarated with handle_import_exception
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also fixed the call ordering, it was wrong earlier. can check code again for this. |
||
cost_category_import_log.status = 'FAILED' | ||
cost_category_import_log.error_log = {'message': 'Importing COST_CODE failed'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
cost_category_import_log.save() | ||
return | ||
else: | ||
post_dependent_cost_type(cost_category_import_log, dependent_field_setting, platform, filters) | ||
|
||
DependentFieldSetting.objects.filter(workspace_id=workspace_id).update(last_successful_import_at=datetime.now()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should bring this inside else block, else we might mark timestamp even for failed runs |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from apps.sage300.models import CostCategory | ||
from apps.fyle.models import DependentFieldSetting | ||
from apps.sage300.dependent_fields import post_dependent_cost_code | ||
from apps.mappings.models import ImportLog | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.level = logging.INFO | ||
|
@@ -134,9 +135,10 @@ def update_and_disable_cost_code(workspace_id: int, cost_codes_to_disable: Dict, | |
'job_id__in':list(cost_codes_to_disable.keys()), | ||
'workspace_id': workspace_id | ||
} | ||
cost_code_import_log = ImportLog.create_import_log('COST_CODE', workspace_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The
Analysis chainEnsure that the Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify exception handling in `create_import_log` method.
# Test: Search for exception handling in `create_import_log` method. Expect: Proper exception handling.
rg --type python $'def create_import_log($$$) { try { $$$ } catch ($$$) { $$$ } }' apps/mappings/models.py
Length of output: 281 Script: #!/bin/bash
# Description: Verify exception handling in `create_import_log` method.
# Find the definition of the `create_import_log` method in the codebase.
ast-grep --lang python --pattern 'def create_import_log($_) { $$$ }'
# Check for try-except blocks within the `create_import_log` method.
ast-grep --lang python --pattern 'def create_import_log($_) { try: $$$ except: $$$ }'
Length of output: 959 Script: #!/bin/bash
# Description: Verify exception handling in `create_import_log` method.
# Find the definition of the `create_import_log` method in the codebase.
rg --type python --pcre2 'def create_import_log\('
# Check for try-except blocks within the `create_import_log` method.
rg --type python --pcre2 'def create_import_log\([^\)]*\):(\n|.)*?try:(\n|.)*?except:'
Length of output: 203 Script: #!/bin/bash
# Description: Verify exception handling in `create_import_log` method.
# Find the definition of the `create_import_log` method in the codebase.
rg --type py --pcre2 'def create_import_log\('
# Check for try-except blocks within the `create_import_log` method.
rg --type py --pcre2 'def create_import_log\([^\)]*\):(\n|.)*?try:(\n|.)*?except:'
Length of output: 220 |
||
|
||
# This call will disable the cost codes in Fyle that has old project name | ||
posted_cost_codes = post_dependent_cost_code(dependent_field_setting, platform, filters, is_enabled=False) | ||
posted_cost_codes = post_dependent_cost_code(cost_code_import_log, dependent_field_setting, platform, filters, is_enabled=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The
Please add appropriate docstrings or comments to document the new Analysis chainCorrect usage of the Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.
# Test: Search for updated documentation for `post_dependent_cost_code`. Expect: Updated documentation reflecting new parameters.
rg --type python $'def post_dependent_cost_code($$$) { $$$ }' apps/sage300/dependent_fields.py
Length of output: 255 Script: #!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.
# Test: Search for the function definition and its associated documentation.
rg --type python 'def post_dependent_cost_code' -A 10 apps/sage300/dependent_fields.py
Length of output: 119 Script: #!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.
# Test: Search for the function definition and its associated documentation.
rg --type py 'def post_dependent_cost_code' -A 10 apps/sage300/dependent_fields.py
Length of output: 748 Script: #!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.
# Test: Search for documentation comments or docstrings around the function definition.
rg --type py 'def post_dependent_cost_code' -A 20 -B 5 apps/sage300/dependent_fields.py
Length of output: 1329 |
||
|
||
logger.info(f"Disabled Cost Codes in Fyle | WORKSPACE_ID: {workspace_id} | COUNT: {len(posted_cost_codes)}") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import_dependent_fields_to_fyle | ||
) | ||
from apps.fyle.models import DependentFieldSetting | ||
from apps.mappings.models import ImportLog | ||
|
||
|
||
def test_construct_custom_field_placeholder(): | ||
|
@@ -64,14 +65,24 @@ def test_post_dependent_cost_code( | |
} | ||
|
||
dependent_field_settings = DependentFieldSetting.objects.get(workspace_id=workspace_id) | ||
cost_code_import_log = ImportLog.create_import_log('COST_CODE', workspace_id) | ||
|
||
result = post_dependent_cost_code( | ||
cost_code_import_log, | ||
dependent_field_setting=dependent_field_settings, | ||
platform=platform.return_value, | ||
filters=filters | ||
) | ||
|
||
assert result == ['Direct Mail Campaign', 'Platform APIs'] | ||
assert cost_code_import_log.status == 'COMPLETE' | ||
|
||
post_dependent_cost_code( | ||
cost_code_import_log, | ||
dependent_field_setting=dependent_field_settings, | ||
platform=platform.return_value | ||
) | ||
assert cost_code_import_log.status == 'FATAL' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how it this going fatal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passed a less param (removed filters). To explicitly raise an error. |
||
|
||
|
||
def test_post_dependent_cost_type( | ||
|
@@ -96,13 +107,24 @@ def test_post_dependent_cost_type( | |
|
||
dependent_field_settings = DependentFieldSetting.objects.get(workspace_id=workspace_id) | ||
|
||
cost_category_import_log = ImportLog.create_import_log('COST_CATEGORY', workspace_id) | ||
|
||
post_dependent_cost_type( | ||
cost_category_import_log, | ||
dependent_field_setting=dependent_field_settings, | ||
platform=platform.return_value, | ||
filters=filters | ||
) | ||
|
||
assert platform.return_value.dependent_fields.bulk_post_dependent_expense_field_values.call_count == 2 | ||
assert cost_category_import_log.status == 'COMPLETE' | ||
|
||
post_dependent_cost_type( | ||
cost_category_import_log, | ||
dependent_field_setting=dependent_field_settings, | ||
platform=platform.return_value | ||
) | ||
assert cost_category_import_log.status == 'FATAL' | ||
|
||
|
||
def test_post_dependent_expense_field_values( | ||
|
@@ -124,6 +146,9 @@ def test_post_dependent_expense_field_values( | |
|
||
dependent_field_settings = DependentFieldSetting.objects.get(workspace_id=workspace_id) | ||
|
||
ImportLog.create_import_log('COST_CODE', workspace_id) | ||
ImportLog.create_import_log('COST_CATEGORY', workspace_id) | ||
|
||
post_dependent_expense_field_values( | ||
workspace_id=workspace_id, | ||
dependent_field_setting=dependent_field_settings | ||
|
@@ -150,6 +175,9 @@ def test_import_dependent_fields_to_fyle( | |
'dependent_fields.bulk_post_dependent_expense_field_values' | ||
) | ||
|
||
ImportLog.create_import_log('COST_CODE', workspace_id) | ||
ImportLog.create_import_log('COST_CATEGORY', workspace_id) | ||
|
||
import_dependent_fields_to_fyle(workspace_id) | ||
|
||
assert platform.return_value.dependent_fields.bulk_post_dependent_expense_field_values.call_count == 4 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create()