commit eb83c48513d73cccbb88b741435cb2f06bcbdea1
Author: Sofia Papagiannaki <spapagian@grnet.gr>
Date:   Thu Oct 10 17:41:44 2013 +0300

    pithos: Update reconcile-resources-pithos command

diff --git a/snf-pithos-backend/pithos/backends/lib/sqlalchemy/node.py b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/node.py
index 4b1cfdb..fccb2ad 100644
--- a/snf-pithos-backend/pithos/backends/lib/sqlalchemy/node.py
+++ b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/node.py
@@ -1,4 +1,4 @@
-# Copyright 2011-2012 GRNET S.A. All rights reserved.
+# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
 # without modification, are permitted provided that the following
@@ -34,6 +34,7 @@
 from time import time
 from operator import itemgetter
 from itertools import groupby
+from collections import defaultdict
 
 from sqlalchemy import (Table, Integer, BigInteger, DECIMAL, Boolean,
                         Column, String, MetaData, ForeignKey)
@@ -45,7 +46,7 @@ from dbworker import DBWorker, ESCAPE_CHAR
 
 from pithos.backends.filter import parse_filters
 
-
+DEFAULT_DISKSPACE_RESOURCE = 'pithos.diskspace'
 ROOTNODE = 0
 
 (SERIAL, NODE, HASH, SIZE, TYPE, SOURCE, MTIME, MUSER, UUID, CHECKSUM,
@@ -499,11 +500,12 @@ class Node(DBWorker):
         r.close()
         return dict(rows)
 
-    def node_account_usage(self, account=None, cluster=0):
-        """Return usage for a specific account.
+    def node_account_usage(self, account=None, project=None, cluster=0):
+        """Return a dict of dicts with the project usage for a specific account.
 
         Keyword arguments:
-        account -- (default None: list usage for all the accounts)
+        account -- (default None: list usage for all accounts)
+        project -- (default None: list usage for all projects)
         cluster -- list current, history or deleted usage (default 0: normal)
         """
 
@@ -511,20 +513,61 @@ class Node(DBWorker):
         n2 = self.nodes.alias('n2')
         n3 = self.nodes.alias('n3')
 
-        s = select([n3.c.path, func.sum(self.versions.c.size)])
+        s = select([n3.c.path, self.policy.c.value,
+                    func.sum(self.versions.c.size)])
+        s = s.where(self.policy.c.key == 'project')
+        s = s.where(self.policy.c.node == n2.c.node)
         s = s.where(n1.c.node == self.versions.c.node)
         s = s.where(self.versions.c.cluster == cluster)
         s = s.where(n1.c.parent == n2.c.node)
         s = s.where(n2.c.parent == n3.c.node)
         s = s.where(n3.c.parent == 0)
         s = s.where(n3.c.node != 0)
+        s = s.group_by(n3.c.path, self.policy.c.value)
         if account:
             s = s.where(n3.c.path == account)
-        s = s.group_by(n3.c.path)
+        if project:
+            s = s.where(self.policy.c.value == project)
         r = self.conn.execute(s)
-        usage = r.fetchall()
+        rows = r.fetchall()
         r.close()
-        return dict(usage)
+        d = defaultdict(dict)
+        for account, project, usage in rows:
+            d[account][project][DEFAULT_DISKSPACE_RESOURCE] = usage
+        return d
+
+    def node_project_usage(self, project=None, cluster=0):
+        """Return a dict of dicts with the project usage for a specific account.
+
+        Keyword arguments:
+        project -- (default None: list usage for all projects)
+        cluster -- list current, history or deleted usage (default 0: normal)
+        """
+
+        n1 = self.nodes.alias('n1')
+        n2 = self.nodes.alias('n2')
+        n3 = self.nodes.alias('n3')
+
+        s = select([self.policy.c.value,
+                    func.sum(self.versions.c.size)])
+        s = s.where(self.policy.c.key == 'project')
+        s = s.where(self.policy.c.node == n2.c.node)
+        s = s.where(n1.c.node == self.versions.c.node)
+        s = s.where(self.versions.c.cluster == cluster)
+        s = s.where(n1.c.parent == n2.c.node)
+        s = s.where(n2.c.parent == n3.c.node)
+        # s = s.where(n3.c.parent == 0)
+        # s = s.where(n3.c.node != 0)
+        s = s.group_by(self.policy.c.value)
+        if project:
+            s = s.where(self.policy.c.value == project)
+        r = self.conn.execute(s)
+        rows = r.fetchall()
+        r.close()
+        d = defaultdict(dict)
+        for project, usage in rows:
+            d[project][DEFAULT_DISKSPACE_RESOURCE] = usage
+        return d
 
     def policy_get(self, node):
         s = select([self.policy.c.key, self.policy.c.value],
