Revision eb83c485 snf-pithos-backend/pithos/backends/lib/sqlalchemy/node.py

b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/node.py
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
1
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
34 34
from time import time
35 35
from operator import itemgetter
36 36
from itertools import groupby
37
from collections import defaultdict
37 38

  
38 39
from sqlalchemy import (Table, Integer, BigInteger, DECIMAL, Boolean,
39 40
                        Column, String, MetaData, ForeignKey)
......
45 46

  
46 47
from pithos.backends.filter import parse_filters
47 48

  
48

  
49
DEFAULT_DISKSPACE_RESOURCE = 'pithos.diskspace'
49 50
ROOTNODE = 0
50 51

  
51 52
(SERIAL, NODE, HASH, SIZE, TYPE, SOURCE, MTIME, MUSER, UUID, CHECKSUM,
......
499 500
        r.close()
500 501
        return dict(rows)
501 502

  
502
    def node_account_usage(self, account=None, cluster=0):
503
        """Return usage for a specific account.
503
    def node_account_usage(self, account=None, project=None, cluster=0):
504
        """Return a dict of dicts with the project usage for a specific account.
504 505

  
505 506
        Keyword arguments:
506
        account -- (default None: list usage for all the accounts)
507
        account -- (default None: list usage for all accounts)
508
        project -- (default None: list usage for all projects)
507 509
        cluster -- list current, history or deleted usage (default 0: normal)
508 510
        """
509 511

  
......
511 513
        n2 = self.nodes.alias('n2')
512 514
        n3 = self.nodes.alias('n3')
513 515

  
514
        s = select([n3.c.path, func.sum(self.versions.c.size)])
516
        s = select([n3.c.path, self.policy.c.value,
517
                    func.sum(self.versions.c.size)])
518
        s = s.where(self.policy.c.key == 'project')
519
        s = s.where(self.policy.c.node == n2.c.node)
515 520
        s = s.where(n1.c.node == self.versions.c.node)
516 521
        s = s.where(self.versions.c.cluster == cluster)
517 522
        s = s.where(n1.c.parent == n2.c.node)
518 523
        s = s.where(n2.c.parent == n3.c.node)
519 524
        s = s.where(n3.c.parent == 0)
520 525
        s = s.where(n3.c.node != 0)
526
        s = s.group_by(n3.c.path, self.policy.c.value)
521 527
        if account:
522 528
            s = s.where(n3.c.path == account)
523
        s = s.group_by(n3.c.path)
529
        if project:
530
            s = s.where(self.policy.c.value == project)
524 531
        r = self.conn.execute(s)
525
        usage = r.fetchall()
532
        rows = r.fetchall()
526 533
        r.close()
527
        return dict(usage)
534
        d = defaultdict(dict)
535
        for account, project, usage in rows:
536
            d[account][project][DEFAULT_DISKSPACE_RESOURCE] = usage
537
        return d
538

  
539
    def node_project_usage(self, project=None, cluster=0):
540
        """Return a dict of dicts with the project usage for a specific account.
541

  
542
        Keyword arguments:
543
        project -- (default None: list usage for all projects)
544
        cluster -- list current, history or deleted usage (default 0: normal)
545
        """
546

  
547
        n1 = self.nodes.alias('n1')
548
        n2 = self.nodes.alias('n2')
549
        n3 = self.nodes.alias('n3')
550

  
551
        s = select([self.policy.c.value,
552
                    func.sum(self.versions.c.size)])
553
        s = s.where(self.policy.c.key == 'project')
554
        s = s.where(self.policy.c.node == n2.c.node)
555
        s = s.where(n1.c.node == self.versions.c.node)
556
        s = s.where(self.versions.c.cluster == cluster)
557
        s = s.where(n1.c.parent == n2.c.node)
558
        s = s.where(n2.c.parent == n3.c.node)
559
        # s = s.where(n3.c.parent == 0)
560
        # s = s.where(n3.c.node != 0)
561
        s = s.group_by(self.policy.c.value)
562
        if project:
563
            s = s.where(self.policy.c.value == project)
564
        r = self.conn.execute(s)
565
        rows = r.fetchall()
566
        r.close()
567
        d = defaultdict(dict)
568
        for project, usage in rows:
569
            d[project][DEFAULT_DISKSPACE_RESOURCE] = usage
570
        return d
528 571

  
529 572
    def policy_get(self, node):
530 573
        s = select([self.policy.c.key, self.policy.c.value],

Also available in: Unified diff