<commit_before># -*- coding=utf -*-
from ...model import *
from ...browser import *
from ...stores import Store
from ...errors import *
from .mixpanel import *
from string import capwords

DIMENSION_COUNT_LIMIT = 100

time_dimension_md = {
    "name": "time",
    "levels": ["year", "month", "day", "hour"],
    "hierarchies": [
        {"name":"mdh", "levels": ["year", "month", "day", "hour"]}
    ]
}

_time_dimension = create_dimension(time_dimension_md)

class MixpanelModelProvider(ModelProvider):
    def cube(self, name):
        result = self.store.request(["events", "properties", "top"],
                            {"event":name, "limit":DIMENSION_COUNT_LIMIT})
        if not result:
            raise NoSuchCubeError(name)

        dims = result.keys()
        dims.append("time")

        measures = attribute_list(["total", "uniques"])

        cube = Cube(name=name,
                    measures=measures,
                    required_dimensions=dims,
                    store=self.store_name)

        # TODO: this is new (remove this comment)
        cube.category = self.store.category

        # TODO: required_drilldown might be a cube's attribute (fixed_dd?)
        cube.info = {
                    "required_drilldown": "date",
                    "category": cube.category
                }

        return cube

    def dimension(self, name):
        if name == "time":
            return _time_dimension

        level = Level(name, attribute_list([name]))
        dim = Dimension(name,
                         levels=[level])

        return dim

    def list_cubes(self):
        result = self.store.request(["events", "names"],
                                    {"type":"general", })
        cubes = []

        for name in result:
            label = capwords(name.replace("_", " "))
            cube = {
                    "name": name,
                    "label": label
                    }
            cubes.append(cube)

        return cubes

class MixpanelStore(Store):
    def __init__(self, api_key, api_secret, category=None):
        self.mixpanel = Mixpanel(api_key, api_secret)
        self.category = category or "Mixpanel"

    def model_provider_name(self):
        return "mixpanel"

    def request(self, *args, **kwargs):
        """Performs a mixpanel HTTP request. Raises a BackendError when
        mixpanel returns `error` in the response."""

        response = self.mixpanel.request(*args, **kwargs)

        if "error" in response:
            raise BackendError("Mixpanel request error: %s" % response["error"])

        return response
<commit_msg>replace $ with _ in dimension names<commit_after># -*- coding=utf -*-
from ...model import *
from ...browser import *
from ...stores import Store
from ...errors import *
from .mixpanel import *
from string import capwords

DIMENSION_COUNT_LIMIT = 100

time_dimension_md = {
    "name": "time",
    "levels": ["year", "month", "day", "hour"],
    "hierarchies": [
        {"name":"mdh", "levels": ["year", "month", "day", "hour"]}
    ]
}

_time_dimension = create_dimension(time_dimension_md)

class MixpanelModelProvider(ModelProvider):
    def cube(self, name):
        result = self.store.request(["events", "properties", "top"],
                            {"event":name, "limit":DIMENSION_COUNT_LIMIT})
        if not result:
            raise NoSuchCubeError(name)

        names = result.keys()
        # Replace $ with underscore _
        dims = [dim_name.replace("$", "_") for dim_name in names]
        dims.append("time")

        measures = attribute_list(["total", "uniques"])

        cube = Cube(name=name,
                    measures=measures,
                    required_dimensions=dims,
                    store=self.store_name)

        # TODO: this is new (remove this comment)
        cube.category = self.store.category

        # TODO: required_drilldown might be a cube's attribute (fixed_dd?)
        cube.info = {
                    "required_drilldown": "date",
                    "category": cube.category
                }

        return cube

    def dimension(self, name):
        if name == "time":
            return _time_dimension

        level = Level(name, attribute_list([name]))
        dim = Dimension(name,
                         levels=[level])

        return dim

    def list_cubes(self):
        result = self.store.request(["events", "names"],
                                    {"type":"general", })
        cubes = []

        for name in result:
            label = capwords(name.replace("_", " "))
            cube = {
                    "name": name,
                    "label": label
                    }
            cubes.append(cube)

        return cubes

class MixpanelStore(Store):
    def __init__(self, api_key, api_secret, category=None):
        self.mixpanel = Mixpanel(api_key, api_secret)
        self.category = category or "Mixpanel"

    def model_provider_name(self):
        return "mixpanel"

    def request(self, *args, **kwargs):
        """Performs a mixpanel HTTP request. Raises a BackendError when
        mixpanel returns `error` in the response."""

        response = self.mixpanel.request(*args, **kwargs)

        if "error" in response:
            raise BackendError("Mixpanel request error: %s" % response["error"])

        return response

{'fraction_non_alphanumeric': 0.1226890756302521, 'fraction_numerical': 0.0011204481792717086, 'mean_word_length': 1.9755555555555555, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5373496', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1461, 'n_words': 375}