Skip to content
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

Fixing performance issue on excel paste #622

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions forecast/utils/edit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,33 @@ def set_monthly_figure_amount(cost_centre_code, cell_data, financial_year): # n
).count()
+ 1
)

for financial_period_month in range(start_period, period_max):
try:
monthly_figure = ForecastMonthlyFigure.objects.filter(
financial_code__cost_centre__cost_centre_code=cost_centre_code,
financial_year__financial_year=financial_year,
financial_period__financial_period_code=financial_period_month,
financial_code__programme__programme_code=check_empty(cell_data[0]),
financial_code__natural_account_code__natural_account_code=cell_data[2],
financial_code__analysis1_code=check_empty(cell_data[4]),
financial_code__analysis2_code=check_empty(cell_data[5]),
financial_code__project_code=check_empty(cell_data[6]),
archived_status=None,
).first()
except (IndexError, ValueError):
try:
annual_figures = monthly_figure = ForecastMonthlyFigure.objects.filter(
financial_code__cost_centre__cost_centre_code=cost_centre_code,
financial_year__financial_year=financial_year,
financial_period__financial_period_code__in=range(start_period, period_max),
financial_code__programme__programme_code=check_empty(cell_data[0]),
financial_code__natural_account_code__natural_account_code=cell_data[2],
financial_code__analysis1_code=check_empty(cell_data[4]),
financial_code__analysis2_code=check_empty(cell_data[5]),
financial_code__project_code=check_empty(cell_data[6]),
archived_status=None,
).order_by("financial_period__financial_period_code")
if annual_figures.count() != 12 - (start_period - 1):
raise CannotFindForecastMonthlyFigureException(
"Could not find forecast row, please check that you "
"have pasted ALL columns from the spreadsheet. "
"Some values may have been updated."
)

col = (settings.NUM_META_COLS + financial_period_month) - 1
except (IndexError, ValueError):
raise CannotFindForecastMonthlyFigureException(
"Could not find forecast row, please check that you "
"have pasted ALL columns from the spreadsheet. "
"Some values may have been updated."
)
for monthly_figure in annual_figures:
col = settings.NUM_META_COLS + monthly_figure.financial_period_id

try:
new_value = convert_forecast_amount(cell_data[col])
Expand Down Expand Up @@ -117,7 +122,7 @@ def set_monthly_figure_amount(cost_centre_code, cell_data, financial_year): # n
project_code=check_empty(cell_data[6]),
)
financial_period = FinancialPeriod.objects.get(
financial_period_code=financial_period_month,
financial_period_code=monthly_figure.financial_period_id,
)
monthly_figure = ForecastMonthlyFigure.objects.create(
financial_year_id=financial_year,
Expand Down
11 changes: 4 additions & 7 deletions forecast/views/edit_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,12 @@ def form_valid(self, form): # noqa: C901
status=400,
)

# Check for header row
has_start_row = False
# Check for header row and reduces row count
if rows[0].lower().startswith("programme"):
has_start_row = True

# Account for header row in paste
if has_start_row:
pasted_row_count -= 1
else:
has_start_row = False

if all_selected and row_count < pasted_row_count:
return JsonResponse(
Expand All @@ -306,12 +304,12 @@ def form_valid(self, form): # noqa: C901
)

try:
# print(rows)
for index, row in enumerate(rows):
if index == 0 and has_start_row:
continue

cell_data = re.split(r"\t", row.rstrip("\t"))

# Check that pasted at content and desired first row match
check_row_match(
index,
Expand All @@ -321,7 +319,6 @@ def form_valid(self, form): # noqa: C901

# Check cell data length against expected number of cols
check_cols_match(cell_data)

set_monthly_figure_amount(
cost_centre_code, cell_data, self.financial_year
)
Expand Down
28 changes: 18 additions & 10 deletions front_end/src/Components/EditForecast/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,28 @@ function EditForecast() {
postData(
`/forecast/paste-forecast/${window.cost_centre}/${window.financial_year}`,
payload,
).then((response) => {
if (response.status === 200) {
setSheetUpdating(false);
let rows = processForecastData(response.data);
dispatch(SET_CELLS({ cells: rows }));
} else {
setSheetUpdating(false);
)
.then((response) => {
if (response.status === 200) {
setSheetUpdating(false);
let rows = processForecastData(response.data);
dispatch(SET_CELLS({ cells: rows }));
} else {
setSheetUpdating(false);
dispatch(
SET_ERROR({
errorMessage: response.data.error,
}),
);
}
})
.catch((e) =>
dispatch(
SET_ERROR({
errorMessage: response.data.error,
}),
);
}
});
),
);
};

capturePaste();
Expand Down
Loading