# Copyright (C) 2010 Peter Teichman

import logging
import os
import sys
import time

from .stats import Histogram, Statistics

log = logging.getLogger("instatrace")

class HistogramsCommand:
    @classmethod
    def add_subparser(cls, parser):
        subparser = parser.add_parser("histograms", help="Stat histograms")
        subparser.add_argument("file", nargs="+")
        subparser.set_defaults(run=cls.run)

    @staticmethod
    def run(args):
        stats = Statistics()

        for filename in args.file:
            count = 0
            fd = open(filename)
            for line in fd.xreadlines():
                line = line.strip()

                stat = line.split(" ", 2)
                stats.add_sample(stat[0], int(stat[1]))

            fd.close()

        names = stats.statistics.keys()
        names.sort()

        for name in names:
            histogram = stats.statistics.get(name)
            histogram.text(sys.stdout)

@@ -350,267 +350,700 @@
 nt(%22
-file%22, nargs=%22+%22)%0A        subparser.set_defaults(run=cls.run)%0A%0A    @staticmethod%0A    def run(args):%0A        stats = Statistics()%0A%0A        for filename in args.file:%0A            count = 0%0A            fd = open(filename)%0A            for line in fd.xreadlines():
+--filter%22, action=%22store_true%22,%0A                               help=%22Filter out any lines that don't contain INSTATRACE%22)%0A        subparser.add_argument(%22file%22, nargs=%22+%22)%0A        subparser.set_defaults(run=cls.run,%0A                               filter_marker=%22INSTATRACE: %22)%0A%0A    @staticmethod%0A    def run(args):%0A        stats = Statistics()%0A%0A        for filename in args.file:%0A            count = 0%0A            fd = open(filename)%0A            for line in fd.xreadlines():%0A                if args.filter:%0A                    pos = line.find(args.filter_marker)%0A                    if pos == -1:%0A                        continue%0A                    line = line%5Bpos+len(args.filter_marker):%5D%0A
 %0A   

Add a --filter flag to histograms