telemetry.py (1660B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import time 6 7 import requests 8 9 from qm_try_analysis.logging import info 10 11 TELEMETRY_BASE_URL = "https://sql.telemetry.mozilla.org/api/" 12 13 14 def query(key, query, p_params): 15 headers = {"Authorization": f"Key {key}"} 16 start_url = TELEMETRY_BASE_URL + f"queries/{query}/refresh?{p_params}" 17 info(f"Starting job using url {start_url}") 18 resp = requests.post(url=start_url, headers=headers) 19 job = resp.json()["job"] 20 job_id = job["id"] 21 info(f"Started job {job_id}") 22 23 poll_url = TELEMETRY_BASE_URL + f"jobs/{job_id}" 24 info(f"Polling query status from {poll_url}") 25 poll = True 26 status = 0 27 qresultid = 0 28 while poll: 29 print(".", end="", flush=True) 30 resp = requests.get(url=poll_url, headers=headers) 31 status = resp.json()["job"]["status"] 32 if status > 2: 33 # print(resp.json()) 34 poll = False 35 qresultid = resp.json()["job"]["query_result_id"] 36 else: 37 time.sleep(0.2) 38 print(".") 39 info(f"Finished with status {status}") 40 41 if status == 3: 42 results_url = TELEMETRY_BASE_URL + f"queries/78691/results/{qresultid}.json" 43 44 info(f"Querying result from {results_url}") 45 resp = requests.get(url=results_url, headers=headers) 46 return resp.json() 47 48 return {"query_result": {"data": {"rows": {}}}} 49 50 51 def getLastEventTimeAbs(rows): 52 if len(rows) == 0: 53 return 0 54 return rows[len(rows) - 1]["submit_timeabs"]