import logging

from system import *
from statistics import *

class Simulation:

    logger = logging.getLogger("sim")

    def __init__(self, mission_time, iterations, raid_type, raid_num, disk_capacity, 
            disk_fail_parms, disk_repair_parms, disk_lse_parms, disk_scrubbing_parms):
        self.mission_time = mission_time
        self.iterations = iterations
        self.raid_type = raid_type
        self.raid_num = raid_num
        self.disk_capacity = disk_capacity
        self.disk_fail_parms = disk_fail_parms
        self.disk_repair_parms = disk_repair_parms
        self.disk_lse_parms = disk_lse_parms
        self.disk_scrubbing_parms = disk_scrubbing_parms

        self.logger.debug("Simulation: iterations = %d" % iterations)

        self.system = None

    def simulate(self):
        sample_list = []

        for i in range(self.iterations):
            self.system = System(self.mission_time, self.raid_type, self.raid_num, self.disk_capacity, self.disk_fail_parms,
                self.disk_repair_parms, self.disk_lse_parms, self.disk_scrubbing_parms)

            result = self.system.run()

            self.logger.debug("Simulation result: %s" % result[0])
            if result[0] == System.EVENT_NOTHING_LOST:
                sample_list.append(0)
            elif result[0] == System.EVENT_RAID_FAILURE:
                sample_list.append(result[1])
            elif result[0] == System.EVENT_SECTORS_LOST:
                sample_list.append(sum(result[1:]))
            else:
                sys.exit(2)
        
        prob_result = None
        byte_result = None

        print sample_list
        samples = Samples(sample_list)
        byte_result = samples.getResults()

        for i in range(len(sample_list)):
            if sample_list[i] > 0:
                sample_list[i] = 1

        print sample_list
        samples = Samples(sample_list)
        prob_result = samples.getResults()

        # finished, return results
        # the format of result:
        # (mean, re, low_ci, high_ci)
        return (prob_result, byte_result) 

@@ -1125,75 +1125,8 @@
 ()%0A%0A
-            self.logger.debug(%22Simulation result: %25s%22 %25 result%5B0%5D)%0A
     
@@ -1292,93 +1292,300 @@
    s
-ample_list.append(result%5B1%5D)%0A            elif result%5B0%5D == System.EVENT_SECTORS_LOST:
+elf.logger.warning(%22%25dth iteration: %25s, %25d bytes lost%22 %25 (i, result%5B0%5D, result%5B1%5D))%0A                sample_list.append(result%5B1%5D)%0A            elif result%5B0%5D == System.EVENT_SECTORS_LOST:%0A                self.logger.debug(%22%25dth iterations: %25s, %25d bytes lost%22 %25 (i, result%5B0%5D, sum(result%5B1:%5D)))
 %0A   
@@ -1747,34 +1747,8 @@
 ne%0A%0A
-        print sample_list%0A
     
@@ -1943,34 +1943,8 @@
  1%0A%0A
-        print sample_list%0A
     

print raid failure