Esempio n. 1
0
 def setUp(self):
     super().setUp()
     self.computer = ComputerFactory(nutzer=self.nutzer)
     self.traffic_entries = []
     for timetag in range(timetag_today()-21, timetag_today()):
         traffic_entry = TrafficFactory.create(timetag=timetag, ip=self.computer.c_ip)
         self.traffic_entries.append(traffic_entry)
Esempio n. 2
0
class CreditFactory(WuFactory):
    class Meta:
        model = Credit

    nutzer = SubFactory(NutzerFactory)
    user_id = LazyAttribute(lambda self: self.nutzer.nutzer_id)
    amount = FuzzyInteger(low=0, high=63 * 1024 * 1024)
    timetag = FuzzyInteger(low=timetag_today() - 21, high=timetag_today())
Esempio n. 3
0
 def setUp(self):
     super().setUp()
     self.nutzer = NutzerFactory.create()
     self.credit_entries = []
     self.timetag_range = range(timetag_today()-21, timetag_today())
     for timetag in self.timetag_range:
         self.credit_entries.append(CreditFactory.create(nutzer=self.nutzer,
                                                         timetag=timetag))
Esempio n. 4
0
 def setUp(self):
     super().setUp()
     self.computer = ComputerFactory(nutzer=self.nutzer)
     self.traffic_entries = []
     for timetag in range(timetag_today() - 21, timetag_today()):
         traffic_entry = TrafficFactory.create(timetag=timetag,
                                               ip=self.computer.c_ip)
         self.traffic_entries.append(traffic_entry)
Esempio n. 5
0
 def setUp(self):
     super().setUp()
     self.nutzer = NutzerFactory.create()
     self.credit_entries = []
     self.timetag_range = range(timetag_today() - 21, timetag_today())
     for timetag in self.timetag_range:
         self.credit_entries.append(
             CreditFactory.create(nutzer=self.nutzer, timetag=timetag))
Esempio n. 6
0
class TrafficFactory(WuFactory):
    class Meta:
        model = Traffic
    # TODO: `unique` constraint not met
    timetag = FuzzyInteger(low=timetag_today() - 21,
                           high=timetag_today())
    ip = Faker('ipv4')
    input = FuzzyDecimal(low=0, high=10*1024*1024)
    output = FuzzyDecimal(low=0, high=10*1024*1024)
Esempio n. 7
0
File: user.py Progetto: gwTumm/sipa
    def traffic_history(self):
        traffic_history = []

        credit_entries = reversed(
            db.session.query(Credit)
            .filter_by(user_id=self._nutzer.nutzer_id)
            .order_by(Credit.timetag.desc())
            .limit(7).all()
        )

        accountable_ips = [c.c_ip for c in self._nutzer.computer]

        for credit_entry in credit_entries:
            traffic_entries = (db.session.query(Traffic)
                               .filter_by(timetag=credit_entry.timetag)
                               .filter(Traffic.ip.in_(accountable_ips))
                               .all()) if accountable_ips else []

            traffic_history.append({
                'day': (datetime.today() + timedelta(
                    days=credit_entry.timetag - timetag_today()
                )).weekday(),
                'input': sum(t.input for t in traffic_entries),
                'output': sum(t.output for t in traffic_entries),
                'throughput': sum(t.overall for t in traffic_entries),
                'credit': credit_entry.amount,
            })

        return traffic_history
Esempio n. 8
0
File: user.py Progetto: gwTumm/sipa
    def traffic_history(self):
        traffic_history = []

        credit_entries = reversed(
            db.session.query(Credit).filter_by(
                user_id=self._nutzer.nutzer_id).order_by(
                    Credit.timetag.desc()).limit(7).all())

        accountable_ips = [c.c_ip for c in self._nutzer.computer]

        for credit_entry in credit_entries:
            traffic_entries = (db.session.query(Traffic).filter_by(
                timetag=credit_entry.timetag).filter(
                    Traffic.ip.in_(accountable_ips)).all()
                               ) if accountable_ips else []

            traffic_history.append({
                'day':
                (datetime.today() + timedelta(days=credit_entry.timetag -
                                              timetag_today())).weekday(),
                'input':
                sum(t.input for t in traffic_entries),
                'output':
                sum(t.output for t in traffic_entries),
                'throughput':
                sum(t.overall for t in traffic_entries),
                'credit':
                credit_entry.amount,
            })

        return traffic_history
Esempio n. 9
0
 def test_today_timetag(self):
     assert timetag_today() == time() // 86400
Esempio n. 10
0
File: user.py Progetto: fgrsnau/sipa
    def cache_information(self):
        user = sql_query(
            "SELECT nutzer_id, wheim_id, etage, zimmernr, status "
            "FROM nutzer "
            "WHERE unix_account = %s",
            (self.uid,)
        ).fetchone()

        if not user:
            logger.critical("User %s does not have a database entry", self.uid,
                            extra={'stack': True})
            raise DBQueryEmpty("No User found for unix_account '{}'"
                               .format(self.uid))

        self._id = user['nutzer_id']
        self._address = "{0} / {1} {2}".format(
            # MySQL Dormitory IDs in are from 1-11, so we map to 0-10 with x-1
            DORMITORIES[user['wheim_id'] - 1],
            user['etage'],
            user['zimmernr']
        )
        self._status_id = user['status']

        devices = sql_query(
            "SELECT c_etheraddr, c_ip, c_hname, c_alias "
            "FROM computer "
            "WHERE nutzer_id = %s",
            (user['nutzer_id'])
        ).fetchall()

        if devices:
            self._devices = [{
                'ip': device['c_ip'],
                'mac': device['c_etheraddr'].upper(),
                'hostname': device['c_hname'],
                'hostalias': device['c_alias'],
            } for device in devices]
        else:
            logger.warning("User {} (id {}) does not have any devices"
                           .format(self.uid, self._id))
            self._devices = []

        # cache credit
        current_timetag = timetag_today()

        try:
            # aggregated credit from 1(MEZ)/2(MESZ) AM
            credit = sql_query(
                "SELECT amount FROM credit "
                "WHERE user_id = %(id)s "
                "AND timetag >= %(today)s - 1 "
                "ORDER BY timetag DESC LIMIT 1",
                {'today': current_timetag, 'id': self._id}
            ).fetchone()['amount']

            # subtract the current traffic not yet aggregated in `credit`
            traffic = sql_query(
                "SELECT input + output as throughput "
                "FROM traffic.tuext AS t "
                "LEFT JOIN computer AS c on c.c_ip = t.ip "
                "WHERE c.nutzer_id =  %(id)s AND t.timetag = %(today)s",
                {'today': current_timetag, 'id': self._id}
            ).fetchone()

            credit -= traffic['throughput']

        except OperationalError as e:
            logger.critical("Unable to connect to MySQL server",
                            extra={'data': {'exception_args': e.args}})
            self._credit = None
            raise

        else:
            self._credit = round(credit / 1024, 2)

        # cache traffic history
        self._traffic_history = []

        for delta in range(-6, 1):
            current_timetag = timetag_today() + delta
            day = datetime.today() + timedelta(days=delta)

            traffic_of_the_day = sql_query(
                "SELECT sum(t.input) as input, sum(t.output) as output, "
                "sum(t.input+t.output) as throughput "
                "FROM traffic.tuext as t "
                "LEFT JOIN computer AS c ON c.c_ip = t.ip "
                "WHERE t.timetag = %(timetag)s AND c.nutzer_id = %(id)s",
                {'timetag': current_timetag, 'id': self._id},
            ).fetchone()

            credit_of_the_day = sql_query(
                "SELECT amount FROM credit "
                "WHERE user_id = %(id)s "
                "AND timetag >= %(timetag)s - 1 "
                "ORDER BY timetag DESC LIMIT 1",
                {'timetag': current_timetag, 'id': self._id},
            ).fetchone()['amount']

            self._traffic_history.append({
                'day': day.weekday(),
                'input': traffic_of_the_day['input'] / 1024,
                'output': traffic_of_the_day['output'] / 1024,
                'throughput': traffic_of_the_day['throughput'] / 1024,
                'credit': credit_of_the_day / 1024,
            })