github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/pull_request.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  
    18  def builds_to_table(jobs):
    19      """
    20      Convert a list of builds into arguments suitable for rendering.
    21  
    22      Args:
    23          jobs: a dict of {job: [(build, started.json, finished.json), ...]}
    24      Returns:
    25          max_builds: the number of build columns
    26          headings: a list of [(version, column width, timestamp)]
    27          rows: a list of [(build, [(number, status) or None])]
    28      """
    29      # pylint: disable=too-many-locals
    30  
    31      def commit(started):
    32          if 'pull' in started:
    33              return started['pull'].split(':')[-1]
    34          return started['version'].split('+')[-1]
    35  
    36      # Compute the headings first -- versions and their maximum build counts.
    37  
    38      versions = {}       # {version: {job: build_count}}
    39      version_start = {}  # {version: first_build_start_time}
    40      for job, builds in jobs.iteritems():
    41          for build, started, finished in builds:
    42              if not started:
    43                  continue
    44              version = commit(started)
    45              if not version:
    46                  continue
    47              versions.setdefault(version, {}).setdefault(job, 0)
    48              versions[version][job] += 1
    49              begin = int(started['timestamp'])
    50              version_start[version] = min(begin, version_start.get(version, begin))
    51  
    52      version_widths = {version: max(jobs.values()) for version, jobs in versions.iteritems()}
    53      versions_ordered = sorted(versions, key=lambda v: version_start[v], reverse=True)
    54      version_colstart = {}
    55      cur = 0
    56      for version in versions_ordered:
    57          version_colstart[version] = cur
    58          cur += version_widths[version]
    59  
    60      max_builds = cur
    61      headings = [(version, version_widths[version], version_start[version])
    62                  for version in versions_ordered]
    63  
    64      rows = []
    65      for job, builds in sorted(jobs.iteritems()):
    66          row = []
    67          n = 0
    68          for build, started, finished in builds:
    69              if not started or not commit(started):
    70                  minspan = 0
    71              else:
    72                  minspan = version_colstart[commit(started)]
    73              while n < minspan:
    74                  row.append(None)
    75                  n += 1
    76              row.append((build, finished['result'] if finished else 'unfinished'))
    77              n += 1
    78          rows.append((job, row))
    79  
    80      return max_builds, headings, rows