def __checkThreads(self): timestamp = rmCurrentTimestamp() # Work around for a system date change resetWatcher = not self.__lastCheckTimestamp is None and \ abs(timestamp - self.__lastCheckTimestamp) >= self.__systemTimeChangeThreshold everythingOk = True if resetWatcher: fromTime = rmTimestampToDateAsString( self.__lastCheckTimestamp ) if self.__lastCheckTimestamp else None toTime = rmTimestampToDateAsString(timestamp) log.warning( "System time has changed (from %s, to %s)! Resetting thread watcher!" % (fromTime, toTime)) self.__lastWatchdogTimestamp = None for threadId in self.__data: details = self.__getThreadEntry(threadId) details[RMThreadWatcher.LastUpdateKey] = None if self.__mainManager: self.__mainManager.systemDateTimeChanged() else: for threadId in self.__data: details = self.__getThreadEntry(threadId) timeoutHint = details[RMThreadWatcher.TimeoutHintKey] lastUpdate = details[RMThreadWatcher.LastUpdateKey] if timeoutHint is None: log.debug("Thread %d (%s) has no timeout hint!" % (threadId, details[RMThreadWatcher.NameHintKey])) elif lastUpdate is None: log.debug("Thread %d (%s) is not started yet!" % (threadId, details[RMThreadWatcher.NameHintKey])) elif timeoutHint < (timestamp - lastUpdate): everythingOk = False log.debug("Thread %d (%s) didn't responde since [%s] (timeoutHint=%d, timeout=%d)!" % \ (threadId, details[RMThreadWatcher.NameHintKey], rmTimestampToDateAsString(lastUpdate), timeoutHint, (timestamp - lastUpdate))) self.__lastCheckTimestamp = timestamp return everythingOk
def __checkThreads(self): timestamp = rmCurrentTimestamp() # Work around for a system date change resetWatcher = not self.__lastCheckTimestamp is None and \ abs(timestamp - self.__lastCheckTimestamp) >= self.__systemTimeChangeThreshold everythingOk = True if resetWatcher: fromTime = rmTimestampToDateAsString(self.__lastCheckTimestamp) if self.__lastCheckTimestamp else None toTime = rmTimestampToDateAsString(timestamp) log.warning("System time has changed (from %s, to %s)! Resetting thread watcher!" % (fromTime, toTime)) self.__lastWatchdogTimestamp = None for threadId in self.__data: details = self.__getThreadEntry(threadId) details[RMThreadWatcher.LastUpdateKey] = None if self.__mainManager: self.__mainManager.systemDateTimeChanged() else: for threadId in self.__data: details = self.__getThreadEntry(threadId) timeoutHint = details[RMThreadWatcher.TimeoutHintKey] lastUpdate = details[RMThreadWatcher.LastUpdateKey] if timeoutHint is None: log.debug("Thread %d (%s) has no timeout hint!" % (threadId, details[RMThreadWatcher.NameHintKey])) elif lastUpdate is None: log.debug("Thread %d (%s) is not started yet!" % (threadId, details[RMThreadWatcher.NameHintKey])) elif timeoutHint < (timestamp - lastUpdate): everythingOk = False log.debug("Thread %d (%s) didn't responde since [%s] (timeoutHint=%d, timeout=%d)!" % \ (threadId, details[RMThreadWatcher.NameHintKey], rmTimestampToDateAsString(lastUpdate), timeoutHint, (timestamp - lastUpdate))) self.__lastCheckTimestamp = timestamp return everythingOk
def getStatus(self): return { "lastCheck": rmTimestampToDateAsString(self.lastCheck), "lastCheckTimestamp": self.lastCheck, "bootCompleted": self.__bootCompleted, "networkStatus": self.__networkStatus, "internetStatus": self.__internetStatus, "cloudStatus": self.__cloudStatus, "locationStatus": self.__locationStatus, "weatherStatus": self.__weatherStatus, "timeStatus": self.__timeStatus, "hasWifi": self.__hasWifi, "softwareVersion": globalSettings.softwareVersion, "wizardHasRun": globalSettings.wizardHasRun, "standaloneMode": globalSettings.standaloneMode, "wifiMode": globalWIFI.wifiInterface.mode, "cpuUsage": self.__cpuUsage, "memUsage": self.__memoryUsage, "uptime": self.__uptimeString, "uptimeSeconds": self.__uptimeSeconds, "gatewayAddress": self.__networkGateway }
def __repr__(self): return "(id=" + ` self.id ` + ", time=" + rmTimestampToDateAsString( self.timestamp) + ", processed=" + ` self.processed ` + ")"
def dump(self): if self.database.isOpen(): results = self.database.execute("SELECT timestamp, COUNT(parserID) FROM parserData GROUP BY timestamp;") for row in results: log.debug("(", rmTimestampToDateAsString(row[0]), ", ", row[1], ")") pass
def dump(self): if self.database.isOpen(): cursor = self.database.execute("SELECT * FROM mixerData ORDER BY timestamp DESC, forecastID DESC") for row in cursor: log.debug("fID=%d, fTs=%s, dTs=%s" % (row[0], rmTimestampToDateAsString(row[1]), rmTimestampToDateAsString(row[2])))
def __repr__(self): return "(id=" + `self.id` + ", time=" + rmTimestampToDateAsString(self.timestamp) + ", processed=" + `self.processed` + ")"
def getRecordsEx(self, minTimestamp, maxTimestamp, withManualPrograms=False): if (self.database.isOpen()): #if withManualPrograms: # sqlCondition = " AND usersch_id != 0 " # sqlConditionForced = " WHERE usersch_id != 0" #else: # sqlCondition = "" # sqlConditionForced = "" if minTimestamp and maxTimestamp: records = self.database.execute("SELECT tokenTimestamp, SUM(real_sec) realDuration, SUM(user_sec) userDuration, usersch_id FROM %s "\ "WHERE ?<=tokenTimestamp AND tokenTimestamp<? GROUP BY tokenTimestamp "\ "ORDER BY tokenTimestamp" % self._tableName, (minTimestamp, maxTimestamp) ) elif minTimestamp: records = self.database.execute("SELECT tokenTimestamp, SUM(real_sec) realDuration, SUM(user_sec) userDuration, usersch_id FROM %s "\ "WHERE ?<=tokenTimestamp GROUP BY tokenTimestamp "\ "ORDER BY tokenTimestamp" % self._tableName, (minTimestamp, ) ) elif maxTimestamp: records = self.database.execute("SELECT tokenTimestamp, SUM(real_sec), SUM(user_sec) userDuration duration, usersch_id FROM %s "\ "WHERE tokenTimestamp<? GROUP BY tokenTimestamp "\ "ORDER BY tokenTimestamp" % self._tableName, (maxTimestamp, ) ) else: records = self.database.execute("SELECT tokenTimestamp, SUM(real_sec) realDuration, SUM(user_sec) userDuration, usersch_id FROM %s "\ "GROUP BY tokenTimestamp ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName ) tempResults = OrderedDict() for row in records: if not withManualPrograms and int(row[3]) == 0: continue dayTimestamp = rmGetStartOfDay(int(row[0])) totalDurations = tempResults.get( dayTimestamp, None) # List with real and user durations if totalDurations is None: totalDurations = [int(row[1]), int(row[2])] else: totalDurations[0] += int(row[1]) totalDurations[1] += int(row[2]) tempResults[dayTimestamp] = totalDurations results = {"days": []} for dayTimestamp in tempResults: date = rmTimestampToDateAsString(dayTimestamp, "%Y-%m-%d") day = { "dayTimestamp": dayTimestamp, "date": date, "realDuration": tempResults.get(dayTimestamp)[0], "userDuration": tempResults.get(dayTimestamp)[1] } results["days"].append(day) return results return None
def getRecords(self, minTimestamp, maxTimestamp): if (self.database.isOpen()): if minTimestamp and maxTimestamp: records = self.database.execute("SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s "\ "WHERE ?<=tokenTimestamp AND tokenTimestamp<? "\ "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName, (minTimestamp, maxTimestamp) ) elif minTimestamp: records = self.database.execute("SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s "\ "WHERE ?<=tokenTimestamp "\ "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName, (minTimestamp, ) ) elif maxTimestamp: records = self.database.execute("SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s "\ "WHERE tokenTimestamp<? "\ "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName, (maxTimestamp, ) ) else: records = self.database.execute("SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s "\ "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName ) tempResults = OrderedDict() for row in records: token = row[7] dayTimestamp = rmGetStartOfDay(int(row[8])) dayGroup = tempResults.get(dayTimestamp, None) if dayGroup is None: dayGroup = OrderedDict() tempResults[dayTimestamp] = dayGroup programGroup = dayGroup.get(token, None) if programGroup is None: programGroup = OrderedDict() dayGroup[token] = programGroup zones = programGroup.get(row[1], None) if zones is None: zones = OrderedDict() programGroup[row[1]] = zones zone = zones.get(row[2], None) if zone is None: zone = OrderedDict() zone["uid"] = row[2] zone["flag"] = row[6] zone["cycles"] = [] zones[row[2]] = zone cycles = zone["cycles"] info = OrderedDict() info["id"] = len(cycles) + 1 info["startTime"] = rmTimestampToDateAsString(row[0]) info["startTimestamp"] = row[0] info["userDuration"] = row[3] info["machineDuration"] = row[4] info["realDuration"] = row[5] cycles.append(info) results = {"days": []} for dayTimestamp in tempResults: programs = [] day = { "date": rmTimestampToDateAsString(dayTimestamp, "%Y-%m-%d"), "dateTimestamp": dayTimestamp, "programs": programs } results["days"].append(day) dayGroup = tempResults[dayTimestamp] for token in dayGroup: tempPrograms = dayGroup[token] for programId in tempPrograms: zones = [] program = OrderedDict() program["id"] = programId program["zones"] = zones programs.append(program) tempZones = tempPrograms[programId] for zoneId in tempZones: zones.append(tempZones[zoneId]) return results return None
def getRecordsEx(self, minTimestamp, maxTimestamp, withManualPrograms=False): if self.database.isOpen(): # if withManualPrograms: # sqlCondition = " AND usersch_id != 0 " # sqlConditionForced = " WHERE usersch_id != 0" # else: # sqlCondition = "" # sqlConditionForced = "" if minTimestamp and maxTimestamp: records = self.database.execute( "SELECT tokenTimestamp, SUM(real_sec) realDuration, SUM(user_sec) userDuration, usersch_id FROM %s " "WHERE ?<=tokenTimestamp AND tokenTimestamp<? GROUP BY tokenTimestamp " "ORDER BY tokenTimestamp" % self._tableName, (minTimestamp, maxTimestamp), ) elif minTimestamp: records = self.database.execute( "SELECT tokenTimestamp, SUM(real_sec) realDuration, SUM(user_sec) userDuration, usersch_id FROM %s " "WHERE ?<=tokenTimestamp GROUP BY tokenTimestamp " "ORDER BY tokenTimestamp" % self._tableName, (minTimestamp,), ) elif maxTimestamp: records = self.database.execute( "SELECT tokenTimestamp, SUM(real_sec), SUM(user_sec) userDuration duration, usersch_id FROM %s " "WHERE tokenTimestamp<? GROUP BY tokenTimestamp " "ORDER BY tokenTimestamp" % self._tableName, (maxTimestamp,), ) else: records = self.database.execute( "SELECT tokenTimestamp, SUM(real_sec) realDuration, SUM(user_sec) userDuration, usersch_id FROM %s " "GROUP BY tokenTimestamp ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName ) tempResults = OrderedDict() for row in records: if not withManualPrograms and int(row[3]) == 0: continue dayTimestamp = rmGetStartOfDay(int(row[0])) totalDurations = tempResults.get(dayTimestamp, None) # List with real and user durations if totalDurations is None: totalDurations = [int(row[1]), int(row[2])] else: totalDurations[0] += int(row[1]) totalDurations[1] += int(row[2]) tempResults[dayTimestamp] = totalDurations results = {"days": []} for dayTimestamp in tempResults: date = rmTimestampToDateAsString(dayTimestamp, "%Y-%m-%d") day = { "dayTimestamp": dayTimestamp, "date": date, "realDuration": tempResults.get(dayTimestamp)[0], "userDuration": tempResults.get(dayTimestamp)[1], } results["days"].append(day) return results return None
def getRecords(self, minTimestamp, maxTimestamp): if self.database.isOpen(): if minTimestamp and maxTimestamp: records = self.database.execute( "SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s " "WHERE ?<=tokenTimestamp AND tokenTimestamp<? " "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName, (minTimestamp, maxTimestamp), ) elif minTimestamp: records = self.database.execute( "SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s " "WHERE ?<=tokenTimestamp " "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName, (minTimestamp,), ) elif maxTimestamp: records = self.database.execute( "SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s " "WHERE tokenTimestamp<? " "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName, (maxTimestamp,), ) else: records = self.database.execute( "SELECT ts_started, usersch_id, zid, user_sec, machine_sec, real_sec, flag, token, tokenTimestamp FROM %s " "ORDER BY tokenTimestamp, usersch_id, zid" % self._tableName ) tempResults = OrderedDict() for row in records: token = row[7] dayTimestamp = rmGetStartOfDay(int(row[8])) dayGroup = tempResults.get(dayTimestamp, None) if dayGroup is None: dayGroup = OrderedDict() tempResults[dayTimestamp] = dayGroup programGroup = dayGroup.get(token, None) if programGroup is None: programGroup = OrderedDict() dayGroup[token] = programGroup zones = programGroup.get(row[1], None) if zones is None: zones = OrderedDict() programGroup[row[1]] = zones zone = zones.get(row[2], None) if zone is None: zone = OrderedDict() zone["uid"] = row[2] zone["flag"] = row[6] zone["cycles"] = [] zones[row[2]] = zone cycles = zone["cycles"] info = OrderedDict() info["id"] = len(cycles) + 1 info["startTime"] = rmTimestampToDateAsString(row[0]) info["startTimestamp"] = row[0] info["userDuration"] = row[3] info["machineDuration"] = row[4] info["realDuration"] = row[5] cycles.append(info) results = {"days": []} for dayTimestamp in tempResults: programs = [] day = { "date": rmTimestampToDateAsString(dayTimestamp, "%Y-%m-%d"), "dateTimestamp": dayTimestamp, "programs": programs, } results["days"].append(day) dayGroup = tempResults[dayTimestamp] for token in dayGroup: tempPrograms = dayGroup[token] for programId in tempPrograms: zones = [] program = OrderedDict() program["id"] = programId program["zones"] = zones programs.append(program) tempZones = tempPrograms[programId] for zoneId in tempZones: zones.append(tempZones[zoneId]) return results return None