github.com/abayer/test-infra@v0.0.5/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          if 'version' in started:
    35              return started['version'].split('+')[-1]
    36          return 'unknown'
    37  
    38      # Compute the headings first -- versions and their maximum build counts.
    39  
    40      versions = {}       # {version: {job: build_count}}
    41      version_start = {}  # {version: first_build_start_time}
    42      for job, builds in jobs.iteritems():
    43          for build, started, finished in builds:
    44              if not started:
    45                  continue
    46              version = commit(started)
    47              if not version:
    48                  continue
    49              versions.setdefault(version, {}).setdefault(job, 0)
    50              versions[version][job] += 1
    51              begin = int(started['timestamp'])
    52              version_start[version] = min(begin, version_start.get(version, begin))
    53  
    54      version_widths = {version: max(jobs.values()) for version, jobs in versions.iteritems()}
    55      versions_ordered = sorted(versions, key=lambda v: version_start[v], reverse=True)
    56      version_colstart = {}
    57      cur = 0
    58      for version in versions_ordered:
    59          version_colstart[version] = cur
    60          cur += version_widths[version]
    61  
    62      max_builds = cur
    63      headings = [(version, version_widths[version], version_start[version])
    64                  for version in versions_ordered]
    65  
    66      rows = []
    67      for job, builds in sorted(jobs.iteritems()):
    68          row = []
    69          n = 0
    70          for build, started, finished in builds:
    71              if not started or not commit(started):
    72                  minspan = 0
    73              else:
    74                  minspan = version_colstart[commit(started)]
    75              while n < minspan:
    76                  row.append(None)
    77                  n += 1
    78              row.append((build, finished['result'] if finished else 'unfinished'))
    79              n += 1
    80          rows.append((job, row))
    81  
    82      return max_builds, headings, rows