buildtables.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #
  2. # BitBake Toaster Implementation
  3. #
  4. # Copyright (C) 2016 Intel Corporation
  5. #
  6. # SPDX-License-Identifier: GPL-2.0-only
  7. #
  8. from orm.models import Build, Task, Target, Package
  9. from django.db.models import Q, Sum
  10. import toastergui.tables as tables
  11. from toastergui.widgets import ToasterTable
  12. from toastergui.tablefilter import TableFilter
  13. from toastergui.tablefilter import TableFilterActionToggle
  14. class BuildTablesMixin(ToasterTable):
  15. def get_context_data(self, **kwargs):
  16. # We need to be explicit about which superclass we're calling here
  17. # Otherwise the MRO gets in a right mess
  18. context = ToasterTable.get_context_data(self, **kwargs)
  19. context['build'] = Build.objects.get(pk=kwargs['build_id'])
  20. return context
  21. class BuiltPackagesTableBase(tables.PackagesTable):
  22. """ Table to display all the packages built in a build """
  23. def __init__(self, *args, **kwargs):
  24. super(BuiltPackagesTableBase, self).__init__(*args, **kwargs)
  25. self.title = "Packages built"
  26. self.default_orderby = "name"
  27. def setup_queryset(self, *args, **kwargs):
  28. build = Build.objects.get(pk=kwargs['build_id'])
  29. self.static_context_extra['build'] = build
  30. self.static_context_extra['target_name'] = None
  31. self.queryset = build.package_set.all().exclude(recipe=None)
  32. self.queryset = self.queryset.order_by(self.default_orderby)
  33. def setup_columns(self, *args, **kwargs):
  34. super(BuiltPackagesTableBase, self).setup_columns(*args, **kwargs)
  35. def pkg_link_template(val):
  36. """ return the template used for the link with the val as the
  37. element value i.e. inside the <a></a>"""
  38. return ('''
  39. <a href="
  40. {%% url "package_built_detail" extra.build.pk data.pk %%}
  41. ">%s</a>
  42. ''' % val)
  43. def recipe_link_template(val):
  44. return ('''
  45. {%% if data.recipe %%}
  46. <a href="
  47. {%% url "recipe" extra.build.pk data.recipe.pk %%}
  48. ">%(value)s</a>
  49. {%% else %%}
  50. %(value)s
  51. {%% endif %%}
  52. ''' % {'value': val})
  53. add_pkg_link_to = 'name'
  54. add_recipe_link_to = 'recipe__name'
  55. # Add the recipe and pkg build links to the required columns
  56. for column in self.columns:
  57. # Convert to template field style accessors
  58. tmplv = column['field_name'].replace('__', '.')
  59. tmplv = "{{data.%s}}" % tmplv
  60. if column['field_name'] is add_pkg_link_to:
  61. # Don't overwrite an existing template
  62. if column['static_data_template']:
  63. column['static_data_template'] =\
  64. pkg_link_template(column['static_data_template'])
  65. else:
  66. column['static_data_template'] = pkg_link_template(tmplv)
  67. column['static_data_name'] = column['field_name']
  68. elif column['field_name'] is add_recipe_link_to:
  69. # Don't overwrite an existing template
  70. if column['static_data_template']:
  71. column['static_data_template'] =\
  72. recipe_link_template(column['static_data_template'])
  73. else:
  74. column['static_data_template'] =\
  75. recipe_link_template(tmplv)
  76. column['static_data_name'] = column['field_name']
  77. self.add_column(title="Layer",
  78. field_name="recipe__layer_version__layer__name",
  79. hidden=True,
  80. orderable=True)
  81. layer_branch_template = '''
  82. {%if not data.recipe.layer_version.layer.local_source_dir %}
  83. <span class="text-muted">{{data.recipe.layer_version.branch}}</span>
  84. {% else %}
  85. <span class="text-muted">Not applicable</span>
  86. <span class="glyphicon glyphicon-question-sign get-help" data-original-title="" title="The source code of {{data.recipe.layer_version.layer.name}} is not in a Git repository, so there is no branch associated with it"> </span>
  87. {% endif %}
  88. '''
  89. self.add_column(title="Layer branch",
  90. field_name="recipe__layer_version__branch",
  91. hidden=True,
  92. static_data_name="recipe__layer_version__branch",
  93. static_data_template=layer_branch_template,
  94. orderable=True)
  95. git_rev_template = '''
  96. {% if not data.recipe.layer_version.layer.local_source_dir %}
  97. {% with vcs_ref=data.recipe.layer_version.commit %}
  98. {% include 'snippets/gitrev_popover.html' %}
  99. {% endwith %}
  100. {% else %}
  101. <span class="text-muted">Not applicable</span>
  102. <span class="glyphicon glyphicon-question-sign get-help" data-original-title="" title="The source code of {{data.recipe.layer_version.layer.name}} is not in a Git repository, so there is no revision associated with it"> </span>
  103. {% endif %}
  104. '''
  105. self.add_column(title="Layer commit",
  106. static_data_name='vcs_ref',
  107. static_data_template=git_rev_template,
  108. hidden=True)
  109. class BuiltPackagesTable(BuildTablesMixin, BuiltPackagesTableBase):
  110. """ Show all the packages built for the selected build """
  111. def __init__(self, *args, **kwargs):
  112. super(BuiltPackagesTable, self).__init__(*args, **kwargs)
  113. self.title = "Packages built"
  114. self.default_orderby = "name"
  115. self.empty_state =\
  116. ('<strong>No packages were built.</strong> How did this happen? '
  117. 'Well, BitBake reuses as much stuff as possible. '
  118. 'If all of the packages needed were already built and available '
  119. 'in your build infrastructure, BitBake '
  120. 'will not rebuild any of them. This might be slightly confusing, '
  121. 'but it does make everything faster.')
  122. def setup_columns(self, *args, **kwargs):
  123. super(BuiltPackagesTable, self).setup_columns(*args, **kwargs)
  124. def remove_dep_cols(columns):
  125. for column in columns:
  126. # We don't need these fields
  127. if column['static_data_name'] in ['reverse_dependencies',
  128. 'dependencies']:
  129. continue
  130. yield column
  131. self.columns = list(remove_dep_cols(self.columns))
  132. class InstalledPackagesTable(BuildTablesMixin, BuiltPackagesTableBase):
  133. """ Show all packages installed in an image """
  134. def __init__(self, *args, **kwargs):
  135. super(InstalledPackagesTable, self).__init__(*args, **kwargs)
  136. self.title = "Packages Included"
  137. self.default_orderby = "name"
  138. def make_package_list(self, target):
  139. # The database design means that you get the intermediate objects and
  140. # not package objects like you'd really want so we get them here
  141. pkgs = target.target_installed_package_set.values_list('package',
  142. flat=True)
  143. return Package.objects.filter(pk__in=pkgs)
  144. def get_context_data(self, **kwargs):
  145. context = super(InstalledPackagesTable,
  146. self).get_context_data(**kwargs)
  147. target = Target.objects.get(pk=kwargs['target_id'])
  148. packages = self.make_package_list(target)
  149. context['packages_sum'] = packages.aggregate(
  150. Sum('installed_size'))['installed_size__sum']
  151. context['target'] = target
  152. return context
  153. def setup_queryset(self, *args, **kwargs):
  154. build = Build.objects.get(pk=kwargs['build_id'])
  155. self.static_context_extra['build'] = build
  156. target = Target.objects.get(pk=kwargs['target_id'])
  157. # We send these separately because in the case of image details table
  158. # we don't have a target just the recipe name as the target
  159. self.static_context_extra['target_name'] = target.target
  160. self.static_context_extra['target_id'] = target.pk
  161. self.static_context_extra['add_links'] = True
  162. self.queryset = self.make_package_list(target)
  163. self.queryset = self.queryset.order_by(self.default_orderby)
  164. def setup_columns(self, *args, **kwargs):
  165. super(InstalledPackagesTable, self).setup_columns(**kwargs)
  166. self.add_column(title="Installed size",
  167. static_data_name="installed_size",
  168. static_data_template="{% load projecttags %}"
  169. "{{data.size|filtered_filesizeformat}}",
  170. orderable=True,
  171. hidden=True)
  172. # Add the template to show installed name for installed packages
  173. install_name_tmpl =\
  174. ('<a href="{% url "package_included_detail" extra.build.pk'
  175. ' extra.target_id data.pk %}">{{data.name}}</a>'
  176. '{% if data.installed_name and data.installed_name !='
  177. ' data.name %}'
  178. '<span class="text-muted"> as {{data.installed_name}}</span>'
  179. ' <span class="glyphicon glyphicon-question-sign get-help hover-help"'
  180. ' title="{{data.name}} was renamed at packaging time and'
  181. ' was installed in your image as {{data.installed_name}}'
  182. '"></span>{% endif %} ')
  183. for column in self.columns:
  184. if column['static_data_name'] == 'name':
  185. column['static_data_template'] = install_name_tmpl
  186. break
  187. class BuiltRecipesTable(BuildTablesMixin):
  188. """ Table to show the recipes that have been built in this build """
  189. def __init__(self, *args, **kwargs):
  190. super(BuiltRecipesTable, self).__init__(*args, **kwargs)
  191. self.title = "Recipes built"
  192. self.default_orderby = "name"
  193. def setup_queryset(self, *args, **kwargs):
  194. build = Build.objects.get(pk=kwargs['build_id'])
  195. self.static_context_extra['build'] = build
  196. self.queryset = build.get_recipes()
  197. self.queryset = self.queryset.order_by(self.default_orderby)
  198. def setup_columns(self, *args, **kwargs):
  199. recipe_name_tmpl =\
  200. '<a href="{% url "recipe" extra.build.pk data.pk %}">'\
  201. '{{data.name}}'\
  202. '</a>'
  203. recipe_file_tmpl =\
  204. '{{data.file_path}}'\
  205. '{% if data.pathflags %}<i>({{data.pathflags}})</i>'\
  206. '{% endif %}'
  207. git_branch_template = '''
  208. {% if data.layer_version.layer.local_source_dir %}
  209. <span class="text-muted">Not applicable</span>
  210. <span class="glyphicon glyphicon-question-sign get-help" data-original-title="" title="The source code of {{data.layer_version.layer.name}} is not in a Git repository, so there is no branch associated with it"> </span>
  211. {% else %}
  212. <span>{{data.layer_version.branch}}</span>
  213. {% endif %}
  214. '''
  215. git_rev_template = '''
  216. {% if data.layer_version.layer.local_source_dir %}
  217. <span class="text-muted">Not applicable</span>
  218. <span class="glyphicon glyphicon-question-sign get-help" data-original-title="" title="The source code of {{data.layer_version.layer.name}} is not in a Git repository, so there is no commit associated with it"> </span>
  219. {% else %}
  220. {% with vcs_ref=data.layer_version.commit %}
  221. {% include 'snippets/gitrev_popover.html' %}
  222. {% endwith %}
  223. {% endif %}
  224. '''
  225. depends_on_tmpl = '''
  226. {% with deps=data.r_dependencies_recipe.all %}
  227. {% with count=deps|length %}
  228. {% if count %}
  229. <a class="btn btn-default" title="
  230. <a href='{% url "recipe" extra.build.pk data.pk %}#dependencies'>
  231. {{data.name}}</a> dependencies"
  232. data-content="<ul class='list-unstyled'>
  233. {% for dep in deps|dictsort:"depends_on.name"%}
  234. <li><a href='{% url "recipe" extra.build.pk dep.depends_on.pk %}'>
  235. {{dep.depends_on.name}}</a></li>
  236. {% endfor %}
  237. </ul>">
  238. {{count}}
  239. </a>
  240. {% endif %}{% endwith %}{% endwith %}
  241. '''
  242. rev_depends_tmpl = '''
  243. {% with revs=data.r_dependencies_depends.all %}
  244. {% with count=revs|length %}
  245. {% if count %}
  246. <a class="btn btn-default"
  247. title="
  248. <a href='{% url "recipe" extra.build.pk data.pk %}#brought-in-by'>
  249. {{data.name}}</a> reverse dependencies"
  250. data-content="<ul class='list-unstyled'>
  251. {% for dep in revs|dictsort:"recipe.name" %}
  252. <li>
  253. <a href='{% url "recipe" extra.build.pk dep.recipe.pk %}'>
  254. {{dep.recipe.name}}
  255. </a></li>
  256. {% endfor %}
  257. </ul>">
  258. {{count}}
  259. </a>
  260. {% endif %}{% endwith %}{% endwith %}
  261. '''
  262. self.add_column(title="Recipe",
  263. field_name="name",
  264. static_data_name='name',
  265. orderable=True,
  266. hideable=False,
  267. static_data_template=recipe_name_tmpl)
  268. self.add_column(title="Version",
  269. hideable=False,
  270. field_name="version")
  271. self.add_column(title="Dependencies",
  272. static_data_name="dependencies",
  273. static_data_template=depends_on_tmpl)
  274. self.add_column(title="Reverse dependencies",
  275. static_data_name="revdeps",
  276. static_data_template=rev_depends_tmpl,
  277. help_text='Recipe build-time reverse dependencies'
  278. ' (i.e. the recipes that depend on this recipe)')
  279. self.add_column(title="Recipe file",
  280. field_name="file_path",
  281. static_data_name="file_path",
  282. static_data_template=recipe_file_tmpl,
  283. hidden=True)
  284. self.add_column(title="Section",
  285. field_name="section",
  286. orderable=True,
  287. hidden=True)
  288. self.add_column(title="License",
  289. field_name="license",
  290. help_text='Multiple license names separated by the'
  291. ' pipe character indicates a choice between licenses.'
  292. ' Multiple license names separated by the ampersand'
  293. ' character indicates multiple licenses exist that'
  294. ' cover different parts of the source',
  295. orderable=True)
  296. self.add_column(title="Layer",
  297. field_name="layer_version__layer__name",
  298. orderable=True)
  299. self.add_column(title="Layer branch",
  300. field_name="layer_version__branch",
  301. static_data_name="layer_version__branch",
  302. static_data_template=git_branch_template,
  303. orderable=True,
  304. hidden=True)
  305. self.add_column(title="Layer commit",
  306. static_data_name="commit",
  307. static_data_template=git_rev_template,
  308. hidden=True)
  309. class BuildTasksTable(BuildTablesMixin):
  310. """ Table to show the tasks that run in this build """
  311. def __init__(self, *args, **kwargs):
  312. super(BuildTasksTable, self).__init__(*args, **kwargs)
  313. self.title = "Tasks"
  314. self.default_orderby = "order"
  315. # Toggle these columns on off for Time/CPU usage/Disk I/O tables
  316. self.toggle_columns = {}
  317. def setup_queryset(self, *args, **kwargs):
  318. build = Build.objects.get(pk=kwargs['build_id'])
  319. self.static_context_extra['build'] = build
  320. self.queryset = build.task_build.filter(~Q(order=None))
  321. self.queryset = self.queryset.order_by(self.default_orderby)
  322. def setup_filters(self, *args, **kwargs):
  323. # Execution outcome types filter
  324. executed_outcome = TableFilter(name="execution_outcome",
  325. title="Filter Tasks by 'Executed")
  326. exec_outcome_action_exec = TableFilterActionToggle(
  327. "executed",
  328. "Executed Tasks",
  329. Q(task_executed=True))
  330. exec_outcome_action_not_exec = TableFilterActionToggle(
  331. "not_executed",
  332. "Not Executed Tasks",
  333. Q(task_executed=False))
  334. executed_outcome.add_action(exec_outcome_action_exec)
  335. executed_outcome.add_action(exec_outcome_action_not_exec)
  336. # Task outcome types filter
  337. task_outcome = TableFilter(name="task_outcome",
  338. title="Filter Task by 'Outcome'")
  339. for outcome_enum, title in Task.TASK_OUTCOME:
  340. if outcome_enum is Task.OUTCOME_NA:
  341. continue
  342. action = TableFilterActionToggle(
  343. title.replace(" ", "_").lower(),
  344. "%s Tasks" % title,
  345. Q(outcome=outcome_enum))
  346. task_outcome.add_action(action)
  347. # SSTATE outcome types filter
  348. sstate_outcome = TableFilter(name="sstate_outcome",
  349. title="Filter Task by 'Cache attempt'")
  350. for sstate_result_enum, title in Task.SSTATE_RESULT:
  351. action = TableFilterActionToggle(
  352. title.replace(" ", "_").lower(),
  353. "Tasks with '%s' attempts" % title,
  354. Q(sstate_result=sstate_result_enum))
  355. sstate_outcome.add_action(action)
  356. self.add_filter(sstate_outcome)
  357. self.add_filter(executed_outcome)
  358. self.add_filter(task_outcome)
  359. def setup_columns(self, *args, **kwargs):
  360. self.toggle_columns['order'] = len(self.columns)
  361. recipe_name_tmpl =\
  362. '<a href="{% url "recipe" extra.build.pk data.recipe.pk %}">'\
  363. '{{data.recipe.name}}'\
  364. '</a>'
  365. def task_link_tmpl(val):
  366. return ('<a name="task-{{data.order}}"'
  367. 'href="{%% url "task" extra.build.pk data.pk %%}">'
  368. '%s'
  369. '</a>') % str(val)
  370. self.add_column(title="Order",
  371. static_data_name="order",
  372. static_data_template='{{data.order}}',
  373. hideable=False,
  374. orderable=True)
  375. self.add_column(title="Task",
  376. static_data_name="task_name",
  377. static_data_template=task_link_tmpl(
  378. "{{data.task_name}}"),
  379. hideable=False,
  380. orderable=True)
  381. self.add_column(title="Recipe",
  382. static_data_name='recipe__name',
  383. static_data_template=recipe_name_tmpl,
  384. hideable=False,
  385. orderable=True)
  386. self.add_column(title="Recipe version",
  387. field_name='recipe__version',
  388. hidden=True)
  389. self.add_column(title="Executed",
  390. static_data_name="task_executed",
  391. static_data_template='{{data.get_executed_display}}',
  392. filter_name='execution_outcome',
  393. orderable=True)
  394. self.static_context_extra['OUTCOME_FAILED'] = Task.OUTCOME_FAILED
  395. outcome_tmpl = '{{data.outcome_text}}'
  396. outcome_tmpl = ('%s '
  397. '{%% if data.outcome = extra.OUTCOME_FAILED %%}'
  398. '<a href="{%% url "build_artifact" extra.build.pk '
  399. ' "tasklogfile" data.pk %%}">'
  400. ' <span class="glyphicon glyphicon-download-alt'
  401. ' get-help" title="Download task log file"></span>'
  402. '</a> {%% endif %%}'
  403. '<span class="glyphicon glyphicon-question-sign'
  404. ' get-help hover-help" style="visibility: hidden;" '
  405. 'title="{{data.get_outcome_help}}"></span>'
  406. ) % outcome_tmpl
  407. self.add_column(title="Outcome",
  408. static_data_name="outcome",
  409. static_data_template=outcome_tmpl,
  410. filter_name="task_outcome",
  411. orderable=True)
  412. self.toggle_columns['sstate_result'] = len(self.columns)
  413. self.add_column(title="Cache attempt",
  414. static_data_name="sstate_result",
  415. static_data_template='{{data.sstate_text}}',
  416. filter_name="sstate_outcome",
  417. orderable=True)
  418. self.toggle_columns['elapsed_time'] = len(self.columns)
  419. self.add_column(
  420. title="Time (secs)",
  421. static_data_name="elapsed_time",
  422. static_data_template='{% load projecttags %}{% load humanize %}'
  423. '{{data.elapsed_time|format_none_and_zero|floatformat:2}}',
  424. orderable=True,
  425. hidden=True)
  426. self.toggle_columns['cpu_time_sys'] = len(self.columns)
  427. self.add_column(
  428. title="System CPU time (secs)",
  429. static_data_name="cpu_time_system",
  430. static_data_template='{% load projecttags %}{% load humanize %}'
  431. '{{data.cpu_time_system|format_none_and_zero|floatformat:2}}',
  432. hidden=True,
  433. orderable=True)
  434. self.toggle_columns['cpu_time_user'] = len(self.columns)
  435. self.add_column(
  436. title="User CPU time (secs)",
  437. static_data_name="cpu_time_user",
  438. static_data_template='{% load projecttags %}{% load humanize %}'
  439. '{{data.cpu_time_user|format_none_and_zero|floatformat:2}}',
  440. hidden=True,
  441. orderable=True)
  442. self.toggle_columns['disk_io'] = len(self.columns)
  443. self.add_column(
  444. title="Disk I/O (ms)",
  445. static_data_name="disk_io",
  446. static_data_template='{% load projecttags %}{% load humanize %}'
  447. '{{data.disk_io|format_none_and_zero|filtered_filesizeformat}}',
  448. hidden=True,
  449. orderable=True)
  450. class BuildTimeTable(BuildTasksTable):
  451. """ Same as tasks table but the Time column is default displayed"""
  452. def __init__(self, *args, **kwargs):
  453. super(BuildTimeTable, self).__init__(*args, **kwargs)
  454. self.default_orderby = "-elapsed_time"
  455. def setup_columns(self, *args, **kwargs):
  456. super(BuildTimeTable, self).setup_columns(**kwargs)
  457. self.columns[self.toggle_columns['order']]['hidden'] = True
  458. self.columns[self.toggle_columns['order']]['hideable'] = True
  459. self.columns[self.toggle_columns['sstate_result']]['hidden'] = True
  460. self.columns[self.toggle_columns['elapsed_time']]['hidden'] = False
  461. class BuildCPUTimeTable(BuildTasksTable):
  462. """ Same as tasks table but the CPU usage columns are default displayed"""
  463. def __init__(self, *args, **kwargs):
  464. super(BuildCPUTimeTable, self).__init__(*args, **kwargs)
  465. self.default_orderby = "-cpu_time_system"
  466. def setup_columns(self, *args, **kwargs):
  467. super(BuildCPUTimeTable, self).setup_columns(**kwargs)
  468. self.columns[self.toggle_columns['order']]['hidden'] = True
  469. self.columns[self.toggle_columns['order']]['hideable'] = True
  470. self.columns[self.toggle_columns['sstate_result']]['hidden'] = True
  471. self.columns[self.toggle_columns['cpu_time_sys']]['hidden'] = False
  472. self.columns[self.toggle_columns['cpu_time_user']]['hidden'] = False
  473. class BuildIOTable(BuildTasksTable):
  474. """ Same as tasks table but the Disk IO column is default displayed"""
  475. def __init__(self, *args, **kwargs):
  476. super(BuildIOTable, self).__init__(*args, **kwargs)
  477. self.default_orderby = "-disk_io"
  478. def setup_columns(self, *args, **kwargs):
  479. super(BuildIOTable, self).setup_columns(**kwargs)
  480. self.columns[self.toggle_columns['order']]['hidden'] = True
  481. self.columns[self.toggle_columns['order']]['hideable'] = True
  482. self.columns[self.toggle_columns['sstate_result']]['hidden'] = True
  483. self.columns[self.toggle_columns['disk_io']]['hidden'] = False