forked from Code4HR/va-circuit-court-search
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sandbox.py
114 lines (105 loc) · 3 KB
/
sandbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import pymongo
from bson.son import SON
from pprint import pprint
client = pymongo.MongoClient(os.environ['MONGO_URI'])
db = client.va_circuit_court_cases
def num_cases_per_month_by_court():
return db.criminal_cases.aggregate([
{'$group':{
'_id': {
'Court': '$Court',
'year': {'$year': '$OffenseDate'},
'month': {'$month': '$OffenseDate'}
},
'count': {'$sum': 1}
}},
{'$match' : { '_id.year' : 2014 } },
{'$sort': SON([
('_id.Court', 1),
('_id.year', 1),
('_id.month', 1)
])},
{'$group':{
'_id': {
'Court': '$_id.Court'
},
'data': {'$push': {
'month': '$_id.month',
'year': '$_id.year',
'count': '$count'
}}
}},
{'$sort': SON([
('_id.Court', 1)
])}
])
def crime_type():
return db.criminal_cases.aggregate([
{'$group':{
'_id': {
'Charge': '$Charge'
},
'count': {'$sum': 1}
}},
{'$sort': SON([
('count', -1)
])},
{'$limit': 10}
])
def charges_by_race():
return db.criminal_cases.aggregate([
{'$group':{
'_id': {
'CodeSection': '$CodeSection',
'Race': '$Race'
},
'charge': {'$first': '$Charge'},
'avgSentenceTime': {'$avg': '$SentenceTimeDays'},
'maxSentenceTime': {'$max': '$SentenceTimeDays'},
'minSentenceTime': {'$min': '$SentenceTimeDays'},
'count': {'$sum': 1}
}},
{'$match' : {
'count' : {'$gt' : 10},
'avgSentenceTime': {'$gt': 0.0}
}},
{'$sort': SON([
('_id.CodeSection', 1)
])}
])
def sentence_time_overview():
return db.criminal_cases.aggregate([
{'$group':{
'_id': None,
'avgSentenceTime': {'$avg': '$SentenceTimeDays'},
'maxSentenceTime': {'$max': '$SentenceTimeDays'},
'totalSentenceTime': {'$sum': '$SentenceTimeDays'},
'avgSentenceSuspended': {'$avg': '$SentenceSuspendedDays'},
'maxSentenceSuspended': {'$max': '$SentenceSuspendedDays'},
'totalSentenceSuspended': {'$sum': '$SentenceSuspendedDays'},
'count': {'$sum': 1}
}},
{'$sort': SON([
('avgSentenceSuspended', 1)
])}
])
def sandbox():
return db.case_numbers.aggregate([
{'$sort': SON([
('court', 1),
('name', 1)
])},
{'$group':{
'_id': {
'court': '$court'
},
'firstName': {'$first': '$name'},
'lastName': {'$last': '$name'},
'count': {'$sum': 1}
}},
{'$sort': SON([
('_id.court', 1)
])}
])
pprint(sandbox()['result'])