## The contents of this file are subject to the Common Public Attribution
## License Version 1.0. (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License at
## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
## License Version 1.1, but Sections 14 and 15 have been added to cover use of
## software over a computer network and provide for limited attribution for the
## Original Developer. In addition, Exhibit A has been modified to be consistent
## with Exhibit B.
##
## Software distributed under the License is distributed on an "AS IS" basis,
## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
## the specific language governing rights and limitations under the License.
##
## The Original Code is Reddit.
##
## The Original Developer is the Initial Developer.  The Initial Developer of
## the Original Code is CondeNet, Inc.
##
## All portions of the code written by CondeNet are Copyright (c) 2006-2010
## CondeNet, Inc. All Rights Reserved.
################################################################################
<%!
   from r2.lib.template_helpers import static
   from r2.lib import js
   import locale
   from r2.models.subreddit import DomainSR, FakeSubreddit
   def num(x):
      return locale.format('%d', x, True)
 %>

${unsafe(js.use('flot'))}

<div class="promoted-traffic">

  <div class="instructions">
    <p>
      Below you will see your promotion's impression and click traffic per hour of promotion.  Please note that these traffic totals will lag behind by two to three hours, and that daily totals will be preliminary till 24 hours after the link has finished its run.
    </p>
    <p>
      Also below is a form which can be used to share traffic results with another user.  
    </p>
  </div>

${thing.viewers}

%if thing.traffic:
  <h1>
    ${_("Promotion Traffic")}
      <a href="/traffic/${thing.thing._id36}.csv">
      ${_("(download as .csv)")}
      </a>
  </h1>
  %if thing.imp_graph:
    ${thing.imp_graph}
  %endif
  %if thing.cli_graph:
    ${thing.cli_graph}
  %endif
  <table class="traffic-table">
    <tr>
      <th>date</th>
      <th colspan="2">Impresions</th>
      <th colspan="2">Clicks</th>
      <th colspan="2">click-thru (%)</th>
    </tr>
    <tr>
      <th></th>
      <th>unique</th>
      <th>total</th>
      <th>unique</th>
      <th>total</th>
      <th>unique</th>
      <th>total</th>
    </tr>
    %for x in thing.to_iter():
       <tr>
         %if x[1] != '0' or x[2] != '0':
           %for y in x:
             <td>${y}</td>
           %endfor
         %endif
       </tr>
    %endfor
    %if thing.totals:
      <% 
         uimp, nimp, ucli, ncli = thing.totals
       %>
     <tr style="font-weight:bold">
        <td>
          total 
          %if thing.preliminary:
            (users approx)
          %endif
        </td>
        <td ${"class='prelim'" if thing.preliminary else ''}>
          ${num(uimp)}${'*' if thing.preliminary else ''}
        </td>
        <td>${num(nimp)}</td>
        <td ${"class='prelim'" if thing.preliminary else ''}>
          ${num(ucli)}${'*' if thing.preliminary else ''}
        </td>
        <td>${num(ncli)}</td>
        <td>${("%.2f%%" % (float(100*ucli) / uimp)) if nimp else "--.--%"}</td>
        <td>${("%.2f%%" % (float(100*ncli) / nimp)) if nimp else "--.--%"}</td>
      </tr>
      %if thing.preliminary:
      <tr>
        <td colspan="7" class="prelim error">
          * totals are preliminary until 24 hours after the end of the promotion.
        </td>
      </tr>
      %endif
    %endif
  </table>
%endif
</div>
