Example #1
0
File: tests.py Project: slok/dwarf
    def test_click_findall(self):
        url = "http://xlarrakoetxea.org"
        clicks = set()

        # Save
        sl = ShortLink(url=url)
        sl.save()

        # Clicks
        for i in range(random.randrange(0, 5)):
            c = Click(token=sl.token, os="linux")
            c.save()
            clicks.add(c)

        clicks2 = Click.findall(sl.token)

        self.assertEquals(len(clicks), len(clicks2))

        for i in clicks:
            clicks2_aux = set(clicks2)
            for j in clicks2_aux:
                if i == j:
                    clicks2.remove(j)

        self.assertEquals(0, len(clicks2))
Example #2
0
File: tests.py Project: slok/dwarf
    def test_click_store(self):
        token = utils.counter_to_token(random.randrange(0, 100000))
        click_id = random.randrange(0, 100000)
        OS = "linux"
        browser = "firefox"
        ip = "111.222.333.444"
        click_date = dateutils.unix_now_utc()
        language = "EN_us"
        location = "US"

        c = Click(click_id, token, ip, OS, browser, click_date, language, location)

        c.save()

        # Check the stored object
        key = Click.REDIS_CLICK_KEY.format(token, click_id)
        r = get_redis_connection()
        values = r.hgetall(key)

        self.assertEquals(OS, values["os"])
        self.assertEquals(browser, values["browser"])
        self.assertEquals(ip, values["ip"])
        self.assertEquals(click_date, int(values["click_date"]))
        self.assertEquals(language, values["language"])
        self.assertEquals(location, values["location"])
Example #3
0
File: tests.py Project: slok/dwarf
    def test_click_store_autofields(self):

        token = utils.counter_to_token(random.randrange(0, 100000))
        url = "http://xlarrakoetxea.org"
        OS = "linux"
        ip = "111.222.333.444"
        incr_times = 4

        # Store a link
        sl = ShortLink(token=token, url=url)
        sl.save()

        for i in range(incr_times):
            ShortLink.incr_clicks(token)

        c = Click(token=token, os=OS, ip=ip)

        c.save()

        # The save method has set the click_date
        click_date = c.click_date
        self.assertIsNotNone(click_date)

        # Check the stored object
        key = Click.REDIS_CLICK_KEY.format(token, c.click_id)
        r = get_redis_connection()
        values = r.hgetall(key)

        # Check the key is correctly set (this means that the counter has
        # increased correctly)
        correct_key = Click.REDIS_CLICK_KEY.format(token, incr_times + 1)
        self.assertEquals(correct_key, key)

        self.assertEquals(OS, values["os"])
        self.assertEquals(ip, values["ip"])
Example #4
0
File: tests.py Project: slok/dwarf
    def test_delete_link(self):
        counter = random.randrange(0, 100000)
        url = "xlarrakoetxea.org"

        sl = ShortLink(counter=counter, url=url)
        sl.save()

        r = get_redis_connection()

        times = random.randrange(10, 100)

        for i in range(times):
            c = Click(token=sl.token)
            c.save()

        self.assertTrue(r.exists(ShortLink.REDIS_TOKEN_KEY.format(sl.token)))
        self.assertTrue(r.sismember(ShortLink.REDIS_URL_KEY.format(sl.url),
                                    sl.token))
        self.assertEquals(times, len(r.keys(
                                Click.REDIS_CLICK_KEY.format(sl.token, "*"))))

        sl.delete()

        self.assertFalse(r.exists(ShortLink.REDIS_TOKEN_KEY.format(sl.token)))
        self.assertFalse(r.sismember(ShortLink.REDIS_URL_KEY.format(sl.url),
                                     sl.token))
        self.assertEquals(0, len(r.keys(
                                Click.REDIS_CLICK_KEY.format(sl.token, "*"))))
Example #5
0
File: views.py Project: slok/dwarf
def details(request, token):

    clicks = list(Click.findall(token=token))
    # Sort
    clicks.sort(key=lambda click: click.click_id)
    
    short_link = ShortLink.find(token=token)

    # Get data for the charts
    browsers = get_data_for_charts(clicks, "browser")
    browsers.append("Browsers")
    os = get_data_for_charts(clicks, "os")
    os.append("OS")
    country = get_data_for_charts(clicks, "location")
    country.append("Country")

    not_json_data = (browsers, os, country)

    #Serialize
    json_data = json.dumps(not_json_data)

    data = {
        'shortlink': short_link,
        'clicks': clicks,
        'json_data': json_data
    }

    return render_to_response('simple/details.html',
                            data,
                            context_instance=RequestContext(request))
Example #6
0
File: tests.py Project: slok/dwarf
    def test_click_link(self):

        data = {
            "LANG": "en_US",
            "REMOTE_ADDR": "72.14.207.99",
            "HTTP_USER_AGENT": "Mozilla/5.0 (X11; Linux x86_64; rv:17.0)" + " Gecko/17.0 Firefox/17.0",
        }

        # Prepare the database
        counter = random.randrange(0, 100000)
        url = "http://xlarrakoetxea.org"
        sl = ShortLink(counter=counter, url=url)
        sl.save()

        # Call teh task
        result = tasks.click_link.delay(sl.token, data)
        # Wait (for testing only)
        result.get()

        # Check the stored information
        c = Click.find(sl.token, 1)

        self.assertEquals("en_US", c.language)
        self.assertEquals("linux x86_64", c.os)
        self.assertEquals("firefox", c.browser)
        self.assertEquals("72.14.207.99", c.ip)
        self.assertEquals("US", c.location)
Example #7
0
File: tasks.py Project: slok/dwarf
def click_link(token, request_meta_dict):

    #Extract data
    user_agent = request_meta_dict.get('HTTP_USER_AGENT')
    data = detect_browser_and_OS(user_agent)
    ip = request_meta_dict.get('REMOTE_ADDR')
    language = request_meta_dict.get('HTTP_ACCEPT_LANGUAGE',
                                     "None").split(",")[0]
    location = detect_country_location_with_geoip(ip).get('country_code')

    #TODO: remote host

    c = Click(token=token, browser=data[1], os=data[2], ip=ip,
              language=language, location=location)

    # Metrics
    ClickMetrics().increment()
    TotalClickMetrics().increment()
    c.save()
Example #8
0
File: tests.py Project: slok/dwarf
    def test_click_find(self):
        token = utils.counter_to_token(random.randrange(0, 100000))
        OS = "linux"
        ip = "111.222.333.444"
        browser = "firefox"
        click_date = dateutils.unix_now_utc()
        language = "EN_us"
        location = "US"
        url = "http://xlarrakoetxea.org"

        sl = ShortLink(token=token, url=url)
        sl.save()

        c = Click(
            token=token, os=OS, ip=ip, browser=browser, click_date=click_date, language=language, location=location
        )
        c.save()

        c2 = Click.find(token, c.click_id)
        self.assertEquals(c, c2)
Example #9
0
File: tests.py Project: slok/dwarf
    def test_click_basic_object(self):
        token = utils.counter_to_token(random.randrange(0, 100000))
        click_id = random.randrange(0, 100000)
        OS = "linux"
        browser = "firefox"
        ip = "111.222.333.444"
        click_date = dateutils.unix_now_utc()
        language = "EN_us"
        location = "US"

        c = Click(click_id, token, ip, OS, browser, click_date, language, location)

        # Check getters
        self.assertEquals(token, c.token)
        self.assertEquals(click_id, c.click_id)
        self.assertEquals(OS, c.os)
        self.assertEquals(browser, c.browser)
        self.assertEquals(ip, c.ip)
        self.assertEquals(click_date, c.click_date)
        self.assertEquals(language, c.language)
        self.assertEquals(location, c.location)

        # Check setters
        c2 = Click()
        c2.token = token
        c2.click_id = click_id
        c2.os = OS
        c2.browser = browser
        c2.ip = ip
        c2.click_date = click_date
        c2.language = language
        c2.location = location

        self.assertEquals(token, c2.token)
        self.assertEquals(click_id, c2.click_id)
        self.assertEquals(OS, c2.os)
        self.assertEquals(browser, c2.browser)
        self.assertEquals(ip, c2.ip)
        self.assertEquals(click_date, c2.click_date)
        self.assertEquals(language, c2.language)
        self.assertEquals(location, c2.location)
Example #10
0
File: views.py Project: slok/dwarf
def links_info(request, token):

    sl = ShortLink.find(token=token)

    clicks = Click.findall(token)

    # get browsers:
    browsers = {}
    os = {}
    languages = {}
    countries = {}
    countries_map = {}  # The countries map doesn't have unknown
    dates_tmp = {}
    dates = []
    date_format = "Date({0}, {1}, {2})"

    for i in clicks:

        # Browsers
        try:
            browsers[i.browser] += 1
        except KeyError:
            browsers[i.browser] = 1

        # Operative Systems
        try:
            os[i.os] += 1
        except KeyError:
            os[i.os] = 1

        # Languages (Browser)
        try:
            languages[i.language] += 1
        except KeyError:
            languages[i.language] = 1

        # Countries
        try:
            countries[i.location] += 1

            if i.location:
                countries_map[i.location] += 1

        except KeyError:
            countries[i.location] = 1

            if i.location:
                countries_map[i.location] = 1

        # dates
        dt = unix_to_datetime(i.click_date)
        dt_str = date_format.format(dt.year, dt.month-1, dt.day)
        try:
            dates_tmp[dt_str] += 1
        except KeyError:
            dates_tmp[dt_str] = 1

    # Fill the dates until now
    now = datetime_now_utc()
    temp_date = unix_to_datetime(sl.creation_date)

    # If the date doesn't have enough days in between then create a new range
    # of dates with more days (For graph visualization)
    rel_delta = relativedelta(now, temp_date)

    if rel_delta.year == 0 and rel_delta.month == 0 and rel_delta.day < 5 or\
       rel_delta.hours < 24:
        try:
            days = MINIMUN_DAYS_FOR_CHART-rel_delta.day
        except TypeError:
            days = MINIMUN_DAYS_FOR_CHART
        now = now + relativedelta(days=days)

    while (temp_date.day != now.day or
            temp_date.month != now.month or
            temp_date.year != now.year):

        dt_str = date_format.format(temp_date.year,
                                    temp_date.month-1,
                                    temp_date.day)
        try:
            dates.append((dt_str, dates_tmp[dt_str]))
        except KeyError:
            dates.append((dt_str, 0))

        temp_date += relativedelta(days=1)

    # Change None for unknown for the countries
    try:
        countries[_("Unknown")] = countries[None]
        del countries[None]
    except KeyError:
        pass

    context = {
        'browser_data': pie_chart_json_transform("Browsers", browsers),
        'os_data': pie_chart_json_transform("Operative systems", os),
        'languages_data': pie_chart_json_transform("Languages", languages),
        'countries_data': pie_chart_json_transform("Countries", countries),
        'countries_map_data': pie_chart_json_transform("Countries", countries_map),
        'dates_data': single_linechart_json_transform_with_list("Clicks", "Days", dates),
        'short_link': sl
    }

    return render_to_response('links/link_info.html',
                              context,
                              context_instance=RequestContext(request))