Example #1
0
class StatsReportLogin(StatsReport):
    filename = 'access.pdf'
    name = _('Users access report by date')  # Report name
    description = _(
        'Report of user access to platform by date')  # Report description
    uuid = '0f62f19a-f166-11e4-8f59-10feed05884b'

    # Input fields
    startDate = gui.DateField(order=1,
                              label=_('Starting date'),
                              tooltip=_('starting date for report'),
                              defvalue=datetime.date.min,
                              required=True)

    endDate = gui.DateField(order=2,
                            label=_('Finish date'),
                            tooltip=_('finish date for report'),
                            defvalue=datetime.date.max,
                            required=True)

    samplingPoints = gui.NumericField(
        order=3,
        label=_('Number of intervals'),
        length=3,
        minValue=0,
        maxValue=128,
        tooltip=_('Number of sampling points used in charts'),
        defvalue='64')

    def initialize(self, values):
        pass

    def initGui(self):
        pass

    def getRangeData(self):
        start = self.startDate.stamp()
        end = self.endDate.stamp()
        if self.samplingPoints.num() < 8:
            self.samplingPoints.value = (self.endDate.date() -
                                         self.startDate.date()).days
        if self.samplingPoints.num() < 2:
            self.samplingPoints.value = 2
        if self.samplingPoints.num() > 128:
            self.samplingPoints.value = 128

        samplingPoints = self.samplingPoints.num()

        # x axis label format
        if end - start > 3600 * 24 * 2:
            xLabelFormat = 'SHORT_DATE_FORMAT'
        else:
            xLabelFormat = 'SHORT_DATETIME_FORMAT'

        samplingIntervals = []
        prevVal = None
        for val in range(start, end, int(
            (end - start) / (samplingPoints + 1))):
            if prevVal is None:
                prevVal = val
                continue
            samplingIntervals.append((prevVal, val))
            prevVal = val

        data = []
        reportData = []
        for interval in samplingIntervals:
            key = (interval[0] + interval[1]) / 2
            val = events.statsManager().getEvents(events.OT_AUTHENTICATOR,
                                                  events.ET_LOGIN,
                                                  since=interval[0],
                                                  to=interval[1]).count()
            data.append((key, val))  # @UndefinedVariable
            reportData.append({
                'date':
                tools.timestampAsStr(interval[0], xLabelFormat) + ' - ' +
                tools.timestampAsStr(interval[1], xLabelFormat),
                'users':
                val
            })

        return xLabelFormat, data, reportData

    def getWeekHourlyData(self):
        start = self.startDate.stamp()
        end = self.endDate.stamp()

        dataWeek = [0] * 7
        dataHour = [0] * 24
        dataWeekHour = [[0] * 24 for _ in range(7)]
        for val in events.statsManager().getEvents(events.OT_AUTHENTICATOR,
                                                   events.ET_LOGIN,
                                                   since=start,
                                                   to=end):
            s = datetime.datetime.fromtimestamp(val.stamp)
            dataWeek[s.weekday()] += 1
            dataHour[s.hour] += 1
            dataWeekHour[s.weekday()][s.hour] += 1
            logger.debug('Data: {} {}'.format(s.weekday(), s.hour))

        return dataWeek, dataHour, dataWeekHour

    def generate(self):
        # Sample query:
        #   'SELECT *, count(*) as number, CEIL(stamp/(3600))*3600 as block'
        #   ' FROM {table}'
        #   ' WHERE event_type = 0 and stamp >= {start} and stamp <= {end}'
        #   ' GROUP BY CEIL(stamp/(3600))'
        #   ' ORDER BY block'

        xLabelFormat, data, reportData = self.getRangeData()

        #
        # User access by date graph
        #
        graph1 = io.BytesIO()

        X = [v[0] for v in data]
        d = {
            'title':
            _('Users Access (global)'),
            'x':
            X,
            'xtickFnc':
            lambda l: filters.date(datetime.datetime.fromtimestamp(l),
                                   xLabelFormat),
            'xlabel':
            _('Date'),
            'y': [{
                'label': 'Users',
                'data': [v[1] for v in data]
            }],
            'ylabel':
            'Users',
            'allTicks':
            False
        }

        graphs.lineChart(SIZE, d, graph1)

        graph2 = io.BytesIO()
        graph3 = io.BytesIO()
        graph4 = io.BytesIO()
        dataWeek, dataHour, dataWeekHour = self.getWeekHourlyData()

        X = list(range(7))
        d = {
            'title':
            _('Users Access (by week)'),
            'x':
            X,
            'xtickFnc':
            lambda l: [
                _('Monday'),
                _('Tuesday'),
                _('Wednesday'),
                _('Thursday'),
                _('Friday'),
                _('Saturday'),
                _('Sunday')
            ][l],
            'xlabel':
            _('Day of week'),
            'y': [{
                'label': 'Users',
                'data': [v for v in dataWeek]
            }],
            'ylabel':
            'Users'
        }

        graphs.barChart(SIZE, d, graph2)

        X = list(range(24))
        d = {
            'title': _('Users Access (by hour)'),
            'x': X,
            'xlabel': _('Hour'),
            'y': [{
                'label': 'Users',
                'data': [v for v in dataHour]
            }],
            'ylabel': 'Users'
        }

        graphs.barChart(SIZE, d, graph3)

        X = list(range(24))
        Y = list(range(7))
        d = {
            'title':
            _('Users Access (by hour)'),
            'x':
            X,
            'xlabel':
            _('Hour'),
            'xtickFnc':
            lambda l: l,
            'y':
            Y,
            'ylabel':
            _('Day of week'),
            'ytickFnc':
            lambda l: [
                _('Monday'),
                _('Tuesday'),
                _('Wednesday'),
                _('Thursday'),
                _('Friday'),
                _('Saturday'),
                _('Sunday')
            ][l],
            'z':
            dataWeekHour,
            'zlabel':
            _('Users')
        }

        graphs.surfaceChart(SIZE, d, graph4)

        return self.templateAsPDF(
            'uds/reports/stats/user-access.html',
            dct={
                'data': reportData,
                'beginning': self.startDate.date(),
                'ending': self.endDate.date(),
                'intervals': self.samplingPoints.num(),
            },
            header=ugettext('Users access to UDS'),
            water=ugettext('UDS Report for users access'),
            images={
                'graph1': graph1.getvalue(),
                'graph2': graph2.getvalue(),
                'graph3': graph3.getvalue(),
                'graph4': graph4.getvalue()
            },
        )
Example #2
0
class StatsReportLogin(StatsReport):
    filename = 'access.pdf'
    name = _('Users access report by date')  # Report name
    description = _(
        'Report of user access to platform by date')  # Report description
    uuid = '0f62f19a-f166-11e4-8f59-10feed05884b'

    # Input fields
    startDate = gui.DateField(order=1,
                              label=_('Starting date'),
                              tooltip=_('starting date for report'),
                              defvalue=datetime.date.min,
                              required=True)

    endDate = gui.DateField(order=2,
                            label=_('Finish date'),
                            tooltip=_('finish date for report'),
                            defvalue=datetime.date.max,
                            required=True)

    samplingPoints = gui.NumericField(
        order=3,
        label=_('Number of intervals'),
        length=3,
        minValue=0,
        maxValue=128,
        tooltip=_('Number of sampling points used in charts'),
        defvalue='64')

    def initialize(self, values):
        pass

    def initGui(self):
        pass

    def getRangeData(self):
        start = self.startDate.stamp()
        end = self.endDate.stamp()
        if self.samplingPoints.num() < 8:
            self.samplingPoints.value = (self.endDate.date() -
                                         self.startDate.date()).days
        if self.samplingPoints.num() < 2:
            self.samplingPoints.value = 2
        if self.samplingPoints.num() > 128:
            self.samplingPoints.value = 128

        samplingPoints = self.samplingPoints.num()

        # x axis label format
        if end - start > 3600 * 24 * 2:
            xLabelFormat = 'SHORT_DATE_FORMAT'
        else:
            xLabelFormat = 'SHORT_DATETIME_FORMAT'

        samplingIntervals = []
        prevVal = None
        for val in range(start, end, (end - start) / (samplingPoints + 1)):
            if prevVal is None:
                prevVal = val
                continue
            samplingIntervals.append((prevVal, val))
            prevVal = val

        data = []
        reportData = []
        for interval in samplingIntervals:
            key = (interval[0] + interval[1]) / 2
            val = events.statsManager().getEvents(events.OT_AUTHENTICATOR,
                                                  events.ET_LOGIN,
                                                  since=interval[0],
                                                  to=interval[1]).count()
            data.append((key, val))  # @UndefinedVariable
            reportData.append({
                'date':
                tools.timestampAsStr(interval[0], xLabelFormat) + ' - ' +
                tools.timestampAsStr(interval[1], xLabelFormat),
                'users':
                val
            })

        return (xLabelFormat, data, reportData)

    def getWeekHourlyData(self):
        start = self.startDate.stamp()
        end = self.endDate.stamp()

        dataWeek = [0] * 7
        dataHour = [0] * 24
        for val in events.statsManager().getEvents(events.OT_AUTHENTICATOR,
                                                   events.ET_LOGIN,
                                                   since=start,
                                                   to=end):
            s = datetime.datetime.fromtimestamp(val.stamp)
            dataWeek[s.weekday()] += 1
            dataHour[s.hour] += 1

        return (dataWeek, dataHour)

    def generate(self):
        # Sample query:
        #   'SELECT *, count(*) as number, CEIL(stamp/(3600))*3600 as block'
        #   ' FROM {table}'
        #   ' WHERE event_type = 0 and stamp >= {start} and stamp <= {end}'
        #   ' GROUP BY CEIL(stamp/(3600))'
        #   ' ORDER BY block'

        # Generate the sampling intervals and get data from db
        start = self.startDate.stamp()
        end = self.endDate.stamp()

        xLabelFormat, data, reportData = self.getRangeData()

        #
        # User access by date graph
        #

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH,
                                     HEIGHT)  # @UndefinedVariable

        dataset = ((ugettext('Users access to UDS'), data), )

        options = {
            'encoding': 'utf-8',
            'axis': {
                'x': {
                    'ticks': [
                        dict(v=i,
                             label=filters.date(
                                 datetime.datetime.fromtimestamp(i),
                                 xLabelFormat))
                        for i in range(start, end, (end - start) / 11)
                    ],
                    'range': (start, end),
                    'showLines':
                    True,
                },
                'y': {
                    'tickCount': 10,
                    'showLines': True,
                },
                'tickFontSize': 16,
            },
            'background': {
                'chartColor': '#f0f0f0',
                'baseColor': '#f0f0f0',
                'lineColor': '#187FF2'
            },
            'colorScheme': {
                'name': 'gradient',
                'args': {
                    'initialColor': '#B8CA16',
                },
            },
            'legend': {
                'hide': False,
                'legendFontSize': 16,
                'position': {
                    'left': 48,
                    'bottom': 8,
                }
            },
            'padding': {
                'left': 48,
                'top': 16,
                'right': 48,
                'bottom': 48,
            },
            'title': _('Users access to UDS')
        }

        chart = pycha.line.LineChart(surface, options)
        chart.addDataset(dataset)
        chart.render()

        img = PILImage.frombuffer("RGBA",
                                  (surface.get_width(), surface.get_height()),
                                  surface.get_data(), "raw", "BGRA", 0, 1)

        #
        # User access by day of week
        #
        dataWeek, dataHour = self.getWeekHourlyData()

        dataset = ((ugettext('Users access to UDS'), [(i, dataWeek[i])
                                                      for i in range(0, 7)]), )

        options['axis'] = {
            'x': {
                'ticks':
                [dict(v=i, label='Day {}'.format(i)) for i in range(0, 7)],
                'range': (0, 6),
                'showLines': True,
            },
            'y': {
                'tickCount': 10,
                'showLines': True,
            },
            'tickFontSize': 16,
        }

        chart = pycha.bar.VerticalBarChart(surface, options)
        chart.addDataset(dataset)
        chart.render()

        img2 = PILImage.frombuffer("RGBA",
                                   (surface.get_width(), surface.get_height()),
                                   surface.get_data(), "raw", "BGRA", 0, 1)

        # Hourly chart
        dataset = ((ugettext('Users access to UDS'),
                    [(i, dataHour[i]) for i in range(0, 24)]), )

        options['axis'] = {
            'x': {
                'ticks':
                [dict(v=i, label='{}:00'.format(i)) for i in range(0, 24)],
                'range': (0, 24),
                'showLines': True,
            },
            'y': {
                'tickCount': 10,
                'showLines': True,
            },
            'tickFontSize': 16,
        }

        chart = pycha.bar.VerticalBarChart(surface, options)
        chart.addDataset(dataset)
        chart.render()

        img3 = PILImage.frombuffer("RGBA",
                                   (surface.get_width(), surface.get_height()),
                                   surface.get_data(), "raw", "BGRA", 0, 1)

        output = six.StringIO()

        queryset = [{
            'image': img,
            'image2': img2,
            'image3': img3,
            'data': reportData
        }]

        logger.debug(queryset)

        try:
            report = AccessReport(queryset=queryset)
            report.title = ugettext('Users access to UDS')
            # report = UsersReport(queryset=users)
            report.generate_by(PDFGenerator, filename=output)
            return output.getvalue()
        except Exception:
            logger.exception('Errool')
            return None
Example #3
0
class PoolPerformanceReport(StatsReport):
    filename = 'pools_performance.pdf'
    name = _('Pools performance by date')  # Report name
    description = _('Pools performance report by date')  # Report description
    uuid = '88932b48-1fd3-11e5-a776-10feed05884b'

    # Input fields
    pools = gui.MultiChoiceField(order=1,
                                 label=_('Pools'),
                                 tooltip=_('Pools for report'),
                                 required=True)

    startDate = gui.DateField(order=2,
                              label=_('Starting date'),
                              tooltip=_('starting date for report'),
                              defvalue=datetime.date.min,
                              required=True)

    endDate = gui.DateField(order=3,
                            label=_('Finish date'),
                            tooltip=_('finish date for report'),
                            defvalue=datetime.date.max,
                            required=True)

    samplingPoints = gui.NumericField(
        order=4,
        label=_('Number of intervals'),
        length=3,
        minValue=0,
        maxValue=32,
        tooltip=_('Number of sampling points used in charts'),
        defvalue='8')

    def initialize(self, values):
        pass

    def initGui(self):
        logger.debug('Initializing gui')
        vals = [
            gui.choiceItem(v.uuid, v.name)
            for v in ServicePool.objects.all().order_by('name')
        ]
        self.pools.setValues(vals)

    def getPools(self):
        return [(v.id, v.name)
                for v in ServicePool.objects.filter(uuid__in=self.pools.value)]

    def getRangeData(self):
        start = self.startDate.stamp()
        end = self.endDate.stamp()

        if self.samplingPoints.num() < 2:
            self.samplingPoints.value = (self.endDate.date() -
                                         self.startDate.date()).days
        if self.samplingPoints.num() < 2:
            self.samplingPoints.value = 2
        if self.samplingPoints.num() > 32:
            self.samplingPoints.value = 32

        samplingPoints = self.samplingPoints.num()

        pools = self.getPools()

        if len(pools) == 0:
            raise Exception(_('Select at least a service pool for the report'))

        logger.debug('Pools: {}'.format(pools))

        # x axis label format
        if end - start > 3600 * 24 * 2:
            xLabelFormat = 'SHORT_DATE_FORMAT'
        else:
            xLabelFormat = 'SHORT_DATETIME_FORMAT'

        # Generate samplings interval
        samplingIntervals = []
        prevVal = None
        for val in range(start, end, int(
            (end - start) / (samplingPoints + 1))):
            if prevVal is None:
                prevVal = val
                continue
            samplingIntervals.append((prevVal, val))
            prevVal = val

        # Store dataUsers for all pools
        poolsData = []

        fld = events.statsManager().getEventFldFor('username')

        reportData = []
        for p in pools:
            dataUsers = []
            dataAccesses = []
            for interval in samplingIntervals:
                key = (interval[0] + interval[1]) / 2
                q = events.statsManager().getEvents(
                    events.OT_DEPLOYED,
                    events.ET_ACCESS,
                    since=interval[0],
                    to=interval[1],
                    owner_id=p[0]).values(fld).annotate(cnt=Count(fld))
                accesses = 0
                for v in q:
                    accesses += v['cnt']

                dataUsers.append((key, len(q)))  # @UndefinedVariable
                dataAccesses.append((key, accesses))
                reportData.append({
                    'name':
                    p[1],
                    'date':
                    tools.timestampAsStr(interval[0], xLabelFormat) + ' - ' +
                    tools.timestampAsStr(interval[1], xLabelFormat),
                    'users':
                    len(q),
                    'accesses':
                    accesses
                })
            poolsData.append({
                'pool': p[0],
                'name': p[1],
                'dataUsers': dataUsers,
                'dataAccesses': dataAccesses,
            })

        return xLabelFormat, poolsData, reportData

    def generate(self):
        # Generate the sampling intervals and get dataUsers from db
        xLabelFormat, poolsData, reportData = self.getRangeData()

        graph1 = io.BytesIO()
        graph2 = io.BytesIO()

        # surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)  # @UndefinedVariable

        # logger.debug('PoolsData: %s', poolsData)

        X = [v[0] for v in poolsData[0]['dataUsers']]
        data = {
            'title':
            _('Distinct Users'),
            'x':
            X,
            'xtickFnc':
            lambda l: filters.date(datetime.datetime.fromtimestamp(X[int(l)]),
                                   xLabelFormat) if int(l) >= 0 else '',
            'xlabel':
            _('Date'),
            'y': [{
                'label': p['name'],
                'data': [v[1] for v in p['dataUsers']]
            } for p in poolsData],
            'ylabel':
            _('Users')
        }

        graphs.barChart(SIZE, data, graph1)

        X = [v[0] for v in poolsData[0]['dataAccesses']]
        data = {
            'title':
            _('Accesses'),
            'x':
            X,
            'xtickFnc':
            lambda l: filters.date(datetime.datetime.fromtimestamp(X[int(l)]),
                                   xLabelFormat) if int(l) >= 0 else '',
            'xlabel':
            _('Date'),
            'y': [{
                'label': p['name'],
                'data': [v[1] for v in p['dataAccesses']]
            } for p in poolsData],
            'ylabel':
            _('Accesses')
        }

        graphs.barChart(SIZE, data, graph2)

        # Generate Data for pools, basically joining all pool data

        return self.templateAsPDF(
            'uds/reports/stats/pools-performance.html',
            dct={
                'data': reportData,
                'pools': [i[1] for i in self.getPools()],
                'beginning': self.startDate.date(),
                'ending': self.endDate.date(),
                'intervals': self.samplingPoints.num(),
            },
            header=ugettext('UDS Pools Performance Report'),
            water=ugettext('Pools Performance'),
            images={
                'graph1': graph1.getvalue(),
                'graph2': graph2.getvalue()
            },
        )
Example #4
0
class UsageByPool(StatsReport):
    filename = 'pools_usage.pdf'
    name = _('Pools usage by users')  # Report name
    description = _('Pools usage by user report')  # Report description
    uuid = '38ec12dc-beaf-11e5-bd0a-10feed05884b'

    # Input fields
    pool = gui.ChoiceField(order=1,
                           label=_('Pool'),
                           tooltip=_('Pool for report'),
                           required=True)

    startDate = gui.DateField(order=2,
                              label=_('Starting date'),
                              tooltip=_('starting date for report'),
                              defvalue=datetime.date.min,
                              required=True)

    endDate = gui.DateField(order=3,
                            label=_('Finish date'),
                            tooltip=_('finish date for report'),
                            defvalue=datetime.date.max,
                            required=True)

    def initialize(self, values):
        pass

    def initGui(self):
        logger.debug('Initializing gui')
        vals = [
            gui.choiceItem(v.uuid, v.name) for v in ServicePool.objects.all()
        ]
        self.pool.setValues(vals)

    def getData(self):
        # Generate the sampling intervals and get dataUsers from db
        start = self.startDate.stamp()
        end = self.endDate.stamp()
        logger.debug(self.pool.value)
        pool = ServicePool.objects.get(uuid=self.pool.value)

        items = events.statsManager().getEvents(
            events.OT_DEPLOYED, (events.ET_LOGIN, events.ET_LOGOUT),
            owner_id=pool.id,
            since=start,
            to=end).order_by('stamp')

        logins = {}
        data = []
        for i in items:
            # if '\\' in i.fld1:
            #    continue

            if i.event_type == events.ET_LOGIN:
                logins[i.fld4] = i.stamp
            else:
                if i.fld4 in logins:
                    stamp = logins[i.fld4]
                    del logins[i.fld4]
                    total = i.stamp - stamp
                    data.append({
                        'name': i.fld4,
                        'date': datetime.datetime.fromtimestamp(stamp),
                        'time': total
                    })

        logger.debug('data')

        return data, pool.name

    def generate(self):
        items, poolName = self.getData()

        output = StringIO.StringIO()

        report = UsersReport(queryset=items)
        report.title = _('Users usage list for {}').format(poolName)
        report.generate_by(PDFGenerator, filename=output)
        return output.getvalue()
Example #5
0
class PoolPerformanceReport(StatsReport):
    filename = 'pools_performance.pdf'
    name = _('Pools performance by date')  # Report name
    description = _('Pools performance report by date')  # Report description
    uuid = '88932b48-1fd3-11e5-a776-10feed05884b'

    # Input fields
    pools = gui.MultiChoiceField(order=1,
                                 label=_('Pools'),
                                 tooltip=_('Pools for report'),
                                 required=True)

    startDate = gui.DateField(order=2,
                              label=_('Starting date'),
                              tooltip=_('starting date for report'),
                              defvalue=datetime.date.min,
                              required=True)

    endDate = gui.DateField(order=3,
                            label=_('Finish date'),
                            tooltip=_('finish date for report'),
                            defvalue=datetime.date.max,
                            required=True)

    samplingPoints = gui.NumericField(
        order=4,
        label=_('Number of intervals'),
        length=3,
        minValue=0,
        maxValue=32,
        tooltip=_('Number of sampling points used in charts'),
        defvalue='8')

    def initialize(self, values):
        pass

    def initGui(self):
        logger.debug('Initializing gui')
        vals = [
            gui.choiceItem(v.uuid, v.name) for v in ServicePool.objects.all()
        ]
        self.pools.setValues(vals)

    def getRangeData(self):
        start = self.startDate.stamp()
        end = self.endDate.stamp()

        if self.samplingPoints.num() < 2:
            self.samplingPoints.value = (self.endDate.date() -
                                         self.startDate.date()).days
        if self.samplingPoints.num() < 2:
            self.samplingPoints.value = 2
        if self.samplingPoints.num() > 32:
            self.samplingPoints.value = 32

        samplingPoints = self.samplingPoints.num()

        pools = [(v.id, v.name)
                 for v in ServicePool.objects.filter(uuid__in=self.pools.value)
                 ]
        if len(pools) == 0:
            raise Exception(_('Select at least a service pool for the report'))

        logger.debug('Pools: {}'.format(pools))

        # x axis label format
        if end - start > 3600 * 24 * 2:
            xLabelFormat = 'SHORT_DATE_FORMAT'
        else:
            xLabelFormat = 'SHORT_DATETIME_FORMAT'

        # Generate samplings interval
        samplingIntervals = []
        prevVal = None
        for val in range(start, end, (end - start) / (samplingPoints + 1)):
            if prevVal is None:
                prevVal = val
                continue
            samplingIntervals.append((prevVal, val))
            prevVal = val

        # Store dataUsers for all pools
        poolsData = []

        fld = events.statsManager().getEventFldFor('username')

        reportData = []
        for p in pools:
            dataUsers = []
            dataAccesses = []
            for interval in samplingIntervals:
                key = (interval[0] + interval[1]) / 2
                q = events.statsManager().getEvents(
                    events.OT_DEPLOYED,
                    events.ET_ACCESS,
                    since=interval[0],
                    to=interval[1],
                    owner_id=p[0]).values(fld).annotate(cnt=Count(fld))
                accesses = 0
                for v in q:
                    accesses += v['cnt']

                dataUsers.append((key, len(q)))  # @UndefinedVariable
                dataAccesses.append((key, accesses))
                reportData.append({
                    'name':
                    p[1],
                    'date':
                    tools.timestampAsStr(interval[0], xLabelFormat) + ' - ' +
                    tools.timestampAsStr(interval[1], xLabelFormat),
                    'users':
                    len(q),
                    'accesses':
                    accesses
                })
            poolsData.append({
                'pool': p[0],
                'name': p[1],
                'dataUsers': dataUsers,
                'dataAccesses': dataAccesses,
            })

        return (xLabelFormat, poolsData, reportData)

    def generate(self):
        # Generate the sampling intervals and get dataUsers from db
        start = self.startDate.stamp()
        end = self.endDate.stamp()

        xLabelFormat, poolsData, reportData = self.getRangeData()

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH,
                                     HEIGHT)  # @UndefinedVariable

        options = {
            'encoding': 'utf-8',
            'axis': {
                'x': {
                    'ticks': [
                        dict(v=i,
                             label=filters.date(
                                 datetime.datetime.fromtimestamp(l),
                                 xLabelFormat)) for i, l in enumerate(
                                     range(start, end, (end - start) /
                                           self.samplingPoints.num()))
                    ],
                    'range': (0, self.samplingPoints.num()),
                    'showLines':
                    True,
                },
                'y': {
                    'tickCount': 10,
                    'showLines': True,
                },
                'tickFontSize': 16,
            },
            'background': {
                'chartColor': '#f0f0f0',
                'baseColor': '#f0f0f0',
                'lineColor': '#187FF2'
            },
            'colorScheme': {
                'name': 'rainbow',
                'args': {
                    'initialColor': 'blue',
                },
            },
            'legend': {
                'hide': False,
                'legendFontSize': 16,
                'position': {
                    'left': 96,
                    'top': 40,
                }
            },
            'padding': {
                'left': 48,
                'top': 16,
                'right': 48,
                'bottom': 48,
            },
            'title': _('Users by pool'),
        }

        # chart = pycha.line.LineChart(surface, options)
        # chart = pycha.bar.VerticalBarChart(surface, options)
        chart = pycha.stackedbar.StackedVerticalBarChart(surface, options)

        dataset = []
        for pool in poolsData:
            logger.debug(pool['dataUsers'])
            ds = list((i, l[1]) for i, l in enumerate(pool['dataUsers']))
            logger.debug(ds)
            dataset.append((ugettext('Users for {}').format(pool['name']), ds))

        logger.debug('Dataset: {}'.format(dataset))
        chart.addDataset(dataset)

        chart.render()

        img = PILImage.frombuffer("RGBA",
                                  (surface.get_width(), surface.get_height()),
                                  surface.get_data(), "raw", "BGRA", 0, 1)

        # Accesses
        chart = pycha.stackedbar.StackedVerticalBarChart(surface, options)

        dataset = []
        for pool in poolsData:
            logger.debug(pool['dataAccesses'])
            ds = list((i, l[1]) for i, l in enumerate(pool['dataAccesses']))
            logger.debug(ds)
            dataset.append(
                (ugettext('Accesses for {}').format(pool['name']), ds))

        logger.debug('Dataset: {}'.format(dataset))
        chart.addDataset(dataset)

        chart.render()

        img2 = PILImage.frombuffer("RGBA",
                                   (surface.get_width(), surface.get_height()),
                                   surface.get_data(), "raw", "BGRA", 0, 1)

        # Generate Data for pools, basically joining all pool data

        queryset = [{'image': img, 'image2': img2, 'data': reportData}]

        logger.debug(queryset)

        output = six.StringIO()

        try:
            report = AccessReport(queryset=queryset)
            report.title = ugettext('UDS Pools Performance Report')
            report.generate_by(PDFGenerator, filename=output)
            return output.getvalue()
        except Exception:
            logger.exception('Errool')
            return None
Example #6
0
class UsageSummaryByPool(StatsReport):
    filename = 'pools_usage.pdf'
    name = _('Summary of pools usage')  # Report name
    description = _('Generates a report with the summary of a pool usage'
                    )  # Report description
    uuid = '202c6438-30a8-11e7-80e4-77c1e4cb9e09'

    # Input fields
    pool = gui.ChoiceField(order=1,
                           label=_('Pool'),
                           tooltip=_('Pool for report'),
                           required=True)

    startDate = gui.DateField(order=2,
                              label=_('Starting date'),
                              tooltip=_('starting date for report'),
                              defvalue=datetime.date.min,
                              required=True)

    endDate = gui.DateField(order=3,
                            label=_('Finish date'),
                            tooltip=_('finish date for report'),
                            defvalue=datetime.date.max,
                            required=True)

    def initialize(self, values):
        pass

    def initGui(self):
        logger.debug('Initializing gui')
        vals = [
            gui.choiceItem(v.uuid, v.name) for v in ServicePool.objects.all()
        ]
        self.pool.setValues(vals)

    def getPoolData(self, pool):
        start = self.startDate.stamp()
        end = self.endDate.stamp()
        logger.debug(self.pool.value)

        items = events.statsManager().getEvents(
            events.OT_DEPLOYED, (events.ET_LOGIN, events.ET_LOGOUT),
            owner_id=pool.id,
            since=start,
            to=end).order_by('stamp')

        logins = {}
        users = {}
        for i in items:
            # if '\\' in i.fld1:
            #    continue
            username = i.fld4
            if i.event_type == events.ET_LOGIN:
                logins[username] = i.stamp
            else:
                if username in logins:
                    stamp = logins[username]
                    del logins[username]
                    total = i.stamp - stamp
                    if username not in users:
                        users[username] = {'sessions': 0, 'time': 0}
                    users[username]['sessions'] += 1
                    users[username]['time'] += total
                    # data.append({
                    #    'name': i.fld4,
                    #    'date': datetime.datetime.fromtimestamp(stamp),
                    #    'time': total
                    # })

        # Extract different number of users
        data = []
        for k, v in users.items():
            data.append({
                'user':
                k,
                'sessions':
                v['sessions'],
                'hours':
                '{:.2f}'.format(float(v['time']) / 3600),
                'average':
                '{:.2f}'.format(float(v['time']) / 3600 / v['sessions'])
            })

        return data, pool.name

    def getData(self):
        return self.getPoolData(ServicePool.objects.get(uuid=self.pool.value))

    def generate(self):
        items, poolName = self.getData()

        return self.templateAsPDF(
            'uds/reports/stats/pool-usage-summary.html',
            dct={
                'data': items,
                'pool': poolName,
                'beginning': self.startDate.date(),
                'ending': self.endDate.date(),
            },
            header=ugettext('Users usage list for {}').format(poolName),
            water=ugettext('UDS Report of users in {}').format(poolName))
Example #7
0
class CountersPoolAssigned(StatsReport):
    filename = 'pools_counters.pdf'
    name = _('Pools usage on a day')  # Report name
    description = _('Pools usage counters for an specific day')  # Report description
    uuid = '0b429f70-2fc6-11e7-9a2a-8fc37101e66a'

    startDate = gui.DateField(
        order=2,
        label=_('Date'),
        tooltip=_('Date for report'),
        defvalue=datetime.date.min,
        required=True
    )

    pools = gui.MultiChoiceField(
        order=1,
        label=_('Pools'),
        tooltip=_('Pools for report'),
        required=True
    )

    def initialize(self, values):
        pass

    def initGui(self):
        logger.debug('Initializing gui')
        vals = [
            gui.choiceItem(v.uuid, v.name) for v in ServicePool.objects.all()
        ]
        self.pools.setValues(vals)

    def getData(self):
        # Generate the sampling intervals and get dataUsers from db
        start = self.startDate.date()
        end = self.startDate.date() + datetime.timedelta(days=1)

        data = []

        for poolUuid in self.pools.value:
            try:
                pool = ServicePool.objects.get(uuid=poolUuid)
            except Exception:
                pass  # Ignore pool

            hours = {}
            for i in range(24):
                hours[i] = 0

            for x in counters.getCounters(pool, counters.CT_ASSIGNED, since=start, to=end, limit=24, use_max=True, all=False):
                hour = x[0].hour
                val = int(x[1])
                if hours[hour] < val:
                    hours[hour] = val

            data.append({'uuid':pool.uuid, 'name': pool.name, 'hours': hours})


        logger.debug('data: {}'.format(data))

        return data

    def generate(self):
        items = self.getData()

        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)  # @UndefinedVariable

        options = {
            'encoding': 'utf-8',
            'axis': {
                'x': {
                    'ticks': [
                        dict(v=i, label='{:02d}'.format(i)) for i in range(24)
                    ],
                    'range': (0, 23),
                    'showLines': True,
                },
                'y': {
                    'tickCount': 10,
                    'showLines': True,
                },
                'tickFontSize': 16,
            },
            'background': {
                'chartColor': '#f0f0f0',
                'baseColor': '#f0f0f0',
                'lineColor': '#187FF2'
            },
            'colorScheme': {
                'name': 'rainbow',
                'args': {
                    'initialColor': 'blue',
                },
            },
            'legend': {
                'hide': False,
                'legendFontSize': 16,
                'position': {
                    'left': 96,
                    'top': 40,
                }
            },
            'padding': {
                'left': 48,
                'top': 16,
                'right': 48,
                'bottom': 48,
            },
            'title': _('Services by hour'),
        }

        # chart = pycha.line.LineChart(surface, options)
        # chart = pycha.bar.VerticalBarChart(surface, options)
        chart = pycha.stackedbar.StackedVerticalBarChart(surface, options)

        dataset = []
        for pool in items:
            logger.debug(pool['hours'])
            ds = list((i, pool['hours'][i]) for i in range(24))
            logger.debug(ds)
            dataset.append((ugettext('Services for {}').format(pool['name']), ds))

        logger.debug('Dataset: {}'.format(dataset))
        chart.addDataset(dataset)

        chart.render()

        img = PILImage.frombuffer("RGBA", (surface.get_width(), surface.get_height()), surface.get_data(), "raw", "BGRA", 0, 1)


        output = StringIO.StringIO()

        # Regenerate data

        # Generate data
        data = []
        for i in items:
            for j in range(24):
                data.append({'name': i['name'], 'time': '{:02d}'.format(j), 'value': i['hours'][j]})

        report = CountersPoolsReport(queryset=[{'data': data, 'image': img}])
        report.title = six.text_type(_('Services usage report'))
        report.generate_by(PDFGenerator, filename=output)
        return output.getvalue()
Example #8
0
class CountersPoolAssigned(StatsReport):
    filename = 'pools_counters.pdf'
    name = _('Pools usage on a day')  # Report name
    description = _('Pools usage counters for an specific day')  # Report description
    uuid = '0b429f70-2fc6-11e7-9a2a-8fc37101e66a'

    startDate = gui.DateField(
        order=2,
        label=_('Date'),
        tooltip=_('Date for report'),
        defvalue='',
        required=True
    )

    pools = gui.MultiChoiceField(
        order=1,
        label=_('Pools'),
        tooltip=_('Pools for report'),
        required=True
    )

    def initialize(self, values):
        pass

    def initGui(self):
        logger.debug('Initializing gui')
        vals = [
            gui.choiceItem(v.uuid, v.name) for v in ServicePool.objects.all().order_by('name')
        ]
        self.pools.setValues(vals)

    def getData(self):
        # Generate the sampling intervals and get dataUsers from db
        start = self.startDate.date()
        end = self.startDate.date() + datetime.timedelta(days=1)

        data = []

        pool = None
        for poolUuid in self.pools.value:
            try:
                pool = ServicePool.objects.get(uuid=poolUuid)
            except Exception:
                pass  # Ignore pool

            hours = {}
            for i in range(24):
                hours[i] = i * i

            for x in counters.getCounters(pool, counters.CT_ASSIGNED, since=start, to=end, limit=24, use_max=True, all=False):
                hour = x[0].hour
                val = int(x[1])
                if hours[hour] < val:
                    hours[hour] = val

            data.append({'uuid':pool.uuid, 'name': pool.name, 'hours': hours})

        logger.debug('data: {}'.format(data))

        return data

    def generate(self):
        items = self.getData()

        graph1 = io.BytesIO()

        X = list(range(24))
        d = {
            'title': _('Services by hour'),
            'x': X,
            'xtickFnc': lambda l: '{:02d}'.format(l),
            'xlabel': _('Hour'),
            'y': [
                {
                    'label': i['name'],
                    'data': [i['hours'][v] for v in X]
                } for i in items
            ],
            'ylabel': 'Services'
        }

        graphs.barChart(SIZE, d, graph1)

        return self.templateAsPDF(
            'uds/reports/stats/pools-usage-day.html',
            dct={
                'data': items,
                'pools': [v.name for v in ServicePool.objects.filter(uuid__in=self.pools.value)],
                'beginning': self.startDate.date(),
            },
            header=ugettext('Services usage report for a day'),
            water=ugettext('Service usage report'),
            images={'graph1': graph1.getvalue()},
        )