async def _request_dispatch( self, n: Notification, log: NotificationLoggerAdapter, body: dict, headers: Dict[AnyStr, List[AnyStr]], pushkeys: List[str], span: Span, ) -> Tuple[List[str], List[str]]: poke_start_time = time.time() failed = [] response, response_text = await self._perform_http_request( body, headers) RESPONSE_STATUS_CODES_COUNTER.labels(pushkin=self.name, code=response.code).inc() log.debug("GCM request took %f seconds", time.time() - poke_start_time) span.set_tag(tags.HTTP_STATUS_CODE, response.code) if 500 <= response.code < 600: log.debug("%d from server, waiting to try again", response.code) retry_after = None for header_value in response.headers.getRawHeaders(b"retry-after", default=[]): retry_after = int(header_value) span.log_kv({ "event": "gcm_retry_after", "retry_after": retry_after }) raise TemporaryNotificationDispatchException( "GCM server error, hopefully temporary.", custom_retry_delay=retry_after) elif response.code == 400: log.error( "%d from server, we have sent something invalid! Error: %r", response.code, response_text, ) # permanent failure: give up raise NotificationDispatchException("Invalid request") elif response.code == 401: log.error("401 from server! Our API key is invalid? Error: %r", response_text) # permanent failure: give up raise NotificationDispatchException("Not authorised to push") elif response.code == 404: # assume they're all failed log.info("Reg IDs %r get 404 response; assuming unregistered", pushkeys) return pushkeys, [] elif 200 <= response.code < 300: try: resp_object = json_decoder.decode(response_text) except ValueError: raise NotificationDispatchException( "Invalid JSON response from GCM.") if "results" not in resp_object: log.error( "%d from server but response contained no 'results' key: %r", response.code, response_text, ) if len(resp_object["results"]) < len(pushkeys): log.error( "Sent %d notifications but only got %d responses!", len(n.devices), len(resp_object["results"]), ) span.log_kv({ logs.EVENT: "gcm_response_mismatch", "num_devices": len(n.devices), "num_results": len(resp_object["results"]), }) # determine which pushkeys to retry or forget about new_pushkeys = [] for i, result in enumerate(resp_object["results"]): if "error" in result: log.warning("Error for pushkey %s: %s", pushkeys[i], result["error"]) span.set_tag("gcm_error", result["error"]) if result["error"] in BAD_PUSHKEY_FAILURE_CODES: log.info( "Reg ID %r has permanently failed with code %r: " "rejecting upstream", pushkeys[i], result["error"], ) failed.append(pushkeys[i]) elif result["error"] in BAD_MESSAGE_FAILURE_CODES: log.info( "Message for reg ID %r has permanently failed with code %r", pushkeys[i], result["error"], ) else: log.info( "Reg ID %r has temporarily failed with code %r", pushkeys[i], result["error"], ) new_pushkeys.append(pushkeys[i]) return failed, new_pushkeys else: raise NotificationDispatchException( f"Unknown GCM response code {response.code}")
def get_report_summary(objectives: Iterator[Objective], unit: str, start: datetime, end: datetime, current_span: opentracing.Span) -> List[dict]: summary = [] start = truncate(start) for objective in objectives: days = collections.defaultdict(dict) if not len(objective.targets): current_span.log_kv({ 'objective_skipped': True, 'objective': objective.id }) continue current_span.log_kv({ 'objective_target_count': len(objective.targets), 'objective_id': objective.id }) # Instrument objective summary! objective_summary_span = opentracing.tracer.start_span( operation_name='report_objective_summary', child_of=current_span) objective_summary_span.set_tag('objective_id', objective.id) with objective_summary_span: for target in objective.targets: objective_summary_span.log_kv({ 'target_id': target.id, 'indicator_id': target.indicator_id }) ivs = (IndicatorValue.query.filter( IndicatorValue.indicator_id == target.indicator_id, IndicatorValue.timestamp >= start, IndicatorValue.timestamp < end).order_by( IndicatorValue.timestamp)) target_values_truncated = truncate_values( ivs, parent_span=objective_summary_span) for truncated_date, target_values in target_values_truncated.items( ): target_form = target.target_from or float('-inf') target_to = target.target_to or float('inf') target_count = len(target_values) target_sum = sum(target_values) breaches = target_count - len([ v for v in target_values if v >= target_form and v <= target_to ]) days[truncated_date.isoformat()][target.indicator.name] = { 'aggregation': target.indicator.aggregation, 'avg': target_sum / target_count, 'breaches': breaches, 'count': target_count, 'max': max(target_values), 'min': min(target_values), 'sum': target_sum, } summary.append({ 'title': objective.title, 'description': objective.description, 'id': objective.id, 'targets': [{ 'from': t.target_from, 'to': t.target_to, 'sli_name': t.indicator.name, 'unit': t.indicator.unit, 'aggregation': t.indicator.aggregation } for t in objective.targets], 'days': days }) return summary