<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
The 'polyjit' experiment.

This experiment uses likwid to measure the performance of all binaries
when running with polyjit support enabled.
"""
from pprof.experiment import step, substep, RuntimeExperiment
from pprof.settings import config

from plumbum import local
from os import path

class PolyJIT(RuntimeExperiment):

    """The polyjit experiment."""

    def run_step_jit(self, p):
        """Run the experiment without likwid."""
        with step("JIT, no instrumentation"):
            p.download()
            with substep("Build"):
                p.configure()
                p.build()
            with substep("Execute {}".format(p.name)):
                def run_with_time(run_f, args, **kwargs):
                    from plumbum.cmd import time
                    from pprof.utils.run import fetch_time_output, handle_stdin

                    project_name = kwargs.get("project_name", p.name)
                    timing_tag = "PPROF-JIT: "

                    run_cmd = time["-f", timing_tag + "%U-%S-%e", run_f]
                    run_cmd = handle_stdin(run_cmd[args], kwargs)
                    _, _, stderr = run_cmd.run()

                    timings = fetch_time_output(timing_tag,
                                                timing_tag + "{:g}-{:g}-{:g}",
                                                stderr.split("\n"))
                    if len(timings) == 0:
                        return

                    super(PolyJIT, self).persist_run(str(run_cmd),
                                                     project_name,
                                                     p.run_uuid,
                                                     timings)
                p.run(run_with_time)

    def run_step_likwid(self, p):
        """Run the experiment with likwid."""
        with step("JIT, likwid"):
            p.clean()
            p.prepare()
            p.download()
            p.cflags = ["-DLIKWID_PERFMON"] + p.cflags

            with substep("Build"):
                p.configure()
                p.build()
            with substep("Execute {}".format(p.name)):
                from pprof.settings import config

                def run_with_likwid(run_f, args, **kwargs):
                    from pprof.utils.run import handle_stdin
                    from pprof.likwid import get_likwid_perfctr
                    from plumbum.cmd import rm

                    project_name = kwargs.get("project_name", p.name)
                    likwid_f = p.name + ".txt"

                    for group in ["CLOCK"]:
                        likwid_path = path.join(config["likwiddir"], "bin")
                        likwid_perfctr = local[
                            path.join(likwid_path, "likwid-perfctr")]
                        for i in range(int(config["jobs"])):
                            run_cmd = \
                                likwid_perfctr["-O", "-o", likwid_f, "-m",
                                               "-C", "0-{:d}".format(i),
                                               "-g", group, run_f]

                            run_cmd = handle_stdin(run_cmd[args], kwargs)
                            run_cmd()

                            likwid_measurement = get_likwid_perfctr(likwid_f)
                            """ Use the project_name from the binary, because we
                                might encounter dynamically generated projects.
                            """
                            self.persist_run(
                                project_name, p.run_uuid, str(run_cmd),
                                likwid_measurement)
                            rm("-f", likwid_f)
                p.run(run_with_likwid)

    def run_project(self, p):
        """
        Execute the pprof experiment.

        We perform this experiment in 2 steps:
            1. with likwid disabled.
            2. with likwid enabled.
        """
        p.ldflags = ["-lpjit", "-lgomp"]

        ld_lib_path = filter(None, config["ld_library_path"].split(":"))
        p.ldflags = ["-L" + el for el in ld_lib_path] + p.ldflags
        p.cflags = ["-Rpass=\"polyjit*\"",
                    "-Xclang", "-load",
                    "-Xclang", "LLVMPolyJIT.so",
                    "-O3",
                    "-mllvm", "-jitable",
                    "-mllvm", "-polly-delinearize=false",
                    "-mllvm", "-polly-detect-keep-going",
                    "-mllvm", "-polli"]
        with local.env(PPROF_ENABLE=0):
            self.run_step_likwid(p)
            self.run_step_jit(p)

    def persist_run(self, project_name, group, cmd, measurements):
        """ Persist all likwid results. """
        from pprof.utils import schema as s
        from pprof.utils.db import create_run

        run, session = create_run(str(cmd), project_name, self.name, group)
        for (region, name, core, value) in measurements:
            m = s.Likwid(metric=name, region=region, value=value, core=core,
                         run_id=run.id)
            session.add(m)
        session.commit()
<commit_msg>polyjit: add compilestats tracking to the experiment<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
The 'polyjit' experiment.

This experiment uses likwid to measure the performance of all binaries
when running with polyjit support enabled.
"""
from pprof.experiment import step, substep, RuntimeExperiment
from pprof.experiment import compilestats
from pprof.settings import config
from pprof.utils.schema import CompileStat

from plumbum import local
from os import path

class PolyJIT(RuntimeExperiment):

    """The polyjit experiment."""

    def run_step_compilestats(self, p):
        """ Compile the project and track the compilestats. """
        llvm_libs = path.join(config["llvmdir"], "lib")

        with step("Track Compilestats @ -O3"):
            with substep("Configure Project"):
                p.download()

                def track_compilestats(cc, **kwargs):
                    from pprof.utils.run import handle_stdin
                    new_cc = handle_stdin(cc["-mllvm", "-stats"], kwargs)

                    retcode, stdout, stderr = new_cc.run()
                    if retcode == 0:
                        stats = []
                        for stat in compilestats.get_compilestats(stderr):
                            c = CompileStat()
                            c.name = stat["desc"].rstrip()
                            c.component = stat["component"].rstrip()
                            c.value = stat["value"]
                            stats.append(c)
                        self.persist_run(str(new_cc), p.name, p.run_uuid,
                                         stats)

                p.compiler_extension = track_compilestats
                p.configure()
            with substep("Build Project"):
                p.build()

    def run_step_jit(self, p):
        """Run the experiment without likwid."""
        with step("JIT, no instrumentation"):
            p.download()
            with substep("Build"):
                p.configure()
                p.build()
            with substep("Execute {}".format(p.name)):
                def run_with_time(run_f, args, **kwargs):
                    from plumbum.cmd import time
                    from pprof.utils.run import fetch_time_output, handle_stdin

                    project_name = kwargs.get("project_name", p.name)
                    timing_tag = "PPROF-JIT: "

                    run_cmd = time["-f", timing_tag + "%U-%S-%e", run_f]
                    run_cmd = handle_stdin(run_cmd[args], kwargs)
                    _, _, stderr = run_cmd.run()

                    timings = fetch_time_output(timing_tag,
                                                timing_tag + "{:g}-{:g}-{:g}",
                                                stderr.split("\n"))
                    if len(timings) == 0:
                        return

                    super(PolyJIT, self).persist_run(str(run_cmd),
                                                     project_name,
                                                     p.run_uuid,
                                                     timings)
                p.run(run_with_time)

    def run_step_likwid(self, p):
        """Run the experiment with likwid."""
        with step("JIT, likwid"):
            p.clean()
            p.prepare()
            p.download()
            p.cflags = ["-DLIKWID_PERFMON"] + p.cflags

            with substep("Build"):
                p.configure()
                p.build()
            with substep("Execute {}".format(p.name)):
                from pprof.settings import config

                def run_with_likwid(run_f, args, **kwargs):
                    from pprof.utils.run import handle_stdin
                    from pprof.likwid import get_likwid_perfctr
                    from plumbum.cmd import rm

                    project_name = kwargs.get("project_name", p.name)
                    likwid_f = p.name + ".txt"

                    for group in ["CLOCK"]:
                        likwid_path = path.join(config["likwiddir"], "bin")
                        likwid_perfctr = local[
                            path.join(likwid_path, "likwid-perfctr")]
                        for i in range(int(config["jobs"])):
                            run_cmd = \
                                likwid_perfctr["-O", "-o", likwid_f, "-m",
                                               "-C", "0-{:d}".format(i),
                                               "-g", group, run_f]

                            run_cmd = handle_stdin(run_cmd[args], kwargs)
                            run_cmd()

                            likwid_measurement = get_likwid_perfctr(likwid_f)
                            """ Use the project_name from the binary, because we
                                might encounter dynamically generated projects.
                            """
                            self.persist_run(
                                project_name, p.run_uuid, str(run_cmd),
                                likwid_measurement)
                            rm("-f", likwid_f)
                p.run(run_with_likwid)

    def run_project(self, p):
        """
        Execute the pprof experiment.

        We perform this experiment in 2 steps:
            1. with likwid disabled.
            2. with likwid enabled.
        """
        p.ldflags = ["-lpjit", "-lgomp"]

        ld_lib_path = filter(None, config["ld_library_path"].split(":"))
        p.ldflags = ["-L" + el for el in ld_lib_path] + p.ldflags
        p.cflags = ["-Rpass=\"polyjit*\"",
                    "-Xclang", "-load",
                    "-Xclang", "LLVMPolyJIT.so",
                    "-O3",
                    "-mllvm", "-jitable",
                    "-mllvm", "-polly-delinearize=false",
                    "-mllvm", "-polly-detect-keep-going",
                    "-mllvm", "-polli"]
        with local.env(PPROF_ENABLE=0):
            self.run_step_likwid(p)
            self.run_step_jit(p)
            self.run_step_compilestats(p)

    def persist_run(self, project_name, group, cmd, measurements):
        """ Persist all likwid results. """
        from pprof.utils import schema as s
        from pprof.utils.db import create_run

        run, session = create_run(str(cmd), project_name, self.name, group)
        for (region, name, core, value) in measurements:
            m = s.Likwid(metric=name, region=region, value=value, core=core,
                         run_id=run.id)
            session.add(m)
        session.commit()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK

import argparse
from textwrap import dedent
from glob import glob

parser = argparse.ArgumentParser(
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description=dedent("""\
The toolbox frontend of the Matlab2cpp library.  Use this to try to do automatic
and semi-automatic translation.  The program will create files with the same
name as the input, but with various extra extensions.  Scripts will receive the
extension `.cpp`, headers and modules `.hpp`.  A file containing data type and
header information will be stored in a `.py` file. Any errors will be stored in
`.log`.
"""))

parser.add_argument("filename",
        help="File containing valid Matlab code.").completer=\
                lambda prefix, **kws: glob("*.m")

parser.add_argument("-t", '--tree', action="store_true",
        help="""\
Print the underlying node tree. Each line in the output represents a node and
is formated as follows:

`<codeline> <position> <class> <backend> <datatype> <name> <translation>`

The indentation represents the tree structure.
        """)

parser.add_argument("-T", "--tree-full", action="store_true",
        help="""\
Same as -t, but the full node tree, but include meta-nodes.""")

parser.add_argument("-s", '--suggest', action="store_true",
        help="""\
Automatically populate the `<filename>.py` file with datatype with suggestions
if possible.""")

parser.add_argument("-r", '--reset', action="store_true",
        help="""\
Ignore the content of `<filename>.py` and make a fresh translation.""")

parser.add_argument("-d", '--disp', action="store_true",
        help="""\
Print out the progress of the translation process.""")

parser.add_argument("-c", '--comments', action="store_true",
        help="""\
Strip away all the comments in the output of the translation.""")

parser.add_argument("-l", '--line', type=int, dest="line",
        help="Only display code related to code line number `<line>`.")
parser.add_argument("-o", '--original', action="store_true",
        help="Include original matlab code line as comment before C++ translation")

try:
    import argcomplete
    argcomplete.autocomplete(parser)
except:
    pass

if __name__ == "__main__":

    import matlab2cpp
    matlab2cpp.main(args)

<commit_msg>Added missing args creation in mconvert<commit_after>#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK

import argparse
from textwrap import dedent
from glob import glob

parser = argparse.ArgumentParser(
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description=dedent("""\
The toolbox frontend of the Matlab2cpp library.  Use this to try to do automatic
and semi-automatic translation.  The program will create files with the same
name as the input, but with various extra extensions.  Scripts will receive the
extension `.cpp`, headers and modules `.hpp`.  A file containing data type and
header information will be stored in a `.py` file. Any errors will be stored in
`.log`.
"""))

parser.add_argument("filename",
        help="File containing valid Matlab code.").completer=\
                lambda prefix, **kws: glob("*.m")

parser.add_argument("-t", '--tree', action="store_true",
        help="""\
Print the underlying node tree. Each line in the output represents a node and
is formated as follows:

`<codeline> <position> <class> <backend> <datatype> <name> <translation>`

The indentation represents the tree structure.
        """)

parser.add_argument("-T", "--tree-full", action="store_true",
        help="""\
Same as -t, but the full node tree, but include meta-nodes.""")

parser.add_argument("-s", '--suggest', action="store_true",
        help="""\
Automatically populate the `<filename>.py` file with datatype with suggestions
if possible.""")

parser.add_argument("-r", '--reset', action="store_true",
        help="""\
Ignore the content of `<filename>.py` and make a fresh translation.""")

parser.add_argument("-d", '--disp', action="store_true",
        help="""\
Print out the progress of the translation process.""")

parser.add_argument("-c", '--comments', action="store_true",
        help="""\
Strip away all the comments in the output of the translation.""")

parser.add_argument("-l", '--line', type=int, dest="line",
        help="Only display code related to code line number `<line>`.")
parser.add_argument("-o", '--original', action="store_true",
        help="Include original matlab code line as comment before C++ translation")

try:
    import argcomplete
    argcomplete.autocomplete(parser)
except:
    pass

if __name__ == "__main__":

    args = parser.parse_args()
    import matlab2cpp
    matlab2cpp.main(args)

<|endoftext|>


<commit_before>"""
Renderers are used to serialize a response into specific media types.

They give us a generic way of being able to handle various media types
on the response, such as JSON encoded data or HTML output.

REST framework also provides an HTML renderer the renders the browsable API.
"""
import copy
import string
from django import forms
from django.http.multipartparser import parse_header
from django.template import RequestContext, loader, Template
from django.utils import simplejson as json
from rest_framework.compat import yaml
from rest_framework.exceptions import ConfigurationError
from rest_framework.settings import api_settings
from rest_framework.request import clone_request
from rest_framework.utils import dict2xml
from rest_framework.utils import encoders
from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework import VERSION
from rest_framework import serializers, parsers

class BaseRenderer(object):
    """
    All renderers should extend this class, setting the `media_type`
    and `format` attributes, and override the `.render()` method.
    """

    media_type = None
    format = None

    def render(self, data, accepted_media_type=None, renderer_context=None):
        raise NotImplemented('Renderer class requires .render() to be implemented')

class JSONRenderer(BaseRenderer):
    """
    Renderer which serializes to json.
    """

    media_type = 'application/json'
    format = 'json'
    encoder_class = encoders.JSONEncoder

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Render `obj` into json.
        """
        if data is None:
            return ''

        # If 'indent' is provided in the context, then pretty print the result.
        # E.g. If we're being called by the BrowseableAPIRenderer.
        renderer_context = renderer_context or {}
        indent = renderer_context.get('indent', None)

        if accepted_media_type:
            # If the media type looks like 'application/json; indent=4',
            # then pretty print the result.
            base_media_type, params = parse_header(accepted_media_type)
            indent = params.get('indent', indent)
            try:
                indent = max(min(int(indent), 8), 0)
            except (ValueError, TypeError):
                indent = None

        return json.dumps(data, cls=self.encoder_class, indent=indent)

class JSONPRenderer(JSONRenderer):
    """
    Renderer which serializes to json,
    wrapping the json output in a callback function.
    """

    media_type = 'application/javascript'
    format = 'jsonp'
    callback_parameter = 'callback'
    default_callback = 'callback'

    def get_callback(self, renderer_context):
        """
        Determine the name of the callback to wrap around the json output.
        """
        request = renderer_context.get('request', None)
        params = request and request.GET or {}
        return params.get(self.callback_parameter, self.default_callback)

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders into jsonp, wrapping the json output in a callback function.

        Clients may set the callback function name using a query parameter
        on the URL, for example: ?callback=exampleCallbackName
        """
        renderer_context = renderer_context or {}
        callback = self.get_callback(renderer_context)
        json = super(JSONPRenderer, self).render(data, accepted_media_type,
                                                 renderer_context)
        return u"%s(%s);" % (callback, json)

class XMLRenderer(BaseRenderer):
    """
    Renderer which serializes to XML.
    """

    media_type = 'application/xml'
    format = 'xml'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders *obj* into serialized XML.
        """
        if data is None:
            return ''
        return dict2xml(data)

class YAMLRenderer(BaseRenderer):
    """
    Renderer which serializes to YAML.
    """

    media_type = 'application/yaml'
    format = 'yaml'
    encoder = encoders.SafeDumper

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders *obj* into serialized YAML.
        """
        if data is None:
            return ''

        return yaml.dump(data, stream=None, Dumper=self.encoder)

class TemplateHTMLRenderer(BaseRenderer):
    """
    An HTML renderer for use with templates.

    The data supplied to the Response object should be a dictionary that will
    be used as context for the template.

    The template name is determined by (in order of preference):

    1. An explicit `.template_name` attribute set on the response.
    2. An explicit `.template_name` attribute set on this class.
    3. The return result of calling `view.get_template_names()`.

    For example:
        data = {'users': User.objects.all()}
        return Response(data, template_name='users.html')

    For pre-rendered HTML, see StaticHTMLRenderer.
    """

    media_type = 'text/html'
    format = 'html'
    template_name = None
    exception_template_names = [
        '%(status_code)s.html',
        'api_exception.html'
    ]

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders data to HTML, using Django's standard template rendering.

        The template name is determined by (in order of preference):

        1. An explicit .template_name set on the response.
        2. An explicit .template_name set on this class.
        3. The return result of calling view.get_template_names().
        """
        renderer_context = renderer_context or {}
        view = renderer_context['view']
        request = renderer_context['request']
        response = renderer_context['response']

        if response.exception:
            template = self.get_exception_template(response)
        else:
            template_names = self.get_template_names(response, view)
            template = self.resolve_template(template_names)

        context = self.resolve_context(data, request, response)
        return template.render(context)

    def resolve_template(self, template_names):
        return loader.select_template(template_names)

    def resolve_context(self, data, request, response):
        if response.exception:
            data['status_code'] = response.status_code
        return RequestContext(request, data)

    def get_template_names(self, response, view):
        if response.template_name:
            return [response.template_name]
        elif self.template_name:
            return [self.template_name]
        elif hasattr(view, 'get_template_names'):
            return view.get_template_names()
        raise ConfigurationError('Returned a template response with no template_name')

    def get_exception_template(self, response):
        template_names = [name % {'status_code': response.status_code}
                          for name in self.exception_template_names]

        try:
            # Try to find an appropriate error template
            return self.resolve_template(template_names)
        except:
            # Fall back to using eg '404 Not Found'
            return Template('%d %s' % (response.status_code,
                                       response.status_text.title()))

# Note, subclass TemplateHTMLRenderer simply for the exception behavior
class StaticHTMLRenderer(TemplateHTMLRenderer):
    """
    An HTML renderer class that simply returns pre-rendered HTML.

    The data supplied to the Response object should be a string representing
    the pre-rendered HTML content.

    For example:
        data = 'example'
        return Response(data)

    For template rendered HTML, see TemplateHTMLRenderer.
    """
    media_type = 'text/html'
    format = 'html'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        renderer_context = renderer_context or {}
        response = renderer_context['response']

        if response and response.exception:
            request = renderer_context['request']
            template = self.get_exception_template(response)
            context = self.resolve_context(data, request, response)
            return template.render(context)

        return data

class BrowsableAPIRenderer(BaseRenderer):
    """
    HTML renderer used to self-document the API.
    """
    media_type = 'text/html'
    format = 'api'
    template = 'rest_framework/api.html'

    def get_default_renderer(self, view):
        """
        Return an instance of the first valid renderer.
        (Don't use another documenting renderer.)
        """
        renderers = [renderer for renderer in view.renderer_classes
                     if not issubclass(renderer, BrowsableAPIRenderer)]
        if not renderers:
            return None
        return renderers[0]()

    def get_content(self, renderer, data,
                    accepted_media_type, renderer_context):
        """
        Get the content as if it had been rendered by the default
        non-documenting renderer.
        """
        if not renderer:
            return '[No renderers were found]'

        renderer_context['indent'] = 4
        content = renderer.render(data, accepted_media_type, renderer_context)

        if not all(char in string.printable for char in content):
            return '[%d bytes of binary content]'

        return content

    def show_form_for_method(self, view, method, request, obj):
        """
        Returns True if a form should be shown for this method.
        """
        if not method in view.allowed_methods:
            return  # Not a valid method

        if not api_settings.FORM_METHOD_OVERRIDE:
            return  # Cannot use form overloading

        request = clone_request(request, method)
        try:
            if not view.has_permission(request, obj):
                return  # Don't have permission
        except:
            return  # Don't have permission and exception explicitly raise
        return True

    def serializer_to_form_fields(self, serializer):
        field_mapping = {
            serializers.FloatField: forms.FloatField,
            serializers.IntegerField: forms.IntegerField,
            serializers.DateTimeField: forms.DateTimeField,
            serializers.DateField: forms.DateField,
            serializers.EmailField: forms.EmailField,
            serializers.CharField: forms.CharField,
            serializers.ChoiceField: forms.ChoiceField,
            serializers.BooleanField: forms.BooleanField,
            serializers.PrimaryKeyRelatedField: forms.ChoiceField,
            serializers.ManyPrimaryKeyRelatedField: forms.MultipleChoiceField,
            serializers.SlugRelatedField: forms.ChoiceField,
            serializers.ManySlugRelatedField: forms.MultipleChoiceField,
            serializers.HyperlinkedRelatedField: forms.ChoiceField,
            serializers.ManyHyperlinkedRelatedField: forms.MultipleChoiceField
        }

        fields = {}
        for k, v in serializer.get_fields(True).items():
            if getattr(v, 'read_only', True):
                continue

            kwargs = {}
            kwargs['required'] = v.required

            #if getattr(v, 'queryset', None):
            #    kwargs['queryset'] = v.queryset

            if getattr(v, 'choices', None) is not None:
                kwargs['choices'] = v.choices

            if getattr(v, 'widget', None):
                widget = copy.deepcopy(v.widget)
                kwargs['widget'] = widget

            if getattr(v, 'default', None) is not None:
                kwargs['initial'] = v.default

            kwargs['label'] = k

            try:
                fields[k] = field_mapping[v.__class__](**kwargs)
            except KeyError:
                if getattr(v, 'choices', None) is not None:
                    fields[k] = forms.ChoiceField(**kwargs)
                else:
                    fields[k] = forms.CharField(**kwargs)
        return fields

    def get_form(self, view, method, request):
        """
        Get a form, possibly bound to either the input or output data.
        In the absence on of the Resource having an associated form then
        provide a form that can be used to submit arbitrary content.
        """
        obj = getattr(view, 'object', None)
        if not self.show_form_for_method(view, method, request, obj):
            return

        if method == 'DELETE' or method == 'OPTIONS':
            return True  # Don't actually need to return a form

        if not getattr(view, 'get_serializer', None) or not parsers.FormParser in view.parser_classes:
            media_types = [parser.media_type for parser in view.parser_classes]
            return self.get_generic_content_form(media_types)

        serializer = view.get_serializer(instance=obj)
        fields = self.serializer_to_form_fields(serializer)

        # Creating an on the fly form see:
        # http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
        OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
        data = (obj is not None) and serializer.data or None
        form_instance = OnTheFlyForm(data)
        return form_instance

    def get_generic_content_form(self, media_types):
        """
        Returns a form that allows for arbitrary content types to be tunneled
        via standard HTML forms.
        (Which are typically application/x-www-form-urlencoded)
        """

        # If we're not using content overloading there's no point in supplying a generic form,
        # as the view won't treat the form's value as the content of the request.
        if not (api_settings.FORM_CONTENT_OVERRIDE
                and api_settings.FORM_CONTENTTYPE_OVERRIDE):
            return None

        content_type_field = api_settings.FORM_CONTENTTYPE_OVERRIDE
        content_field = api_settings.FORM_CONTENT_OVERRIDE
        choices = [(media_type, media_type) for media_type in media_types]
        initial = media_types[0]

        # NB. http://jacobian.org/writing/dynamic-form-generation/
        class GenericContentForm(forms.Form):
            def __init__(self):
                super(GenericContentForm, self).__init__()

                self.fields[content_type_field] = forms.ChoiceField(
                    label='Content Type',
                    choices=choices,
                    initial=initial
                )
                self.fields[content_field] = forms.CharField(
                    label='Content',
                    widget=forms.Textarea
                )

        return GenericContentForm()

    def get_name(self, view):
        try:
            return view.get_name()
        except AttributeError:
            return view.__doc__

    def get_description(self, view):
        try:
            return view.get_description(html=True)
        except AttributeError:
            return view.__doc__

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders *obj* using the :attr:`template` set on the class.

        The context used in the template contains all the information
        needed to self-document the response to this request.
        """
        accepted_media_type = accepted_media_type or ''
        renderer_context = renderer_context or {}

        view = renderer_context['view']
        request = renderer_context['request']
        response = renderer_context['response']

        renderer = self.get_default_renderer(view)
        content = self.get_content(renderer, data, accepted_media_type, renderer_context)

        put_form = self.get_form(view, 'PUT', request)
        post_form = self.get_form(view, 'POST', request)
        delete_form = self.get_form(view, 'DELETE', request)
        options_form = self.get_form(view, 'OPTIONS', request)

        name = self.get_name(view)
        description = self.get_description(view)
        breadcrumb_list = get_breadcrumbs(request.path)

        template = loader.get_template(self.template)
        context = RequestContext(request, {
            'content': content,
            'view': view,
            'request': request,
            'response': response,
            'description': description,
            'name': name,
            'version': VERSION,
            'breadcrumblist': breadcrumb_list,
            'allowed_methods': view.allowed_methods,
            'available_formats': [renderer.format for renderer in view.renderer_classes],
            'put_form': put_form,
            'post_form': post_form,
            'delete_form': delete_form,
            'options_form': options_form,
            'api_settings': api_settings
        })

        ret = template.render(context)

        # Munge DELETE Response code to allow us to return content
        # (Do this *after* we've rendered the template so that we include
        # the normal deletion response code in the output)
        if response.status_code == 204:
            response.status_code = 200

        return ret
<commit_msg>updated to buildin status codes<commit_after>"""
Renderers are used to serialize a response into specific media types.

They give us a generic way of being able to handle various media types
on the response, such as JSON encoded data or HTML output.

REST framework also provides an HTML renderer the renders the browsable API.
"""
import copy
import string
from django import forms
from django.http.multipartparser import parse_header
from django.template import RequestContext, loader, Template
from django.utils import simplejson as json
from rest_framework.compat import yaml
from rest_framework.exceptions import ConfigurationError
from rest_framework.settings import api_settings
from rest_framework.request import clone_request
from rest_framework.utils import dict2xml
from rest_framework.utils import encoders
from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework import VERSION, status
from rest_framework import serializers, parsers

class BaseRenderer(object):
    """
    All renderers should extend this class, setting the `media_type`
    and `format` attributes, and override the `.render()` method.
    """

    media_type = None
    format = None

    def render(self, data, accepted_media_type=None, renderer_context=None):
        raise NotImplemented('Renderer class requires .render() to be implemented')

class JSONRenderer(BaseRenderer):
    """
    Renderer which serializes to json.
    """

    media_type = 'application/json'
    format = 'json'
    encoder_class = encoders.JSONEncoder

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Render `obj` into json.
        """
        if data is None:
            return ''

        # If 'indent' is provided in the context, then pretty print the result.
        # E.g. If we're being called by the BrowseableAPIRenderer.
        renderer_context = renderer_context or {}
        indent = renderer_context.get('indent', None)

        if accepted_media_type:
            # If the media type looks like 'application/json; indent=4',
            # then pretty print the result.
            base_media_type, params = parse_header(accepted_media_type)
            indent = params.get('indent', indent)
            try:
                indent = max(min(int(indent), 8), 0)
            except (ValueError, TypeError):
                indent = None

        return json.dumps(data, cls=self.encoder_class, indent=indent)

class JSONPRenderer(JSONRenderer):
    """
    Renderer which serializes to json,
    wrapping the json output in a callback function.
    """

    media_type = 'application/javascript'
    format = 'jsonp'
    callback_parameter = 'callback'
    default_callback = 'callback'

    def get_callback(self, renderer_context):
        """
        Determine the name of the callback to wrap around the json output.
        """
        request = renderer_context.get('request', None)
        params = request and request.GET or {}
        return params.get(self.callback_parameter, self.default_callback)

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders into jsonp, wrapping the json output in a callback function.

        Clients may set the callback function name using a query parameter
        on the URL, for example: ?callback=exampleCallbackName
        """
        renderer_context = renderer_context or {}
        callback = self.get_callback(renderer_context)
        json = super(JSONPRenderer, self).render(data, accepted_media_type,
                                                 renderer_context)
        return u"%s(%s);" % (callback, json)

class XMLRenderer(BaseRenderer):
    """
    Renderer which serializes to XML.
    """

    media_type = 'application/xml'
    format = 'xml'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders *obj* into serialized XML.
        """
        if data is None:
            return ''
        return dict2xml(data)

class YAMLRenderer(BaseRenderer):
    """
    Renderer which serializes to YAML.
    """

    media_type = 'application/yaml'
    format = 'yaml'
    encoder = encoders.SafeDumper

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders *obj* into serialized YAML.
        """
        if data is None:
            return ''

        return yaml.dump(data, stream=None, Dumper=self.encoder)

class TemplateHTMLRenderer(BaseRenderer):
    """
    An HTML renderer for use with templates.

    The data supplied to the Response object should be a dictionary that will
    be used as context for the template.

    The template name is determined by (in order of preference):

    1. An explicit `.template_name` attribute set on the response.
    2. An explicit `.template_name` attribute set on this class.
    3. The return result of calling `view.get_template_names()`.

    For example:
        data = {'users': User.objects.all()}
        return Response(data, template_name='users.html')

    For pre-rendered HTML, see StaticHTMLRenderer.
    """

    media_type = 'text/html'
    format = 'html'
    template_name = None
    exception_template_names = [
        '%(status_code)s.html',
        'api_exception.html'
    ]

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders data to HTML, using Django's standard template rendering.

        The template name is determined by (in order of preference):

        1. An explicit .template_name set on the response.
        2. An explicit .template_name set on this class.
        3. The return result of calling view.get_template_names().
        """
        renderer_context = renderer_context or {}
        view = renderer_context['view']
        request = renderer_context['request']
        response = renderer_context['response']

        if response.exception:
            template = self.get_exception_template(response)
        else:
            template_names = self.get_template_names(response, view)
            template = self.resolve_template(template_names)

        context = self.resolve_context(data, request, response)
        return template.render(context)

    def resolve_template(self, template_names):
        return loader.select_template(template_names)

    def resolve_context(self, data, request, response):
        if response.exception:
            data['status_code'] = response.status_code
        return RequestContext(request, data)

    def get_template_names(self, response, view):
        if response.template_name:
            return [response.template_name]
        elif self.template_name:
            return [self.template_name]
        elif hasattr(view, 'get_template_names'):
            return view.get_template_names()
        raise ConfigurationError('Returned a template response with no template_name')

    def get_exception_template(self, response):
        template_names = [name % {'status_code': response.status_code}
                          for name in self.exception_template_names]

        try:
            # Try to find an appropriate error template
            return self.resolve_template(template_names)
        except:
            # Fall back to using eg '404 Not Found'
            return Template('%d %s' % (response.status_code,
                                       response.status_text.title()))

# Note, subclass TemplateHTMLRenderer simply for the exception behavior
class StaticHTMLRenderer(TemplateHTMLRenderer):
    """
    An HTML renderer class that simply returns pre-rendered HTML.

    The data supplied to the Response object should be a string representing
    the pre-rendered HTML content.

    For example:
        data = 'example'
        return Response(data)

    For template rendered HTML, see TemplateHTMLRenderer.
    """
    media_type = 'text/html'
    format = 'html'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        renderer_context = renderer_context or {}
        response = renderer_context['response']

        if response and response.exception:
            request = renderer_context['request']
            template = self.get_exception_template(response)
            context = self.resolve_context(data, request, response)
            return template.render(context)

        return data

class BrowsableAPIRenderer(BaseRenderer):
    """
    HTML renderer used to self-document the API.
    """
    media_type = 'text/html'
    format = 'api'
    template = 'rest_framework/api.html'

    def get_default_renderer(self, view):
        """
        Return an instance of the first valid renderer.
        (Don't use another documenting renderer.)
        """
        renderers = [renderer for renderer in view.renderer_classes
                     if not issubclass(renderer, BrowsableAPIRenderer)]
        if not renderers:
            return None
        return renderers[0]()

    def get_content(self, renderer, data,
                    accepted_media_type, renderer_context):
        """
        Get the content as if it had been rendered by the default
        non-documenting renderer.
        """
        if not renderer:
            return '[No renderers were found]'

        renderer_context['indent'] = 4
        content = renderer.render(data, accepted_media_type, renderer_context)

        if not all(char in string.printable for char in content):
            return '[%d bytes of binary content]'

        return content

    def show_form_for_method(self, view, method, request, obj):
        """
        Returns True if a form should be shown for this method.
        """
        if not method in view.allowed_methods:
            return  # Not a valid method

        if not api_settings.FORM_METHOD_OVERRIDE:
            return  # Cannot use form overloading

        request = clone_request(request, method)
        try:
            if not view.has_permission(request, obj):
                return  # Don't have permission
        except:
            return  # Don't have permission and exception explicitly raise
        return True

    def serializer_to_form_fields(self, serializer):
        field_mapping = {
            serializers.FloatField: forms.FloatField,
            serializers.IntegerField: forms.IntegerField,
            serializers.DateTimeField: forms.DateTimeField,
            serializers.DateField: forms.DateField,
            serializers.EmailField: forms.EmailField,
            serializers.CharField: forms.CharField,
            serializers.ChoiceField: forms.ChoiceField,
            serializers.BooleanField: forms.BooleanField,
            serializers.PrimaryKeyRelatedField: forms.ChoiceField,
            serializers.ManyPrimaryKeyRelatedField: forms.MultipleChoiceField,
            serializers.SlugRelatedField: forms.ChoiceField,
            serializers.ManySlugRelatedField: forms.MultipleChoiceField,
            serializers.HyperlinkedRelatedField: forms.ChoiceField,
            serializers.ManyHyperlinkedRelatedField: forms.MultipleChoiceField
        }

        fields = {}
        for k, v in serializer.get_fields(True).items():
            if getattr(v, 'read_only', True):
                continue

            kwargs = {}
            kwargs['required'] = v.required

            #if getattr(v, 'queryset', None):
            #    kwargs['queryset'] = v.queryset

            if getattr(v, 'choices', None) is not None:
                kwargs['choices'] = v.choices

            if getattr(v, 'widget', None):
                widget = copy.deepcopy(v.widget)
                kwargs['widget'] = widget

            if getattr(v, 'default', None) is not None:
                kwargs['initial'] = v.default

            kwargs['label'] = k

            try:
                fields[k] = field_mapping[v.__class__](**kwargs)
            except KeyError:
                if getattr(v, 'choices', None) is not None:
                    fields[k] = forms.ChoiceField(**kwargs)
                else:
                    fields[k] = forms.CharField(**kwargs)
        return fields

    def get_form(self, view, method, request):
        """
        Get a form, possibly bound to either the input or output data.
        In the absence on of the Resource having an associated form then
        provide a form that can be used to submit arbitrary content.
        """
        obj = getattr(view, 'object', None)
        if not self.show_form_for_method(view, method, request, obj):
            return

        if method == 'DELETE' or method == 'OPTIONS':
            return True  # Don't actually need to return a form

        if not getattr(view, 'get_serializer', None) or not parsers.FormParser in view.parser_classes:
            media_types = [parser.media_type for parser in view.parser_classes]
            return self.get_generic_content_form(media_types)

        serializer = view.get_serializer(instance=obj)
        fields = self.serializer_to_form_fields(serializer)

        # Creating an on the fly form see:
        # http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
        OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
        data = (obj is not None) and serializer.data or None
        form_instance = OnTheFlyForm(data)
        return form_instance

    def get_generic_content_form(self, media_types):
        """
        Returns a form that allows for arbitrary content types to be tunneled
        via standard HTML forms.
        (Which are typically application/x-www-form-urlencoded)
        """

        # If we're not using content overloading there's no point in supplying a generic form,
        # as the view won't treat the form's value as the content of the request.
        if not (api_settings.FORM_CONTENT_OVERRIDE
                and api_settings.FORM_CONTENTTYPE_OVERRIDE):
            return None

        content_type_field = api_settings.FORM_CONTENTTYPE_OVERRIDE
        content_field = api_settings.FORM_CONTENT_OVERRIDE
        choices = [(media_type, media_type) for media_type in media_types]
        initial = media_types[0]

        # NB. http://jacobian.org/writing/dynamic-form-generation/
        class GenericContentForm(forms.Form):
            def __init__(self):
                super(GenericContentForm, self).__init__()

                self.fields[content_type_field] = forms.ChoiceField(
                    label='Content Type',
                    choices=choices,
                    initial=initial
                )
                self.fields[content_field] = forms.CharField(
                    label='Content',
                    widget=forms.Textarea
                )

        return GenericContentForm()

    def get_name(self, view):
        try:
            return view.get_name()
        except AttributeError:
            return view.__doc__

    def get_description(self, view):
        try:
            return view.get_description(html=True)
        except AttributeError:
            return view.__doc__

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        Renders *obj* using the :attr:`template` set on the class.

        The context used in the template contains all the information
        needed to self-document the response to this request.
        """
        accepted_media_type = accepted_media_type or ''
        renderer_context = renderer_context or {}

        view = renderer_context['view']
        request = renderer_context['request']
        response = renderer_context['response']

        renderer = self.get_default_renderer(view)
        content = self.get_content(renderer, data, accepted_media_type, renderer_context)

        put_form = self.get_form(view, 'PUT', request)
        post_form = self.get_form(view, 'POST', request)
        delete_form = self.get_form(view, 'DELETE', request)
        options_form = self.get_form(view, 'OPTIONS', request)

        name = self.get_name(view)
        description = self.get_description(view)
        breadcrumb_list = get_breadcrumbs(request.path)

        template = loader.get_template(self.template)
        context = RequestContext(request, {
            'content': content,
            'view': view,
            'request': request,
            'response': response,
            'description': description,
            'name': name,
            'version': VERSION,
            'breadcrumblist': breadcrumb_list,
            'allowed_methods': view.allowed_methods,
            'available_formats': [renderer.format for renderer in view.renderer_classes],
            'put_form': put_form,
            'post_form': post_form,
            'delete_form': delete_form,
            'options_form': options_form,
            'api_settings': api_settings
        })

        ret = template.render(context)

        # Munge DELETE Response code to allow us to return content
        # (Do this *after* we've rendered the template so that we include
        # the normal deletion response code in the output)
        if response.status_code == status.HTTP_204_NO_CONTENT:
            response.status_code = status.HTTP_200_OK

        return ret
<|endoftext|>


<commit_before>from ctypes import CDLL, c_char_p, c_long
from re import compile, MULTILINE

from hypothesis import given
from hypothesis.strategies import text

from not_the_sparrow import break_lines

cmark = CDLL('libcmark.dylib')

cmark_markdown_to_html = cmark.cmark_markdown_to_html
cmark_markdown_to_html.argtypes = (c_char_p, c_long, c_long)
cmark_markdown_to_html.restype = c_char_p

RE_COMMONMARK_CODE = compile('
(.*)', MULTILINE)

def commonmark(string):
    bytestring = string.encode('utf8')
    length = len(bytestring)
    return cmark_markdown_to_html(bytestring, length, 0).decode('utf8')

@given(string=text(alphabet=(' ', '\t')))
def test_break_indent_commonmark(string):
    string += 'chickens'
    a = break_lines(string)
    b = commonmark(string)

    if b.startswith('
'):
        assert not a.startswith('')
    else:
        wrapped = RE_COMMONMARK_CODE.match(b)
        assert wrapped
        assert a == '%s' % wrapped.group(1)
<commit_msg>Likeness<commit_after>from ctypes import CDLL, c_char_p, c_long
from re import compile, MULTILINE

from hypothesis import given, example
from hypothesis.strategies import text

from not_the_sparrow import break_lines

cmark = CDLL('libcmark.dylib')

cmark_markdown_to_html = cmark.cmark_markdown_to_html
cmark_markdown_to_html.argtypes = (c_char_p, c_long, c_long)
cmark_markdown_to_html.restype = c_char_p

RE_COMMONMARK_CODE = compile('
(.*)', MULTILINE)

spaces = (' ', '\t')

def commonmark(string):
    bytestring = string.encode('utf8')
    length = len(bytestring)
    return cmark_markdown_to_html(bytestring, length, 0).decode('utf8')

def assert_likeness(string):
    a = break_lines(string)
    b = commonmark(string)

    if not b:
        assert not string or string.isspace()
        return

    if b.startswith('
'):
        assert not a.startswith('')
    else:
        wrapped = RE_COMMONMARK_CODE.match(b)
        assert wrapped
        assert a == '%s' % wrapped.group(1)

@given(before=text(alphabet=spaces), after=text(alphabet=spaces))
@example(before='\t\t', after='')
def test_indent_commonmark(before, after):
    string = 'chickens'.join([before, after])
    assert_likeness(string)

@given(string=text(alphabet=('x', *spaces)))
@example(string='x'.join(4 * (4 * ' ',)))
def test_spaces_commonmark(string):
    assert_likeness(string)
<|endoftext|>


<commit_before> # -*- coding: utf-8 -*-

import datetime
import json
import pytz

from flask import (
    Blueprint, render_template
)
from tzlocal import get_localzone

from feedback.dashboard.vendorsurveys import (
    make_typeform_call, make_textit_call
)

from feedback.dashboard.permits import (
    api_health, get_lifespan, get_avg_cost,
    get_permit_types,
    get_master_permit_counts
)

blueprint = Blueprint(
    "dashboard", __name__,
    template_folder='../templates',
    static_folder="../static"
)

TYPEFORM_SCALE_EN = 'opinionscale_9825055'
TYPEFORM_SCALE_ES = 'opinionscale_9825056'

TEXTIT_UUID_EN = '920cec13-ffc0-4fe9-92c3-1cced2073498'
TEXTIT_UUID_OPINION = '53249739-7b72-43c2-9463-e4cd4963a408'

json_obj = {}
stats = {}
total = 0.0
sms_total = 0.0
sms_en = 0
sms_es = 0
surveys_by_date = {}
surveys_date_array = []
surveys_value_array = []

local_tz = get_localzone()

def utc_to_local(utc_dt):
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    return local_tz.normalize(local_dt)  # .normalize might be unnecessary

def get_typeform_by_meta(json_result):
    web_en = 0
    web_es = 0
    total = 0

    for survey_response in json_result['responses']:
        # Go through each entry in responses, and pull out opinionscale_7205022 / opinionscale_8228843, whichever is not null. Convert to integer.
        try:
            ans = survey_response['answers'][TYPEFORM_SCALE_EN]
            web_en = web_en + 1
        except KeyError:
            try:
                ans = survey_response['answers'][TYPEFORM_SCALE_ES]
                web_es = web_es + 1
            except KeyError:
                # print 'ERROR! one of these opinion scales should show up.'
                pass
        total = total + int(ans)
    return {
        "en": web_en,
        "es": web_es,
        "total": total,
        "completed": int(json_result['stats']['responses']['showing'])
    }

def get_textit_by_meta(json_result):
    sms_en = 0
    sms_es = 0
    sms_total = 0

    obj_completed = [result for result in json_result['results'] if result['completed']]
    sms_completed_responses = len(obj_completed)

    for obj in obj_completed:
        if obj['flow_uuid'] == TEXTIT_UUID_EN:
            sms_en += 1
        else:
            sms_es += 1

        # filter for the node ID of the opinion scale,
        # which is 0a77d0af-2685-4d8d-b4be-732e376f2f85
        values_array = obj['values']
        for value in values_array:
            if value['node'] == TEXTIT_UUID_OPINION and value['category'] == '1 - 7':
                try:
                    sms_total = sms_total + float(value['value'])
                except IndexError:
                    pass

    return {
        "en": sms_en,
        "es": sms_es,
        "total": sms_total,
        "completed": sms_completed_responses
    }

def get_typeform_by_date(json_result, surveys_by_date):
    for survey_response in json_result['responses']:
        # Iterate through the metadata. In the API there is a date_land field in the format of "2015-08-04 22:13:38". Parse this into our surveys_by_date array and increase these by 1.
        date_object = datetime.datetime.strptime(survey_response['metadata']['date_submit'], '%Y-%m-%d %H:%M:%S')
        surveys_by_date[date_object.strftime("%m-%d")] += 1
    return surveys_by_date

def get_textit_by_date(json_result, surveys_by_date):
    for result in json_result['results']:
        if result['completed']:
            obj = result['modified_on']
            obj = datetime.datetime.strptime(obj, '%Y-%m-%dT%H:%M:%S.%fZ')
            date_object = utc_to_local(obj)
            try:
                surveys_by_date[date_object.strftime("%m-%d")] += 1
            except KeyError:
                pass
    return surveys_by_date

for i in range(7, -1, -1):
    time_i = (datetime.date.today() - datetime.timedelta(i))
    date_index = time_i.strftime("%m-%d")
    surveys_by_date[date_index] = 0
    surveys_date_array.append(date_index)

# Get unix timestamp of a week ago
timestamp = datetime.date.today() - datetime.timedelta(7)
json_result = make_typeform_call(timestamp)

web_meta = get_typeform_by_meta(json_result)
web_date = get_typeform_by_date(json_result, surveys_by_date)

# TEXTIT API CALLS
'''
Each survey is called a "flow". For now, we will hardcode two particular flows
from textit so we can wrap our heads around how this works.
'''
sms_result = make_textit_call(timestamp)
sms_meta = get_textit_by_meta(sms_result)
sms_date = get_textit_by_date(sms_result, surveys_by_date)

# ANALYTICS CODE

try:
    rating = (web_meta['total'] + sms_meta['total']) / (web_meta['completed'] + sms_meta['completed'])
except ZeroDivisionError:
    rating = 0

for i in range(7, -1, -1):
    time_i = (datetime.date.today() - datetime.timedelta(i))
    date_index = time_i.strftime("%m-%d")
    surveys_value_array.append(surveys_by_date[date_index])

dashboard_collection = [
    {
        "id": "graph",
        "title": "Surveys Submitted - Last 7 Days",
        "data": {
            "new_reviews": web_meta['completed'] + sms_meta['completed'],
            "graph": {
                "datetime": {
                    "data": surveys_date_array
                },
                "series": [
                    {
                        "data": surveys_value_array
                    }
                ]
            }
        }
    },
    {
        "title": "Satisfaction Rating - Last 7 Days",
        "data": "{0:.2f}".format(rating)
    },
    {
        "title": "Survey Type - Last 7 Days",
        "data": {
            "web_en": web_meta['en'],
            "web_es": web_meta['es'],
            "sms_en": sms_meta['en'],
            "sms_es": sms_meta['es']
        },
        "labels": {
            "web_en": "Web (EN)",
            "web_es": "Web (ES)",
            "sms_en": "SMS (EN)",
            "sms_es": "SMS (ES)"
        }
    },
    {
        "title": "Average time from application date to permit issuance, Commercial Permits, Last 30 Days",
        "data": get_lifespan('c')
    },
    {
        "title": "Average time from application date to permit issuance, Residential Permits, Last 30 Days",
        "data": get_lifespan('r')
    },
    {
        "title": "Average time from application date to permit issuance, Owner/Builder Permits, Last 30 Days",
        "data": get_lifespan('h')
    },
    {
        "title": "Avg Cost of an Open Commercial Permit",
        "data": float(get_avg_cost('c'))/100
    },
    {
        "title": "Avg Cost of an Open Residential Permit",
        "data": float(get_avg_cost('r'))/100
    },
    {
        "title": "Avg Cost of an Owner/Builder Permit",
        "data": float(get_avg_cost('h'))/100
    },
    {
        "title": "Permits issued by type, Last 30 Days",
        "data": get_permit_types()
    },
    {
        "title": "Average age of an Open Permit (in Days)",
        "data": -1
    },
    {
        "title": "Inspections Completed, Last 30 Days",
        "data": get_master_permit_counts('last_inspection_date')
    },
    {
        "title": "Master Permits Issued, Last 30 Days",
        "data": get_master_permit_counts('permit_issued_date')
    }
]

json_obj['test'] = json.dumps(dashboard_collection[0]['data']['graph'])
json_obj['surveys_type'] = json.dumps(dashboard_collection[2])
json_obj['permits_type'] = json.dumps(dashboard_collection[9])

@blueprint.route("/", methods=["GET", "POST"])
def home():
    today = datetime.date.today()
    return render_template("public/home.html", api=api_health(), date=today.strftime('%B %d, %Y'), stats=stats, json_obj=json_obj, dash_obj=dashboard_collection, title='Dashboard')

@blueprint.route('/dashboard', methods=['GET', 'POST'])
def survey_index():
    form = {}
    return render_template('dashboard/home.html', form=form)

@blueprint.route("/edit-public/",  methods=['GET'])
def edit_public():
    return render_template("public/edit-public.html", stats=stats, json_obj=json_obj, dash_obj=dashboard_collection, title='Dashboard Editor - Public')

@blueprint.route("/edit-internal/",  methods=['GET'])
def edit_internal():
    return render_template("public/edit-internal.html", stats=stats, json_obj=json_obj, dash_obj=dashboard_collection, title='Dashboard Editor - Internal')

<commit_msg>Fixes and closes #266<commit_after> # -*- coding: utf-8 -*-

import datetime
import json
import pytz

from flask import (
    Blueprint, render_template
)
from tzlocal import get_localzone

from feedback.dashboard.vendorsurveys import (
    make_typeform_call, make_textit_call
)

from feedback.dashboard.permits import (
    api_health, get_lifespan, get_avg_cost,
    get_permit_types,
    get_master_permit_counts
)

blueprint = Blueprint(
    "dashboard", __name__,
    template_folder='../templates',
    static_folder="../static"
)

TYPEFORM_SCALE_EN = 'opinionscale_9825055'
TYPEFORM_SCALE_ES = 'opinionscale_9825056'

TEXTIT_UUID_EN = '920cec13-ffc0-4fe9-92c3-1cced2073498'
TEXTIT_UUID_OPINION = '53249739-7b72-43c2-9463-e4cd4963a408'

json_obj = {}
stats = {}
total = 0.0
sms_total = 0.0
sms_en = 0
sms_es = 0
surveys_by_date = {}
surveys_date_array = []
surveys_value_array = []

local_tz = get_localzone()

def utc_to_local(utc_dt):
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    return local_tz.normalize(local_dt)  # .normalize might be unnecessary

def get_typeform_by_meta(json_result):
    web_en = 0
    web_es = 0
    total = 0

    for survey_response in json_result['responses']:
        # Go through each entry in responses, and pull out opinionscale_7205022 / opinionscale_8228843, whichever is not null. Convert to integer.
        try:
            ans = survey_response['answers'][TYPEFORM_SCALE_EN]
            web_en = web_en + 1
        except KeyError:
            try:
                ans = survey_response['answers'][TYPEFORM_SCALE_ES]
                web_es = web_es + 1
            except KeyError:
                # print 'ERROR! one of these opinion scales should show up.'
                pass
        total = total + int(ans)
    return {
        "en": web_en,
        "es": web_es,
        "total": total,
        "completed": int(json_result['stats']['responses']['showing'])
    }

def get_textit_by_meta(json_result):
    sms_en = 0
    sms_es = 0
    sms_total = 0

    obj_completed = [result for result in json_result['results'] if result['completed']]
    sms_completed_responses = len(obj_completed)

    for obj in obj_completed:
        if obj['flow_uuid'] == TEXTIT_UUID_EN:
            sms_en += 1
        else:
            sms_es += 1

        # filter for the node ID of the opinion scale,
        # which is 0a77d0af-2685-4d8d-b4be-732e376f2f85
        values_array = obj['values']
        for value in values_array:
            if value['node'] == TEXTIT_UUID_OPINION and value['category'] == '1 - 7':
                try:
                    sms_total = sms_total + float(value['value'])
                except IndexError:
                    pass

    return {
        "en": sms_en,
        "es": sms_es,
        "total": sms_total,
        "completed": sms_completed_responses
    }

def get_typeform_by_date(json_result, surveys_by_date):
    for survey_response in json_result['responses']:
        # Iterate through the metadata. In the API there is a date_land field in the format of "2015-08-04 22:13:38". Parse this into our surveys_by_date array and increase these by 1.
        date_object = datetime.datetime.strptime(survey_response['metadata']['date_submit'], '%Y-%m-%d %H:%M:%S')
        surveys_by_date[date_object.strftime("%m-%d")] += 1
    return surveys_by_date

def get_textit_by_date(json_result, surveys_by_date):
    for result in json_result['results']:
        if result['completed']:
            obj = result['modified_on']
            obj = datetime.datetime.strptime(obj, '%Y-%m-%dT%H:%M:%S.%fZ')
            date_object = utc_to_local(obj)
            try:
                surveys_by_date[date_object.strftime("%m-%d")] += 1
            except KeyError:
                pass
    return surveys_by_date

for i in range(7, -1, -1):
    time_i = (datetime.date.today() - datetime.timedelta(i))
    date_index = time_i.strftime("%m-%d")
    surveys_by_date[date_index] = 0
    surveys_date_array.append(date_index)

# Get unix timestamp of a week ago
timestamp = datetime.date.today() - datetime.timedelta(7)
json_result = make_typeform_call(timestamp)

web_meta = get_typeform_by_meta(json_result)
web_date = get_typeform_by_date(json_result, surveys_by_date)

# TEXTIT API CALLS
'''
Each survey is called a "flow". For now, we will hardcode two particular flows
from textit so we can wrap our heads around how this works.
'''
sms_result = make_textit_call(timestamp)
sms_meta = get_textit_by_meta(sms_result)
sms_date = get_textit_by_date(sms_result, surveys_by_date)

# ANALYTICS CODE

try:
    rating = (web_meta['total'] + sms_meta['total']) / (web_meta['completed'] + sms_meta['completed'])
except ZeroDivisionError:
    rating = 0

for i in range(7, -1, -1):
    time_i = (datetime.date.today() - datetime.timedelta(i))
    date_index = time_i.strftime("%m-%d")
    surveys_value_array.append(surveys_by_date[date_index])

dashboard_collection = [
    {
        "id": "graph",
        "title": "Surveys Submitted - Last 7 Days",
        "data": {
            "new_reviews": web_meta['completed'] + sms_meta['completed'],
            "graph": {
                "datetime": {
                    "data": surveys_date_array
                },
                "series": [
                    {
                        "data": surveys_value_array
                    }
                ]
            }
        }
    },
    {
        "title": "Satisfaction Rating - Last 7 Days",
        "data": "{0:.2f}".format(rating)
    },
    {
        "title": "Survey Type - Last 7 Days",
        "data": {
            "web_en": web_meta['en'],
            "web_es": web_meta['es'],
            "sms_en": sms_meta['en'],
            "sms_es": sms_meta['es']
        },
        "labels": {
            "web_en": "Web (EN)",
            "web_es": "Web (ES)",
            "sms_en": "SMS (EN)",
            "sms_es": "SMS (ES)"
        }
    },
    {
        "title": "Average time from application date to permit issuance, Commercial Permits, Last 30 Days",
        "data": get_lifespan('c')
    },
    {
        "title": "Average time from application date to permit issuance, Residential Permits, Last 30 Days",
        "data": get_lifespan('r')
    },
    {
        "title": "Average time from application date to permit issuance, Owner/Builder Permits, Last 30 Days",
        "data": get_lifespan('h')
    },
    {
        "title": "Avg Cost of an Open Commercial Permit",
        "data": float(get_avg_cost('c'))
    },
    {
        "title": "Avg Cost of an Open Residential Permit",
        "data": float(get_avg_cost('r'))
    },
    {
        "title": "Avg Cost of an Owner/Builder Permit",
        "data": float(get_avg_cost('h'))
    },
    {
        "title": "Permits issued by type, Last 30 Days",
        "data": get_permit_types()
    },
    {
        "title": "Average age of an Open Permit (in Days)",
        "data": -1
    },
    {
        "title": "Inspections Completed, Last 30 Days",
        "data": get_master_permit_counts('last_inspection_date')
    },
    {
        "title": "Master Permits Issued, Last 30 Days",
        "data": get_master_permit_counts('permit_issued_date')
    }
]

json_obj['test'] = json.dumps(dashboard_collection[0]['data']['graph'])
json_obj['surveys_type'] = json.dumps(dashboard_collection[2])
json_obj['permits_type'] = json.dumps(dashboard_collection[9])

@blueprint.route("/", methods=["GET", "POST"])
def home():
    today = datetime.date.today()
    return render_template("public/home.html", api=api_health(), date=today.strftime('%B %d, %Y'), stats=stats, json_obj=json_obj, dash_obj=dashboard_collection, title='Dashboard')

@blueprint.route('/dashboard', methods=['GET', 'POST'])
def survey_index():
    form = {}
    return render_template('dashboard/home.html', form=form)

@blueprint.route("/edit-public/",  methods=['GET'])
def edit_public():
    return render_template("public/edit-public.html", stats=stats, json_obj=json_obj, dash_obj=dashboard_collection, title='Dashboard Editor - Public')

@blueprint.route("/edit-internal/",  methods=['GET'])
def edit_internal():
    return render_template("public/edit-internal.html", stats=stats, json_obj=json_obj, dash_obj=dashboard_collection, title='Dashboard Editor - Internal')

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012:
#    Hartmut Goebel <h.goebel@crazy-compilers.com>
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

"""
Test default values for item types.
"""

import unittest

import __import_shinken
from shinken.property import UnusedProp, none_object
import shinken.daemon

class PropertiesTester(object):

    def test_unused_properties(self):
        item = self.item # shortcut
        for name in self.unused_props:
            self.assertIn(name, item.properties,
                          msg='property %r not found in %s' % (name, self.item.my_type))
            self.assertIsInstance(item.properties[name], UnusedProp)

    def test_properties_without_default(self):
        item = self.item # shortcut
        for name in self.without_default:
            self.assertIn(name, item.properties,
                          msg='property %r not found in %s' % (name, self.item.my_type))
            self.assertIs(item.properties[name].default, none_object,
                          msg='property %r is not `none_object` but %r' % (name, item.properties[name]))
            self.assertTrue(item.properties[name].required)

    def test_default_values(self):
        item = self.item # shortcut
        for name, value in self.properties.iteritems():
            self.assertIn(name, item.properties,
                          msg='property %r not found in %s' % (name, self.item.my_type))
            self.assertEqual(item.properties[name].default, value)

    def test_all_props_are_tested(self):
        item = self.item # shortcut
        prop_names = set(list(self.properties.keys()) + self.unused_props + self.without_default)

        for name in item.properties:
            if name.startswith('$') and name.endswith('$'):
                continue
            self.assertIn(name, prop_names,
                          msg='unknown property %r found' % name)

class TestConfig(unittest.TestCase, PropertiesTester):

    unused_props = [
        'log_file', 'object_cache_file', 'precached_object_file',
        'temp_file', 'status_file', 'status_update_interval',
        'command_check_interval', 'external_command_buffer_slots',
        'check_for_updates', 'bare_update_checks',
        'retain_state_information', 'use_retained_program_state',
        'use_retained_scheduling_info',
        'retained_host_attribute_mask',
        'retained_service_attribute_mask',
        'retained_process_host_attribute_mask',
        'retained_process_service_attribute_mask',
        'retained_contact_host_attribute_mask',
        'retained_contact_service_attribute_mask', 'sleep_time',
        'service_inter_check_delay_method',
        'service_interleave_factor', 'max_concurrent_checks',
        'check_result_reaper_frequency',
        'max_check_result_reaper_time', 'check_result_path',
        'max_check_result_file_age', 'host_inter_check_delay_method',
        'free_child_process_memory', 'child_processes_fork_twice',
        'admin_email', 'admin_pager', 'event_broker_options',
        'debug_file', 'debug_level', 'debug_verbosity',
        'max_debug_file_size']

    without_default = []

    properties = dict([
        ('prefix', '/usr/local/shinken/'),
        ('workdir', ''),
        ('config_base_dir', ''),
        ('use_local_log', '1'),
        ('log_level', 20),
        ('local_log', 'arbiterd.log'),
        ('resource_file', '/tmp/ressources.txt'),
        ('shinken_user', shinken.daemon.get_cur_user()),
        ('shinken_group', shinken.daemon.get_cur_group()),
        ('enable_notifications', '1'),
        ('execute_service_checks', '1'),
        ('accept_passive_service_checks', '1'),
        ('execute_host_checks', '1'),
        ('accept_passive_host_checks', '1'),
        ('enable_event_handlers', '1'),
        ('log_rotation_method', 'd'),
        ('log_archive_path', '/usr/local/shinken/var/archives'),
        ('check_external_commands', '1'),
        ('command_file', ''),
        ('lock_file', 'arbiterd.pid'),
        ('state_retention_file', ''),
        ('retention_update_interval', '60'),
        ('use_syslog', '0'),
        ('log_notifications', '1'),
        ('log_service_retries', '1'),
        ('log_host_retries', '1'),
        ('log_event_handlers', '1'),
        ('log_initial_states', '1'),
        ('log_external_commands', '1'),
        ('log_passive_checks', '1'),
        ('global_host_event_handler', ''),
        ('global_service_event_handler', ''),
        ('max_service_check_spread', '30'),
        ('max_host_check_spread', '30'),
        ('interval_length', '60'),
        ('auto_reschedule_checks', '1'),
        ('auto_rescheduling_interval', '1'),
        ('auto_rescheduling_window', '180'),
        ('use_aggressive_host_checking', '0'),
        ('translate_passive_host_checks', '1'),
        ('passive_host_checks_are_soft', '1'),
        ('enable_predictive_host_dependency_checks', '1'),
        ('enable_predictive_service_dependency_checks', '1'),
        ('cached_host_check_horizon', '0'),
        ('cached_service_check_horizon', '0'),
        ('use_large_installation_tweaks', '0'),
        ('enable_environment_macros', '1'),
        ('enable_flap_detection', '1'),
        ('low_service_flap_threshold', '20'),
        ('high_service_flap_threshold', '30'),
        ('low_host_flap_threshold', '20'),
        ('high_host_flap_threshold', '30'),
        ('soft_state_dependencies', '0'),
        ('service_check_timeout', '60'),
        ('host_check_timeout', '30'),
        ('event_handler_timeout', '30'),
        ('notification_timeout', '30'),
        ('ocsp_timeout', '15'),
        ('ochp_timeout', '15'),
        ('perfdata_timeout', '5'),
        ('obsess_over_services', '0'),
        ('ocsp_command', ''),
        ('obsess_over_hosts', '0'),
        ('ochp_command', ''),
        ('process_performance_data', '1'),
        ('host_perfdata_command', ''),
        ('service_perfdata_command', ''),
        ('host_perfdata_file', ''),
        ('service_perfdata_file', ''),
        ('host_perfdata_file_template', '/tmp/host.perf'),
        ('service_perfdata_file_template', '/tmp/host.perf'),
        ('host_perfdata_file_mode', 'a'),
        ('service_perfdata_file_mode', 'a'),
        ('host_perfdata_file_processing_interval', '15'),
        ('service_perfdata_file_processing_interval', '15'),
        ('host_perfdata_file_processing_command', ''),
        ('service_perfdata_file_processing_command', None),
        ('check_for_orphaned_services', '1'),
        ('check_for_orphaned_hosts', '1'),
        ('check_service_freshness', '1'),
        ('service_freshness_check_interval', '60'),
        ('check_host_freshness', '1'),
        ('host_freshness_check_interval', '60'),
        ('additional_freshness_latency', '15'),
        ('enable_embedded_perl', '1'),
        ('use_embedded_perl_implicitly', '0'),
        ('date_format', None),
        ('use_timezone', ''),
        ('illegal_object_name_chars', '`~!$%^&*"|\'<>?,()='),
        ('illegal_macro_output_chars', ''),
        ('use_regexp_matching', '0'),
        ('use_true_regexp_matching', None),
        ('broker_module', ''),
        ('modified_attributes', 0L),

        # Shinken specific
        ('idontcareaboutsecurity', '0'),
        ('flap_history', '20'),
        ('max_plugins_output_length', '8192'),
        ('no_event_handlers_during_downtimes', '0'),
        ('cleaning_queues_interval', '900'),
        ('disable_old_nagios_parameters_whining', '0'),
        ('enable_problem_impacts_states_change', '0'),
        ('resource_macros_names', []),

        # SSL part
        ('use_ssl', '0'),
        ('certs_dir', 'etc/certs'),
        ('ca_cert', 'etc/certs/ca.pem'),
        ('server_cert', 'etc/certs/server.pem'),
        ('hard_ssl_name_check', '0'),

        ('human_timestamp_log', '0'),

        # Discovery part
        ('strip_idname_fqdn', '1'),
        ('runners_timeout', '3600'),
        ('pack_distribution_file', 'pack_distribution.dat'),

        # WebUI part
        ('webui_lock_file', 'webui.pid'),
        ('webui_port', '8080'),
        ('webui_host', '0.0.0.0'),
        ])

    def setUp(self):
        from shinken.objects.config import Config
        self.item = Config()

class TestCommand(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['command_name', 'command_line']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('poller_tag', 'None'),
        ('reactionner_tag', 'None'),
        ('module_type', None),
        ('timeout', '-1'),
        ])

    def setUp(self):
        from shinken.objects.command import Command
        self.item = Command()

class TestContactgroup(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['members', 'contactgroup_name', 'alias']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('unknown_members', []),
        ('id', 0),
        ])

    def setUp(self):
        from shinken.objects.contactgroup import Contactgroup
        self.item = Contactgroup()

class TestContact(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = [
        'contact_name',
        'host_notification_period', 'service_notification_period',
        'host_notification_options', 'service_notification_options',
        'host_notification_commands', 'service_notification_commands'
        ]

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('alias', 'none'),
        ('contactgroups', ''),
        ('host_notifications_enabled', '1'),
        ('service_notifications_enabled', '1'),
        ('min_business_impact', '0'),
        ('email', 'none'),
        ('pager', 'none'),
        ('address1', 'none'),
        ('address2', 'none'),
        ('address3', 'none'),
        ('address4', 'none'),
        ('address5', 'none'),
        ('address6', 'none'),
        ('can_submit_commands', '0'),
        ('is_admin', '0'),
        ('retain_status_information', '1'),
        ('notificationways', ''),
        ('password', 'NOPASSWORDSET'),
        ])

    def setUp(self):
        from shinken.objects.contact import Contact
        self.item = Contact()

class TestDiscoveryrule(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['discoveryrule_name']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('creation_type', 'service'),
        ('discoveryrule_order', '0'),
        ])

    def setUp(self):
        from shinken.objects.discoveryrule import Discoveryrule
        self.item = Discoveryrule()

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add test-suite verifying default values of properties, here: Discoveryrun.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012:
#    Hartmut Goebel <h.goebel@crazy-compilers.com>
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

"""
Test default values for item types.
"""

import unittest

import __import_shinken
from shinken.property import UnusedProp, none_object
import shinken.daemon

class PropertiesTester(object):

    def test_unused_properties(self):
        item = self.item # shortcut
        for name in self.unused_props:
            self.assertIn(name, item.properties,
                          msg='property %r not found in %s' % (name, self.item.my_type))
            self.assertIsInstance(item.properties[name], UnusedProp)

    def test_properties_without_default(self):
        item = self.item # shortcut
        for name in self.without_default:
            self.assertIn(name, item.properties,
                          msg='property %r not found in %s' % (name, self.item.my_type))
            self.assertIs(item.properties[name].default, none_object,
                          msg='property %r is not `none_object` but %r' % (name, item.properties[name]))
            self.assertTrue(item.properties[name].required)

    def test_default_values(self):
        item = self.item # shortcut
        for name, value in self.properties.iteritems():
            self.assertIn(name, item.properties,
                          msg='property %r not found in %s' % (name, self.item.my_type))
            self.assertEqual(item.properties[name].default, value)

    def test_all_props_are_tested(self):
        item = self.item # shortcut
        prop_names = set(list(self.properties.keys()) + self.unused_props + self.without_default)

        for name in item.properties:
            if name.startswith('$') and name.endswith('$'):
                continue
            self.assertIn(name, prop_names,
                          msg='unknown property %r found' % name)

class TestConfig(unittest.TestCase, PropertiesTester):

    unused_props = [
        'log_file', 'object_cache_file', 'precached_object_file',
        'temp_file', 'status_file', 'status_update_interval',
        'command_check_interval', 'external_command_buffer_slots',
        'check_for_updates', 'bare_update_checks',
        'retain_state_information', 'use_retained_program_state',
        'use_retained_scheduling_info',
        'retained_host_attribute_mask',
        'retained_service_attribute_mask',
        'retained_process_host_attribute_mask',
        'retained_process_service_attribute_mask',
        'retained_contact_host_attribute_mask',
        'retained_contact_service_attribute_mask', 'sleep_time',
        'service_inter_check_delay_method',
        'service_interleave_factor', 'max_concurrent_checks',
        'check_result_reaper_frequency',
        'max_check_result_reaper_time', 'check_result_path',
        'max_check_result_file_age', 'host_inter_check_delay_method',
        'free_child_process_memory', 'child_processes_fork_twice',
        'admin_email', 'admin_pager', 'event_broker_options',
        'debug_file', 'debug_level', 'debug_verbosity',
        'max_debug_file_size']

    without_default = []

    properties = dict([
        ('prefix', '/usr/local/shinken/'),
        ('workdir', ''),
        ('config_base_dir', ''),
        ('use_local_log', '1'),
        ('log_level', 20),
        ('local_log', 'arbiterd.log'),
        ('resource_file', '/tmp/ressources.txt'),
        ('shinken_user', shinken.daemon.get_cur_user()),
        ('shinken_group', shinken.daemon.get_cur_group()),
        ('enable_notifications', '1'),
        ('execute_service_checks', '1'),
        ('accept_passive_service_checks', '1'),
        ('execute_host_checks', '1'),
        ('accept_passive_host_checks', '1'),
        ('enable_event_handlers', '1'),
        ('log_rotation_method', 'd'),
        ('log_archive_path', '/usr/local/shinken/var/archives'),
        ('check_external_commands', '1'),
        ('command_file', ''),
        ('lock_file', 'arbiterd.pid'),
        ('state_retention_file', ''),
        ('retention_update_interval', '60'),
        ('use_syslog', '0'),
        ('log_notifications', '1'),
        ('log_service_retries', '1'),
        ('log_host_retries', '1'),
        ('log_event_handlers', '1'),
        ('log_initial_states', '1'),
        ('log_external_commands', '1'),
        ('log_passive_checks', '1'),
        ('global_host_event_handler', ''),
        ('global_service_event_handler', ''),
        ('max_service_check_spread', '30'),
        ('max_host_check_spread', '30'),
        ('interval_length', '60'),
        ('auto_reschedule_checks', '1'),
        ('auto_rescheduling_interval', '1'),
        ('auto_rescheduling_window', '180'),
        ('use_aggressive_host_checking', '0'),
        ('translate_passive_host_checks', '1'),
        ('passive_host_checks_are_soft', '1'),
        ('enable_predictive_host_dependency_checks', '1'),
        ('enable_predictive_service_dependency_checks', '1'),
        ('cached_host_check_horizon', '0'),
        ('cached_service_check_horizon', '0'),
        ('use_large_installation_tweaks', '0'),
        ('enable_environment_macros', '1'),
        ('enable_flap_detection', '1'),
        ('low_service_flap_threshold', '20'),
        ('high_service_flap_threshold', '30'),
        ('low_host_flap_threshold', '20'),
        ('high_host_flap_threshold', '30'),
        ('soft_state_dependencies', '0'),
        ('service_check_timeout', '60'),
        ('host_check_timeout', '30'),
        ('event_handler_timeout', '30'),
        ('notification_timeout', '30'),
        ('ocsp_timeout', '15'),
        ('ochp_timeout', '15'),
        ('perfdata_timeout', '5'),
        ('obsess_over_services', '0'),
        ('ocsp_command', ''),
        ('obsess_over_hosts', '0'),
        ('ochp_command', ''),
        ('process_performance_data', '1'),
        ('host_perfdata_command', ''),
        ('service_perfdata_command', ''),
        ('host_perfdata_file', ''),
        ('service_perfdata_file', ''),
        ('host_perfdata_file_template', '/tmp/host.perf'),
        ('service_perfdata_file_template', '/tmp/host.perf'),
        ('host_perfdata_file_mode', 'a'),
        ('service_perfdata_file_mode', 'a'),
        ('host_perfdata_file_processing_interval', '15'),
        ('service_perfdata_file_processing_interval', '15'),
        ('host_perfdata_file_processing_command', ''),
        ('service_perfdata_file_processing_command', None),
        ('check_for_orphaned_services', '1'),
        ('check_for_orphaned_hosts', '1'),
        ('check_service_freshness', '1'),
        ('service_freshness_check_interval', '60'),
        ('check_host_freshness', '1'),
        ('host_freshness_check_interval', '60'),
        ('additional_freshness_latency', '15'),
        ('enable_embedded_perl', '1'),
        ('use_embedded_perl_implicitly', '0'),
        ('date_format', None),
        ('use_timezone', ''),
        ('illegal_object_name_chars', '`~!$%^&*"|\'<>?,()='),
        ('illegal_macro_output_chars', ''),
        ('use_regexp_matching', '0'),
        ('use_true_regexp_matching', None),
        ('broker_module', ''),
        ('modified_attributes', 0L),

        # Shinken specific
        ('idontcareaboutsecurity', '0'),
        ('flap_history', '20'),
        ('max_plugins_output_length', '8192'),
        ('no_event_handlers_during_downtimes', '0'),
        ('cleaning_queues_interval', '900'),
        ('disable_old_nagios_parameters_whining', '0'),
        ('enable_problem_impacts_states_change', '0'),
        ('resource_macros_names', []),

        # SSL part
        ('use_ssl', '0'),
        ('certs_dir', 'etc/certs'),
        ('ca_cert', 'etc/certs/ca.pem'),
        ('server_cert', 'etc/certs/server.pem'),
        ('hard_ssl_name_check', '0'),

        ('human_timestamp_log', '0'),

        # Discovery part
        ('strip_idname_fqdn', '1'),
        ('runners_timeout', '3600'),
        ('pack_distribution_file', 'pack_distribution.dat'),

        # WebUI part
        ('webui_lock_file', 'webui.pid'),
        ('webui_port', '8080'),
        ('webui_host', '0.0.0.0'),
        ])

    def setUp(self):
        from shinken.objects.config import Config
        self.item = Config()

class TestCommand(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['command_name', 'command_line']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('poller_tag', 'None'),
        ('reactionner_tag', 'None'),
        ('module_type', None),
        ('timeout', '-1'),
        ])

    def setUp(self):
        from shinken.objects.command import Command
        self.item = Command()

class TestContactgroup(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['members', 'contactgroup_name', 'alias']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('unknown_members', []),
        ('id', 0),
        ])

    def setUp(self):
        from shinken.objects.contactgroup import Contactgroup
        self.item = Contactgroup()

class TestContact(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = [
        'contact_name',
        'host_notification_period', 'service_notification_period',
        'host_notification_options', 'service_notification_options',
        'host_notification_commands', 'service_notification_commands'
        ]

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('alias', 'none'),
        ('contactgroups', ''),
        ('host_notifications_enabled', '1'),
        ('service_notifications_enabled', '1'),
        ('min_business_impact', '0'),
        ('email', 'none'),
        ('pager', 'none'),
        ('address1', 'none'),
        ('address2', 'none'),
        ('address3', 'none'),
        ('address4', 'none'),
        ('address5', 'none'),
        ('address6', 'none'),
        ('can_submit_commands', '0'),
        ('is_admin', '0'),
        ('retain_status_information', '1'),
        ('notificationways', ''),
        ('password', 'NOPASSWORDSET'),
        ])

    def setUp(self):
        from shinken.objects.contact import Contact
        self.item = Contact()

class TestDiscoveryrule(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['discoveryrule_name']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ('creation_type', 'service'),
        ('discoveryrule_order', '0'),
        ])

    def setUp(self):
        from shinken.objects.discoveryrule import Discoveryrule
        self.item = Discoveryrule()

class TestDiscoveryrun(unittest.TestCase, PropertiesTester):

    unused_props = []

    without_default = ['discoveryrun_name', 'discoveryrun_command']

    properties = dict([
        ('imported_from', 'unknown'),
        ('use', ''),
        ('name', ''),
        ])

    def setUp(self):
        from shinken.objects.discoveryrun import Discoveryrun
        self.item = Discoveryrun()

if __name__ == '__main__':
    unittest.main()
<|endoftext|>