-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonthly_visits.py
42 lines (32 loc) · 1.19 KB
/
monthly_visits.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
'''Usage:
python monthly_visits.py <project_label>
This is sort of a contrived example meant to display different features.
'''
from scitran_client import ScitranClient, query, Projects
import sys
from collections import Counter
from fnmatch import fnmatch
client = ScitranClient()
# Search for the project via label
project = client.search(query(Projects).filter(Projects.label.match(sys.argv[1])))[0]
# fetch the sessions related to this project
sessions = client.request('projects/{}/sessions'.format(project['_id'])).json()
# count session by month by taking first 7 characters of date string:
# example: 2016-01-01T00:00:00, so first 7 are 2016-01
ct = Counter(
s['timestamp'][:7]
for s in sessions
)
# logging the months and visit counts
print 'month | number of visits'
for month, count in sorted(ct.items(), reverse=True):
print month, '|', count
# Let's find an image in our project to download
acquisition, f = next(
(a, f)
for s in sessions
for a in client.request('sessions/{}/acquisitions'.format(s['_id'])).json()
for f in a['files']
if fnmatch(f['name'], '*.png')
)
client.download_file('acquisitions', acquisition['_id'], f['name'], f['hash'], dest_dir='.')