Skip to content

Commit 9cfa417

Browse files
committed
Send usage statistics in a thread when Orange is started.
1 parent 622bc48 commit 9cfa417

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

Orange/canvas/__main__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from Orange.canvas.gui.splashscreen import SplashScreen
3535
from Orange.canvas.config import cache_dir
3636
from Orange.canvas import config
37+
from Orange.canvas.document.usagestatistics import UsageStatistics
3738

3839
from Orange.canvas.registry import qt
3940
from Orange.canvas.registry import WidgetRegistry, set_global_registry
@@ -271,6 +272,16 @@ def compare_versions(latest):
271272
thread.start()
272273
return thread
273274

275+
def send_usage_statistics():
276+
277+
class SendUsageStatistics(QThread):
278+
def run(self):
279+
usage = UsageStatistics()
280+
usage.send_statistics()
281+
282+
thread = SendUsageStatistics()
283+
thread.start()
284+
return thread
274285

275286
def main(argv=None):
276287
if argv is None:
@@ -545,6 +556,7 @@ def show_message(message):
545556
# local references prevent destruction
546557
survey = show_survey()
547558
update_check = check_for_updates()
559+
send_stat = send_usage_statistics()
548560

549561
# Tee stdout and stderr into Output dock
550562
log_view = canvas_window.log_view()
@@ -577,6 +589,7 @@ def show_message(message):
577589
del canvas_window
578590
del survey
579591
del update_check
592+
del send_stat
580593

581594
app.processEvents()
582595
app.flush()

Orange/canvas/document/usagestatistics.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ def log_node_added(self, widget_name, extended_widget=None):
105105
def set_node_type(self, addition_type):
106106
self.__node_addition_type = addition_type
107107

108+
def send_statistics(self):
109+
if release and config.settings()["error-reporting/send-statistics"]:
110+
if os.path.isfile(statistics_path):
111+
with open(statistics_path) as f:
112+
data = json.load(f)
113+
try:
114+
r = requests.post(server_url, files={'file': json.dumps(data)})
115+
if r.status_code != 200:
116+
log.warning("Error communicating with server while attempting to send "
117+
"usage statistics.")
118+
return
119+
# success - wipe statistics file
120+
log.info("Usage statistics sent.")
121+
with open(statistics_path, 'w') as f:
122+
json.dump([], f)
123+
except (ConnectionError, requests.exceptions.RequestException):
124+
log.warning("Connection error while attempting to send usage statistics.")
125+
except Exception:
126+
log.warning("Failed to send usage statistics.")
127+
108128
def write_statistics(self):
109129
if not release:
110130
log.info("Not sending usage statistics (non-release version of Orange detected).")
@@ -134,24 +154,9 @@ def write_statistics(self):
134154

135155
data.append(statistics)
136156

137-
def store_data(d):
138-
with open(statistics_path, 'w') as f:
139-
json.dump(d, f)
140-
141-
try:
142-
r = requests.post(server_url, files={'file': json.dumps(data)})
143-
if r.status_code != 200:
144-
log.warning("Error communicating with server while attempting to send "
145-
"usage statistics.")
146-
store_data(data)
147-
return
148-
# success - wipe statistics file
149-
log.info("Usage statistics sent.")
150-
with open(statistics_path, 'w') as f:
151-
json.dump([], f)
152-
except (ConnectionError, requests.exceptions.RequestException):
153-
log.warning("Connection error while attempting to send usage statistics.")
154-
store_data(data)
157+
with open(statistics_path, 'w') as f:
158+
json.dump(data, f)
159+
155160

156161
@staticmethod
157162
def set_last_search_query(query):

0 commit comments

Comments
 (0)