How big is my OpenHab Setup

Tags: #<Tag:0x00007f616f77dcb0>

that is true, but now I have proof, haha

1 Like

I have modified the code a little bit :slight_smile:
Fix all the pylint warnings and added the ability to count the JSR rules also. Which are provided by the API.

import sys
import glob
import re
import logging

import requests

# Modify these variables to fit your system
URL_OPENHAB = "http://localhost:8080"
THING_ITEM_NAME = "openHAB_AllThings"
ITEM_ITEM_NAME = "openHAB_AllItems"
RULE_ITEM_NAME = "openHAB_AllRules"
RULE_PATH = "/etc/openhab2/rules/*.rules"
PATH_FOR_LOG = "/var/log/openhab2/rules_count.log"

logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger('rules_count')

#LOGGER.setLevel(logging.DEBUG)
FH = logging.FileHandler(PATH_FOR_LOG)
FH.setLevel(logging.DEBUG)
FORMATTER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
FH.setFormatter(FORMATTER)
LOGGER.addHandler(FH)

try:
    ITEMS = len(requests.get(url=URL_OPENHAB + "/rest/items?recursive=false").json())
    LOGGER.info("found %s Items", str(ITEMS))
    requests.post(url=URL_OPENHAB + "/rest/items/" + ITEM_ITEM_NAME, data=str(ITEMS), verify=False)
except: # catch *all* exceptions
    LOGGER.error(sys.exc_info())

try:
    THINGS = len(requests.get(url=URL_OPENHAB + "/rest/things").json())
    LOGGER.info("found %s Things", str(THINGS))
    requests.post(url=URL_OPENHAB + "/rest/items/" + THING_ITEM_NAME, data=str(THINGS), verify=False)
except: # catch *all* exceptions
    LOGGER.error(sys.exc_info())

try:
    JSR_RULES_COUNT = len(requests.get(url=URL_OPENHAB + "/rest/rules").json())
    LOGGER.info("found %s JSR Rules", str(JSR_RULES_COUNT))
except:
    LOGGER.error(sys.exc_info())

try:
    RULES = 0
    WHENS = 0
    THENS = 0
    ENDS = 0
    COUNTER = []
    for fileName in glob.glob(RULE_PATH):
        with open(fileName, 'r') as rule_file:

            fileContent = rule_file.read()
            fileContent = re.sub(re.compile(r"/\*.*?\*/", re.DOTALL), "", fileContent)
            fileContent = re.sub(re.compile(r"//.*?\n"), "", fileContent)

            rules_in_this_file = len(re.findall(r'\brule\b', fileContent, re.IGNORECASE))
            RULES += rules_in_this_file
            LOGGER.debug("found " + str(rules_in_this_file) + " rules in file" + fileName)

            whens_in_this_file = len(re.findall(r'\bwhen\b', fileContent, re.IGNORECASE))
            WHENS += whens_in_this_file
            LOGGER.debug("found " + str(whens_in_this_file) + " whens in file" + fileName)

            thens_in_this_file = len(re.findall(r'\bthen\b', fileContent, re.IGNORECASE))
            THENS += thens_in_this_file
            LOGGER.debug("found " + str(thens_in_this_file) + " thens in file" + fileName)

            ends_in_this_file = len(re.findall(r'\bend\b', fileContent, re.IGNORECASE))
            ENDS += ends_in_this_file
            LOGGER.debug("found " + str(ends_in_this_file) + " ends in file" + fileName)

    COUNTER.append(RULES)
    COUNTER.append(WHENS)
    COUNTER.append(THENS)
    COUNTER.append(ENDS)
    RULES_COUNT = min(COUNTER)

    LOGGER.info("counting %s rules", str(RULES_COUNT))

    RULES_COUNT += JSR_RULES_COUNT
    requests.post(url=URL_OPENHAB + "/rest/items/" + RULE_ITEM_NAME, data=str(RULES_COUNT), verify=False)
except: # catch *all* exceptions
    LOGGER.error(sys.exc_info())

2 Likes