#!/usr/bin/env python

import MySQLdb
import sys

if __name__ == '__main__':
    nodes = []
    nodesFile = open('/home/panstarrs/schastel/local/share/ippMhpccStorageNodes')
    for line in nodesFile:
        nodes.append(line[:-1])

    filesMonitoring = MySQLdb.connect( 'ippc11.ipp.ifa.hawaii.edu',
                                       'ippuser', 'ipp',
                                       'FilesMonitoring' )
    letters = '0123456789abcdef'
    template_query = 'SELECT MAX(LENGTH(filename)), MAX(LENGTH(nebkey)) FROM Files_%s_%s%s'
    max_filename_length = 0
    max_filename_reacehd_for = ''
    max_nebkey_length = 0
    max_nebkey_reached_for = ''
    for node in nodes:
        sys.stderr.write('%s\n' % node)
        node_max_filename_length = 0
        node_max_filename_reacehd_for = ''
        node_max_nebkey_length = 0
        node_max_nebkey_reached_for = ''
        for letter1 in letters:
            for letter2 in letters:
                cursor = filesMonitoring.cursor()
                cursor.execute(template_query % (node, letter1, letter2))
                rows = cursor.fetchall()
                cursor.close()
                for row in rows:
                    if node_max_filename_length < row[0]:
                        node_max_filename_length = row[0]
                        node_max_filename_reached_for = 'Files_%s_%s%s' % (node, letter1, letter2)
                    if node_max_nebkey_length < row[1]:
                        node_max_nebkey_length = row[1]
                        node_max_nebkey_reached_for = 'Files_%s_%s%s' % (node, letter1, letter2)
                    if max_filename_length < row[0]:
                        max_filename_length = row[0]
                        max_filename_reached_for = 'Files_%s_%s%s' % (node, letter1, letter2)
                    if max_nebkey_length < row[1]:
                        max_nebkey_length = row[1]
                        max_nebkey_reached_for = 'Files_%s_%s%s' % (node, letter1, letter2)
        sys.stderr.write('  Node filename length: %d (%s)\n' % (node_max_filename_length, node_max_filename_reached_for))
        sys.stderr.write('  Node nebkey length: %d (%s)\n' % (node_max_nebkey_length, node_max_nebkey_reached_for))
        sys.stderr.write('  Until now:\n')
        sys.stderr.write('  Max filename length: %d (%s)\n' % (max_filename_length, max_filename_reached_for))
        sys.stderr.write('  Max nebkey length: %d (%s)\n' % (max_nebkey_length, max_nebkey_reached_for))
    print
    print 'Max filename length: %d (%s)' % (max_filename_length, max_filename_reached_for)
    print 'Max nebkey length: %d (%s)' % (max_nebkey_length, max_nebkey_reached_for)
    print
    print 'SELECT filename FROM %s WHERE LENGTH(filename) = %d;' % (max_filename_reached_for, max_filename_length)
    print 'SELECT nebkey FROM %s WHERE LENGTH(nebkey) = %d;' % (max_nebkey_reached_for, max_nebkey_length)
