#!/usr/bin/env python
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, "core"))

import re
import csv
import numpy as np

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmudbac.settings")
import django
django.setup()

from library.models import *

NUM_BINS = 10

def query_stats():
    stats = {}

    for repo in Repository.objects.filter(latest_attempt__result = 'OK'):
        actions = Action.objects.filter(attempt = repo.latest_attempt)
        if len(actions) == 0:
            continue
        
        for action in actions:
            counters = Counter.objects.filter(action = action)
            for counter in counters:
                stats[counter.description] = stats.get(counter.description, 0) + counter.count
                stats['TOTAL'] = stats.get('TOTAL', 0) + counter.count

    print stats

def main():
    query_stats()

if __name__ == '__main__':
    main()

@@ -922,25 +922,1266 @@
 def 
-main():%0A    query
+table_coverage_stats(directory = '.'):%0A    stats = %5B%5D%0A%0A    for repo in Repository.objects.filter(latest_attempt__result = 'OK'):%0A        actions = Action.objects.filter(attempt = repo.latest_attempt)%0A        if len(actions) == 0:%0A            continue%0A%0A        statistics = Statistic.objects.filter(attempt = repo.latest_attempt)%0A        if len(statistics) == 0:%0A            continue%0A        statistic = statistics.get(description = 'num_tables')%0A        %0A        covered_tables = set()%0A        for action in actions:%0A            for query in Query.objects.filter(action = action):%0A                for table in re.findall('FROM%5Cs*%5CS+', query.content.upper()):%0A                    table_name = table.replace('FROM', '').replace(%22'%22, %22%22).replace(' ', '')%0A                    covered_tables.add(table_name)%0A               %0A        percentage = int(float(len(covered_tables) * 100) / statistic.count / 10)%0A        stats.append(percentage)%0A%0A    hist, bin_edges = np.histogram(stats, NUM_BINS)%0A        %0A    with open(os.path.join(directory, 'table_coverage.csv'), 'wb') as csv_file:%0A        writer = csv.writer(csv_file)%0A        for i in xrange(NUM_BINS):%0A            writer.writerow(%5Bint(bin_edges%5Bi%5D), hist%5Bi%5D%5D)%0A%0Adef main():%0A    # query_stats()%0A    table_coverage
 _sta

add analysis : table coverage stats