2016-08-03 22:18:51 +03:00
|
|
|
# Copyright 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2014-12-14 00:39:33 +02:00
|
|
|
import atexit
|
2014-11-29 22:48:15 +02:00
|
|
|
import platform
|
2015-09-03 19:04:09 +03:00
|
|
|
import Queue
|
2015-05-26 13:07:36 +03:00
|
|
|
import sys
|
2014-12-14 00:39:33 +02:00
|
|
|
import threading
|
2014-11-29 22:48:15 +02:00
|
|
|
import uuid
|
2015-09-03 19:04:09 +03:00
|
|
|
from collections import deque
|
2015-07-30 17:33:45 +03:00
|
|
|
from os import getenv
|
2015-09-03 19:04:09 +03:00
|
|
|
from time import sleep, time
|
2015-12-18 19:58:09 +02:00
|
|
|
from traceback import format_exc
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
import click
|
|
|
|
import requests
|
|
|
|
|
2015-06-06 15:02:12 +03:00
|
|
|
from platformio import __version__, app, exception, util
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TelemetryBase(object):
|
|
|
|
|
|
|
|
MACHINE_ID = str(uuid.uuid5(uuid.NAMESPACE_OID, str(uuid.getnode())))
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._params = {}
|
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
return self._params.get(name, None)
|
|
|
|
|
|
|
|
def __setitem__(self, name, value):
|
|
|
|
self._params[name] = value
|
|
|
|
|
|
|
|
def __delitem__(self, name):
|
|
|
|
if name in self._params:
|
|
|
|
del self._params[name]
|
|
|
|
|
2015-12-29 21:03:51 +02:00
|
|
|
def get_cid(self):
|
|
|
|
cid = app.get_state_item("cid")
|
|
|
|
if not cid:
|
|
|
|
cid = self.MACHINE_ID
|
|
|
|
app.set_state_item("cid", cid)
|
|
|
|
return cid
|
|
|
|
|
2014-11-29 22:48:15 +02:00
|
|
|
def send(self, hittype):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class MeasurementProtocol(TelemetryBase):
|
|
|
|
|
2016-08-26 11:46:59 +03:00
|
|
|
TID = "UA-1768265-9"
|
2014-11-29 22:48:15 +02:00
|
|
|
PARAMS_MAP = {
|
|
|
|
"screen_name": "cd",
|
|
|
|
"event_category": "ec",
|
|
|
|
"event_action": "ea",
|
|
|
|
"event_label": "el",
|
|
|
|
"event_value": "ev"
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
TelemetryBase.__init__(self)
|
|
|
|
self['v'] = 1
|
2016-08-26 11:46:59 +03:00
|
|
|
self['tid'] = self.TID
|
2015-12-29 21:03:51 +02:00
|
|
|
self['cid'] = self.get_cid()
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
self['sr'] = "%dx%d" % click.get_terminal_size()
|
|
|
|
self._prefill_screen_name()
|
|
|
|
self._prefill_appinfo()
|
|
|
|
self._prefill_custom_data()
|
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
if name in self.PARAMS_MAP:
|
|
|
|
name = self.PARAMS_MAP[name]
|
|
|
|
return TelemetryBase.__getitem__(self, name)
|
|
|
|
|
|
|
|
def __setitem__(self, name, value):
|
|
|
|
if name in self.PARAMS_MAP:
|
|
|
|
name = self.PARAMS_MAP[name]
|
|
|
|
TelemetryBase.__setitem__(self, name, value)
|
|
|
|
|
|
|
|
def _prefill_appinfo(self):
|
|
|
|
self['av'] = __version__
|
|
|
|
|
|
|
|
# gather dependent packages
|
|
|
|
dpdata = []
|
2016-02-23 21:24:00 +02:00
|
|
|
dpdata.append("PlatformIO/%s" % __version__)
|
2015-09-03 19:04:09 +03:00
|
|
|
if app.get_session_var("caller_id"):
|
|
|
|
dpdata.append("Caller/%s" % app.get_session_var("caller_id"))
|
2016-01-28 00:20:01 +02:00
|
|
|
if getenv("PLATFORMIO_IDE"):
|
|
|
|
dpdata.append("IDE/%s" % getenv("PLATFORMIO_IDE"))
|
2014-11-29 22:48:15 +02:00
|
|
|
self['an'] = " ".join(dpdata)
|
|
|
|
|
|
|
|
def _prefill_custom_data(self):
|
2015-04-29 18:17:14 +01:00
|
|
|
self['cd1'] = util.get_systype()
|
2014-11-29 22:48:15 +02:00
|
|
|
self['cd2'] = "Python/%s %s" % (platform.python_version(),
|
|
|
|
platform.platform())
|
2016-08-26 11:46:59 +03:00
|
|
|
self['cd4'] = 1 if not util.is_ci() else 0
|
|
|
|
if app.get_session_var("caller_id"):
|
|
|
|
self['cd5'] = str(app.get_session_var("caller_id")).lower()
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
def _prefill_screen_name(self):
|
2015-09-03 19:04:09 +03:00
|
|
|
self['cd3'] = " ".join([str(s).lower() for s in sys.argv[1:]])
|
|
|
|
|
2015-09-03 20:07:35 +03:00
|
|
|
if not app.get_session_var("command_ctx"):
|
|
|
|
return
|
|
|
|
ctx_args = app.get_session_var("command_ctx").args
|
2015-09-03 19:04:09 +03:00
|
|
|
args = [str(s).lower() for s in ctx_args if not str(s).startswith("-")]
|
2014-11-29 22:48:15 +02:00
|
|
|
if not args:
|
|
|
|
return
|
2016-08-11 10:54:28 +03:00
|
|
|
if args[0] in ("lib", "platform", "serialports", "settings"):
|
2014-11-29 22:48:15 +02:00
|
|
|
cmd_path = args[:2]
|
|
|
|
else:
|
|
|
|
cmd_path = args[:1]
|
|
|
|
self['screen_name'] = " ".join([p.title() for p in cmd_path])
|
|
|
|
|
|
|
|
def send(self, hittype):
|
2014-12-06 19:00:36 +02:00
|
|
|
if not app.get_setting("enable_telemetry"):
|
|
|
|
return
|
|
|
|
|
2014-11-29 22:48:15 +02:00
|
|
|
self['t'] = hittype
|
|
|
|
|
|
|
|
# correct queue time
|
|
|
|
if "qt" in self._params and isinstance(self['qt'], float):
|
|
|
|
self['qt'] = int((time() - self['qt']) * 1000)
|
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
MPDataPusher().push(self._params)
|
2014-12-14 00:39:33 +02:00
|
|
|
|
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
@util.singleton
|
|
|
|
class MPDataPusher(object):
|
2014-12-14 00:39:33 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
MAX_WORKERS = 5
|
2014-12-14 00:39:33 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
2015-09-03 19:04:09 +03:00
|
|
|
self._queue = Queue.LifoQueue()
|
|
|
|
self._failedque = deque()
|
|
|
|
self._http_session = requests.Session()
|
|
|
|
self._http_offline = False
|
|
|
|
self._workers = []
|
|
|
|
|
|
|
|
def push(self, item):
|
|
|
|
# if network is off-line
|
|
|
|
if self._http_offline:
|
|
|
|
if "qt" not in item:
|
|
|
|
item['qt'] = time()
|
|
|
|
self._failedque.append(item)
|
|
|
|
return
|
2014-12-14 00:39:33 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
self._queue.put(item)
|
|
|
|
self._tune_workers()
|
2014-12-14 00:39:33 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
def in_wait(self):
|
|
|
|
return self._queue.unfinished_tasks
|
2014-12-14 00:39:33 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
def get_items(self):
|
|
|
|
items = list(self._failedque)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
items.append(self._queue.get_nowait())
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
return items
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
def _tune_workers(self):
|
|
|
|
for i, w in enumerate(self._workers):
|
|
|
|
if not w.is_alive():
|
|
|
|
del self._workers[i]
|
|
|
|
|
|
|
|
need_nums = min(self._queue.qsize(), self.MAX_WORKERS)
|
|
|
|
active_nums = len(self._workers)
|
|
|
|
if need_nums <= active_nums:
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in range(need_nums - active_nums):
|
|
|
|
t = threading.Thread(target=self._worker)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
self._workers.append(t)
|
|
|
|
|
|
|
|
def _worker(self):
|
|
|
|
while True:
|
2015-11-17 23:04:49 +02:00
|
|
|
try:
|
|
|
|
item = self._queue.get()
|
|
|
|
_item = item.copy()
|
|
|
|
if "qt" not in _item:
|
|
|
|
_item['qt'] = time()
|
|
|
|
self._failedque.append(_item)
|
|
|
|
if self._send_data(item):
|
|
|
|
self._failedque.remove(_item)
|
|
|
|
self._queue.task_done()
|
|
|
|
except: # pylint: disable=W0702
|
|
|
|
pass
|
2015-09-03 19:04:09 +03:00
|
|
|
|
|
|
|
def _send_data(self, data):
|
2015-12-29 21:03:51 +02:00
|
|
|
if self._http_offline:
|
|
|
|
return False
|
2015-09-03 19:04:09 +03:00
|
|
|
try:
|
|
|
|
r = self._http_session.post(
|
|
|
|
"https://ssl.google-analytics.com/collect",
|
|
|
|
data=data,
|
|
|
|
headers=util.get_request_defheaders(),
|
2016-08-03 23:38:20 +03:00
|
|
|
timeout=1)
|
2015-09-03 19:04:09 +03:00
|
|
|
r.raise_for_status()
|
2015-12-29 21:03:51 +02:00
|
|
|
return True
|
2015-09-03 19:04:09 +03:00
|
|
|
except: # pylint: disable=W0702
|
2015-12-29 21:03:51 +02:00
|
|
|
self._http_offline = True
|
|
|
|
return False
|
2015-09-03 19:04:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
def on_command():
|
|
|
|
resend_backuped_reports()
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
mp = MeasurementProtocol()
|
2014-12-14 00:39:33 +02:00
|
|
|
mp.send("screenview")
|
2015-09-03 19:04:09 +03:00
|
|
|
|
2015-07-30 17:33:45 +03:00
|
|
|
if util.is_ci():
|
|
|
|
measure_ci()
|
|
|
|
|
|
|
|
|
|
|
|
def measure_ci():
|
2016-08-03 23:38:20 +03:00
|
|
|
event = {"category": "CI", "action": "NoName", "label": None}
|
2015-07-30 17:33:45 +03:00
|
|
|
|
|
|
|
envmap = {
|
|
|
|
"APPVEYOR": {"label": getenv("APPVEYOR_REPO_NAME")},
|
|
|
|
"CIRCLECI": {"label": "%s/%s" % (getenv("CIRCLE_PROJECT_USERNAME"),
|
|
|
|
getenv("CIRCLE_PROJECT_REPONAME"))},
|
|
|
|
"TRAVIS": {"label": getenv("TRAVIS_REPO_SLUG")},
|
|
|
|
"SHIPPABLE": {"label": getenv("REPO_NAME")},
|
|
|
|
"DRONE": {"label": getenv("DRONE_REPO_SLUG")}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value in envmap.iteritems():
|
|
|
|
if getenv(key, "").lower() != "true":
|
|
|
|
continue
|
|
|
|
event.update({"action": key, "label": value['label']})
|
|
|
|
|
|
|
|
on_event(**event)
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
|
2014-11-30 17:00:30 +02:00
|
|
|
def on_run_environment(options, targets):
|
2014-12-04 23:17:45 +02:00
|
|
|
opts = ["%s=%s" % (opt, value) for opt, value in sorted(options.items())]
|
2014-11-30 17:00:30 +02:00
|
|
|
targets = [t.title() for t in targets or ["run"]]
|
2014-12-04 23:17:45 +02:00
|
|
|
on_event("Env", " ".join(targets), "&".join(opts))
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def on_event(category, action, label=None, value=None, screen_name=None):
|
|
|
|
mp = MeasurementProtocol()
|
|
|
|
mp['event_category'] = category[:150]
|
|
|
|
mp['event_action'] = action[:500]
|
|
|
|
if label:
|
|
|
|
mp['event_label'] = label[:500]
|
|
|
|
if value:
|
|
|
|
mp['event_value'] = int(value)
|
|
|
|
if screen_name:
|
|
|
|
mp['screen_name'] = screen_name[:2048]
|
2014-12-14 00:39:33 +02:00
|
|
|
mp.send("event")
|
2014-11-29 22:48:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def on_exception(e):
|
2015-06-06 15:02:12 +03:00
|
|
|
if isinstance(e, exception.AbortedByUser):
|
|
|
|
return
|
2015-12-18 19:58:09 +02:00
|
|
|
is_crash = any([
|
2015-11-26 22:02:59 +02:00
|
|
|
not isinstance(e, exception.PlatformioException),
|
|
|
|
"Error" in e.__class__.__name__
|
2015-12-18 19:58:09 +02:00
|
|
|
])
|
|
|
|
mp = MeasurementProtocol()
|
|
|
|
mp['exd'] = "%s: %s" % (type(e).__name__, format_exc() if is_crash else e)
|
|
|
|
mp['exf'] = 1 if is_crash else 0
|
2014-12-14 00:39:33 +02:00
|
|
|
mp.send("exception")
|
|
|
|
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
@atexit.register
|
|
|
|
def _finalize():
|
|
|
|
timeout = 1000 # msec
|
|
|
|
elapsed = 0
|
2016-01-14 01:07:57 +02:00
|
|
|
try:
|
|
|
|
while elapsed < timeout:
|
|
|
|
if not MPDataPusher().in_wait():
|
|
|
|
break
|
|
|
|
sleep(0.2)
|
|
|
|
elapsed += 200
|
|
|
|
backup_reports(MPDataPusher().get_items())
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2015-09-03 19:04:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
def backup_reports(items):
|
|
|
|
if not items:
|
2014-12-14 00:39:33 +02:00
|
|
|
return
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
KEEP_MAX_REPORTS = 100
|
2014-11-29 22:48:15 +02:00
|
|
|
tm = app.get_state_item("telemetry", {})
|
|
|
|
if "backup" not in tm:
|
|
|
|
tm['backup'] = []
|
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
for params in items:
|
2014-12-14 00:39:33 +02:00
|
|
|
# skip static options
|
|
|
|
for key in params.keys():
|
|
|
|
if key in ("v", "tid", "cid", "cd1", "cd2", "sr", "an"):
|
|
|
|
del params[key]
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2014-12-14 00:39:33 +02:00
|
|
|
# store time in UNIX format
|
|
|
|
if "qt" not in params:
|
|
|
|
params['qt'] = time()
|
|
|
|
elif not isinstance(params['qt'], float):
|
|
|
|
params['qt'] = time() - (params['qt'] / 1000)
|
|
|
|
|
|
|
|
tm['backup'].append(params)
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
tm['backup'] = tm['backup'][KEEP_MAX_REPORTS * -1:]
|
2014-11-29 22:48:15 +02:00
|
|
|
app.set_state_item("telemetry", tm)
|
|
|
|
|
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
def resend_backuped_reports():
|
2014-12-14 00:39:33 +02:00
|
|
|
tm = app.get_state_item("telemetry", {})
|
|
|
|
if "backup" not in tm or not tm['backup']:
|
|
|
|
return False
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
for report in tm['backup']:
|
|
|
|
mp = MeasurementProtocol()
|
|
|
|
for key, value in report.items():
|
|
|
|
mp[key] = value
|
|
|
|
mp.send(report['t'])
|
2014-11-29 22:48:15 +02:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
# clean
|
|
|
|
tm['backup'] = []
|
|
|
|
app.set_state_item("telemetry", tm)
|