def get_json_raw(request, response, table, description, order, tqx_args): """Returns the statistic data as a Google Visualization compatible reply. The return can be either JSON or JSONP, depending if the header 'X-DataSource-Auth' is set in the request. Note that this is not real JSON, as explained in developers.google.com/chart/interactive/docs/dev/implementing_data_source Exposes the data in the format described at https://developers.google.com/chart/interactive/docs/reference#dataparam and https://developers.google.com/chart/interactive/docs/querylanguage Arguments: - request: A webapp2.Request. - response: A webapp2.Response. - table: Raw data ready to be sent back. - description: Dict describing the columns. - order: List describing the order to use for the columns. - tqx_args: Dict of flags used in the tqx query parameter. Raises: ValueError if a 400 should be returned. """ if request.headers.get('X-DataSource-Auth'): # Client requested JSON. response.headers['Content-Type'] = 'application/json; charset=utf-8' # TODO(maruel): This manual packaging is annoying, figure out a way to # make this cleaner. # pylint: disable=W0212 response_obj = { 'reqId': str(tqx_args.get('req_id', 0)), 'status': 'ok', 'table': gviz_api.DataTable(description, table)._ToJSonObj(columns_order=order), 'version': '0.6', } encoder = gviz_api.DataTableJSONEncoder() response.write(encoder.encode(response_obj).encode('utf-8')) else: # Client requested JSONP. response.headers['Content-Type'] = ( 'application/javascript; charset=utf-8') response.write( gviz_api.DataTable(description, table).ToJSonResponse(columns_order=order, **tqx_args))
def get(self): """Presents nice recent statistics. It fetches data from the 'JSON' API. """ # Preloads the data to save a complete request. resolution = self.request.params.get('resolution', 'hours') if resolution not in ('days', 'hours', 'minutes'): resolution = 'hours' duration = utils.get_request_as_int(self.request, 'duration', 120, 1, 1000) description = _GVIZ_DESCRIPTION.copy() description.update( stats_framework_gviz.get_description_key(resolution)) table = stats_framework.get_stats(stats.STATS_HANDLER, resolution, None, duration, True) params = { 'duration': duration, 'initial_data': gviz_api.DataTable( description, table).ToJSon(columns_order=_GVIZ_COLUMNS_ORDER), 'now': datetime.datetime.utcnow(), 'resolution': resolution, } self.response.write(template.render('isolate/stats.html', params))
def getDataTableObjectForOverall(self, statistic): """Returns dataTable object for overall statistics. """ chart_options = simplejson.loads(statistic.chart_json) description = [tuple(item) for item in chart_options['description']] final_stat = simplejson.loads(statistic.final_json) data = [t for t in final_stat.iteritems()] data_table = gviz_api.DataTable(description) data_table.LoadData(data) return data_table
def process_data(description, stats_data): def sorted_unique_list_from_itr(i): return natsort.natsorted(set(itertools.chain.from_iterable(i))) dimensions = sorted_unique_list_from_itr( (i.dimensions for i in line.values.buckets) for line in stats_data) table = _stats_data_to_summary(stats_data) # TODO(maruel): 'dimensions' should be updated when the user changes the # resolution at which the data is displayed. return { 'dimensions': json.dumps(dimensions), 'initial_data': gviz_api.DataTable(description, table).ToJSon(columns_order=_Summary.ORDER), }
def getDataTableObjectForColumns(self, statistic, columns=None): """Returns data table object for a given statistic. """ if statistic.chart_json: chart_json = simplejson.loads(statistic.chart_json) description_columns = [column + 1 for column in columns] description_columns.insert(0, 0) description = [tuple(item) for index, item in enumerate(chart_json['description']) if index in description_columns] column_types = [column[1] for column in description] data = self._getDataForColumns(statistic, column_types, columns) data.sort() if data is None: return None data_table = gviz_api.DataTable(description) data_table.LoadData(data) return data_table else: return None