def run(self, form): selection = SelectionSearch(form) queries = selection.get_queries() articlesets = form.cleaned_data["articlesets"] mediums = form.cleaned_data["mediums"] statistics = selection.get_statistics() if hasattr(statistics, "start_date"): start_date = statistics.start_date end_date = statistics.end_date else: start_date = None end_date = None return json.dumps({ "queries": {q.label: q.query for q in queries}, "mediums": {m.id: m.name for m in mediums}, "articlesets": {a.id: a.name for a in articlesets}, "statistics": { "start_date": start_date, "end_date": end_date, "narticles": statistics.n } }, cls=DjangoJSONEncoder)
def run(self, form): selection = SelectionSearch(form) queries = selection.get_queries() articlesets = form.cleaned_data["articlesets"] mediums = form.cleaned_data["mediums"] statistics = selection.get_statistics() if hasattr(statistics, "start_date"): start_date = statistics.start_date end_date = statistics.end_date else: start_date = None end_date = None return json.dumps( { "queries": {q.label: q.query for q in queries}, "mediums": {m.id: m.name for m in mediums}, "articlesets": {a.id: a.name for a in articlesets}, "statistics": { "start_date": start_date, "end_date": end_date, "narticles": statistics.n } }, cls=DjangoJSONEncoder)
def run(self, form): form_data = dict(form.data.lists()) for value in form_data.values(): if value == [None]: value.pop() form_data = json.dumps(form_data, indent=4) size = form.cleaned_data['size'] offset = form.cleaned_data['offset'] number_of_fragments = form.cleaned_data['number_of_fragments'] fragment_size = form.cleaned_data['fragment_size'] show_fields = sorted(form.cleaned_data['show_fields']) show_aggregation = form.cleaned_data['aggregations'] with Timer() as timer: selection = SelectionSearch(form) self.monitor.update(message="Executing query..") narticles = selection.get_count() self.monitor.update(message="Fetching articles..".format( **locals())) articles = selection.get_articles(size=size, offset=offset).as_dicts() articles = get_fragments(selection.get_query(), [a["id"] for a in articles], fragment_size, number_of_fragments) if show_aggregation: self.monitor.update(message="Aggregating..".format(**locals())) statistics = selection.get_statistics() try: delta_start_end = statistics.end_date - statistics.start_date interval = next( interval for (interval, delta) in TIMEDELTAS if MAX_DATE_GROUPS * delta > delta_start_end) except (StopIteration, TypeError): interval = "day" date_aggr = selection.get_aggregate( [IntervalCategory(interval)], objects=False) else: # Increase progress without doing anything (because we don't have to aggregate) self.monitor.update() self.monitor.update(message="Rendering results..".format( **locals())) return TEMPLATE.render( Context( dict(locals(), **{ "project": self.project, "user": self.user })))
def run(self, form): form_data = json.dumps(dict(form.data.lists())) size = form.cleaned_data['size'] offset = form.cleaned_data['offset'] show_aggregation = form.cleaned_data['aggregations'] with Timer() as timer: selection = SelectionSearch(form) self.monitor.update(1, "Executing query..") narticles = selection.get_count() self.monitor.update(39, "Fetching mediums..".format(**locals())) mediums = selection.get_mediums() self.monitor.update(59, "Fetching articles..".format(**locals())) articles = [escape_article_result(art) for art in selection.get_articles(size=size, offset=offset)] if show_aggregation: self.monitor.update(69, "Aggregating..".format(**locals())) statistics = selection.get_statistics() try: delta_start_end = statistics.end_date - statistics.start_date interval = next(interval for (interval, delta) in TIMEDELTAS if MAX_DATE_GROUPS * delta > delta_start_end) except (StopIteration, TypeError): interval = "day" date_aggr = selection.get_nested_aggregate([IntervalCategory(interval)]) date_aggr = fill_zeroes((((date,),(value,)) for date,value in date_aggr), IntervalCategory(interval)) medium_aggr = selection.get_nested_aggregate([MediumCategory()]) self.monitor.update(79, "Rendering results..".format(**locals())) return TEMPLATE.render(Context(dict(locals(), **{ "project": self.project, "user": self.user })))