@@ -132,6 +132,33 @@ def post(self):
132132ONE_YEAR = 60 * 60 * 24 * 365.25
133133
134134
135+ class QueryResultSetResource (BaseResource ):
136+ @require_permission ('view_query' )
137+ def get (self , query_id = None , filetype = 'json' ):
138+ query = get_object_or_404 (models .Query .get_by_id_and_org , query_id , self .current_org )
139+ if not query .schedule_resultset_size :
140+ abort (404 , message = "query does not keep multiple results" )
141+
142+ # Synthesize a result set from the last N results.
143+ total = len (query .query_results )
144+ offset = max (total - query .schedule_resultset_size , 0 )
145+ results = [qr .to_dict () for qr in query .query_results [offset :]]
146+ if not results :
147+ aggregate_result = {}
148+ else :
149+ # Start a synthetic data set with the data from the first result...
150+ aggregate_result = results [0 ].copy ()
151+ aggregate_result ['data' ] = {'columns' : results [0 ]['data' ]['columns' ],
152+ 'rows' : []}
153+ # .. then add each subsequent result set into it.
154+ for r in results :
155+ aggregate_result ['data' ]['rows' ].extend (r ['data' ]['rows' ])
156+
157+ data = json .dumps ({'query_result' : aggregate_result }, cls = utils .JSONEncoder )
158+ headers = {'Content-Type' : "application/json" }
159+ return make_response (data , 200 , headers )
160+
161+
135162class QueryResultResource (BaseResource ):
136163 @staticmethod
137164 def add_cors_headers (headers ):
@@ -194,7 +221,7 @@ def get(self, query_id=None, query_result_id=None, filetype='json'):
194221 query_result = run_query_sync (query .data_source , parameter_values , query .query_text , max_age = max_age )
195222 elif query .latest_query_data_id is not None :
196223 query_result = get_object_or_404 (models .QueryResult .get_by_id_and_org , query .latest_query_data_id , self .current_org )
197-
224+
198225 if query is not None and query_result is not None and self .current_user .is_api_user ():
199226 if query .query_hash != query_result .query_hash :
200227 abort (404 , message = 'No cached result found for this query.' )
0 commit comments