from ...model import *
from ...browser import *
from ...stores import Store
from .mixpanel import *

DIMENSION_COUNT_LIMIT = 100

time_dimension_md = {
    "name": "time",
    "levels": ["month", "week", "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:
            cube = {
                    "name": name,
                    "label": name
                    }
            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):
        return self.mixpanel.request(*args, **kwargs)

@@ -1,16 +1,37 @@
+# -*- coding=utf -*-%0A
 from ...model im
@@ -100,25 +100,77 @@
 om .
-mixpanel import *
+..errors import *%0Afrom .mixpanel import *%0Afrom string import capwords
 %0A%0ADI
@@ -253,16 +253,24 @@
 vels%22: %5B
+%22year%22, 
 %22month%22,
@@ -275,12 +275,92 @@
 %22, %22
-week
+day%22, %22hour%22%5D,%0A    %22hierarchies%22: %5B%0A        %7B%22name%22:%22mdh%22, %22levels%22: %5B%22year%22, %22month
 %22, %22
@@ -364,24 +364,31 @@
 , %22day%22,
+ 
 %22hour%22%5D
-,
+%7D%0A    %5D
 %0A%7D%0A%0A_tim
@@ -1717,16 +1717,17 @@
 es = %5B%5D%0A
+%0A
         
@@ -1738,32 +1738,85 @@
 name in result:%0A
+            label = capwords(name.replace(%22_%22, %22 %22))%0A
             cube
@@ -1883,20 +1883,21 @@
 label%22: 
-name
+label
 %0A       
@@ -2269,50 +2269,319 @@
     
-return self.mixpanel.request(*args, **kwargs)
+%22%22%22Performs a mixpanel HTTP request. Raises a BackendError when%0A        mixpanel returns %60error%60 in the response.%22%22%22%0A%0A        response = self.mixpanel.request(*args, **kwargs)%0A%0A        if %22error%22 in response:%0A            raise BackendError(%22Mixpanel request error: %25s%22 %25 response%5B%22error%22%5D)%0A%0A        return response
 %0A

raise exception on response error; updated model to contain year; nicer cube label