Merge lp:~jderose/dmedia/project-stats into lp:dmedia

Proposed by Jason Gerard DeRose
Status: Merged
Merged at revision: 476
Proposed branch: lp:~jderose/dmedia/project-stats
Merge into: lp:dmedia
Diff against target: 179 lines (+48/-24)
4 files modified
dmedia/core.py (+27/-11)
dmedia/schema.py (+10/-0)
dmedia/tests/test_core.py (+5/-3)
ui/dmedia.js (+6/-10)
To merge this branch: bzr merge lp:~jderose/dmedia/project-stats
Reviewer Review Type Date Requested Status
James Raymond Approve
Review via email: mp+130232@code.launchpad.net

Description of the change

* Changes dmedia/project schema to include doc.count, doc.bytes

* Changes Importer.on_items() (in dmedia.js) to use count and bytes from project directory docs rather than hitting a view in each project database

* Changes Core.init_project_views() to update the doc.count, doc.type in the project doc in the project database, and also to sync the changes into the project directory doc in dmedia-0 if needed

To post a comment you must log in.
Revision history for this message
James Raymond (jamesmr) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'dmedia/core.py'
2--- dmedia/core.py 2012-10-13 15:50:03 +0000
3+++ dmedia/core.py 2012-10-17 21:36:22 +0000
4@@ -96,8 +96,8 @@
5 log.info('** %.2f to snapshot all database', time.time() - start)
6
7
8-def projects_iter(env):
9- server = Server(env)
10+def projects_iter(server):
11+ assert isinstance(server, Server)
12 for name in server.get('_all_dbs'):
13 if name.startswith('_'):
14 continue
15@@ -107,12 +107,21 @@
16 yield (name, _id)
17
18
19+def update_count_and_bytes(db, doc):
20+ stats = db.view('user', 'bytes', reduce=True)['rows'][0]['value']
21+ if doc.get('count') != stats['count'] or doc.get('bytes') != stats['sum']:
22+ doc['count'] = stats['count']
23+ doc['bytes'] = stats['sum']
24+ db.save(doc)
25+
26+
27 class Core:
28 def __init__(self, env, private=None, shared=None):
29 self.env = env
30 self._private = (PRIVATE if private is None else private)
31 self._shared = (SHARED if shared is None else shared)
32 self.db = util.get_db(env, init=True)
33+ self.server = self.db.server()
34 self.ms = MetaStore(self.db)
35 self.stores = LocalStores()
36 self.queue = Queue()
37@@ -201,21 +210,28 @@
38 self.thread = start_thread(self._background_worker)
39
40 def init_project_views(self):
41+ start = time.time()
42 try:
43- for (name, _id) in projects_iter(self.env):
44- db = util.get_project_db(_id, self.env, True)
45+ self.db.view('project', 'atime')
46+ log.info('%.3f to prep project/atime view', time.time() - start)
47+ for (name, _id) in projects_iter(self.server):
48+ pdb = util.get_project_db(_id, self.env, True)
49+ pdoc = pdb.get(_id, attachments=True)
50+ update_count_and_bytes(pdb, pdoc)
51+ del pdoc['_rev']
52 try:
53- doc = self.db.get(_id)
54+ doc = self.db.get(_id, attachments=True)
55+ rev = doc.pop('_rev')
56+ if doc != pdoc:
57+ log.info('syncing project metadata for %s', _id)
58+ pdoc['_rev'] = rev
59+ self.db.save(pdoc)
60 except NotFound:
61 log.info('missing project doc for %s', _id)
62- doc = db.get(_id)
63- del doc['_rev']
64- self.db.save(doc)
65+ self.db.save(pdoc)
66+ log.info('%.3f for Core.init_project_views() to complete', time.time() - start)
67 except Exception:
68 log.exception('Error in Core.init_project_views():')
69- log.info('prepping project/atime view...')
70- self.db.view('project', 'atime')
71- log.info('Core.init_project_views() complete')
72
73 def snapshot(self, dumpdir, dbname):
74 start_process(snapshot_db, self.env, dumpdir, dbname)
75
76=== modified file 'dmedia/schema.py'
77--- dmedia/schema.py 2012-08-13 02:04:32 +0000
78+++ dmedia/schema.py 2012-10-17 21:36:22 +0000
79@@ -960,6 +960,8 @@
80 ... 'time': 1234567890,
81 ... 'db_name': 'dmedia-0-hb6ysckay27kiwutwkgkctni',
82 ... 'title': 'UDS-P',
83+ ... 'count': 42,
84+ ... 'bytes': 22020096000,
85 ... }
86 ...
87 >>> check_project(doc)
88@@ -976,6 +978,12 @@
89 (_equals, project_db_name(doc['_id'])),
90 )
91 _check(doc, ['title'], str),
92+ _check(doc, ['count'], int,
93+ (_at_least, 0),
94+ )
95+ _check(doc, ['count'], int,
96+ (_at_least, 0),
97+ )
98
99
100 def project_db_name(_id):
101@@ -1024,6 +1032,8 @@
102 'atime': ts,
103 'db_name': project_db_name(_id),
104 'title': title,
105+ 'count': 0,
106+ 'bytes': 0,
107 }
108
109
110
111=== modified file 'dmedia/tests/test_core.py'
112--- dmedia/tests/test_core.py 2012-10-10 22:47:53 +0000
113+++ dmedia/tests/test_core.py 2012-10-17 21:36:22 +0000
114@@ -42,14 +42,14 @@
115
116 class TestCouchFunctions(CouchCase):
117 def test_projects_iter(self):
118- self.assertEqual(list(core.projects_iter(self.env)), [])
119+ server = microfiber.Server(self.env)
120+ self.assertEqual(list(core.projects_iter(server)), [])
121 ids = tuple(random_id() for i in range(20))
122- server = microfiber.Server(self.env)
123 for _id in ids:
124 db_name = project_db_name(_id)
125 server.put(None, db_name)
126 self.assertEqual(
127- list(core.projects_iter(self.env)),
128+ list(core.projects_iter(server)),
129 [(project_db_name(_id), _id) for _id in sorted(ids)]
130 )
131
132@@ -59,6 +59,8 @@
133 inst = core.Core(self.env)
134 self.assertIsInstance(inst.db, microfiber.Database)
135 self.assertEqual(inst.db.name, DB_NAME)
136+ self.assertIsInstance(inst.server, microfiber.Server)
137+ self.assertIs(inst.db.ctx, inst.server.ctx)
138 self.assertIsInstance(inst.stores, LocalStores)
139 self.assertEqual(inst.local, {'_id': '_local/dmedia', 'stores': {}})
140
141
142=== modified file 'ui/dmedia.js'
143--- ui/dmedia.js 2012-08-21 18:21:45 +0000
144+++ ui/dmedia.js 2012-10-17 21:36:22 +0000
145@@ -106,20 +106,16 @@
146 Importer.prototype = {
147 load_items: function() {
148 var callback = $bind(this.on_items, this);
149- db.view(callback, 'project', 'title');
150+ db.view(callback, 'project', 'title', {'include_docs': true});
151 },
152
153 on_items: function(req) {
154 this.items.replace(req.read().rows,
155 function(row, items) {
156- var pdb = new couch.Database("dmedia-0-" + row.id.toLowerCase());
157- try{
158- var filecount = pdb.view_sync('doc', 'type', {key: 'dmedia/file'}).rows[0].value;
159- }
160- catch(e){
161- var filecount = 0;
162- }
163-
164+ var doc = row.doc;
165+ var count = doc.count ? doc.count : 0;
166+ var bytes = doc.bytes ? doc.bytes : 0;
167+
168 var li = $el('li', {'class': 'project', 'id': row.id});
169
170 var thumb = $el('div', {'class': 'thumbnail'});
171@@ -135,7 +131,7 @@
172 );
173
174 info.appendChild(
175- $el('p', {'textContent': filecount + ' files'})
176+ $el('p', {'textContent': count_n_size(count, bytes)})
177 );
178
179 li.appendChild(thumb);

Subscribers

People subscribed via source and target branches