def get_wifi_from_index(self, index_name): urlfetch.set_default_fetch_deadline(30) output = {"data": {}, "error": ""} index = ApiClient( self.searchify_credentials.api_client).get_index(index_name) query = SearchifyQuery() try: device_id = self.request.get("device_id", "") query.set_query("text:UNIQUE") query.set_category_filters({"device_id": device_id}) query.set_length(min(700, int(self.request.get("length", 100)))) query.set_fetch_fields(['text', 'device_id', 'timestamp']) results = index.search(**query.mapping())['results'] regex_pattern = "(.*?) (-[0-9]+) ([0-2]) ([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):([a-z0-9]+):" latest_log_with_unique_ssid = sorted( [ r for r in results if r['device_id'] == device_id and "SSID RSSI UNIQUE" in r['text'] ], key=lambda z: int(z.get('timestamp', 0))) if len(latest_log_with_unique_ssid) > 0: matches = re.findall( regex_pattern, latest_log_with_unique_ssid[-1] ['text'].split("SSID RSSI UNIQUE")[-1]) all_wifis_seen = [{ 'network_name': item[0], 'signal_strength': item[1], 'network_security': item[2] } for item in matches] output['data'] = { "scan_time": latest_log_with_unique_ssid[-1]['timestamp'], "networks": sorted(all_wifis_seen, key=lambda w: int(w.get("signal_strength", 0)), reverse=True) } except Exception as e: output['error'] = display_error(e) log.error('ERROR: {}'.format(display_error(e))) return output
def get(self): output = {'data': {'status': {}, 'recipient': {}}, 'error': ''} zendesk_cred = self.zendesk_credentials if not zendesk_cred: self.error(500) zen_auth = (zendesk_cred.email_account + '/token', zendesk_cred.api_token) try: for ticket_type in ['new', 'open', 'pending', 'solved', 'closed']: zen_url = "{}/api/v2/search.json?query=type:ticket%20status:{}".format( zendesk_cred.domain, ticket_type) zen_response = requests.get(zen_url, auth=zen_auth) if zen_response.ok: output['data']['status'].update( {ticket_type: zen_response.json()['count']}) for ticket_recipient in [ '*****@*****.**', '*****@*****.**', '*****@*****.**', 'chat', 'web_form' ]: zen_url = "{}/api/v2/search.json?query=type:ticket%20via:{}".format( zendesk_cred.domain, ticket_recipient) zen_response = requests.get(zen_url, auth=zen_auth) if zen_response.ok: output['data']['recipient'].update({ ticket_recipient or 'unknown_recipient': zen_response.json()['count'] }) except Exception as e: output['error'] += display_error(e) self.response.write(json.dumps(output))
def get(self): output = {'data': {}, 'error': ''} start_date = self.request.get('start_date') ## yyyy-mm-dd end_date = self.request.get('end_date') ## yyyy-mm-dd date_type = self.request.get('date_type', default_value="created") zendesk_cred = self.zendesk_credentials if not zendesk_cred: self.error(500) tickets = [] zen_api = "{}/api/v2/search.json?query=type:ticket%20".format( zendesk_cred.domain) if start_date and end_date: search_url = zen_api + "{}>{}+{}<{}".format( date_type, start_date, date_type, end_date) elif start_date: search_url = zen_api + "{}>{}".format(date_type, start_date) elif end_date: search_url = zen_api + "{}<{}".format(date_type, end_date) else: search_url = zen_api + "{}".format(date_type) zen_auth = (zendesk_cred.email_account + '/token', zendesk_cred.api_token) try: zen_response = requests.get(search_url, auth=zen_auth) if zen_response.ok: tickets += zen_response.json().get('results', []) else: raise RuntimeError("Fail to retrieve tickets") # Keep querying on as long as paginating is possible while zen_response.json().get('next_page') is not None: zen_response = requests.get( zen_response.json().get('next_page'), auth=zen_auth) if zen_response.ok: tickets += zen_response.json().get('results', []) # tickets = sorted(tickets, key=lambda k: iso_to_utc_timestamp(k.get('created_at'))) if not tickets: raise RuntimeError("There is no ticket for specified query") output['data'] = get_zendesk_stats(tickets) except Exception as e: output['error'] = display_error(e) self.response.write(json.dumps(output))
def search_within_index(self, index_name): output = {'results': [], 'error': {}} index = ApiClient( self.searchify_credentials.api_client).get_index(index_name) log.info("searchify request {}".format(self.searchify_request)) try: if "1" not in index.list_functions().keys(): index.add_function(1, "-doc.var[0]") output.update(index.search(**self.searchify_request)) except Exception as e: output['error'] = {index_name: display_error(e)} log.info("Searching in {}".format(index_name)) return output
def get(self): output = {'data': []} zendesk_cred = self.zendesk_credentials tickets = [] zen_api = "{}/api/v2/search.json?query=type:ticket%20".format( zendesk_cred.domain) start_date = (get_current_pacific_datetime() - datetime.timedelta(days=2)).strftime('%Y-%m-%d') date_type = "created" search_url = zen_api + "{}>{}".format(date_type, start_date) zen_auth = (zendesk_cred.email_account + '/token', zendesk_cred.api_token) try: zen_response = requests.get(search_url, auth=zen_auth) if zen_response.ok: tickets += zen_response.json().get('results', []) else: raise RuntimeError("Fail to retrieve tickets") # Keep querying on as long as paginating is possible while zen_response.json().get('next_page') is not None: zen_response = requests.get( zen_response.json().get('next_page'), auth=zen_auth) if zen_response.ok: tickets += zen_response.json().get('results', []) output['data'] = get_zendesk_stats(tickets) log.info(output) tickets_status = output['data']['total_breakdown']['status'] zendesk_cron_stats = ZendeskDailyStats( new_tickets=tickets_status['new'], open_tickets=tickets_status['open'], pending_tickets=tickets_status['pending'], solved_tickets=tickets_status['solved'], closed_tickets=tickets_status['closed']) zendesk_cron_stats.put() except Exception as e: log.error('ERROR: {}'.format(display_error(e)))
def get_facets(self, index_name): output = {"data": [], "error": "Facets not found!"} index = ApiClient( self.searchify_credentials.api_client).get_index(index_name) try: output['data'] = index.search(query="text:{}".format(self.pattern), category_filters={ "middle_fw_version": self.middle_fw_version.upper() }, docvar_filters={ 0: [[self.start_ts, self.end_ts]] }).get("facets", {}) if output['data']: output['error'] = "" except Exception as e: output['error'] = display_error(e) log.error("") return output
def get(self): output = {'data': [], 'error': ''} try: searchify_cred = self.searchify_credentials searchify_client = ApiClient(searchify_cred.api_client) index_stats = [{ "name": index.__dict__['_IndexClient__index_url'].split("indexes/") [-1], "size": index.get_size(), "created_at": int(index.get_creation_time().strftime("%s")) * 1000 } for index in searchify_client.list_indexes()] output['data'] = sorted(index_stats, key=lambda i: i['created_at']) except Exception as e: output['error'] = display_error(e) self.response.write(json.dumps(output))
def get(self): output = {'data': [], 'error': ''} try: for daily_stats in ZendeskDailyStats.query_stats(): output['data'].append({ 'new_tickets': daily_stats.new_tickets, 'open_tickets': daily_stats.open_tickets, 'solved_tickets': daily_stats.solved_tickets, 'closed_tickets': daily_stats.closed_tickets, 'pending_tickets': daily_stats.pending_tickets, 'created_at': int(daily_stats.created_at.strftime("%s")) }) except Exception as e: output['error'] = display_error(e) self.response.write(json.dumps(output))
def get(self): urlfetch.set_default_fetch_deadline(20) output = {"data": [], "error": ""} now_ts = int(datetime.datetime.now().strftime("%s")) * 1000 stats_type = self.request.get("type", default_value="heap") input_ts = self.request.get("start_time", default_value=now_ts) index = self.request.get("index", default_value=self.SENSE_LOGS_INDEX_PATTERN) device_id = self.request.get("device_id", "") length = min(10000, int(self.request.get("length", 100))) try: response = requests.post(url="{}/{}/_search".format( self.base_url, index), data=json.dumps({ "query": { "filtered": { "query": { "bool": { "must": [{ "match": { "sense_id": device_id } }] } }, "filter": { "range": { "epoch_millis": { "gte": input_ts } } } } }, "size": length, "sort": [{ "epoch_millis": { "order": "asc" } }] }), headers={"Authorization": self.token}) results = [] for hit in json.loads(response.content)["hits"]["hits"]: results.append(hit["_source"]) if stats_type == "voc": regex_pattern = "TVOC (\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)\\n" else: regex_pattern = "collecting time (\\d+).*\\nheap (\\d+) \\+: (\\d+) -: (\\d+)\\n" matches = [re.findall(regex_pattern, r['text']) for r in results] if stats_type == "voc": output['data'] = [{ 'timestamp': int(item[0]) * 1000, 'tvoc': int(item[1]), 'eco2': int(item[2]), 'current': int(item[3]), 'voltage': int(item[4]), 'temp': int(item[5]), 'humid': int(item[6]) } for sublist in sorted(matches, key=self.getTime) for item in sublist if all([i.isdigit() for i in item])] else: output['data'] = [{ 'timestamp': int(item[0]) * 1000, 'free': int(item[1]), 'max': int(item[2]), 'min': int(item[3]) } for sublist in sorted(matches, key=self.getTime) for item in sublist if all([i.isdigit() for i in item])] except Exception as e: output['error'] = display_error(e) log.error('ERROR: {}'.format(display_error(e))) self.response.write(json.dumps(output))