# Copyright (C) 2013-2014  Stefano Zacchiroli <zack@upsilon.cc>
#
# This file is part of Debsources.
#
# Debsources is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy import distinct
from sqlalchemy import func as sql_func

from models import Checksum, Metric, SlocCount, SuitesMapping, Version


def disk_usage(session, suite=None):
    """overall or per-suite disk usage"""
    q = session.query(sql_func.sum(Metric.value)) \
               .filter(Metric.metric == 'size')
    if suite:
        q = q.join(Version) \
             .join(SuitesMapping) \
             .filter(SuitesMapping.suite == suite)

    size = q.first()[0]
    if not size:
        size = 0

    return size


def versions(session, suite=None):
    """overall or per-suite count of versioned source packages

    multiple versions of the same source package count adds up to the result of
    this query. When doing per-suite queries that doesn't (shouldn't) happen,
    as each suite is (usually) guaranteed to contain at most one version of
    each packages

    """
    q = session.query(sql_func.count(Version.id))
    if suite:
        q = q.join(SuitesMapping) \
            .filter(SuitesMapping.suite == suite)

    count = q.first()[0]
    if not count:
        count = 0

    return count


def source_files(session, suite=None):
    """overall or per-suite count of indexed source files

    Return 0 if the checksum plugin is not enabled

    """
    # TODO when a separate File table will be present, this will need to be
    # adapted to use that instead of Checksum
    q = session.query(sql_func.count(Checksum.id))
    if suite:
        q = q.join(Version) \
             .join(SuitesMapping) \
             .filter(SuitesMapping.suite == suite)

    count = q.first()[0]
    if not count:
        count = 0

    return count


def sloccount_lang(session, language, suite=None):
    """query the DB via session and return the LOCS written in language

    return LOCS relative to suite, if given, or DB-wide if not

    """
    q = session.query(sql_func.sum(SlocCount.count)) \
               .filter(SlocCount.language == language)
    if suite:
        q = q.join(Version) \
             .join(SuitesMapping) \
             .filter(SuitesMapping.suite == suite)

    count = q.first()[0]
    if not count:
        count = 0

    return count


def sloccount_summary(session, suite=None):
    """query the DB via session and return a per-language summary of LOCS

    return summary relative to suite, if given, or DB-wide if not

    """
    q = session.query(SlocCount.language, sql_func.sum(SlocCount.count))
    if suite:
        q = q.join(Version) \
             .join(SuitesMapping) \
             .filter(SuitesMapping.suite == suite)
    q = q.group_by(SlocCount.language)

    return dict(q.all())

@@ -750,16 +750,76 @@
 ses/%3E.%0A%0A
+%22%22%22Compute several statistics about Debsouces content%0A%0A%22%22%22%0A%0A
 from sql
@@ -1004,39 +1004,113 @@
  %22%22%22
-overall or per-suite disk usage
+disk space used by extracted source packages%0A%0A    only count disk usage relative to suite, if given%0A%0A    
 %22%22%22%0A
@@ -1464,63 +1464,84 @@
  %22%22%22
-overall or per-suite count of versioned source packages
+(versioned) source package count%0A%0A    only count packages in suite, if given
 %0A%0A  
@@ -2081,58 +2081,74 @@
  %22%22%22
-overall or per-suite count of indexed source files
+source files count%0A%0A    only count source files in suite, if given
 %0A%0A  
@@ -2653,52 +2653,37 @@
  %22%22%22
-query the DB via session and return the LOCS
+source lines of codes (SLOCs)
  wri
@@ -2690,16 +2690,36 @@
 tten in 
+a given programming 
 language
@@ -2728,19 +2728,24 @@
     
-return LOCS
+only count SLOCs
  rel
@@ -2768,35 +2768,16 @@
 if given
-, or DB-wide if not
 %0A%0A    %22%22
@@ -3156,36 +3156,67 @@
  %22%22%22
-query the DB via session and
+source lines of code (SLOCs), broken down per language%0A%0A   
  ret
@@ -3225,20 +3225,16 @@
 n a 
-per-
 language
  sum
@@ -3233,44 +3233,63 @@
 uage
- summ
+-indexed diction
 ary of 
+S
 LOC
-S%0A%0A    return summary
+ counts%0A%0A    only count LOCs
  rel
@@ -3316,27 +3316,8 @@
 iven
-, or DB-wide if not
 %0A%0A  

improve and uniform module- and method-docstrings