#!/usr/bin/env python
# -*- coding: utf-8 -*-

from jinja2 import Template

VPD_TXT_TEMPLATE: Template = Template("""Hello {{name.split(' ')[0]}},
A Vulnerabilities per Device (VPD) score has been calculated for
services supported by your team, "{{team_name}}"

From {{start_date}} through {{end_date}}:

    Services  |  VPD  |  %Change (30 day)
========== | ======== | =========
{% for record in records %}
    {{ services.get(record.get('t_appid')).get('name') }}_{{ record.get('t_appid').format(ssssssssss) }} | {{ "%8.2f"|format(record.get('end_vpd')) }} | {{ "%8.2f%%"|format(record.get('vpd_change')) }}
{% endfor %}

How is VPD calculated?
======================

VPD is calculated as [ Number of total vulnerabilities tagged by t_appid ÷ Number of Total Qualys Agents tagged by t_appid ]

The intent of this message is to raise visibility to the VPD trend for services your team is responsible for.

Learn more about t_appid, VPD calculations, and how teams are both derived and opted-in to this communication by reading "Naming Conventions for Services in PagerDuty": https://neo.pearson.com/people/OLSERY/blog/2021/04/06/naming-conventions-for-services-in-pagerduty

Not finding all the services you expect for this team? Check out the self-help Q&A section of "Qualys agent compliance": https://neo.pearson.com/groups/service-workflow-automation/blog/2021/04/08/announcing-qualys-compliance-reporting-exporter-for-slack-mvp

Looking for ways to impact your metrics? Read a blog post on the topic here: https://neo.pearson.com/groups/service-workflow-automation/blog/2021/06/29/how-to-improve-your-metrics-posture

If you would like to give us feedback, please follow here: https://forms.office.com/Pages/ResponsePage.aspx?id=1zTEjNCX00e1xRT-DjPjS0Y8xBKXOmlDhg8pLj3w38BUQkNOWFRHUFNHSkVPVkgwNUQzMUZaNE1INC4u

""")

VPD_HTML_TEMPLATE: Template = Template("""<div>
    <style type="text/css" scoped>
        #main-div {
            background-color: rgb(245, 245, 250); border-radius: 10px; padding: 5px;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            font-size: small;
        }
        #scorecard-table {
            border-collapse: collapse;
            text-align: left;
        }
        #scorecard-table th {
            background-color: rgb(100, 100, 120);
            border: 0.5px solid rgb(150, 150, 170);
            padding: 5px;
            color: white;
        }
        #scorecard-table th a {
            color: rgb(245, 245, 250);
        }
        #scorecard-table td {
            border: 0.5px solid rgb(150, 150, 170);
            padding: 5px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 14%;
        }
        #scorecard-table tr:nth-child(even){
            background-color: rgb(220, 220, 230);
        }
        #scorecard-table tbody tr:hover{
            background-color: rgb(200, 200, 215);
        }
        .section-title-div {
            background-color: rgb(150, 150, 170);
            color: white;
            border-radius: 5px;
            text-align: center;
            margin-top: 20px;
            margin-bottom: 20px;
        }
        .section-title-h3 {
            padding: 5px;
        }
        .score-medium-td {
            background-color: rgb(170, 170, 185);
        }
        .score-poor-td {
            background-color: rgb(130, 130, 150);
            font-weight: bold;
            color: white;
        }
    </style>
    <div id="main-div">
        <p style="margin-top: -1px;">Hello {{ name.split(' ')[0] }},</p>
        <p>Welcome to your <a href="{{ scorecard_url }}">Well Architected Practices Scorecard</a>!</p>
        <h3>Why am I receiving this message?</h3>
        <ul>
            <li>To provide a snapshot of <a href="https://neo.pearson.com/docs/DOC-782433">critical operational metrics</a> for the applications you support.</li>
            <li>To encourage you to visit the <a href="https://dashboards.pearsonserviceinsights.com/well-architected-practices">Well Architected Practices dashboard</a>.</li>
        </ul>
        <h3>What should I do next?</h3>
        <ul>
            <li>View some <a href="https://neo.pearson.com/groups/service-workflow-automation/blog/2021/06/29/how-to-improve-your-metrics-posture">Tips to Improve Your Metrics</a></li>
        </ul>
        <br>
        <hr>
        <br>
        <div class="section-title-div">
            <h3 class="section-title-h3">Scorecard</h2>
        </div>

        <table id="scorecard-table">
            <thead>
                <tr>
                    <th style="text-align:center;">Services</th><th style="text-align:center;">VPD</th><th style="text-align:center;">%Change (30 day)</th>
                </tr>
            </thead>
            <tbody>
                {% for record in records %}
                <tr><td style="text-align:center;">{{ services.get(record.get('t_appid')).get('name') }}_{{ record.get('t_appid').format(ssssssssss) }}</td><td style="text-align:center;">{{ "%8.2f"|format(record.get('end_vpd')) }}</td><td style="text-align:center;">{{ "%8.2f%%"|format(record.get('vpd_change')) }}</td></tr>
                {% endfor %}
            </tbody>
        </table>



        <br>
        <hr>
        <br>
        <h3><u>Support & Contact</u></h3>
            <h4>NEO</h4>
            <p>
            You can find out more about our service and offerings on <a href="https://neo.pearson.com/groups/service-workflow-automation">NEO</a>.
            </p>

            <h4>Slack</h4>
            <p>
            You can reach out to us via <a href="https://aidevops.slack.com/archives/C023YF51EF9">#pearson-service-insights-support</a>
            </p>

            <h4>E-Mail</h4>
            <p>
            Send us an email via <a href="mailto:TOIOServiceWorkflowAutomation@grp.pearson.com">TOIOServiceWorkflowAutomation@grp.pearson.com</a>
            </p>

            <h4>Support</h4>
            <p>
            We hold office hours every week, Wednesdays, 3 PM - 4 PM Eastern. You can join via  <a href="https://teams.microsoft.com/l/meetup-join/19%3ameeting_ODdkOWZlMGEtMzViMS00Zjg1LWFmNTEtM2UwZDgxYzZlZmQ0%40thread.v2/0?context=%7b%22Tid%22%3a%228cc434d7-97d0-47d3-b5c5-14fe0e33e34b%22%2c%22Oid%22%3a%2212c43c46-3a97-4369-860f-292e3df0dfc0%22%7d">MS Teams</a>.
            </p>
        <br>
        <hr>
        <br>
        <h3>FAQ</h3>
            <h4>Q: How is VPD calculated?</h4>
            <p>
            <b>A:</b> <a href="https://neo.pearson.com/docs/DOC-775435">VPD</a> is calculated as [ Number of server vulnerabilities tagged by t_appid with CVSS >= 7 ] ÷ [ Number of servers with an active Qualys Agent tagged by t_appid ]
            </p>

            <h4>Q: What CVSS severity types are included in this report?</h4>
            <p>
            <b>A:</b> VPD score is measured by counting vulnerabilities with a CVSS rating of 7+ (includes critical, high, and medium severity). Read <a href="https://neo.pearson.com/people/USENADU/blog/2021/06/28/vulnerability-management-blog">more.</a>
            </p>

            <h4>Q: My service is not listed, how come?</h4>
            <p>
            <b>A:</b> Learn more about <a href="https://neo.pearson.com/people/OLSERY/blog/2021/04/06/naming-conventions-for-services-in-pagerduty">Naming Conventions for Services in PagerDuty</a>
            </p>

            <h4>Q: I have registered my services via PagerDuty, why do I not see a VPD rating for my services?</h4>
            <p>
            <b>A:</b> Check your <a href="https://neo.pearson.com/groups/service-workflow-automation/blog/2021/04/08/announcing-qualys-compliance-reporting-exporter-for-slack-mvp">Qualys agent configuration</a>
            </p>

            <h4>Q: How do I get to the underlying data for this report?</h4>
            <p>
            <b>A:</b> There are three key ways to get access to Qualys agent data:
            </br>
            <ol>
                <li>Join Pearson I&O's <a href="https://aidevops.slack.com">Slack</a> and request it via <a href="https://aidevops.slack.com/apps/AJATD49NW-io?next_id=0">@IO</a>. Read more about it <a href="https://neo.pearson.com/groups/service-workflow-automation/blog/2021/04/08/announcing-qualys-compliance-reporting-exporter-for-slack-mvp">here</a>.</li>
                <li>Use the PCM Vulnerability API by joining <a href="https://aidevops.slack.com/archives/C016MTG9T1B">#pcm_vulnerability_mgmt-support</a> and requesting access.</li>
                <li>Reach out to CISO (<a href="https://neo.pearson.com/people/jarvsimp">Jarvis Simpson</a>) and request access to the Qualys dashboard direct.</li>
            </ol>
            </p>

            <h4>Q: I do not know anything about these applications or I am not a part of this team, why am I receiving these notices?</h4>
            <p>
            <b>A:</b> You are receiving this notification because you are a part of a PagerDuty team and one or more services for that team have been <a href="https://neo.pearson.com/people/OLSERY/blog/2021/04/06/naming-conventions-for-services-in-pagerduty">opted-in</a>
            </p>

            <h4>Q: How do I provide feedback?</h4>
            <p>
            <b>A:</b> If you would like to give us feedback, please follow <a href="https://forms.office.com/Pages/ResponsePage.aspx?id=1zTEjNCX00e1xRT-DjPjS0Y8xBKXOmlDhg8pLj3w38BUQkNOWFRHUFNHSkVPVkgwNUQzMUZaNE1INC4u">here</a>
            </p>
    </div>
</div>
""")