<s> from __future__ import division <EOL> from datetime import datetime , timedelta <EOL> from flask . ext . restful import reqparse <EOL> from sqlalchemy . orm import contains_eager , joinedload <EOL> from sqlalchemy . sql import func <EOL> from changes . api . auth import requires_admin <EOL> from changes . api . base import APIView <EOL> from changes . config import db <EOL> from changes . constants import Result , Status <EOL> from changes . lib import build_type <EOL> from changes . models . build import Build <EOL> from changes . models . project import Project , ProjectOption , ProjectStatus <EOL> from changes . models . repository import Repository <EOL> from changes . models . source import Source <EOL> OPTION_DEFAULTS = { <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> STATUS_CHOICES = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ProjectDetailsAPIView ( APIView ) : <EOL> post_parser = reqparse . RequestParser ( ) <EOL> post_parser . add_argument ( '<STR_LIT:name>' ) <EOL> post_parser . add_argument ( '<STR_LIT>' ) <EOL> post_parser . add_argument ( '<STR_LIT>' ) <EOL> post_parser . add_argument ( '<STR_LIT:status>' , choices = STATUS_CHOICES ) <EOL> def _get_avg_duration ( self , project , start_period , end_period ) : <EOL> avg_duration = db . session . query ( <EOL> func . avg ( Build . duration ) <EOL> ) . join ( <EOL> Source , Build . source_id == Source . id , <EOL> ) . filter ( <EOL> Build . project_id == project . id , <EOL> Build . date_created >= start_period , <EOL> Build . date_created < end_period , <EOL> Build . status == Status . finished , <EOL> Build . result == Result . passed , <EOL> * build_type . get_any_commit_build_filters ( ) <EOL> ) . scalar ( ) or None <EOL> if avg_duration is not None : <EOL> avg_duration = float ( avg_duration ) <EOL> return avg_duration <EOL> def _get_green_percent ( self , project , start_period , end_period ) : <EOL> build_counts = dict ( db . session . query ( <EOL> Build . result , func . count ( ) <EOL> ) . join ( <EOL> Source , Build . source_id == Source . id , <EOL> ) . filter ( <EOL> Build . project_id == project . id , <EOL> Build . date_created >= start_period , <EOL> Build . date_created < end_period , <EOL> Build . status == Status . finished , <EOL> Build . result . in_ ( [ Result . passed , Result . failed ] ) , <EOL> * build_type . get_any_commit_build_filters ( ) <EOL> ) . group_by ( <EOL> Build . result , <EOL> ) ) <EOL> failed_builds = build_counts . get ( Result . failed ) or <NUM_LIT:0> <EOL> passed_builds = build_counts . get ( Result . passed ) or <NUM_LIT:0> <EOL> if passed_builds : <EOL> green_percent = int ( ( passed_builds / ( failed_builds + passed_builds ) ) * <NUM_LIT:100> ) <EOL> elif failed_builds : <EOL> green_percent = <NUM_LIT:0> <EOL> else : <EOL> green_percent = None <EOL> return green_percent <EOL> def _get_stats ( self , project ) : <EOL> window = timedelta ( days = <NUM_LIT:7> ) <EOL> end_period = datetime . utcnow ( ) <EOL> start_period = end_period - window <EOL> prev_end_period = start_period <EOL> prev_start_period = start_period - window <EOL> green_percent = self . _get_green_percent ( project , start_period , end_period ) <EOL> prev_green_percent = self . _get_green_percent ( <EOL> project , prev_start_period , prev_end_period ) <EOL> avg_duration = self . _get_avg_duration ( project , start_period , end_period ) <EOL> prev_avg_duration = self . _get_avg_duration ( <EOL> project , prev_start_period , prev_end_period ) <EOL> return { <EOL> '<STR_LIT>' : green_percent , <EOL> '<STR_LIT>' : prev_green_percent , <EOL> '<STR_LIT>' : avg_duration , <EOL> '<STR_LIT>' : prev_avg_duration , <EOL> } <EOL> def get ( self , project_id ) : <EOL> project = Project . get ( project_id ) <EOL> if project is None : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> last_build = Build . query . options ( <EOL> joinedload ( '<STR_LIT>' ) , <EOL> contains_eager ( '<STR_LIT:source>' ) <EOL> ) . join ( <EOL> Source , Build . source_id == Source . id , <EOL> ) . filter ( <EOL> Build . project == project , <EOL> Build . status == Status . finished , <EOL> * build_type . get_any_commit_build_filters ( ) <EOL> ) . order_by ( <EOL> Build . date_created . desc ( ) , <EOL> ) . first ( ) <EOL> if not last_build or last_build . result == Result . passed : <EOL> last_passing_build = last_build <EOL> else : <EOL> last_passing_build = Build . query . options ( <EOL> joinedload ( '<STR_LIT>' ) , <EOL> contains_eager ( '<STR_LIT:source>' ) <EOL> ) . join ( <EOL> Source , Build . source_id == Source . id , <EOL> ) . filter ( <EOL> Build . project == project , <EOL> Build . result == Result . passed , <EOL> Build . status == Status . finished , <EOL> * build_type . get_any_commit_build_filters ( ) <EOL> ) . order_by ( <EOL> Build . date_created . desc ( ) , <EOL> ) . first ( ) <EOL> options = dict ( <EOL> ( o . name , o . value ) for o in ProjectOption . query . filter ( <EOL> ProjectOption . project_id == project . id , <EOL> ) <EOL> ) <EOL> for key , value in OPTION_DEFAULTS . iteritems ( ) : <EOL> options . setdefault ( key , value ) <EOL> data = self . serialize ( project ) <EOL> data [ '<STR_LIT>' ] = last_build <EOL> data [ '<STR_LIT>' ] = last_passing_build <EOL> data [ '<STR_LIT>' ] = project . repository <EOL> data [ '<STR_LIT>' ] = options <EOL> data [ '<STR_LIT>' ] = self . _get_stats ( project ) <EOL> return self . respond ( data ) <EOL> @ requires_admin <EOL> def post ( self , project_id ) : <EOL> project = Project . get ( project_id ) <EOL> if project is None : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> args = self . post_parser . parse_args ( ) <EOL> if args . name : <EOL> project . name = args . name <EOL> if args . slug : <EOL> match = Project . query . filter ( <EOL> Project . slug == args . slug , <EOL> Project . id != project . id , <EOL> ) . first ( ) <EOL> if match : <EOL> return '<STR_LIT>' % ( args . slug , ) , <NUM_LIT> <EOL> project . slug = args . slug <EOL> if args . repository : <EOL> repository = Repository . get ( args . repository ) <EOL> if repository is None : <EOL> return '<STR_LIT>' % ( args . repository , ) , <NUM_LIT> <EOL> project . repository = repository <EOL> if args . status == '<STR_LIT>' : <EOL> project . status = ProjectStatus . inactive <EOL> elif args . status == '<STR_LIT>' : <EOL> project . status = ProjectStatus . active <EOL> db . session . add ( project ) <EOL> data = self . serialize ( project ) <EOL> data [ '<STR_LIT>' ] = self . serialize ( project . repository ) <EOL> return self . respond ( data , serialize = False ) </s>
