Fraction of tags with property

I’ve got X thousand tags with lets say a boolean property. I want to estimate the fraction of those tags with that property.

Obviously there’s the naive option to stack readblocking calls in a script and count that way, but is there some smarter way to do it?

(EDIT: yes, there’s also the option of reading like 200 randomized tags assuming noncorrelated failures. still an ugly stack though)

Have you tried system.tag.query()? It is astonishingly fast when you define a HAS property filter.

1 Like

is there a system.tag.query.count? or should I just listify and grab len?

This should work now.

folderPath = "[default]" 

browseFilter = {
    'dataType': 'Boolean',
    'recursive': True
}

browseResults = system.tag.browse(path=folderPath, filter=browseFilter)

tagPaths = [
    str(result['fullPath']) 
    for result in browseResults 
    if isinstance(result, dict) and 'fullPath' in result and result.get('tagType') != 'Folder'
]

if tagPaths:
    qualifiedValues = system.tag.readBlocking(tagPaths)
    trueCount = sum(1 for qv in qualifiedValues if qv.value == True and qv.quality.isGood())

print "Found " + str(trueCount) + " TRUE boolean tags in " + folderPath

This is the alternative to use system.tag.query. You could stop and just do do len(pyResults) but then you’d also get tags with bad quality which depends on if you want to include those.

folderPath = "[default]" 
searchPath = folderPath + "/**" 

query_obj = {
    "condition": {
        "properties":{
            "op": "And",
            "conditions": [
                {
                    "prop": "dataType",
                    "comp": "Like",
                    "value": "Boolean"
                },
                {
                    "prop": "value",
                    "comp": "Like",
                    "value": "True"
                },
            ]
        }
    },
    "returnProperties": [
        "value", 
        "quality"
    ]
}
results = system.tag.query(query=query_obj)
pyResults = results.getResults()
trueCount = 0
for tag in pyResults:
    if 'value' in tag and 'quality' in tag:
        isValueTrue = (tag['value'].getValue() == True)
        isQualityGood = tag['quality'].isGood()
        if isValueTrue and isQualityGood:
            trueCount += 1
print "Found " + str(trueCount) + " TRUE boolean tags in " + folderPath
1 Like

The PyWrapper around the Results object returned by system.tag.query has some additional methods:

e.x.
getTotalAvailableSize()

That will probably be faster than a conversion to a list.