# Path: ob2/util/config_data.py
# def get_assignment_by_name(name, default=None):
#     global _assignment_by_name
#     if _assignment_by_name is None:
#         _assignment_by_name = {assignment.name: assignment
#                                for assignment in config.assignments}
#     return _assignment_by_name[name]
# 
# Path: ob2/util/time.py
# def format_js_compatible_time(s):
#     """Converts a datetime object to serializable format for JavaScript."""
#     return s.strftime("%Y-%m-%dT%H:%M:%S%z")
# 
# def now():
#     """Gets the current datetime (with timezone) as an object."""
#     timezone_obj = pytz.timezone(config.timezone)
#     return datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(timezone_obj)
# 
# def parse_time(s):
#     """Converts serializable datetime to a datetime object."""
#     return DateParser.parse(s)
# 
# Path: ob2/util/build_constants.py
# SUCCESS = 0


import numpy as np
from math import ceil, floor
from ob2.util.config_data import get_assignment_by_name
from ob2.util.time import format_js_compatible_time, now, parse_time
from ob2.util.build_constants import SUCCESS
        """
        Returns a timeseries of grades with percentiles. Here is an example:

            [["2015-07-17 19:00:36-0700", 0.0, 0.0, 0.0, ... 0.0, 0.0],
             ["2015-07-17 19:10:36-0700", 0.0, 0.0, 0.0, ... 1.0, 2.0],
             ["2015-07-17 19:20:36-0700", 0.0, 0.0, 0.0, ... 3.0, 4.0],
             ["2015-07-17 19:30:36-0700", 0.0, 0.0, 0.5, ... 5.0, 6.0],
             ["2015-07-17 19:40:36-0700", 0.0, 0.0, 1.0, ... 7.0, 8.0]]

        """
        data_keys = range(0, 105, 5)
        assignment = get_assignment_by_name(assignment_name)
        if not assignment:
            return
        # There is a slight problem that because of DST, ordering by "started" may not always
        # produce the correct result. When the timezone changes, lexicographical order does not
        # match the actual order of the times. However, this only happens once a year in the middle
        # of the night, so fuck it.
        c.execute('''SELECT source, score, started FROM builds WHERE job = ? AND status = ?
                     ORDER BY started''', [assignment_name, SUCCESS])
        # XXX: There is no easy way to exclude builds started by staff ("super") groups.
        # But because this graph is to show the general trend, it's usually fine if staff builds
        # are included. Plus, the graph only shows up in the admin interface anyway.
        builds = [(source, score, parse_time(started)) for source, score, started in c.fetchall()]
        if not builds:
            return []
        source_set = map(lambda b: b[0], builds)
        started_time_set = map(lambda b: b[2], builds)
        min_started = min(started_time_set)
        max_started = max(started_time_set)

# Path: ob2/util/config_data.py
# def get_assignment_by_name(name, default=None):
#     global _assignment_by_name
#     if _assignment_by_name is None:
#         _assignment_by_name = {assignment.name: assignment
#                                for assignment in config.assignments}
#     return _assignment_by_name[name]
# 
# Path: ob2/util/time.py
# def format_js_compatible_time(s):
#     """Converts a datetime object to serializable format for JavaScript."""
#     return s.strftime("%Y-%m-%dT%H:%M:%S%z")
# 
# def now():
#     """Gets the current datetime (with timezone) as an object."""
#     timezone_obj = pytz.timezone(config.timezone)
#     return datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(timezone_obj)
# 
# def parse_time(s):
#     """Converts serializable datetime to a datetime object."""
#     return DateParser.parse(s)
# 
# Path: ob2/util/build_constants.py
# SUCCESS = 0


# Path: ob2/util/datasets.py
import numpy as np
from math import ceil, floor
from ob2.util.config_data import get_assignment_by_name
from ob2.util.time import format_js_compatible_time, now, parse_time
from ob2.util.build_constants import SUCCESS

        """
        Returns a timeseries of grades with percentiles. Here is an example:

            [["2015-07-17 19:00:36-0700", 0.0, 0.0, 0.0, ... 0.0, 0.0],
             ["2015-07-17 19:10:36-0700", 0.0, 0.0, 0.0, ... 1.0, 2.0],
             ["2015-07-17 19:20:36-0700", 0.0, 0.0, 0.0, ... 3.0, 4.0],
             ["2015-07-17 19:30:36-0700", 0.0, 0.0, 0.5, ... 5.0, 6.0],
             ["2015-07-17 19:40:36-0700", 0.0, 0.0, 1.0, ... 7.0, 8.0]]

        """
        data_keys = range(0, 105, 5)
        assignment = get_assignment_by_name(assignment_name)
        if not assignment:
            return
        # There is a slight problem that because of DST, ordering by "started" may not always
        # produce the correct result. When the timezone changes, lexicographical order does not
        # match the actual order of the times. However, this only happens once a year in the middle
        # of the night, so fuck it.
        c.execute('''SELECT source, score, started FROM builds WHERE job = ? AND status = ?
                     ORDER BY started''', [assignment_name, SUCCESS])
        # XXX: There is no easy way to exclude builds started by staff ("super") groups.
        # But because this graph is to show the general trend, it's usually fine if staff builds
        # are included. Plus, the graph only shows up in the admin interface anyway.
        builds = [(source, score, parse_time(started)) for source, score, started in c.fetchall()]
        if not builds:
            return []
        source_set = map(lambda b: b[0], builds)
        started_time_set = map(lambda b: b[2], builds)
        min_started = min(started_time_set)
        max_started = max(started_time_set)

        assignment_min_started = parse_time(assignment.not_visible_before)