Ejemplo n.º 1
0
    def update_main_gui(self):
        if self.gtkb == None:
            print("MTimeSlot.update_main_gui() - Gui not builded.\n")
            return

        self.gui_main_slots_used.set_text(str(self.slotsUsed))
        self.gui_main_slots_left.set_text(str(self.totalSlots - self.slotsUsed))
        self.gui_main_slots_total.set_text(str(self.totalSlots))

        self.gui_main_thisSlot_timeUsed.set_text(ntmtools.format_time(self.tsTime))
        secLeft = self.slotLength - self.tsTime
        self.gui_main_thisSlot_timeLeft.set_text(ntmtools.format_time(secLeft))
        self.gui_main_thisSlot_timeTotal.set_text(ntmtools.format_time(self.slotLength))

        totalSec = self.totalSlots * self.slotLength
        per = 100.0 * (self.slotsUsed * self.slotLength - secLeft) / totalSec

        last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days)

        if self.now != None:
            period_len_total = ntmtools.timedelta2sec(ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day))
            period_len_used = ntmtools.timedelta2sec(self.now - ntmtools.date_to_datetime_start(self.first_day))
            if period_len_used != 0:
                estimate = self.slotsUsed * period_len_total / period_len_used
                estimate_str = '{0}sl.'.format(int(round(estimate)))
            else: estimate_str = "--"
        else: estimate_str = "--"

        text = "<b>" + _("Time Slot") + "</b>"
        text += " <small>({0:.1f}%) [{1}]</small>".format(per, estimate_str)
        self.gui_timeslot_topLabel.set_markup(text)

        self.gui_main_period.set_markup(
            "<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day)
        )
Ejemplo n.º 2
0
    def update_main_gui(self):
        if self.gtkb == None:
            print("MMTime.update_main_gui(): Gui not builded.\n")
            return

        total_used_sec = self.used_sec + self.this_slot_sec
        (h, m, s) = ntmtools.sec_to_hms(total_used_sec)
        out_str = """<span color='#900000'>{0}h{1}'<small>{2}"</small></span>""".format(
            h, m, s)
        self.gui_main_used_sec.set_markup(out_str)

        time_diff = self.total_sec - total_used_sec
        if (time_diff < 0):
            time_diff = -time_diff
            sign = "-"
        else:
            sign = ""
        (h, m, s) = ntmtools.sec_to_hms(time_diff)
        out_str = """<span color='#009000'>{0}{1}h{2}'<small>{3}"</small></span>""".format(
            sign, h, m, s)
        self.gui_main_left_sec.set_markup(out_str)

        (h, m, s) = ntmtools.sec_to_hms(self.total_sec)
        out_str = """{0}h{1}'<small>{2}"</small>""".format(h, m, s)
        self.gui_main_total_sec.set_markup(out_str)

        total_used_sec = self.used_sec + self.this_slot_sec
        if self.total_sec == 0:
            print _("Warning! mtime.py: total sec is 0.")
            per = 100.0
        else:
            per = 100.0 * total_used_sec / self.total_sec

        last_day = ntmtools.get_last_day(self.first_day, self.period_length,
                                         self.custom_days)

        if self.now != None:
            period_len_total = ntmtools.timedelta2sec(
                ntmtools.date_to_datetime_end(last_day) -
                ntmtools.date_to_datetime_start(self.first_day))
            period_len_used = ntmtools.timedelta2sec(
                self.now - ntmtools.date_to_datetime_start(self.first_day))
            if period_len_used != 0:
                estimate = total_used_sec * period_len_total / period_len_used
                (h, m, s) = ntmtools.sec_to_hms(estimate)
                estimate_str = '''{0}h{1}'{2}"'''.format(h, m, s)
            else:
                estimate_str = "--"
        else:
            estimate_str = "--"

        text = "<b>" + _("Time") + "</b>"
        text += """ <small>({0:.1f}%) [{1}]</small>""".format(
            per, estimate_str)
        self.gui_time_topLabel.set_markup(text)
        #self.gui_time_topLabel.set_markup("""<small>({0:.1f}%) [{1}]</small>""".format(per, estimate_str))

        self.gui_main_period.set_markup(
            "<small><small>" + _("Period") +
            ": {0} - {1}</small></small>".format(self.first_day, last_day))
Ejemplo n.º 3
0
    def update_main_gui(self):
        if self.gtkb == None:
            print("MMTime.update_main_gui(): Gui not builded.\n")
            return

        total_used_sec = self.used_sec + self.this_slot_sec
        (h, m, s) = ntmtools.sec_to_hms(total_used_sec)
        out_str = """<span color='#900000'>{0}h{1}'<small>{2}"</small></span>""".format(h, m, s)
        self.gui_main_used_sec.set_markup(out_str)

        time_diff = self.total_sec - total_used_sec
        if (time_diff < 0):
            time_diff = -time_diff            
            sign = "-"
        else:
            sign = ""
        (h, m, s) = ntmtools.sec_to_hms(time_diff)
        out_str = """<span color='#009000'>{0}{1}h{2}'<small>{3}"</small></span>""".format(sign, h, m, s)
        self.gui_main_left_sec.set_markup(out_str)

        (h, m, s) = ntmtools.sec_to_hms(self.total_sec)
        out_str = """{0}h{1}'<small>{2}"</small>""".format(h, m, s)
        self.gui_main_total_sec.set_markup(out_str)

        total_used_sec = self.used_sec + self.this_slot_sec
        if self.total_sec == 0:
            print _("Warning! mtime.py: total sec is 0.")
            per = 100.0
        else: per = 100.0 * total_used_sec / self.total_sec

        last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days)

        if self.now != None:
            period_len_total = ntmtools.timedelta2sec(ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day))
            period_len_used = ntmtools.timedelta2sec(self.now - ntmtools.date_to_datetime_start(self.first_day))
            if period_len_used != 0:
                estimate = total_used_sec * period_len_total / period_len_used
                (h, m, s) = ntmtools.sec_to_hms(estimate)
                estimate_str = '''{0}h{1}'{2}"'''.format(h, m, s)
            else: estimate_str = "--"
        else: estimate_str = "--"

        text = "<b>" + _("Time") + "</b>"
        text += """ <small>({0:.1f}%) [{1}]</small>""".format(per, estimate_str)
        self.gui_time_topLabel.set_markup(text)
        #self.gui_time_topLabel.set_markup("""<small>({0:.1f}%) [{1}]</small>""".format(per, estimate_str))
        
        self.gui_main_period.set_markup("<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
Ejemplo n.º 4
0
Archivo: mtime.py Proyecto: lgtl/ntm
    def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state):
        if (not self.active): return

        self.now = timestamp
        if not self.update_period(session_start): return

        if (self.last_state == 0):
            if (conn_state == 1): # offline -> online
                last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days)
                self.used_sec = MTime.loadTimeUsed(self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day))
                self.connStartTime = timestamp
                self.this_slot_sec = 0
        else: # last_state is online
            self.this_slot_sec = ntmtools.timedelta2sec(self.now - self.connStartTime)
            if (conn_state == 0): # online -> offline
                pass

        self.last_state = conn_state
        self.update_main_gui()

        if self.logUpdate:
            print('Time: {0} of {1}\n'.
                format( self.used_sec + self.this_slot_sec, self.total_sec)
            )

        self.check_limit()
Ejemplo n.º 5
0
    def update_h(self, timestamp, session_start, update_interval,
                 last_rec_traffic, last_tra_traffic, conn_state):
        ntmtools.dbg_msg("MTime.update_h - conn_state:{0}".format(conn_state))

        if (not self.active): return

        self.now = timestamp
        if not self.update_period(session_start): return

        if (self.last_state == 0):
            if (conn_state == 1):  # offline -> online
                self.dialog_showed = False
                last_day = ntmtools.get_last_day(self.first_day,
                                                 self.period_length,
                                                 self.custom_days)
                self.used_sec = MTime.load_time_used(
                    self.ntm.db_conn,
                    ntmtools.date_to_datetime_start(self.first_day),
                    ntmtools.date_to_datetime_end(last_day))
                self.connStartTime = timestamp
                self.this_slot_sec = 0
        else:  # last_state is online
            self.this_slot_sec = ntmtools.timedelta2sec(self.now -
                                                        self.connStartTime)
            if (conn_state == 0):  # online -> offline
                pass

        self.last_state = conn_state
        self.update_main_gui()

        if self.logUpdate:
            print('Time: {0} of {1}\n'.format(
                self.used_sec + self.this_slot_sec, self.total_sec))

        self.check_limit()
Ejemplo n.º 6
0
    def get_report_from_db(self, first_day, last_day, all_days=False):
        ntmtools.dbg_msg("NTM.get_report_from_db")

        ctra = self.db_conn.cursor()

        if all_days:
            ctra.execute("select * from dailytraffic")
        else:
            last_day_t = last_day + datetime.timedelta(1)
            ctra.execute("select * from dailytraffic where (date>=?) AND (date<?)", (first_day, last_day_t))

        t_count = 0
        t_in = 0
        t_out = 0
        tot_min = tot_max = 0
        tra_list = []
        for r in ctra:
            t_in += r[1]
            t_out += r[2]
            tot = r[1] + r[2]
            if (t_count == 0):
                tot_min = tot
                tot_max = tot
            else:
                if tot > tot_max: tot_max = tot
                elif tot < tot_min: tot_min = tot
            t_count += 1
            tra_list += [r]

        cses = self.db_conn.cursor()

        if all_days:
            cses.execute("select * from session")
        else:
            cses.execute("select * from session where (start>=?) AND (start<?)", (first_day, last_day_t))

        s_count = 0
        tot_time = 0
        s_max = s_min = 0
        ses_list = []
        for r in cses:
            tsStart = datetime.datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S")
            tsEnd = datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S")
            diff = ntmtools.timedelta2sec(tsEnd - tsStart)
            tot_time += diff
            if (s_count == 0):
                s_min = diff
                s_max = diff
            else:
                if diff > s_max: s_max = diff
                elif diff < s_min: s_min = diff
            s_count += 1
            ses_list += [r]

        ntmtools.dbg_msg("END - NTM.get_report_from_db")

        return (tra_list, ses_list, t_count, t_in, t_out, tot_max, tot_min, s_count, tot_time, s_max, s_min)
Ejemplo n.º 7
0
    def update_main_gui(self):
        if self.gtkb == None:
            print("MTimeSlot.update_main_gui() - Gui not builded.\n")
            return

        self.gui_main_slots_used.set_text(str(self.slotsUsed))
        self.gui_main_slots_left.set_text(str(self.totalSlots -
                                              self.slotsUsed))
        self.gui_main_slots_total.set_text(str(self.totalSlots))

        self.gui_main_thisSlot_timeUsed.set_text(
            ntmtools.format_time(self.tsTime))
        secLeft = self.slotLength - self.tsTime
        self.gui_main_thisSlot_timeLeft.set_text(ntmtools.format_time(secLeft))
        self.gui_main_thisSlot_timeTotal.set_text(
            ntmtools.format_time(self.slotLength))

        totalSec = self.totalSlots * self.slotLength
        per = 100.0 * (self.slotsUsed * self.slotLength - secLeft) / totalSec

        last_day = ntmtools.get_last_day(self.first_day, self.period_length,
                                         self.custom_days)

        if self.now != None:
            period_len_total = ntmtools.timedelta2sec(
                ntmtools.date_to_datetime_end(last_day) -
                ntmtools.date_to_datetime_start(self.first_day))
            period_len_used = ntmtools.timedelta2sec(
                self.now - ntmtools.date_to_datetime_start(self.first_day))
            if period_len_used != 0:
                estimate = self.slotsUsed * period_len_total / period_len_used
                estimate_str = '{0}sl.'.format(int(round(estimate)))
            else:
                estimate_str = "--"
        else:
            estimate_str = "--"

        text = "<b>" + _("Time Slot") + "</b>"
        text += " <small>({0:.1f}%) [{1}]</small>".format(per, estimate_str)
        self.gui_timeslot_topLabel.set_markup(text)

        self.gui_main_period.set_markup(
            "<small><small>" + _("Period") +
            ": {0} - {1}</small></small>".format(self.first_day, last_day))
Ejemplo n.º 8
0
 def request_update_db(self, force_update=False):
     #print("request_update_db(force_update={0})".format(force_update))
     self.d_rb_db += self.d_rb
     self.d_tb_db += self.d_tb
     datetime_now = datetime.datetime.now()
     diff_last_update_db = ntmtools.timedelta2sec(datetime_now - self.last_db_update)
     if force_update or (diff_last_update_db >= self.db_update_interval):
         self.last_db_update = datetime_now
         diff = self.d_rb_db + self.d_tb_db
         if diff != 0:
             self.update_db_daily_traffic(self.session_start, self.d_rb_db, self.d_tb_db)
             self.d_rb_db = self.d_tb_db = 0
         self.update_db_session(True)
Ejemplo n.º 9
0
    def update_h(self, timestamp, session_start, update_interval,
                 last_rec_traffic, last_tra_traffic, conn_state):
        if (not self.active): return

        self.now = timestamp
        if not self.update_period(session_start): return

        if (self.last_state == 0):
            if (conn_state == 1):
                self.dialog_showed = False
                self.slotsUsed += 1
                self.tsTime = 0
                self.connStartTime = timestamp
                self.tsStartTime = self.connStartTime
        self.last_state = conn_state

        if self.tsStartTime != None:
            diff = ntmtools.timedelta2sec(self.now - self.tsStartTime)
        else:
            diff = 0
        new_slots = 0
        while diff >= self.slotLength:
            self.slotsUsed += 1
            diff -= self.slotLength
            new_slots += 1

        if (self.alert_active) & (not self.alert_done) & (
            (self.slotLength - diff) <= self.alert_predate):
            self.alarm_player.play(loop=False)
            self.alert_done = True
            if self.notify_ok:
                self.notifyalert_predate.show()

        if new_slots > 0:
            self.alert_done = False
            if self.notify_ok:
                self.notify_new_slot.show()

        self.tsStartTime = self.now - datetime.timedelta(seconds=diff)
        self.tsTime = diff

        self.update_main_gui()

        if self.logUpdate:
            print(
                _('Timeslot:') +
                ' {0}/{1}\t{2}"/{3}"\n'.format(self.slotsUsed, self.totalSlots,
                                               self.tsTime, self.slotLength))

        self.check_limit()
Ejemplo n.º 10
0
Archivo: mtraffic.py Proyecto: lgtl/ntm
    def update_main_gui(self):
        # print("mtraffic.update_main_gui(self)")
        if self.gtkb == None:
            print("MTraffic.update_main_gui(*): Gui not builded.\n")
            return

        mbyte = (1024.0*1024.0)
        traf_tot = (self.rec_traffic + self.tra_traffic)

        per = 100.0 * traf_tot / self.traffic_limit

        self.gui_main_left.set_text("{0:.3f}".format((self.traffic_limit - traf_tot)/mbyte))
        self.gui_main_used.set_text("{0:.3f}".format(traf_tot/mbyte))
        self.gui_main_in.set_text("{0:.3f}".format(self.rec_traffic / mbyte))
        self.gui_main_out.set_text("{0:.3f}".format(self.tra_traffic / mbyte))

        self.gui_main_total.set_text("{0:.0f}".format(self.traffic_limit/mbyte))

        if self.last_speed != None:
            while self.last_speed > self.bwu_max_speed:
                self.bwu_max_speed = self.bwu_max_speed + 10.0
            self.gui_main_bwu.speed = self.last_speed
            self.gui_main_bwu.max_speed = self.bwu_max_speed
            self.gui_main_bwu.queue_draw()

        last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days)

        if self.now != None:
            period_len_total = ntmtools.timedelta2sec(ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day))
            period_len_used = ntmtools.timedelta2sec(self.now - ntmtools.date_to_datetime_start(self.first_day))
            if period_len_used != 0: estimate_str = ntmtools.formatBytes(traf_tot * period_len_total / period_len_used)
            else: estimate_str = "--"
        else: estimate_str = "--"

        self.gui_main_per.set_markup("<small>({0:.1f}%) [{1}]</small>".format(per, estimate_str))

        self.gui_main_period.set_markup("<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
Ejemplo n.º 11
0
 def request_update_db(self, force_update=False):
     #print("request_update_db(force_update={0})".format(force_update))
     self.d_rb_db += self.d_rb
     self.d_tb_db += self.d_tb
     datetime_now = datetime.datetime.now()
     diff_last_update_db = ntmtools.timedelta2sec(datetime_now -
                                                  self.last_db_update)
     if force_update or (diff_last_update_db >= self.db_update_interval):
         self.last_db_update = datetime_now
         diff = self.d_rb_db + self.d_tb_db
         if diff != 0:
             self.update_db_daily_traffic(self.session_start, self.d_rb_db,
                                          self.d_tb_db)
             self.d_rb_db = self.d_tb_db = 0
         self.update_db_session(True)
Ejemplo n.º 12
0
    def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state):
        if (not self.active): return

        self.now = timestamp
        if not self.update_period(session_start): return

        if (self.last_state == 0):
            if (conn_state == 1):
                self.dialog_showed = False
                self.slotsUsed += 1
                self.tsTime = 0
                self.connStartTime = timestamp
                self.tsStartTime = self.connStartTime
        self.last_state = conn_state

        if self.tsStartTime != None:
            diff = ntmtools.timedelta2sec(self.now - self.tsStartTime)
        else: diff = 0
        new_slots = 0
        while diff >= self.slotLength:
            self.slotsUsed += 1
            diff -= self.slotLength
            new_slots += 1

        if (self.alert_active) & (not self.alert_done) & ((self.slotLength - diff) <= self.alert_predate):
            self.alarm_player.play(loop=False)
            self.alert_done = True
            if self.notify_ok:
                self.notifyalert_predate.show ()

        if new_slots > 0:
            self.alert_done = False
            if self.notify_ok:
                self.notify_new_slot.show ()

        self.tsStartTime = self.now - datetime.timedelta(seconds=diff)
        self.tsTime = diff

        self.update_main_gui()

        if self.logUpdate:
            print(_('Timeslot:') + ' {0}/{1}\t{2}"/{3}"\n'.
                format( self.slotsUsed, self.totalSlots, self.tsTime, self.slotLength)
            )

        self.check_limit()
Ejemplo n.º 13
0
    def get_report_from_db(self, first_day, last_day, all_days=False):
        ntmtools.dbg_msg("NTM.get_report_from_db")

        ctra = self.db_conn.cursor()

        if all_days:
            ctra.execute("select * from dailytraffic")
        else:
            last_day_t = last_day + datetime.timedelta(1)
            ctra.execute(
                "select * from dailytraffic where (date>=?) AND (date<?)",
                (first_day, last_day_t))

        t_count = 0
        t_in = 0
        t_out = 0
        tot_min = tot_max = 0
        tra_list = []
        for r in ctra:
            t_in += r[1]
            t_out += r[2]
            tot = r[1] + r[2]
            if (t_count == 0):
                tot_min = tot
                tot_max = tot
            else:
                if tot > tot_max: tot_max = tot
                elif tot < tot_min: tot_min = tot
            t_count += 1
            tra_list += [r]

        cses = self.db_conn.cursor()

        if all_days:
            cses.execute("select * from session")
        else:
            cses.execute(
                "select * from session where (start>=?) AND (start<?)",
                (first_day, last_day_t))

        s_count = 0
        tot_time = 0
        s_max = s_min = 0
        ses_list = []
        for r in cses:
            tsStart = datetime.datetime.strptime(r[0], "%Y-%m-%d %H:%M:%S")
            tsEnd = datetime.datetime.strptime(r[1], "%Y-%m-%d %H:%M:%S")
            diff = ntmtools.timedelta2sec(tsEnd - tsStart)
            tot_time += diff
            if (s_count == 0):
                s_min = diff
                s_max = diff
            else:
                if diff > s_max: s_max = diff
                elif diff < s_min: s_min = diff
            s_count += 1
            ses_list += [r]

        ntmtools.dbg_msg("END - NTM.get_report_from_db")

        return (tra_list, ses_list, t_count, t_in, t_out, tot_max, tot_min,
                s_count, tot_time, s_max, s_min)
Ejemplo n.º 14
0
    def update_main_gui(self):
        ntmtools.dbg_msg("MTraffic.update_main_gui")
        if self.gtkb == None:
            ntmtools.dbg_msg("MTraffic.update_main_gui : Gui not builded.\n",
                             100)
            return

        mbyte = (1024.0 * 1024.0)
        traf_tot = (self.rec_traffic + self.tra_traffic)

        per = 100.0 * traf_tot / self.traffic_limit

        self.gui_main_left.set_text("{0:.3f}".format(
            (self.traffic_limit - traf_tot) / mbyte))
        self.gui_main_left.set_tooltip_text("{0} bytes".format(
            int(self.traffic_limit - traf_tot)))

        self.gui_main_used.set_text("{0:.3f}".format(traf_tot / mbyte))
        self.gui_main_used.set_tooltip_text("{0} bytes".format(int(traf_tot)))

        self.gui_main_in.set_text("{0:.3f}".format(self.rec_traffic / mbyte))
        self.gui_main_in.set_tooltip_text("{0} bytes".format(
            int(self.rec_traffic)))

        self.gui_main_out.set_text("{0:.3f}".format(self.tra_traffic / mbyte))
        self.gui_main_out.set_tooltip_text("{0} bytes".format(
            int(self.tra_traffic)))

        self.gui_main_total.set_text("{0:.0f}".format(self.traffic_limit /
                                                      mbyte))
        self.gui_main_total.set_tooltip_text("{0} bytes".format(
            int(self.traffic_limit)))

        if self.last_speed != None:
            while self.last_speed > self.bwu_max_speed:
                self.bwu_max_speed = self.bwu_max_speed + 10.0
            self.gui_main_bwu.speed = self.last_speed
            self.gui_main_bwu.max_speed = self.bwu_max_speed
            self.gui_main_bwu.queue_draw()

        last_day = ntmtools.get_last_day(self.first_day, self.period_length,
                                         self.custom_days)

        if self.now != None:
            period_len_total = ntmtools.timedelta2sec(
                ntmtools.date_to_datetime_end(last_day) -
                ntmtools.date_to_datetime_start(self.first_day))
            period_len_used = ntmtools.timedelta2sec(
                self.now - ntmtools.date_to_datetime_start(self.first_day))
            if period_len_used != 0:
                estimate_str = ntmtools.format_bytes(
                    traf_tot * period_len_total / period_len_used)
            else:
                estimate_str = "--"
        else:
            estimate_str = "--"

        if self.gui_expander.get_expanded():
            text = "<b>{0}</b> <small>({1:.1f}%) [{2}]</small>".format(
                _("Traffic"), per, estimate_str)
        else:
            text = "<b>{0}</b> {1}<small> ({2:.1f}%)</small>".format(
                _("Traffic"), ntmtools.format_bytes(traf_tot), per)

        self.gui_main_top_label.set_markup(text)

        self.gui_main_period.set_markup(
            "<small><small>" + _("Period") +
            ": {0} - {1}</small></small>".format(self.first_day, last_day))