From cca3bf8ef5d303b69a77379e858e065870ea64aa Mon Sep 17 00:00:00 2001 From: hollowtree11 Date: Tue, 21 Apr 2026 14:20:23 -0500 Subject: [PATCH] Fixed Dynamical Systems page bug --- Backend/postgres_connector.py | 79 +++++++++++++++++++++++++++++------ Frontend/src/api/routes.js | 4 -- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/Backend/postgres_connector.py b/Backend/postgres_connector.py index 9d3020e5..f36051b3 100644 --- a/Backend/postgres_connector.py +++ b/Backend/postgres_connector.py @@ -3,9 +3,11 @@ manages server data queries """ import psycopg2 +import psycopg2.extras from psycopg2 import sql from config import load_config + # important do not store password when dealing with real database # might want to consider SQL injection down the line # gets all systems from the database @@ -53,6 +55,9 @@ def rows_to_dicts(self, cursor, rows): return None def try_query(self, sql_query, params=None, fetch="all"): + if not self.is_connection_active(): + self.connect() + result = None cur = None @@ -147,19 +152,66 @@ def get_system(self, function_id): def get_filtered_systems(self, filters): where_sql, params = self.build_where_text(filters) stats = self.get_statistics(where_sql, params) + query = sql.SQL(""" - SELECT f.function_id, sigma_one, sigma_two, ordinal, degree, - (original_model).coeffs, f.base_field_label + SELECT f.function_id, f.sigma_one, f.sigma_two, f.ordinal, f.degree, + (f.original_model).coeffs, f.base_field_label FROM functions_dim_1_nf f JOIN rational_preperiodic_dim_1_nf r ON f.function_id = r.function_id JOIN graphs_dim_1_nf g ON g.graph_id = r.graph_id {} """).format(where_sql) + rows = self.try_query(query, params, fetch='all') - return rows, stats + result = [] + + if rows: + mon_dict = {} + for row in rows: + try: + d = int(row[4]) + coeffs = row[5] + field_label = row[6] + func_id = row[0] + except (KeyError, TypeError): + d = int(row['degree']) + coeffs = row['coeffs'] + field_label = row['base_field_label'] + func_id = row['function_id'] + + if d not in mon_dict: + mon = [] + for i in range(d+1): + if i == 0: mon.append('x^'+str(d)) + elif i == d: mon.append('y^'+str(d)) + else: + term = 'x' if (d-i) == 1 else 'x^'+str(d-i) + term += 'y' if i == 1 else 'y^'+str(i) + mon.append(term) + mon_dict[d] = mon + else: + mon = mon_dict[d] + + poly = '[' + for j in range(2): + first_term = True + for i in range(d+1): + val = str(coeffs[j][i]) + if val != '0': + if val[0] != '-' and not first_term: + poly += '+' + if val == '1': poly += mon[i] + elif val == '-1': poly += '-' + mon[i] + else: poly += val + mon[i] + first_term = False + if j == 0: poly += ' : ' + poly += ']' + + label = self.construct_label(row) + result.append([label, '1', d, poly, field_label, func_id]) + + return result, stats - # gets a subset of the systems identified by the labels - # input should be json list def get_selected_systems(self, labels): if not labels: return [] @@ -178,17 +230,21 @@ def get_selected_systems(self, labels): def get_statistics(self, where_sql, params): query = sql.SQL(""" - SELECT COUNT(*) as total_count, - AVG(degree) as avg_degree + SELECT count(*) FROM functions_dim_1_nf f JOIN rational_preperiodic_dim_1_nf r ON f.function_id = r.function_id JOIN graphs_dim_1_nf g ON g.graph_id = r.graph_id {} """).format(where_sql) - result = self.try_query(query, params, fetch="one") + + result = self.try_query(query, params, fetch='one') + if result: - return result - return {'total_count': 0, 'avg_degree': 0} + try: + return result[0] + except KeyError: + return result['count'] + return 0 def get_family(self, family_id): # We grab all family data @@ -280,7 +336,6 @@ def build_where_text(self, filters): where_fragment = sql.SQL(" WHERE ") + sql.SQL(" AND ").join(conditions) return where_fragment, params -<<<<<<< HEAD # for fil, values in filters.items(): # if fil == 'family_id': # # Exact match for family_id (numeric) @@ -324,5 +379,3 @@ def build_where_text(self, filters): # filter_text += ' AND '.join(conditions) # return filter_text -======= ->>>>>>> edaed198d4e0f86796bdfdb6b6e37135100a9c31 diff --git a/Frontend/src/api/routes.js b/Frontend/src/api/routes.js index 70207ae9..ec83441d 100644 --- a/Frontend/src/api/routes.js +++ b/Frontend/src/api/routes.js @@ -21,11 +21,7 @@ export const get_families = () => api.get("/get_all_families"); export const get_family = (familyId) => api.post("/get_family", { id: familyId }); // dreyes: used to filter families based on the filters selected in the UI -<<<<<<< HEAD export const get_filtered_families = (filters) => api.post("/get_filtered_familes", filters); -======= -export const get_filtered_families = (filters) => api.post("/get_filtered_families", filters); ->>>>>>> edaed198d4e0f86796bdfdb6b6e37135100a9c31 export const get_rational_periodic_data = (functionId) => api.post("/get_rational_periodic_data", { function_id: functionId });