|
5 | 5 | from orlo import app |
6 | 6 | from orlo.orm import db, Release, Platform, Package, release_platform |
7 | 7 | from orlo.exceptions import OrloError, InvalidUsage |
8 | | -from orlo.util import is_int |
9 | 8 | from collections import OrderedDict |
10 | 9 |
|
11 | 10 | __author__ = 'alforbes' |
|
15 | 14 | """ |
16 | 15 |
|
17 | 16 |
|
18 | | -def _filter_release_status(query, status): |
| 17 | +def filter_release_status(query, status): |
19 | 18 | """ |
20 | 19 | Filter the given query by the given release status |
21 | 20 |
|
@@ -43,7 +42,7 @@ def _filter_release_status(query, status): |
43 | 42 | return query |
44 | 43 |
|
45 | 44 |
|
46 | | -def _filter_release_rollback(query, rollback): |
| 45 | +def filter_release_rollback(query, rollback): |
47 | 46 | """ |
48 | 47 | Filter the given query by whether the releases are rollbacks or not |
49 | 48 |
|
@@ -81,12 +80,13 @@ def apply_filters(query, args): |
81 | 80 | if field == 'latest': # this is not a comparison |
82 | 81 | continue |
83 | 82 |
|
84 | | - # special logic for these ones, as they are package attributes |
| 83 | + # special logic for these ones, as they are release attributes that |
| 84 | + # are JIT calculated from package attributes |
85 | 85 | if field == 'status': |
86 | | - query = _filter_release_status(query, value) |
| 86 | + query = filter_release_status(query, value) |
87 | 87 | continue |
88 | 88 | if field == 'rollback': |
89 | | - query = _filter_release_rollback(query, value) |
| 89 | + query = filter_release_rollback(query, value) |
90 | 90 | continue |
91 | 91 |
|
92 | 92 | if field.startswith('package_'): |
@@ -189,11 +189,15 @@ def releases(**kwargs): |
189 | 189 | query = query.order_by(stime_field()) |
190 | 190 |
|
191 | 191 | if limit: |
192 | | - if not is_int(limit): |
| 192 | + try: |
| 193 | + limit = int(limit) |
| 194 | + except ValueError: |
193 | 195 | raise InvalidUsage("limit must be a valid integer value") |
194 | 196 | query = query.limit(limit) |
195 | 197 | if offset: |
196 | | - if not is_int(offset): |
| 198 | + try: |
| 199 | + offset = int(offset) |
| 200 | + except ValueError: |
197 | 201 | raise InvalidUsage("offset must be a valid integer value") |
198 | 202 | query = query.offset(offset) |
199 | 203 |
|
@@ -420,10 +424,10 @@ def count_releases(user=None, package=None, team=None, platform=None, status=Non |
420 | 424 | query = query.filter(Release.stime <= ftime) |
421 | 425 |
|
422 | 426 | if rollback is not None: |
423 | | - query = _filter_release_rollback(query, rollback) |
| 427 | + query = filter_release_rollback(query, rollback) |
424 | 428 |
|
425 | 429 | if status: |
426 | | - query = _filter_release_status(query, status) |
| 430 | + query = filter_release_status(query, status) |
427 | 431 |
|
428 | 432 | return query |
429 | 433 |
|
@@ -500,116 +504,3 @@ def platform_list(): |
500 | 504 | return query |
501 | 505 |
|
502 | 506 |
|
503 | | -def stats_release_time(unit, summarize_by_unit=False, **kwargs): |
504 | | - """ |
505 | | - Return stats by time from the given arguments |
506 | | -
|
507 | | - Functions in this file usually return a query object, but here we are |
508 | | - returning the result, as there are several queries in play. |
509 | | -
|
510 | | - :param summarize_by_unit: Passed to add_release_by_time_to_dict() |
511 | | - :param unit: Passed to add_release_by_time_to_dict() |
512 | | - """ |
513 | | - |
514 | | - root_query = db.session.query(Release.id, Release.stime).join(Package) |
515 | | - root_query = apply_filters(root_query, kwargs) |
516 | | - |
517 | | - # Build queries for the individual stats |
518 | | - q_normal_successful = _filter_release_status( |
519 | | - _filter_release_rollback(root_query, rollback=False), 'SUCCESSFUL' |
520 | | - ) |
521 | | - q_normal_failed = _filter_release_status( |
522 | | - _filter_release_rollback(root_query, rollback=False), 'FAILED' |
523 | | - ) |
524 | | - q_rollback_successful = _filter_release_status( |
525 | | - _filter_release_rollback(root_query, rollback=True), 'SUCCESSFUL' |
526 | | - ) |
527 | | - q_rollback_failed = _filter_release_status( |
528 | | - _filter_release_rollback(root_query, rollback=True), 'FAILED' |
529 | | - ) |
530 | | - |
531 | | - output_dict = OrderedDict() |
532 | | - |
533 | | - add_releases_by_time_to_dict( |
534 | | - q_normal_successful, output_dict, ('normal', 'successful'), unit, summarize_by_unit) |
535 | | - add_releases_by_time_to_dict( |
536 | | - q_normal_failed, output_dict, ('normal', 'failed'), unit, summarize_by_unit) |
537 | | - add_releases_by_time_to_dict( |
538 | | - q_rollback_successful, output_dict, ('rollback', 'successful'), unit, |
539 | | - summarize_by_unit) |
540 | | - add_releases_by_time_to_dict( |
541 | | - q_rollback_failed, output_dict, ('rollback', 'failed'), unit, summarize_by_unit) |
542 | | - |
543 | | - return output_dict |
544 | | - |
545 | | - |
546 | | -def add_releases_by_time_to_dict(query, releases_dict, t_category, unit='month', |
547 | | - summarize_by_unit=False): |
548 | | - """ |
549 | | - Take a query and add each of its releases to a dictionary, broken down by time |
550 | | -
|
551 | | - :param dict releases_dict: Dict to add to |
552 | | - :param tuple t_category: tuple of headings, i.e. (<normal|rollback>, <successful|failed>) |
553 | | - :param query query: Query object to retrieve releases from |
554 | | - :param string unit: Can be 'iso', 'hour', 'day', 'week', 'month', 'year', |
555 | | - :param boolean summarize_by_unit: Only break down releases by the given unit, i.e. only one |
556 | | - layer deep |
557 | | - :return: |
558 | | - """ |
559 | | - |
560 | | - for release in query: |
561 | | - if summarize_by_unit: |
562 | | - tree_args = [str(getattr(release.stime, unit))] |
563 | | - else: |
564 | | - if unit == 'year': |
565 | | - tree_args = [str(release.stime.year)] |
566 | | - elif unit == 'month': |
567 | | - tree_args = [str(release.stime.year), str(release.stime.month)] |
568 | | - elif unit == 'week': |
569 | | - # First two args of isocalendar(), year and week |
570 | | - tree_args = [str(i) for i in release.stime.isocalendar()][0:2] |
571 | | - elif unit == 'iso': |
572 | | - tree_args = [str(i) for i in release.stime.isocalendar()] |
573 | | - elif unit == 'day': |
574 | | - tree_args = [str(release.stime.year), str(release.stime.month), |
575 | | - str(release.stime.day)] |
576 | | - elif unit == 'hour': |
577 | | - tree_args = [str(release.stime.year), str(release.stime.month), |
578 | | - str(release.stime.day), str(release.stime.hour)] |
579 | | - else: |
580 | | - raise InvalidUsage( |
581 | | - 'Invalid unit "{}" specified for release breakdown'.format( |
582 | | - unit)) |
583 | | - # Append categories |
584 | | - print(tree_args) |
585 | | - tree_args += t_category |
586 | | - append_tree_recursive(releases_dict, tree_args[0], tree_args) |
587 | | - |
588 | | - |
589 | | -def append_tree_recursive(tree, parent, nodes): |
590 | | - """ |
591 | | - Recursively place the nodes under each other |
592 | | -
|
593 | | - :param dict tree: The dictionary we are operating on |
594 | | - :param parent: The parent for this node |
595 | | - :param nodes: The list of nodes |
596 | | - :return: |
597 | | - """ |
598 | | - print('Called recursive function with args:\n{}, {}, {}'.format( |
599 | | - str(tree), str(parent), str(nodes))) |
600 | | - try: |
601 | | - # Get the child, one after the parent |
602 | | - child = nodes[nodes.index(parent) + 1] |
603 | | - except IndexError: |
604 | | - # Must be at end |
605 | | - if parent in tree: |
606 | | - tree[parent] += 1 |
607 | | - else: |
608 | | - tree[parent] = 1 |
609 | | - return tree |
610 | | - |
611 | | - # Otherwise recurse again |
612 | | - if parent not in tree: |
613 | | - tree[parent] = {} |
614 | | - # Child becomes the parent |
615 | | - append_tree_recursive(tree[parent], child, nodes) |
0 commit comments