def job_queue(): """ View for listing current jobs working """ error = get_errors(request.args) p_list = list() p_list.append(Markup('<thead><tr><th>is_alive</th><th>url' '</th></tr></thead>\n<tbody>\n')) keys = req_cb_get_cache_keys(VIEW_LOCK) for key in keys: # Log the status of the job url = req_cb_get_url(key, VIEW_LOCK) is_alive = str(req_cb_get_is_running(key, VIEW_LOCK)) p_list.append('<tr><td>') response_url = "".join(['<a href="', request.url_root, url + '">', url, '</a>']) p_list.append("</td><td>".join([is_alive, escape(Markup(response_url)), ])) p_list.append(Markup('</td></tr>')) p_list.append(Markup('\n</tbody>')) if error: return render_template('queue.html', procs=p_list, error=error) else: return render_template('queue.html', procs=p_list)
def output(cohort, metric): """ View corresponding to a data request - All of the setup and execution for a request happens here. """ # Get URL. Check for refresh flag - drop from url url = request.url.split(request.url_root)[1] refresh = True if 'refresh' in request.args else False if refresh: url = sub(REFRESH_REGEX, '', url) # Get the refresh date of the cohort try: cid = query_mod.get_cohort_id(cohort) cohort_refresh_ts = get_cohort_refresh_datetime(cid) except Exception: cohort_refresh_ts = None logging.error(__name__ + ' :: Could not retrieve refresh ' 'time of cohort.') # Build a request and validate. # # 1. Populate with request parameters from query args. # 2. Filter the input discarding any url junk # 3. Process defaults for request parameters # 4. See if this maps to a single user request # 5. See if this maps to a single user request try: rm = RequestMetaFactory(cohort, cohort_refresh_ts, metric) except MetricsAPIError as e: return redirect(url_for('all_cohorts') + '?error=' + str(e.error_code)) filter_request_input(request, rm) try: format_request_params(rm) except MetricsAPIError as e: return redirect(url_for('all_cohorts') + '?error=' + str(e.error_code)) if rm.is_user: project = rm.project if rm.project else 'enwiki' if not MediaWikiUser.is_user_name(cohort, project): logging.error(__name__ + ' :: "{0}" is not a valid username ' 'in "{1}"'.format(cohort, project)) return redirect(url_for('all_cohorts') + '?error=3') else: # @TODO CALL COHORT VALIDATION HERE pass # Determine if the request maps to an existing response. # # 1. The response already exists in the hash, return. # 2. Otherwise, add the request tot the queue. data = get_data(rm) key_sig = build_key_signature(rm, hash_result=True) # Is the request already running? is_running = req_cb_get_is_running(key_sig, VIEW_LOCK) # Determine if request is already hashed if data and not refresh: return make_response(jsonify(data)) # Determine if the job is already running elif is_running: return render_template('processing.html', error=error_codes[0], url_str=str(rm)) # Add the request to the queue else: api_request_queue.put(unpack_fields(rm), block=True) req_cb_add_req(key_sig, url, VIEW_LOCK) return render_template('processing.html', url_str=str(rm))