def map_url(params, utm={}): if not params: return BASE_PATH if 'begin' in params: params['begin'] = params['begin'].strftime('%Y-%m-%d') if 'end' in params: params['end'] = params['end'].strftime('%Y-%m-%d') if 'geostore' in params and params['geostore'] is None: del params['geostore'] if 'geostore' not in params and 'geom' in params: geojson = json.dumps(params['geom']) if len(geojson) <= 1000: params['geojson'] = geojson if 'topic' in params: topic = Topic.get_by_id(params['topic']) else: topic = Topic.all()[0] baselayer = topic.baselayer url_params = {k: params[k] for k in ALLOWED_PARAMS if k in params} url_params = dict(url_params.items() + utm.items()) query_string = urllib.urlencode(url_params, doseq=True) return BASE_URL + BASE_PATH + '/-1/0/0/' + iso(params) + '/grayscale/' + \ baselayer + '?' + query_string
def send_for_event(self, event): topic_result = self.subscription.run_analysis(event.begin, event.end) if topic_result.is_zero() == False: topic = Topic.get_by_id(event.topic) subscriptions_url = gfw_url('my_gfw/subscriptions', {}) unsubscribe_url = '%s/v2/subscriptions/%s/unsubscribe' % \ (runtime_config['APP_BASE_URL'], str(self.subscription.key.id())) begin = event.begin.strftime('%d %b %Y') end = event.end.strftime('%d %b %Y') url_params = self.subscription.params url_params['begin'] = event.begin url_params['end'] = event.end url_params['fit_to_geom'] = 'true' url_params['tab'] = 'analysis-tab' alert_link = map_url(self.subscription.params) email = self.subscription.email user_profile = self.subscription.user_id.get().get_profile() name = getattr(user_profile, 'name', email) template_params = { 'selected_area': topic_result.area_name(), 'alert_count': topic_result.formatted_value(), 'alert_type': topic.description, 'alert_date': begin + " to " + end, 'alert_summary': summary_for_topic(topic), 'alert_name': self.subscription.formatted_name(), 'alert_link': alert_link, 'unsubscribe_url': unsubscribe_url, 'subscriptions_url': subscriptions_url } if topic.id == 'alerts/viirs': map_image = '%s/v2/subscriptions/%s/overview.png' % \ (runtime_config['APP_BASE_URL'], str(self.subscription.key.id())) template_params['map_image'] = map_image urlfetch.fetch(map_image, method=urlfetch.GET) fire_alerts = topic_result.value()[1][:10] for alert in fire_alerts: alert['acq_date'] = alert['acq_date'].split('T')[0] alert['acq_time'] = alert['acq_time'][:2] + ':' + alert['acq_time'][2:] + ' UTC' alert['latitude'] = "{0:.3f}".format(alert['latitude']) alert['longitude'] = "{0:.3f}".format(alert['longitude']) template_params['fire_alerts'] = fire_alerts response = sparkpost.transmissions.send( recipients=[{'address': { 'email': email, 'name': name }}], template=template_for_topic(topic), substitution_data=template_params ) logging.info("Send Subscription Email Result: %s" % response)
def run_analysis(self, begin, end): params = copy.copy(self.params) params['begin'] = begin params['end'] = end if 'geom' in params: geom = params['geom'] if 'geometry' in geom: geom = geom['geometry'] params['geojson'] = json.dumps(geom) topic = Topic.get_by_id(self.topic) return topic.execute(params)
def __init__(self, subscription): self.subscription = subscription self.topic = Topic.get_by_id(subscription.topic)