Add the dashboard api view, with a few fields still needing to be filled

This commit is contained in:
Matthew Jones 2013-11-20 10:54:25 -05:00
parent 8836bee43a
commit 5a9bf7054c
2 changed files with 86 additions and 0 deletions

View File

@ -151,6 +151,7 @@ v1_urls = patterns('awx.api.views',
url(r'^config/$', 'api_v1_config_view'),
url(r'^authtoken/$', 'auth_token_view'),
url(r'^me/$', 'user_me_list'),
url(r'^dashboard/$', 'dashboard_view'),
url(r'^organizations/', include(organization_urls)),
url(r'^users/', include(user_urls)),
url(r'^projects/', include(project_urls)),

View File

@ -79,6 +79,7 @@ class ApiV1RootView(APIView):
data['authtoken'] = reverse('api:auth_token_view')
data['config'] = reverse('api:api_v1_config_view')
data['me'] = reverse('api:user_me_list')
data['dashboard'] = reverse('api:dashboard_view')
data['organizations'] = reverse('api:organization_list')
data['users'] = reverse('api:user_list')
data['projects'] = reverse('api:project_list')
@ -128,6 +129,90 @@ class ApiV1ConfigView(APIView):
return Response(data)
class DashboardView(APIView):
view_name = "Dashboard"
def get(self, request, format=None):
''' Show Dashboard Details '''
data = SortedDict()
user_inventory = get_user_queryset(request.user, Inventory)
failed_inventory = sum(i.inventory_sources_with_failures for i in user_inventory)
data['inventories'] = SortedDict({'url': reverse('api:inventory_list'),
'total': user_inventory.count(),
#'job_failed'
'inventory_failed': failed_inventory})
user_inventory_sources = get_user_queryset(request.user, InventorySource)
rax_inventory_sources = user_inventory_sources.filter(source='rax')
rax_inventory_failed = rax_inventory_sources.filter(status='failed')
ec2_inventory_sources = user_inventory_sources.filter(source='ec2')
ec2_inventory_failed = ec2_inventory_sources.filter(status='failed')
data['inventory_sources'] = {}
data['inventory_sources']['rax'] = SortedDict({'url': reverse('api:inventory_source_list') + "?source=rax",
'label': 'Rackspace',
'failures_url': reverse('api:inventory_source_list') + "?source=rax&status=failed",
'total': rax_inventory_sources.count(),
'failed': rax_inventory_failed.count()})
data['inventory_sources']['ec2'] = SortedDict({'url': reverse('api:inventory_source_list') + "?source=ec2",
'failures_url': reverse('api:inventory_source_list') + "?source=ec2&status=failed",
'label': 'Amazon EC2',
'total': ec2_inventory_sources.count(),
'failed': ec2_inventory_failed.count()})
user_groups = get_user_queryset(request.user, Group)
data['groups'] = SortedDict({'url': reverse('api:group_list'),
'failures_url': '__fill__',
'total': user_groups.count(),
'job_failed': '__fill__', # # of groups with job failures
'inventory_failed': '__fill__'}) # # of groups with failed cloud updates
user_hosts = get_user_queryset(request.user, Host)
user_hosts_failed = user_hosts.filter(has_active_failures=True)
data['hosts'] = SortedDict({'url': reverse('api:host_list'),
'failures_url': reverse('api:host_list') + "?has_active_failures=True",
'total': user_hosts.count(),
'failed': user_hosts_failed.count()})
user_projects = get_user_queryset(request.user, Project)
user_projects_failed = user_projects.filter(last_update_failed=True)
data['projects'] = SortedDict({'url': reverse('api:project_list'),
'failures_url': reverse('api:project_list') + "?last_update_failed=True",
'total': user_projects.count(),
'failed': user_projects_failed.count()})
git_projects = user_projects.filter(scm_type='git')
git_failed_projects = git_projects.filter(last_update_failed=True)
svn_projects = user_projects.filter(scm_type='svn')
svn_failed_projects = svn_projects.filter(last_update_failed=True)
hg_projects = user_projects.filter(scm_type='hg')
hg_failed_projects = hg_projects.filter(last_update_failed=True)
data['scm_types'] = {}
data['scm_types']['git'] = SortedDict({'url': reverse('api:project_list') + "?scm_type=git",
'label': 'Git',
'failures_url': reverse('api:project_list') + "?scm_type=git&last_update_failed=True",
'total': git_projects.count(),
'failed': git_failed_projects.count()})
data['scm_types']['svn'] = SortedDict({'url': reverse('api:project_list') + "?scm_type=svn",
'label': 'Subversion',
'failures_url': reverse('api:project_list') + "?scm_type=svn&last_update_failed=True",
'total': svn_projects.count(),
'failed': svn_failed_projects.count()})
data['scm_types']['hg'] = SortedDict({'url': reverse('api:project_list') + "?scm_type=hg",
'label': 'Mercurial',
'failures_url': reverse('api:project_list') + "?scm_type=hg&last_update_failed=True",
'total': hg_projects.count(),
'failed': hg_failed_projects.count()})
user_jobs = get_user_queryset(request.user, Job)
user_failed_jobs = user_jobs.filter(failed=True)
data['jobs'] = SortedDict({'url': reverse('api:job_list'),
'failure_url': reverse('api:job_list') + "?failed=True",
'total': user_jobs.count(),
'failed': user_failed_jobs.count()})
return Response(data)
class AuthTokenView(APIView):
authentication_classes = []