'Queries the DB for the user jobs in error.'

@web.expose
def specified_month_in_error(self, trans, **kwd):

    message = ''
    PageSpec = namedtuple('PageSpec', ['entries', 'offset', 'page', 'pages_found'])
    params = util.Params(kwd)
    monitor_email = params.get('monitor_email', 'monitor@bx.psu.edu')
    specs = sorter('date', kwd)
    sort_id = specs.sort_id
    order = specs.order
    arrow = specs.arrow
    _order = specs.exc_order
    offset = 0
    limit = 10
    if ('entries' in kwd):
        entries = int(kwd.get('entries'))
    else:
        entries = 10
    limit = (entries * 4)
    if ('offset' in kwd):
        offset = int(kwd.get('offset'))
    else:
        offset = 0
    if ('page' in kwd):
        page = int(kwd.get('page'))
    else:
        page = 1
    monitor_user_id = get_monitor_id(trans, monitor_email)
    specified_date = kwd.get('specified_date', datetime.utcnow().strftime('%Y-%m-%d'))
    specified_month = specified_date[:7]
    (year, month) = map(int, specified_month.split('-'))
    start_date = date(year, month, 1)
    end_date = (start_date + timedelta(days=calendar.monthrange(year, month)[1]))
    month_label = start_date.strftime('%B')
    year_label = start_date.strftime('%Y')
    month_jobs_in_error = sa.select((sa.func.date(model.Job.table.c.create_time).label('date'), sa.func.count(model.Job.table.c.id).label('total_jobs')), whereclause=sa.and_((model.Job.table.c.user_id != monitor_user_id), (model.Job.table.c.state == 'error'), (model.Job.table.c.create_time >= start_date), (model.Job.table.c.create_time < end_date)), from_obj=[model.Job.table], group_by=['date'], order_by=[_order], offset=offset, limit=limit)
    all_jobs_in_error = sa.select((model.Job.table.c.create_time.label('date'), model.Job.table.c.id.label('id')), whereclause=sa.and_((model.Job.table.c.user_id != monitor_user_id), (model.Job.table.c.state == 'error'), (model.Job.table.c.create_time >= start_date), (model.Job.table.c.create_time < end_date)))
    trends = dict()
    for job in all_jobs_in_error.execute():
        job_hour = int(job.date.strftime('%-H'))
        job_day = job.date.strftime('%d')
        try:
            trends[job_day][job_hour] += 1
        except KeyError:
            trends[job_day] = ([0] * 24)
            trends[job_day][job_hour] += 1
    jobs = []
    for row in month_jobs_in_error.execute():
        row_dayname = row.date.strftime('%A')
        row_day = row.date.strftime('%d')
        jobs.append((row_dayname, row_day, row.total_jobs, row.date))
    pages_found = ceil((len(jobs) / float(entries)))
    page_specs = PageSpec(entries, offset, page, pages_found)
    return trans.fill_template('/webapps/reports/jobs_specified_month_in_error.mako', order=order, arrow=arrow, sort_id=sort_id, month_label=month_label, year_label=year_label, month=month, jobs=jobs, trends=trends, message=message, is_user_jobs_only=monitor_user_id, page_specs=page_specs)
