def startup_stats(self): """Initial statistics collection and transmssion at startup""" with session_scope(MYCODO_DB_PATH) as new_session: misc = new_session.query(Misc).first() self.opt_out_statistics = misc.stats_opt_out if not self.opt_out_statistics: self.logger.info("[Daemon] Anonymous statistics enabled") else: self.logger.info("[Daemon] Anonymous statistics disabled") try: # if statistics file doesn't exist, create it if not os.path.isfile(STATS_CSV): self.logger.debug("[Daemon] Statistics file doesn't " "exist, creating {}".format(STATS_CSV)) daemonutils.recreate_stat_file() daemon_startup_time = timeit.default_timer()-self.startup_timer self.logger.info("[Daemon] Mycodo v{} started in {} seconds".format( MYCODO_VERSION, daemon_startup_time)) daemonutils.add_update_stat( self.logger, 'daemon_startup_seconds', daemon_startup_time) except Exception as msg: self.logger.warning( "[Daemon] Statistics initilization Error: {}".format(msg))
def send_stats(self): """Collect and send statistics""" try: stat_dict = daemonutils.return_stat_file_dict() if float(stat_dict['next_send']) < time.time(): self.timer_stats = time.time()+STATS_INTERVAL daemonutils.add_update_stat(self.logger, 'next_send', self.timer_stats) else: self.timer_stats = float(stat_dict['next_send']) except Exception as msg: self.logger.warning("Error: Cound not read file. Deleting file and regenerating. Error msg: {}".format(msg)) os.remove(STATS_CSV) daemonutils.recreate_stat_file() try: daemonutils.send_stats(self.logger, STATS_HOST, STATS_PORT, STATS_USER, STATS_PASSWORD, STATS_DATABASE) except Exception as msg: self.logger.warning("Error: Cound not send statistics: {}".format(msg))