Example #1
0
    def get(self, request, name=None):
        if name:
            metrics = poem_models.Metric.objects.filter(name=name)
            if metrics.count() == 0:
                raise NotFound(status=404, detail='Metric not found')
        else:
            metrics = poem_models.Metric.objects.all()

        results = []
        for metric in metrics:
            config = two_value_inline(metric.config)
            parent = one_value_inline(metric.parent)
            probeexecutable = one_value_inline(metric.probeexecutable)
            attribute = two_value_inline(metric.attribute)
            dependancy = two_value_inline(metric.dependancy)
            flags = two_value_inline(metric.flags)
            files = two_value_inline(metric.files)
            parameter = two_value_inline(metric.parameter)
            fileparameter = two_value_inline(metric.fileparameter)

            if metric.probekey:
                probeversion = metric.probekey.__str__()
            else:
                probeversion = ''

            if metric.group:
                group = metric.group.name
            else:
                group = ''

            mt = admin_models.MetricTemplateHistory.objects.get(
                name=metric.name, probekey=metric.probekey)

            results.append(
                dict(id=metric.id,
                     name=metric.name,
                     mtype=metric.mtype.name,
                     tags=[tag.name for tag in mt.tags.all()],
                     probeversion=probeversion,
                     group=group,
                     description=metric.description,
                     parent=parent,
                     probeexecutable=probeexecutable,
                     config=config,
                     attribute=attribute,
                     dependancy=dependancy,
                     flags=flags,
                     files=files,
                     parameter=parameter,
                     fileparameter=fileparameter))

        results = sorted(results, key=lambda k: k['name'])

        if name:
            return Response(results[0])
        else:
            return Response(results)
Example #2
0
    def get(self, request, name=None):
        if name:
            metrictemplates = admin_models.MetricTemplate.objects.filter(
                name=name)
            if metrictemplates.count() == 0:
                raise NotFound(status=404, detail='Metric template not found')
        else:
            metrictemplates = admin_models.MetricTemplate.objects.all()

        results = []
        for metrictemplate in metrictemplates:
            config = two_value_inline(metrictemplate.config)
            parent = one_value_inline(metrictemplate.parent)
            probeexecutable = one_value_inline(metrictemplate.probeexecutable)
            attribute = two_value_inline(metrictemplate.attribute)
            dependency = two_value_inline(metrictemplate.dependency)
            flags = two_value_inline(metrictemplate.flags)
            files = two_value_inline(metrictemplate.files)
            parameter = two_value_inline(metrictemplate.parameter)
            fileparameter = two_value_inline(metrictemplate.fileparameter)

            ostag = []
            if metrictemplate.probekey:
                for repo in metrictemplate.probekey.package.repos.all():
                    ostag.append(repo.tag.name)

            if metrictemplate.probekey:
                probeversion = metrictemplate.probekey.__str__()
            else:
                probeversion = ''

            results.append(
                dict(id=metrictemplate.id,
                     name=metrictemplate.name,
                     mtype=metrictemplate.mtype.name,
                     ostag=ostag,
                     probeversion=probeversion,
                     parent=parent,
                     probeexecutable=probeexecutable,
                     config=config,
                     attribute=attribute,
                     dependency=dependency,
                     flags=flags,
                     files=files,
                     parameter=parameter,
                     fileparameter=fileparameter))

        results = sorted(results, key=lambda k: k['name'])

        if name:
            del results[0]['ostag']
            return Response(results[0])
        else:
            return Response(results)
Example #3
0
    def get(self, request, obj, name=None):
        history_model = {
            'probe': admin_models.ProbeHistory,
            'metrictemplate': admin_models.MetricTemplateHistory
        }

        if name:
            history_instance = history_model[obj].objects.filter(name=name)

            if history_instance.count() == 0:
                raise NotFound(status=404, detail='Version not found')

            else:
                instance = history_instance[0].object_id

                vers = history_model[obj].objects.filter(
                    object_id=instance).order_by('-date_created')

                results = []
                for ver in vers:
                    if isinstance(instance, admin_models.Probe):
                        version = ver.package.version
                        fields = {
                            'name': ver.name,
                            'version': ver.package.version,
                            'package': ver.package.__str__(),
                            'description': ver.description,
                            'comment': ver.comment,
                            'repository': ver.repository,
                            'docurl': ver.docurl
                        }
                    else:
                        if ver.probekey:
                            probekey = ver.probekey.__str__()
                            version = ver.probekey.__str__().split(
                                ' ')[1][1:-1]
                        else:
                            probekey = ''
                            version = datetime.datetime.strftime(
                                ver.date_created, '%Y-%m-%d %H:%M:%S')
                        fields = {
                            'name':
                            ver.name,
                            'mtype':
                            ver.mtype.name,
                            'probeversion':
                            probekey,
                            'parent':
                            one_value_inline(ver.parent),
                            'probeexecutable':
                            one_value_inline(ver.probeexecutable),
                            'config':
                            two_value_inline(ver.config),
                            'attribute':
                            two_value_inline(ver.attribute),
                            'dependency':
                            two_value_inline(ver.dependency),
                            'flags':
                            two_value_inline(ver.flags),
                            'files':
                            two_value_inline(ver.files),
                            'parameter':
                            two_value_inline(ver.parameter),
                            'fileparameter':
                            two_value_inline(ver.fileparameter)
                        }

                    results.append(
                        dict(id=ver.id,
                             object_repr=ver.__str__(),
                             fields=fields,
                             user=ver.version_user,
                             date_created=datetime.datetime.strftime(
                                 ver.date_created, '%Y-%m-%d %H:%M:%S'),
                             comment=new_comment(ver.version_comment),
                             version=version))

                results = sorted(results, key=lambda k: k['id'], reverse=True)
                return Response(results)

        else:
            vers = history_model[obj].objects.all()
            results = sorted([ver.__str__() for ver in vers], key=str.lower)

            return Response(results)
Example #4
0
    def get(self, request, obj, name=None):
        models = {
            'metric': poem_models.Metric,
            'metricprofile': poem_models.MetricProfiles,
            'aggregationprofile': poem_models.Aggregation,
            'thresholdsprofile': poem_models.ThresholdsProfiles
        }

        ct = ContentType.objects.get_for_model(models[obj])

        if name:
            try:
                obj = models[obj].objects.get(name=name)
            except models[obj].DoesNotExist:
                if obj.endswith('profile'):
                    ind = obj.find('profile')
                    msg = '{} profile not found.'.format(
                        obj[0:ind].capitalize())
                else:
                    msg = '{} not found.'.format(obj.capitalize())

                raise NotFound(status=404, detail=msg)

            vers = poem_models.TenantHistory.objects.filter(
                object_id=obj.id, content_type=ct).order_by('date_created')

            if vers.count() == 0:
                raise NotFound(status=404, detail='Version not found.')

            else:
                results = []
                for ver in vers:
                    version = datetime.datetime.strftime(
                        ver.date_created, '%Y%m%d-%H%M%S')
                    fields0 = json.loads(ver.serialized_data)[0]['fields']

                    if isinstance(obj, poem_models.Metric):
                        if fields0['probekey']:
                            probeversion = '{} ({})'.format(
                                fields0['probekey'][0], fields0['probekey'][1])
                        else:
                            probeversion = ''

                        fields = {
                            'name':
                            fields0['name'],
                            'mtype':
                            fields0['mtype'][0],
                            'group':
                            fields0['group'][0],
                            'probeversion':
                            probeversion,
                            'parent':
                            one_value_inline(fields0['parent']),
                            'probeexecutable':
                            one_value_inline(fields0['probeexecutable']),
                            'config':
                            two_value_inline(fields0['config']),
                            'attribute':
                            two_value_inline(fields0['attribute']),
                            'dependancy':
                            two_value_inline(fields0['dependancy']),
                            'flags':
                            two_value_inline(fields0['flags']),
                            'files':
                            two_value_inline(fields0['files']),
                            'parameter':
                            two_value_inline(fields0['parameter']),
                            'fileparameter':
                            two_value_inline(fields0['fileparameter'])
                        }

                    elif isinstance(obj, poem_models.MetricProfiles):
                        mi = [{
                            'service': item[0],
                            'metric': item[1]
                        } for item in fields0['metricinstances']]
                        fields = {
                            'name':
                            fields0['name'],
                            'groupname':
                            fields0['groupname'],
                            'description':
                            fields0.get('description', ''),
                            'apiid':
                            fields0['apiid'],
                            'metricinstances':
                            sorted(mi, key=lambda k: k['service'].lower())
                        }

                    else:
                        fields = fields0

                    try:
                        comment = []
                        untracked_fields = [
                            'mtype', 'parent', 'probeexecutable', 'attribute',
                            'dependancy', 'flags', 'files', 'parameter',
                            'fileparameter'
                        ]
                        if isinstance(obj, poem_models.Metric):
                            untracked_fields.append('name')

                        for item in json.loads(ver.comment):
                            if 'changed' in item:
                                action = 'changed'

                            elif 'added' in item:
                                action = 'added'

                            else:
                                action = 'deleted'

                            if 'object' not in item[action]:
                                new_fields = []
                                for field in item[action]['fields']:
                                    if field not in untracked_fields:
                                        new_fields.append(field)

                                if new_fields:
                                    comment.append(
                                        {action: {
                                            'fields': new_fields
                                        }})

                            else:
                                if item[action]['fields'][0] not in \
                                        untracked_fields:
                                    if item[action]['fields'][0] == 'config':
                                        if 'path' in item[action]['object']:
                                            item[action]['object'].remove(
                                                'path')
                                    comment.append(item)

                        comment = json.dumps(comment)

                    except json.JSONDecodeError:
                        comment = ver.comment

                    results.append(
                        dict(id=ver.id,
                             object_repr=ver.object_repr,
                             fields=fields,
                             user=ver.user,
                             date_created=datetime.datetime.strftime(
                                 ver.date_created, '%Y-%m-%d %H:%M:%S'),
                             comment=new_comment(comment),
                             version=version))

                results = sorted(results, key=lambda k: k['id'], reverse=True)
                return Response(results)

        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)
Example #5
0
    def get(self, request, name=None):
        public_tenant = None
        importable = None
        if name:
            metrictemplates = admin_models.MetricTemplate.objects.filter(
                name=name)
            if metrictemplates.count() == 0:
                raise NotFound(status=404, detail='Metric template not found')
        else:
            public_tenant = Tenant.objects.get(
                schema_name=get_public_schema_name())
            metrictemplates = admin_models.MetricTemplate.objects.all()
            if request.tenant != public_tenant:
                avail_metrics = Metric.objects.all().values_list('name',
                                                                 flat=True)
                importable = dict()
                for metrictemplate in metrictemplates:
                    if metrictemplate.name in avail_metrics:
                        importable[metrictemplate.name] = False
                    else:
                        importable[metrictemplate.name] = True

        results = []
        for metrictemplate in metrictemplates:
            config = two_value_inline(metrictemplate.config)
            parent = one_value_inline(metrictemplate.parent)
            probeexecutable = one_value_inline(metrictemplate.probeexecutable)
            attribute = two_value_inline(metrictemplate.attribute)
            dependency = two_value_inline(metrictemplate.dependency)
            flags = two_value_inline(metrictemplate.flags)
            files = two_value_inline(metrictemplate.files)
            parameter = two_value_inline(metrictemplate.parameter)
            fileparameter = two_value_inline(metrictemplate.fileparameter)

            ostag = []
            if metrictemplate.probekey:
                for repo in metrictemplate.probekey.package.repos.all():
                    ostag.append(repo.tag.name)

            tags = []
            for tag in metrictemplate.tags.all():
                tags.append(tag.name)

            if metrictemplate.probekey:
                probeversion = metrictemplate.probekey.__str__()
            else:
                probeversion = ''

            if not name and request.tenant != public_tenant:
                results.append(
                    dict(id=metrictemplate.id,
                         name=metrictemplate.name,
                         importable=importable[metrictemplate.name],
                         mtype=metrictemplate.mtype.name,
                         ostag=ostag,
                         tags=sorted(tags),
                         probeversion=probeversion,
                         description=metrictemplate.description,
                         parent=parent,
                         probeexecutable=probeexecutable,
                         config=config,
                         attribute=attribute,
                         dependency=dependency,
                         flags=flags,
                         files=files,
                         parameter=parameter,
                         fileparameter=fileparameter))
            else:
                results.append(
                    dict(id=metrictemplate.id,
                         name=metrictemplate.name,
                         mtype=metrictemplate.mtype.name,
                         ostag=ostag,
                         tags=sorted(tags),
                         probeversion=probeversion,
                         description=metrictemplate.description,
                         parent=parent,
                         probeexecutable=probeexecutable,
                         config=config,
                         attribute=attribute,
                         dependency=dependency,
                         flags=flags,
                         files=files,
                         parameter=parameter,
                         fileparameter=fileparameter))

        results = sorted(results, key=lambda k: k['name'])

        if name:
            del results[0]['ostag']
            return Response(results[0])
        else:
            return Response(results)