Exemplo n.º 1
0
def init_data(request):
    types = get_types()
    min_time_res = db["needs"].find({"created": {"$exists": True}}).sort("created", 1).limit(1)
    if min_time_res.count() > 0:
        min_time = min_time_res[0]["created"] - 7 * 24 * 3600
    else:
        min_time = 1325376000

    continents = copy.deepcopy(CONTINENTS)
    for continent in continents:
        for country in db["countries"].find({"continent": continent["abbrev"]}).sort("country", 1):
            if "latlng" not in country or country["latlng"] == None:
                res = geocode(country["country"])
                if res:
                    place, latlng, score = res
                    country["latlng"] = latlng
                else:
                    print "No geocode for: %s" % country["country"]
                    country["latlng"] = None
                db["countries"].save(country)

            if "area" in country and country["area"] > 0:
                pop_log = int(math.log(abs(country["area"]), 12))
            else:
                pop_log = 4

            continent["countries"].append(
                {"country": country["country"], "latlng": country["latlng"], "zoom": 10 - pop_log}
            )

    chart_data = copy.deepcopy(_chart_data())
    chart_data.append([int(time.time() * 1000), 0])
    params = dict(types=types, min_time=min_time, continents=continents, chart_data=chart_data)
    return HttpResponse(json.dumps(params), content_type="application/json")
Exemplo n.º 2
0
    def handle(self, *args, **options):
        for num in range(0, 1000):
            data = dict()
    
            data['from_num'] = num
            data['type'] = random.choice(get_types())
            data['created'] = self.random_time()
            data['_id'] = uuid.uuid4().hex
            data['words'] = [data['type'].lower()]
            
            country = self.random_country()
            
            data['country'] = country.get('iso', None)

            data['loc_input'] = '%s, %s' % (country['capital'], country['country'])
    
            res = geocode(data['loc_input'])

            if res:
                place, loc, score = res
                data.update({'loc':loc, 'loc_place':place, 'loc_score':score})

            db.needs.insert(data)        
Exemplo n.º 3
0
def map_data(request):
    req = json.loads(request.raw_post_data)
    start = int(req.get("start", 0))
    end = int(req.get("end", time.time()))
    words = set(req.get("words", "").split(","))
    if "" in words:
        words.remove("")

    if abs(time.time() - end) < 24 * 3600:
        end = time.time() + 3600

    if abs(time.time() - start) < 24 * 3600:
        start = start - 24 * 3600

    _types = req.get("types", None)
    condition = {"loc": {"$exists": True}, "created": {"$lte": end, "$gte": start}}
    if words:
        condition["words"] = {"$in": map(lambda word: word.lower().strip(), words)}
    elif _types:
        condition["type"] = {"$in": _types}
    else:
        condition["type"] = {"$exists": 1}

    # this map/reduce groups needs by location
    mapper = Code(
        """
        function() {
            var key = this.type+this.loc_place;
            emit(key, {sum:1, type:this.type, loc:this.loc, loc_place:this.loc_place, country: this.country});
        }
    """
    )

    reducer = Code(
        """
        function(key, values) {
            var sum = 0.0;
            for (var i = 0; i < values.length; i++) {
                sum += values[i].sum;
            }
            return {sum: sum, type: values[0].type, loc: values[0].loc, loc_place: values[0].loc_place, country: values[0].country}
        }
    """
    )

    # this map/reduce picks the highest needs for a location
    mapper2 = Code(
        """
        function() {
            var key = this.value.loc_place;
            emit(this.value.loc_place, this.value);
        }
    """
    )

    reducer2 = Code(
        """
        function(key, values) {
            var max = values[0];
            for (var i=1; i<values.length; ++i) {
                if (max.sum < values[i].sum) {
                    max = values[i];
                } 
            }
            return max;
        }
    """
    )

    tmp = db["needs"].map_reduce(mapper, reducer, "tmp_%s" % uid_gen(), query=condition)
    res = tmp.map_reduce(mapper2, reducer2, "tmp_%s" % uid_gen())
    locs = []
    for doc in res.find():
        locs.append(doc["value"])

    data = {}
    for loc in locs:
        value = int(math.log(loc["sum"], 10)) + 1
        if value > 5:
            value = 5
        loc["value"] = value

        if "type" in loc and loc["type"]:
            _type = loc["type"].title()
        else:
            _type = "Other"
            loc["type"] = "Other"

        if _type not in data:
            data[_type] = list()
        data[_type].append(loc)

    tmp.drop()
    res.drop()

    return HttpResponse(json.dumps(dict(types=get_types(), data=data)), content_type="application/json")