def _worker(q):
        try:
            while True:
                print "Queue size: ", q.qsize()
                school_id = q.get(True, 10)

                # get all students in the school
                ssa_collection = db["studentSchoolAssociation"]
                students = [
                    s["body"]["studentId"] for s in ssa_collection.find(
                        {"body.schoolId": school_id}, {
                            "_id": 0,
                            "body.studentId": 1
                        }) if s.get("body", {}).get("studentId", None)
                ]

                # print "School:   %s" % school_id
                # print "Students: %s" % len(students)

                if students:
                    # assemble a pipeline
                    cond_stmt = cond_from_bands(src_var, bands)
                    pipeline = [{
                        "$match": {
                            "_id": {
                                "$in": students
                            }
                        }
                    }, {
                        "$project": {
                            "band": cond_stmt
                        }
                    }, {
                        "$group": {
                            "_id": "$band",
                            "count": {
                                "$sum": 1
                            },
                        }
                    }]

                    result = dict([
                        (by_rank[x["_id"]]["abbreviation"], x["count"])
                        for x in db.command("aggregate",
                                            "student",
                                            pipeline=pipeline)["result"]
                    ])
                    edorg_collection.update({"_id": school_id},
                                            {"$set": {
                                                target_var: result
                                            }})
                    # print "Written: ", result

        except Empty, e:
            pass
    def _worker(q):
        try:
            while True:
                print "Queue size: ", q.qsize()
                school_id = q.get(True, 10)

                # get all students in the school
                ssa_collection = db["studentSchoolAssociation"]
                students = [
                    s["body"]["studentId"]
                    for s in ssa_collection.find({"body.schoolId": school_id}, {"_id": 0, "body.studentId": 1})
                    if s.get("body", {}).get("studentId", None)
                ]

                # print "School:   %s" % school_id
                # print "Students: %s" % len(students)

                if students:
                    # assemble a pipeline
                    cond_stmt = cond_from_bands(src_var, bands)
                    pipeline = [
                        {"$match": {"_id": {"$in": students}}},
                        {"$project": {"band": cond_stmt}},
                        {"$group": {"_id": "$band", "count": {"$sum": 1}}},
                    ]

                    result = dict(
                        [
                            (by_rank[x["_id"]]["abbreviation"], x["count"])
                            for x in db.command("aggregate", "student", pipeline=pipeline)["result"]
                        ]
                    )
                    edorg_collection.update({"_id": school_id}, {"$set": {target_var: result}})
                    # print "Written: ", result

        except Empty, e:
            pass
from  aggregatedriver import cond_from_bands
import json 

bands = [
   { "rank" :  0, "description" : "no score",         "abbreviation" : "-", "min" :  0, "max" :  6 },
   { "rank" :  1, "description" : "warning",          "abbreviation" : "W", "min" :  6, "max" : 14 },
   { "rank" :  2, "description" : "below standard",   "abbreviation" : "B", "min" : 15, "max" : 20 },
   { "rank" :  3, "description" : "at standard",      "abbreviation" : "S", "min" : 21, "max" : 27 },
   { "rank" :  4, "description" : "exceeds standard", "abbreviation" : "E", "min" : 28, "max" : 33 }
]


result = cond_from_bands("score", bands)
print "JSON:\n" + json.dumps(result)
Пример #4
0
    "min": 0,
    "max": 6
}, {
    "rank": 1,
    "description": "warning",
    "abbreviation": "W",
    "min": 6,
    "max": 14
}, {
    "rank": 2,
    "description": "below standard",
    "abbreviation": "B",
    "min": 15,
    "max": 20
}, {
    "rank": 3,
    "description": "at standard",
    "abbreviation": "S",
    "min": 21,
    "max": 27
}, {
    "rank": 4,
    "description": "exceeds standard",
    "abbreviation": "E",
    "min": 28,
    "max": 33
}]

result = cond_from_bands("score", bands)
print "JSON:\n" + json.dumps(result)