def format_ticket(t):

    ggus_t = get_ggus_ticket(ticket_id(t))
    assigned_time = ggus_t.assigned_time()
    age = (datetime.now() - assigned_time).days

    warning = ""

    # if ticket_status(t) == 'on hold' and ticket_related_issue(t) is None:
    #    warning = "on hold ticket without related issue!".upper()

    template=Template("""${url} \"${desc}\"
                SU: ${su}
                Prio: ${prio}
                Age: ${age}
                Related issue: ${related}""")

    ticket_str = template.substitute({"url":ticket_url(t),
                         "desc": ticket_short_description(t),
                         "su": ticket_su(t),
                         "prio": ticket_priority(t),
                         "age": "%d days old" % age,
                         "related": None })

    if len(warning) > 0:
        ticket_str = ticket_str + "\n\t\t\tWARNING: %s" % warning

    return ticket_str
def format_ticket_html(t):

    template_str = """
        <tr>
        <td>${su}</td>
        <td><a href="https://ggus.eu/ws/ticket_info.php?ticket=${id}">${id}</a></td>
        <td>${status}</td>
        <td class="prio_${prio_class}">${prio}</td>
        <td><h5>${desc}</h5></td>
        <td><span class=\"badge\">${age}</span></td>
        </tr>"""

    ggus_t = get_ggus_ticket(ticket_id(t))
    assigned_time = ggus_t.assigned_time()
    age = (datetime.now() - assigned_time).days


    template = Template(template_str)
    ticket_str = template.substitute({"id":ticket_id(t),
                         "desc": ticket_short_description(t),
                         "su": ticket_su(t),
                         "prio_class" : ticket_priority(t).replace(' ','_'),
                         "prio": ticket_priority(t),
                         "age": age,
                         "related": None,
                         "status": ticket_status(t)
                         })

    return ticket_str
def classify_su(tickets, sus=emi_3rd_level_su.keys()):
    
    su_classification = {}
    
    for su in sus:
        su_classification[su] = []
    
    for t in tickets:
        su = ticket_su(t)
        su_classification[su].append(t)
        
    return su_classification
def print_on_hold_report():
    print "Producing on-hold tickets status report. Please be patient..."
    print "As of %s the following tickets are in on-hold status but don't have a related issue registered in GGUS." % datetime.now()
    tickets = get_tickets(open_on_hold_tickets())
    for t in filter(lambda x:ticket_related_issue(x) is None, sorted(tickets, key=ticket_su)):
        ggus_t = get_ggus_ticket(ticket_id(t))
        assigned_time = ggus_t.get_sla_compliance_values()[ticket_su(t)].assigned_time
        print "%-22s: https://ggus.eu/tech/ticket_show.php?ticket=%s ( %-3d days old ) %-15s %-15s \"%s\"" %(ticket_su(t),
                                                                                           ticket_id(t),
                                                                                           (datetime.now()-assigned_time).days,
                                                                                           ticket_priority(t),
                                                                                           ticket_status(t),
                                                                                           ticket_short_description(t))
def print_ticket_history(ticket_list):
    for ticket_no in ticket_list:
        try:
            ticket = get_ticket(ticket_no)
        
            ggus_ticket = get_ggus_ticket(ticket_no)
            print "##########"
            print "Ticket: %s" % ticket_url(ticket)
            print "Summary: %s" % ticket_short_description(ticket)
            print "SU: %s" % ticket_su(ticket)
            print 'Last change: %s' % ticket_date_of_change(ticket)
            print "Priority: %s" % ticket_priority(ticket)
            print "Status: %s" % ticket_status(ticket)
            print
            print "Status History: "
            print
            ggus_ticket.print_status_history()
            print
            print "Priority History: "
            print
            ggus_ticket.print_priority_history()
            print
            print "SLA compliance values:"
            print
            val = ggus_ticket.get_sla_compliance_values2()
            counter = 0
            for v in val:
                
                
                if v.sla_delay:
                    sla_check_str = "Delay: %d business minutes (i.e. %d business hours, or  %d business days)" % (v.sla_delay,
                                                                                                                   v.sla_delay // 60,
                                                                                                                   v.sla_delay // 480)
                else:
                    sla_check_str = "SLA Check OK"    
                
                print "#", counter, " (%s)" % v.su, sla_check_str
                print "Priority when assigned: %s" % v.priority_when_assigned
                print "Assigned to SU %s on: %s" % (v.su, v.assigned_time)
                print "SLA constraint: %s" % sla_constraints[v.priority_when_assigned]
                print "Expected takeover: %s" % v.expected_takeover_time
                print "Actual takeover: %s" % v.out_of_assigned_time
                print "Out of assign status: %s" % v.out_of_assigned_status
                print
                counter = counter + 1
            print "##########"
            print 
            
        except WebFault:
            print >> stderr, "Error loading ticket %s" % (ticket_no)
def print_eta_status_report():
    print "Producing ETA status report. Please be patient..."
    
    tickets = get_tickets(open_very_urgent_and_top_priority_tickets())
    print "As of %s there are %d very urgent and top priority tickets which have ETA not set." % (datetime.now(), len(tickets))
    print
    for t in filter(lambda x:ticket_eta(x) is None, sorted(tickets, key=ticket_priority)):
        ggus_t = get_ggus_ticket(ticket_id(t))
        assigned_time = ggus_t.get_sla_compliance_values()[ticket_su(t)].assigned_time
        print "%-22s: https://ggus.eu/tech/ticket_show.php?ticket=%s ( %-3d days old ) %-15s %-18s \"%s\"" %(ticket_su(t),
                                                                                           ticket_id(t),
                                                                                           (datetime.now()-assigned_time).days,
                                                                                           ticket_priority(t),
                                                                                           ticket_status(t),
                                                                                           ticket_short_description(t))
def print_assigned_tickets_report(assigned_priority_classification=None):
    
    now = datetime.now()
    
    if assigned_priority_classification is None:
        assigned_priority_classification = {}
        for i in priorities:
            assigned_priority_classification[i] = []
        
        for t in get_tickets(emi_third_level_assigned_tickets()):
            assigned_priority_classification[ticket_priority(t)].append(t)
    
    for p in priorities:
        if len(assigned_priority_classification[p]) == 0:
            continue
        else:
            print p, ":"
            num_violations = 0
            for t in assigned_priority_classification[p]:
                
                ggus_ticket = get_ggus_ticket(ticket_id(t))
                
                assigned_time = ggus_ticket.get_sla_compliance_values()[ticket_su(t)].assigned_time
                bc = BusinessCalendar(assigned_time)
                
                resolution_date = bc.add_business_time(sla_constraints[p])
                
                if resolution_date >= now:
                    sla_violation_string = "SLA check: OK (Assigned on: %s. To be taken in charge before %s)" % (assigned_time, resolution_date)
                else:
                    if now.day != resolution_date.day:
                        bd_delay = BusinessCalendar(resolution_date).count_business_days(now)
                        delay = "%s work days" % bd_delay
                    else:
                        delay = "%s hours" % (now - resolution_date)
                    
                    sla_violation_string = "SLA VIOLATION: Delay: %s (Assigned on: %s. Had to be taken in charge before %s)" % (delay, assigned_time, resolution_date)
                    num_violations = num_violations + 1
                
                print "\t(%s) \"%s\" https://ggus.eu/tech/ticket_show.php?ticket=%s\n\t%s" % (ticket_su(t),
                                                                                              ticket_short_description(t),
                                                                                              ticket_id(t),
                                                                                              sla_violation_string)
                print
            
            print "Of the %d %s tickets, %d violate the SLA " % (len(assigned_priority_classification[p]), p, num_violations)
            print