Guest User

Untitled

a guest
Dec 12th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import plotly
  2. import plotly.plotly as py
  3. import plotly.graph_objs as go
  4.  
  5. # authentication
  6. from settings_secret import PLOTLY_USERNAME, PLOTLY_API_TOKEN
  7. plotly.tools.set_credentials_file(username=PLOTLY_USERNAME, api_key=PLOTLY_API_TOKEN)
  8.  
  9. import dateutil.parser
  10.  
  11. # relies on gitguard to interface with github API
  12. import gitguard
  13.  
  14. STANDARD_WIDTH = 800
  15. STANDARD_HEIGHT = 640
  16.  
  17. def _make_plot(data, layout, out_file='out.png'):
  18. """
  19. Generates and saves a plotly plot given some data.
  20.  
  21. Args:
  22. data (list): a list of graph_objs (e.g. go.Bar, go.Line)
  23. layout (go.Layout): a go.Layout object
  24. out_file (str): name of the output file; default 'out.png'
  25.  
  26. """
  27. fig = go.Figure(data=data, layout=layout)
  28. py.image.save_as(fig, filename = out_file)
  29. return
  30.  
  31. def _get_team_contribution_data_layout(repo_link, username=None, password=None):
  32. contributor_names = gitguard.get_repo_contributors(repo_link, username, password)
  33. contributor_commits = []
  34. contributor_insertions = []
  35. contributor_deletions = []
  36.  
  37. for contributor in contributor_names:
  38. c, a, d = gitguard.get_stats_by_author(repo_link, contributor, username, password)
  39. contributor_commits.append(c)
  40. contributor_insertions.append(a)
  41. contributor_deletions.append(d)
  42.  
  43. trace_commit = go.Bar(
  44. x = contributor_names,
  45. y = contributor_commits,
  46. name = 'Commits',
  47. xaxis = 'x1',
  48. yaxis = 'y1',
  49. )
  50.  
  51. trace_insertion = go.Bar(
  52. x = contributor_names,
  53. y = contributor_insertions,
  54. name = 'Insertions',
  55. xaxis = 'x2',
  56. yaxis = 'y2',
  57. )
  58.  
  59. trace_deletion = go.Bar(
  60. x = contributor_names,
  61. y = contributor_deletions,
  62. name = 'Deletions',
  63. xaxis = 'x3',
  64. yaxis = 'y3',
  65. )
  66.  
  67. layout_team_contribution = go.Layout(
  68. barmode = 'group',
  69. title = 'Team Contribution for %s' % repo_link,
  70. #width = STANDARD_WIDTH,
  71. #height = STANDARD_HEIGHT,
  72.  
  73. xaxis=dict(
  74. domain=[0, 0.3]
  75. ),
  76. xaxis2=dict(
  77. domain=[0.35, 0.65]
  78. ),
  79. xaxis3=dict(
  80. domain=[0.7, 1]
  81. ),
  82. yaxis=dict(
  83. domain=[0,1],
  84. anchor='x1',
  85. ),
  86. yaxis2=dict(
  87. domain=[0,1],
  88. anchor='x2',
  89. ),
  90. yaxis3=dict(
  91. domain=[0,1],
  92. anchor='x3',
  93. ),
  94. )
  95.  
  96. return [trace_commit, trace_insertion, trace_deletion], layout_team_contribution
  97.  
  98. def get_team_contribution_summary(repo_link, out_file, username=None, password=None):
  99. data, layout = _get_team_contribution_data_layout(repo_link, username, password)
  100. _make_plot(data, layout, out_file)
  101. return
  102.  
  103. def _extract_date_from_timestamp(timestamp):
  104. return dateutil.parser.parse(timestamp).date()
  105.  
  106. def _get_commit_history_for_author(repo_link, author_name=None, start=None, end=None, path=None, username=None, password=None):
  107. commit_history = gitguard.get_commit_history(repo_link, author_name, start, end, path, username, password)
  108. commit_counts = _histogram_commit_history(commit_history)
  109. return _extract_x_y_data(commit_counts)
  110.  
  111. def _histogram_commit_history(commit_history):
  112. commit_counts = {}
  113.  
  114. for commit in commit_history:
  115. date = _extract_date_from_timestamp(commit['timestamp'])
  116. if date not in commit_counts:
  117. commit_counts[date] = 0
  118. commit_counts[date] += 1
  119.  
  120. return commit_counts
  121.  
  122. def _extract_x_y_data(histogram):
  123. x = list(histogram.keys())
  124. x.sort()
  125. y = []
  126. for key in x:
  127. y.append(histogram[key])
  128. return x, y
  129.  
  130. def _get_team_commit_history_data_layout(repo_link, team, start=None, end=None, path=None, username=None, password=None):
  131. data = []
  132. # make a Scatter graph object (trace) for each author labelled appropriately
  133. for author in team:
  134. timestamps, commits = _get_commit_history_for_author(repo_link, author, start, end, path, username, password)
  135. trace = go.Scatter(
  136. x = timestamps,
  137. y = commits,
  138. name = author
  139. )
  140. data.append(trace)
  141.  
  142. title = 'Team Commit History for %s' % repo_link
  143.  
  144. if start:
  145. title += ' from %s' % start
  146. if end:
  147. title += ' to %s' % end
  148. if path:
  149. title += ' for %s' % path
  150.  
  151. layout = go.Layout(
  152. title = title,
  153. yaxis = dict(title = 'Commits'),
  154. )
  155.  
  156. return data, layout
  157.  
  158. def get_team_commit_history(repo_link, out_file, start=None, end=None, path=None, username=None, password=None):
  159. team = gitguard.get_repo_contributors(repo_link)
  160. data, layout = _get_team_commit_history_data_layout(repo_link, team)
  161. _make_plot(data, layout, out_file)
  162. return
Add Comment
Please, Sign In to add comment