<%inherit file="//layout/admin_lite.html"/>
<%
from splunk.appserver.mrsparkle.lib import util
isLite = util.isLite()
liteDict = {'lite': 'Splunk Light', 'lite_free': 'Splunk Light Free'}
%>

<%def name="title()">${_('Licensing')} - ${parent.title()}</%def>
<%def name="get_breadcrumbs()">
    <%
        return [
            [_('Manager'), ['manager']],
            [_('Licensing'), None]
        ]
    %>
</%def>
<%def name="message_renderer(messages)">
            % for cat, details in messages.items():
                <%
                    friendly_message = category_map.get(cat)
                    if not friendly_message:
                        friendly_message = dict(
                            single=_('%s ' + cat + ' message'),
                            plural=_('%s ' + cat + ' messages')
                        )
                %>
                <tr>
                    <td class="light-container"><span class="light msg-${details['severity'] | h}" title="${details['severity'] | h}"/></td>
                    <td class="text">
                        <a href="${make_url(['manager','system','licensing','messages', cat]) | h}">
                            ${ungettext(friendly_message['single'], friendly_message['plural'], details['count']) % details['count']}
                            % if len(details['slaves']) > 0:
                                ${_('reported by')}
                                ${ungettext('%s indexer', '%s indexers', len(details['slaves'])) % len(details['slaves'])}
                            % endif
                        </a>
                    </td>
                    <td class="time">
                        <span title="${i18n.format_datetime(details['latest_time']) | h}">
                            % if cat not in ('pool_warning_count', 'pool_violated_slave_count') and details['severity'] == 'warn':
                                ${i18n.format_timedelta(details['latest_time']) | h}
                            % else:
                                ${_('Correct by midnight to avoid violation')}
                                <a target="_blank" class="spl-icon-external-link-xsm" href="${generateSelfHelpLink('license.overview.warnings')}">${_('Learn more')}</a>
                            % endif
                        </span>
                    </td>
                </tr>
            % endfor
</%def>

% if isLite:
<div class="licenseHeaderInfo">
    ${_('Explore Splunk License options and add capacity on')} <a class="splButton-tertiary orange inline-btn" target="_blank" href="http://www.splunk.com/goto/estore">${_('e-Store')}</a>
</div>
% else:
<div class="serverMode splClearfix">
    <span class="serverModeLabel">
        % if can_be_remote_master:
            ${_('This server is acting as a master license server')}
        % else:
            ${_('This server is acting as a standalone license server')}
        % endif
    </span>
    
	<a class="button" href="${make_url(['manager','system','licensing','self'], _qs=dict(return_to=cherrypy.request.relative_uri))|h}">
    	<span class="switch">${_('Change to slave')}</span>
    </a>
</div>
% endif

<div class="pageContentWrapper">
    % if isLite:
        <h2 class="top">${_('License information') | h}</h2>
        <table class="properties">
            <tr>
                <td class="k">${_('Current license') | h}</td>
                <td class="licenseName">
                    ${_('%s') % liteDict.get(active_group_name.lower(), 'Splunk Light') | h} 
                    <a class="splButton-primary inline-btn" href="${make_url(['manager','system','licensing','licenses','new'], _qs=dict(return_to=cherrypy.request.relative_uri))}">
                        ${_('Add License')}
                    </a>
                </td>
            </tr>
            % if not can_edit_pools and len(license_table) > 0:
                <tr>
                    <td class="k">${_('License expiration')}</td>
                    <td>
                        ${i18n.format_datetime(license_table[0]['expiration_time'])| h}
                    </td>
                </tr>
            % endif
            % if not can_edit_pools and len(stack_table) > 0:
                <tr>
                    <td class="k">${_('Licensed daily volume')}</td>
                    <td>
                        ${i18n.format_bytes(stack_table[0]['quota_bytes'], output_units='MB', mantissa=0)| h}
                    </td>
                </tr>
                <tr>
                    <td class="k t">${_('Volume used today')}</td>
                    <td>
                        ${i18n.format_bytes(local_used_bytes, output_units='MB', mantissa=0) | h}
                        % if stack_table[0]['quota_bytes']:
                            ${_('(%s of quota)') % h(i18n.format_percent(local_used_bytes / stack_table[0]['quota_bytes']))}
                        % endif
                        <div class="licenseUsageBar">
                            % if stack_table[0]['quota_bytes']:
                                <div class="bar" style="width:${_('%s') % h(i18n.format_percent(local_used_bytes / stack_table[0]['quota_bytes']))}"></div>
                             % endif
                        </div>
                        <div>
                            <a href="${make_url(['manager','search','licenseusage'])}">${_('See usage history')}</a>
                            <a href="http://www.splunk.com/goto/estorelogin" target="_blank" class="splunk-components detailed-usage">${_('Add capacity')} <i class="icon-external"></i></a>
                        </div>
                    </td>
                </tr>
            % else:
                <tr>
                    <td class="k">${_('Volume used today')}</td>
                    <td>
                        ${i18n.format_bytes(local_used_bytes, output_units='MB', mantissa=0) | h}
                    </td>
                </tr>
            % endif
        </table>

        <h2>${_('Server information') | h}</h2>
        <table class="properties">
            <tr>
                <td class="k">${_('Indexer name')}</td>
                <td>${local_slave_name | h}</td>
            </tr>
            <tr>
                <td class="k">${_('Warning count')}</td>
                <td>${i18n.format_number(local_warning_count) | h}</td>
            </tr>
            <tr>
                <td class="k t">${_('Debug information')}</td>
                <td>
                    <a href="${make_url(['manager','system','licensing','licenses'])}">${_('All license details')}</a>
                </td>
            </tr>
        </table>
	% else:
        <div class="licenseGroup splClearfix">
            <h1>${_('%s license group') % active_group_name | h}</h1>
            <div class="actionButtons">
                <a class="button" href="${make_url(['manager','system','licensing','switch'], _qs=dict(return_to=cherrypy.request.relative_uri))|h}">
                    <span class="switch">${_('Change license group')}</span>
                </a>
            </div>
        </div>
        <div class="pageContentDesc">
            <span>${_('This server is configured to use licenses from the <strong>%s license group</strong>') % h(active_group_name)}</span>
        </div>
        <p class="splClearfix">
            <a class="splButton-primary" href="${make_url(['manager','system','licensing','licenses','new'], _qs=dict(return_to=cherrypy.request.relative_uri))}">
                <span>${_('Add license')}</span>
            </a>

            % if showLicenseUsage:      
            <a class="splButton-primary" style="margin-left:15px;" href="${make_url(['manager','search','licenseusage'])}">
                <span>${_('Usage report')}</span>
            </a>
            % endif
        </p>
        <%
        category_map = {
            'license_window': {
                    'single': _('%s license window warning'),
                    'plural': _('%s license window warnings')
                },
            'pool_over_quota': {
                    'single': _('%s pool quota overage warning'),
                    'plural': _('%s pool quota overage warnings')
                },
            'orphan_slave': {
                    'single': _('%s orphaned indexer'),
                    'plural': _('%s orphaned indexers')
                },
            'pool_warning_count': {
                    'single': _('%s pool warning'),
                    'plural': _('%s pool warnings')
                },
            'pool_violated_slave_count': {
                    'single': _('%s pool violation'),
                    'plural': _('%s pool violations')
                },
            'stack_over_quota': {
                    'single': _('%s stack quota overage warning'),
                    'plural': _('%s stack quota overage warnings')
                }
        }
        %>
                
        <div class="moduleWrapper">
            <h2>${_('Alerts')}</h2>
            
            <p class="prose">
                ${_('Licensing alerts notify you of excessive indexing warnings and licensing misconfigurations.')}
                <a class="spl-icon-external-link-xsm" href="${generateSelfHelpLink('license.alerts')}" target="_blank">${_('Learn more')}</a>
            </p>
          <div class='subGroupTitle'> ${_('Current')}  </div>
            <table class="msg-listing">
                % if len(soft_messages) == 0:
                    <tr>
                        <td class="light-container"><span class="light msg-info" title="OK"/></td>
                        <td class="text" style="width:90%">${_('No licensing alerts')}</td>
                        <td class="time"></td>
                    </tr>

                % else:
                ${message_renderer(soft_messages)}
                % endif
            </table>
         
            <div class='subGroupTitle'> ${_('Permanent')} </div>
            <table class="msg-listing">
                % if len(hard_messages) == 0:
                    <tr>
                        <td class="light-container"><span class="light msg-info" title="OK"/></td>
                        <td class="text" style="width:90%">${_('No licensing violations')}</td>
                        <td class="time"></td>
                    </tr>

                % else:
                    ${message_renderer(hard_messages)}
                % endif
            </table>     
            

        </div>


        % if can_edit_pools:
            
            % if len(stack_table) == 0:
                <p class="empty-text">
                    ${_('There are currently no stacks associated with the <strong>%s</strong> license type. No usage information is available.') % h(active_group_name)}
                </p>
            % endif

            % for stack in stack_table:
                <div class="moduleWrapper">
                    <div class="splClearfix stack-title">
                        <h2>${_('%s stack') % stack['label'] | h}</h2>
                        <a target="_blank" class="spl-icon-external-link-xsm" href="${generateSelfHelpLink('license.stack')}">${_('Learn more')}</a>
                    </div>
                    
                    <% 
                        working_pools = [x for x in pool_table if x['stack_name'] == stack['name']]
                        pool_count = len(working_pools) 
                        show_pool_info = True
                    %>

                    % if can_edit_pools:
                        <%
                            catchall_within_stack = set(catchall_pool_names) & set([x['name'] for x in working_pools])
                        %>
                        % if len(catchall_within_stack) > 0:
                            <p class="prose">
                                <span class="notable">*</span>
                                ${_('<strong>%s</strong> is currently a default license pool. Slave indexers can be automatically added to this pool by pointing them to the splunkd port on this machine.') % h(catchall_within_stack.pop())}
                            </p>
                        % endif
                    % endif
                        
                    <table class="listing">
                        <thead>
                            <tr>
                                <th>${_('Licenses')}</th>
                                <th class="numeric">${_('Volume')}</th>
                                <th>${_('Expiration')}</th>
                                <th>${_('Status')}</th>
                                <th>&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            % for license in [x for x in license_table if x['stack_name'] == stack['name']]:
                                <%
                                    licVolume = i18n.format_bytes(license['quota_bytes'], output_units='MB', mantissa=0)
                                    if license['is_unlimited'] == True:
                                        licVolume = _('Unlimited')
                                %>
                                <tr>
                                    <td>
                                        ${license['label'] | h}
                                    </td>
                                    <td class="numeric">${licVolume | h}</td>
                                    <td>${i18n.format_datetime(license['expiration_time']) | h}</td>
                                    <td>
                                        % if license['status'] == 'VALID':
                                            ${_('valid')}
                                        % elif license['status'] == 'REVOKED':
                                            ${_('revoked')}
                                        % elif license['status'] == 'EXPIRED':
                                            ${_('expired')}
                                        % else:
                                            ${license['status']|h}
                                        % endif
                                    <td>
                                        % if license['can_remove']:
                                            <a href="${make_url(['manager','system','licensing','licenses','delete'], _qs=dict(license_id=license['name'], return_to=cherrypy.request.relative_uri))|h}">${_('Delete')}</a>
                                        % endif
                                    </td>
                                </tr>
                            % endfor
                            <tr class="total">
                                <%
                                    stackVolume = i18n.format_bytes(stack['quota_bytes'], output_units='MB', mantissa=0)
                                    if stack['is_unlimited'] == True:
                                        stackVolume = _('Unlimited')
                                %>
                                <td>${_('Effective daily volume')}</td>
                                <td class="numeric">${stackVolume | h}</td>
                                <td colspan="3"></td>
                            </tr>
                        </tbody>
                    </table> 
                        
                    % if pool_count == 0:
                        <p class="empty-text">${_('There are no pools configured in the %s stack') % h(stack['label'])}</p>

                    % else:
                        
                        <table class="listing">
                            <thead>
                                <tr>
                                    % if show_pool_info:
                                        <th>${_('Pools')}</th>
                                    % endif
                                    <th>${_('Indexers')}</th>
                                    <th>
                                        ${_('Volume used today')}
                                    </th>
                                    % if show_pool_info:
                                        <th class="actions">&nbsp;</th>
                                    % endif
                                </tr>
                            </thead>
                            <tbody>
                                <%
                                    meter_max_width = 100
                                %>
                                % for pool in working_pools:
                                    % if show_pool_info:
                                        <%
                                            pool_usage_width = 1
                                            try:
                                                pool_usage_width = max(1, int(meter_max_width * (pool['used_bytes'] / pool['quota_bytes'])))
                                                pool_usage_width = min(meter_max_width, pool_usage_width)
                                            except:
                                                pass
                                            usageDenominator = i18n.format_bytes(pool['quota_bytes'], output_units='MB', mantissa=0)
                                            if pool['is_unlimited'] == True:
                                                usageDenominator = _('Unlimited')
                                        %>
                                        <tr class="group-header">
                                            <td>
                                                ${pool['name'] | h} 
                                                % if can_edit_pools and pool['name'] in catchall_pool_names:
                                                    <span class="notable">*</span>
                                                % endif
                                            </td>
                                            <td></td>
                                            <td>
                                                <span class="meter-tray" style="width:${meter_max_width}px">
                                                    <span class="meter-indicator" style="width:${pool_usage_width}px"></span>
                                                </span>&nbsp;
                                                ${i18n.format_bytes(pool['used_bytes'], output_units='MB', mantissa=0)|h} /
                                                ${usageDenominator|h}
                                            </td>
                                            <td class="actions">
                                                % if can_edit_pools:
                                                    <% qs = dict(pool_name=pool['name'], stack_name=pool['stack_name'], return_to=cherrypy.request.relative_uri) %>
                                                    <a href="${make_url(['manager','system','licensing','pools','edit'], _qs=qs)|h}" title="Edit this pool">${_('Edit')}</a> | 
                                                    <a href="${make_url(['manager','system','licensing','pools','delete'], _qs=qs)|h}" title="Delete this pool">${_('Delete')}</a>
                                                % endif
                                            </td>
                                        </tr>
                                        % if pool['name'] not in [row['pool_name'] for row in slave_table]:
                                            <tr>
                                                <td></td>
                                                <td colspan="3" class="empty-text">${_('No indexers have reported into this pool today')}</td>
                                            </tr>
                                        % endif
                                    % endif
                                    % for slave in slave_table:
                                        % if slave['pool_name'] == pool['name']:
                                            <%
                                                slave_usage_width = max(1, (slave['used_perc'] or 0) * meter_max_width)
                                                slave_usage_width = min(meter_max_width, slave_usage_width)
                                            %>
                                            <tr>
                                                % if show_pool_info:
                                                    <td></td>
                                                % endif
                                                <td>${slave['name']|h}</td>
                                                <td>
                                                    <span class="submeter-indicator" style="width:${slave_usage_width}px"></span>&nbsp;
                                                    ${i18n.format_bytes(slave['used_bytes'], mantissa=0, output_units='MB') | h}
                                                    % if slave['used_perc'] is None:
                                                        ${_('(N/A)')}
                                                    % elif slave['used_perc'] > 0 and slave['used_perc'] < .01:
                                                        (&gt;${i18n.format_percent(0)|h})
                                                    % else:
                                                        (${i18n.format_percent(slave['used_perc']) | h})
                                                    % endif
                                                </td>
                                                <td></td>
                                            </tr>
                                        % endif
                                    % endfor
                                % endfor
                            </tbody>
                        </table> 

                    % endif

                    % if can_edit_pools:
                        <div class="splClearfix">
                            <a class="button" href="${make_url(['manager','system','licensing','pools','add'], _qs=dict(return_to=cherrypy.request.relative_uri, stack_name=stack['name']))}">
                                <span class="add">
                                    ${_('Add pool')}
                                </span>
                            </a>
                        </div>
                    % endif

                </div>
            % endfor
        % endif
        <div class="localServer">
            <h1>${_('Local server information') | h}</h1>
            <table class="properties">
                <tr>
                    <td class="k">${_('Indexer name')}</td>
                    <td>${local_slave_name | h}</td>
                </tr>
                % if not can_edit_pools and len(license_table) > 0:
                    <tr>
                        <td class="k">${_('License expiration')}</td>
                        <td>
                            ${i18n.format_datetime(license_table[0]['expiration_time'])| h}
                        </td>
                    </tr>
                % endif
                % if not can_edit_pools and len(stack_table) > 0:
                    <tr>
                        <td class="k">${_('Licensed daily volume')}</td>
                        <td>
                            ${i18n.format_bytes(stack_table[0]['quota_bytes'], output_units='MB', mantissa=0)| h}
                        </td>
                    </tr>
                    <tr>
                        <td class="k">${_('Volume used today')}</td>
                        <td>
                            ${i18n.format_bytes(local_used_bytes, output_units='MB', mantissa=0) | h}
                            % if stack_table[0]['quota_bytes']:
                                ${_('(%s of quota)') % h(i18n.format_percent(local_used_bytes / stack_table[0]['quota_bytes']))}
                            % endif
                        </td>
                    </tr>
                % else:
                    <tr>
                        <td class="k">${_('Volume used today')}</td>
                        <td>
                            ${i18n.format_bytes(local_used_bytes, output_units='MB', mantissa=0) | h}
                        </td>
                    </tr>
                % endif
                <tr>
                    <td class="k">${_('Warning count')}</td>
                    <td>${i18n.format_number(local_warning_count) | h}</td>
                </tr>
                <tr>
                    <td class="k">${_('Debug information')}</td>
                    <td>
                        
                        <a href="${make_url(['manager','system','licensing','licenses'])}">${_('All license details')}</a>
                        <br />
                        <a href="${make_url(['manager','system','licensing','indexers'])}">${_('All indexer details')}</a>
                        
                    </td>
                </tr>
            </table>
        </div>
    % endif	

</div>
