Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #519 from GSA/get-statuses-from-api
Browse files Browse the repository at this point in the history
Get statuses from api
  • Loading branch information
DanielJDufour authored May 1, 2018
2 parents 2d1d506 + fe92959 commit 2528729
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 3,892 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,58 +43,63 @@ export class ComplianceDashboardComponent implements OnInit, OnDestroy {
);
}

_setRequirementStatuses(agencyRequirements) {
let requirements = [];
let overallStatus = 'noncompliant';

for (let requirement in agencyRequirements) {
if (agencyRequirements.hasOwnProperty(requirement)) {
const rValue = agencyRequirements[requirement];

let requirementStatus = 'noncompliant';

if (rValue >= 1) {
requirementStatus = 'compliant';
} else if (rValue >= 0.25 && rValue < 1) {
requirementStatus = 'partial';
}

if (requirement !== 'overallCompliance') {
requirements.push({ text: requirement, status: requirementStatus });
} else {
overallStatus = requirementStatus;
}
}
}
return { requirements, overallStatus };
}

_getCodePath(status) {

if (this.agencyIds.find(x => x === status)) {
return '/explore-code/agencies/' + status;
}

return null;
}
getStatuses() {
this.statusesSub = this.statusService.getJsonFile().
this.statusesSub = this.clientService.getStatuses().
subscribe((result) => {
if (result) {
for (let statusAgency in result.statuses) {
if (result.statuses.hasOwnProperty(statusAgency) &&
result.statuses[statusAgency].metadata.agency.complianceDashboard) {

let agencyStatus = result.statuses[statusAgency];

const {requirements, overallStatus} = this._setRequirementStatuses(agencyStatus.requirements);

// if agencyWidePolicy is null in report.json it means the agency doesn't have
// to comply, so don't include it in the dash.
// TODO: should make this more explicit in the API,
if (result.statuses[statusAgency].requirements['agencyWidePolicy'] !== null) {

let requirements = [];
let overallStatus;

for (let requirement in result.statuses[statusAgency].requirements) {
if (result.statuses[statusAgency].requirements.hasOwnProperty(requirement)) {
const rValue = result.statuses[statusAgency].requirements[requirement];

let requirementStatus = 'noncompliant';

if (rValue >= 1) {
requirementStatus = 'compliant';
}
if (rValue >= 0.25 && rValue < 1) {
requirementStatus = 'partial';
}

if (requirement !== 'overallCompliance') {
requirements.push({ text: requirement, status: requirementStatus });
} else {
overallStatus = requirementStatus;
}
}
}

let codePath = null;

if (this.agencyIds.find((x) => x === status)) {
codePath = '/explore-code/agencies/' + status;
}

let agency = {
id: result.statuses[statusAgency].metadata.agency.id,
name: result.statuses[statusAgency].metadata.agency.name,
overall: overallStatus,
codePath: codePath
};
this.statuses.push({
id: statusAgency,
agency: agency,
agency: {
id: result.statuses[statusAgency].metadata.agency.id,
name: result.statuses[statusAgency].metadata.agency.name,
overall: overallStatus,
codePath: this._getCodePath(status)
},
requirements: requirements
});

this.updated = result.timestamp;
}
}
Expand Down
240 changes: 240 additions & 0 deletions src/app/services/client/client.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@

import { TestBed, inject } from '@angular/core/testing';
import {
HttpModule,
Http,
XHRBackend,
Response,
ResponseOptions
} from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { ClientService } from './client.service';

describe('ClientService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
ClientService,
{ provide: XHRBackend, useClass: MockBackend }
]
});
});

describe('#getStatuses', () => {
it('should return an Observable<AgencyStatus> new', () => {
inject([ClientService, XHRBackend], (clientService, mockBackend) => {
const dummyStatuses = {
timestamp: '',
statuses: {
GSA: {
status: 'NOT FULLY COMPLIANT: 2 ERRORS',
issues: [
{
agency: 'General Services Administration',
project_name: 'Search.gov',
issues: {
enhancements: [],
warnings: [],
errors: [
{
keyword: 'type',
dataPath: '.repositoryURL',
schemaPath: '#/properties/repositoryURL/type',
params: {
type: 'string'
},
'message': 'should be string'
}
]
}
},
{
agency: 'General Services Administration',
project_name: 'Cron scripts',
issues: {
enhancements: [],
warnings: [],
errors: [
{
'keyword': 'type',
'dataPath': '.repositoryURL',
'schemaPath': '#/properties/repositoryURL/type',
'params': {
'type': 'string'
},
'message': 'should be string'
}
]
}
}
],
version: '2.0.0',
metadata: {
agency: {
name: 'General Services Administration',
acronym: 'GSA',
website: 'https://gsa.gov/',
codeUrl: 'https://www.gsa.gov/code.json',
fallback_file: 'GSA.json',
requirements: {
agencyWidePolicy: 1,
openSourceRequirement: 1,
inventoryRequirement: 1,
schemaFormat: 0.5,
overallCompliance: 1
},
complianceDashboard: true
}
},
wasFallbackUsed: false,
requirements: {
agencyWidePolicy: 1,
openSourceRequirement: 1,
inventoryRequirement: 1,
schemaFormat: 0.5,
overallCompliance: 1
}
}
}
};
mockBackend.connections.subscribe(connection => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(dummyStatuses)
})));
});
clientService.getStatuses().subscribe(statuses => {
expect(statuses.statuses['GSA'].issues.length).toBe(2);
});
});
});
});

describe('#getAgencies', () => {
beforeAll(() => {
const dummyAgencyResponse = `{
'total': 10,
'agencies': [
{
'acronym': 'DOT',
'name': 'Department of Transportation',
'website': 'https://www.transportation.gov/',
'codeUrl': 'https://www.transportation.gov/code.json',
'numRepos': 127
},
{
'acronym': 'DOL',
'name': 'Department of Labor',
'website': 'https://www.dol.gov',
'codeUrl': 'https://www.dol.gov/code.json',
'numRepos': 154
},
{
'acronym': 'DOJ',
'name': 'Department of Justice',
'website': 'https://justice.gov',
'codeUrl': 'https://www.justice.gov/code.json',
'numRepos': 14
},
{
'acronym': 'HUD',
'name': 'Department of Housing and Urban Development',
'website': 'https://www.hud.gov/',
'codeUrl': 'https://www.hud.gov/code.json',
'numRepos': 172
},
{
'acronym': 'HHS',
'name': 'Department of Health and Human Services',
'website': 'https://hhs.gov',
'codeUrl': 'https://www.hhs.gov/code.json',
'numRepos': 7
},
{
'acronym': 'DOE',
'name': 'Department of Energy',
'website': 'https://energy.gov/',
'codeUrl': 'https://www.energy.gov/code.json',
'numRepos': 881
},
{
'acronym': 'ED',
'name': 'Department of Education',
'website': 'https://ed.gov/',
'codeUrl': 'https://www.ed.gov/code.json',
'numRepos': 49
},
{
'acronym': 'DOD',
'name': 'Department of Defense',
'website': 'https://www.defense.gov/',
'codeUrl': 'https://www.code.mil/code.json',
'numRepos': 8
},
{
'acronym': 'DOC',
'name': 'Department of Commerce',
'website': 'https://www.commerce.gov/',
'codeUrl': 'https://www.commerce.gov/code.json',
'numRepos': 3
},
{
'acronym': 'USDA',
'name': 'Department of Agriculture',
'website': 'https://usda.gov/',
'codeUrl': 'https://www.usda.gov/code.json',
'numRepos': 21
}
]
}`;
const expectedAgencyList = [
'Department of Transportation',
'Department of Labor',
'Department of Justice',
'Department of Housing and Urban Development',
'Department of Health and Human Services',
'Department of Energy',
'Department of Education',
'Department of Defense',
'Department of Commerce',
'Department of Agriculture'
];
const expectedAgencyInfo = `{
"acronym": "DOT",
"name": "Department of Transportation",
"website": "https://www.transportation.gov/",
"codeUrl": "https://www.transportation.gov/code.json",
"numRepos": 127
}`;
const agency = 'DOT';
});
it('should return a list of agency names', () => {
inject([ClientService, XHRBackend], (clientService, mockBackend) => {

mockBackend.connections.subscribe(connection => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(this.dummyAgencyResponse)
})));
});
clientService.getAgencies().subscribe(agencies => {
expect(agencies).toEqual(this.expectedAgencyList);
});

});
});
it('should return info for the specified agency', () => {
inject([ClientService, XHRBackend], (clientService, mockBackend) => {
mockBackend.connections.subscribe(connection => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(this.dummyAgencyResponse)
})));
});
clientService.getAgencyByAcronym(this.agency).subscribe(agency => {
expect(agency).toEqual(this.expectedAgencyInfo);
});
});
});
});
});

14 changes: 13 additions & 1 deletion src/app/services/client/client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export interface Repo {
repoID: string;
}

export interface AgencyStatus {
timestamp: string;
statuses: object;
}

@Injectable()
export class ClientService {

Expand All @@ -89,7 +94,14 @@ export class ClientService {

constructor (private http: Http) {
}

getStatuses(): Observable<AgencyStatus> {
let url = this.BASE + 'status.json';
return this.http.get(url)
.map((response: Response) => response.json())
.map((data: any) => {
return data;
});
}
getAgencies(): Observable<Agency[]> {
let url = this.BASE + `agencies?size=1000&api_key=${this.KEY}`;
return this.http.get(url)
Expand Down
Loading

0 comments on commit 2528729

Please sign in to comment.