Skip to content

Commit 2f1325f

Browse files
authored
Merge pull request #3632 from rokgomiscek/SendUsageOnStartup
[FIX] Send usage statistics in a thread at startup
2 parents cd0c464 + 75a8c05 commit 2f1325f

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

Orange/canvas/__main__.py

Lines changed: 17 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,20 @@ 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+
try:
280+
usage = UsageStatistics()
281+
usage.send_statistics()
282+
except Exception: # pylint: disable=broad-except
283+
# exceptions in threads would crash Orange
284+
log.warning("Failed to send usage statistics.")
285+
286+
thread = SendUsageStatistics()
287+
thread.start()
288+
return thread
274289

275290
def main(argv=None):
276291
if argv is None:
@@ -545,6 +560,7 @@ def show_message(message):
545560
# local references prevent destruction
546561
survey = show_survey()
547562
update_check = check_for_updates()
563+
send_stat = send_usage_statistics()
548564

549565
# Tee stdout and stderr into Output dock
550566
log_view = canvas_window.log_view()
@@ -577,6 +593,7 @@ def show_message(message):
577593
del canvas_window
578594
del survey
579595
del update_check
596+
del send_stat
580597

581598
app.processEvents()
582599
app.flush()

Orange/canvas/document/usagestatistics.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ 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+
108126
def write_statistics(self):
109127
if not release:
110128
log.info("Not sending usage statistics (non-release version of Orange detected).")
@@ -134,24 +152,9 @@ def write_statistics(self):
134152

135153
data.append(statistics)
136154

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)
155+
with open(statistics_path, 'w') as f:
156+
json.dump(data, f)
157+
155158

156159
@staticmethod
157160
def set_last_search_query(query):

0 commit comments

Comments
 (0)