Skip to content

Commit

Permalink
add flask caching
Browse files Browse the repository at this point in the history
rename home component to search component in frontend
  • Loading branch information
adamstauffer committed Feb 3, 2019
1 parent 6d7b29d commit cd3ad54
Show file tree
Hide file tree
Showing 24 changed files with 213 additions and 154 deletions.
1 change: 1 addition & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lxml = "*"
webargs = "*"
deepdiff = "*"
sqlalchemy-searchable = "*"
flask-caching = "*"

[dev-packages]

Expand Down
240 changes: 123 additions & 117 deletions backend/Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/src/rmfdb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def download_stigs(app, local_file, force):

@main.command()
@click.pass_obj
def seedcontroldata(app):
def seed_control_data(app):
with app.app_context():
controls_json = open('{}/800-53r4.json'.format(FIXTURE_DIR))
controls = json.load(controls_json)
Expand Down
2 changes: 2 additions & 0 deletions backend/src/rmfdb/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'DEBUG': False,
'SECRET_KEY': binascii.hexlify(os.urandom(24)).decode('utf-8'),
'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:',
'CACHE_TYPE': 'simple',
}


Expand Down Expand Up @@ -55,6 +56,7 @@ def create_app(config_path=None, name=None):
init_handlers(app)

# middleware
middleware.cache.init_app(app)
middleware.db.init_app(app)
middleware.limiter.init_app(app)
middleware.ma.init_app(app)
Expand Down
5 changes: 5 additions & 0 deletions backend/src/rmfdb/web/controls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
control_schema,
controls_schema,
)
from rmfdb.web.middleware import cache
from rmfdb.web.stigs.schema import cci_rules_schema


Expand All @@ -23,6 +24,7 @@

class ControlsView(MethodView):

@cache.cached(timeout=86400)
@use_args({
'page': fields.Int(missing=0),
'pageSize': fields.Int(missing=5)
Expand All @@ -40,20 +42,23 @@ def get(self, reqargs):

class ControlView(MethodView):

@cache.cached(timeout=86400)
def get(self, ctrl_id):
control = Control.query.filter_by(control_id=ctrl_id).first_or_404()
return control_schema.jsonify(control)


class CciView(MethodView):

@cache.cached(timeout=86400)
def get(self, cci_id):
cci = Cci.query.filter_by(cci_id=cci_id).first_or_404()
return cci_schema.jsonify(cci)


class CciRulesView(MethodView):

@cache.cached(timeout=86400)
def get(self, cci_id):
cci = Cci.query.filter_by(cci_id=cci_id).first_or_404()
return flask.jsonify(
Expand Down
2 changes: 2 additions & 0 deletions backend/src/rmfdb/web/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

from flask_caching import Cache
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import flask_marshmallow
Expand All @@ -8,6 +9,7 @@
import flask_talisman


cache = Cache()
db = flask_sqlalchemy.SQLAlchemy()
limiter = Limiter(key_func=get_remote_address)
ma = flask_marshmallow.Marshmallow()
Expand Down
2 changes: 2 additions & 0 deletions backend/src/rmfdb/web/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ccis_schema,
controls_schema,
)
from rmfdb.web.middleware import cache
from rmfdb.web.stigs.models import Rule, Stig
from rmfdb.web.stigs.schema import (
rules_schema,
Expand All @@ -25,6 +26,7 @@

class SearchView(MethodView):

@cache.cached(timeout=86400)
@use_args({
'query': fields.Str(required=True),
})
Expand Down
6 changes: 6 additions & 0 deletions backend/src/rmfdb/web/stigs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from webargs import fields
from webargs.flaskparser import use_args

from rmfdb.web.middleware import cache
from rmfdb.web.stigs.models import Rule, Stig
from rmfdb.web.stigs.schema import (
rule_schema,
Expand All @@ -23,6 +24,7 @@

class StigsView(MethodView):

@cache.cached(timeout=86400)
@use_args({
'page': fields.Int(missing=0),
'pageSize': fields.Int(missing=5)
Expand All @@ -40,6 +42,7 @@ def get(self, reqargs):

class StigVersionsView(MethodView):

@cache.cached(timeout=86400)
def get(self, id):
stig = Stig.query.filter_by(id=id).first_or_404()
stig_versions = Stig.query.filter_by(name=stig.name).order_by(
Expand All @@ -49,20 +52,23 @@ def get(self, id):

class StigView(MethodView):

@cache.cached(timeout=86400)
def get(self, id):
stig = Stig.query.filter_by(id=id).first_or_404()
return stig_schema.jsonify(stig)


class RuleView(MethodView):

@cache.cached(timeout=86400)
def get(self, id):
rule = Rule.query.filter_by(full_rule_id=id).first_or_404()
return rule_schema.jsonify(rule)


class RuleVersionsView(MethodView):

@cache.cached(timeout=86400)
def get(self, rule_id):
rule_versions = Rule.query.filter_by(rule_id=rule_id).all()
return rules_schema.jsonify(rule_versions)
Expand Down
6 changes: 4 additions & 2 deletions frontend/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.css"
"src/styles.css",
"src/styles.scss"
],
"scripts": []
},
Expand Down Expand Up @@ -75,7 +76,8 @@
"karmaConfig": "src/karma.conf.js",
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.css"
"src/styles.css",
"src/styles.scss"
],
"scripts": [],
"assets": [
Expand Down
5 changes: 5 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@angular/router": "^7.1.2",
"core-js": "^2.5.4",
"hammerjs": "^2.0.8",
"ionicons": "^4.5.5",
"moment": "^2.22.2",
"ng2-truncate": "^1.3.17",
"ngx-moment": "^3.2.0",
Expand Down
23 changes: 18 additions & 5 deletions frontend/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<mat-toolbar color="primary">
<mat-toolbar-row>
<span routerLink="/home">rmfdb</span>
<span routerLink="/search">rmfdb</span>
<span class='fill-remaining-space'></span>
<a routerLink="/home">
<button mat-icon-button color="accent">
<mat-icon aria-label="Home">home</mat-icon>
<a routerLink="/search">
<button
mat-icon-button
color="accent"
matTooltip="Search"
>
<mat-icon aria-label="Search">search</mat-icon>
</button>
</a>
<a href="https://github.com/atomweight/rmfdb">
<button
mat-icon-button
color="accent"
matTooltip="View GitHub"
>
<i class="icon ion-logo-github" aria-label="GitHub"></i>
</button>
</a>
</mat-toolbar-row>
Expand All @@ -30,7 +43,7 @@
</footer>

<ng-template #loading>
<div>
<div class="loading">
<mat-spinner></mat-spinner> Loading...
</div>
</ng-template>
8 changes: 5 additions & 3 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import { MatToolbarModule,
MatProgressSpinnerModule,
MatChipsModule,
MatTabsModule,
MatBadgeModule } from '@angular/material';
MatBadgeModule,
MatTooltipModule } from '@angular/material';
import { MomentModule } from 'ngx-moment';
import { TruncateModule } from 'ng2-truncate';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routes';
import { HomeComponent } from './home/home.component';
import { SearchComponent } from './search/search.component';
import { StigComponent } from './stig/stig.component';
import { TimeagoComponent } from './timeago/timeago.component';
import { RuleComponent } from './rule/rule.component';
Expand All @@ -40,7 +41,7 @@ import { CciComponent } from './cci/cci.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
SearchComponent,
StigComponent,
TimeagoComponent,
RuleComponent,
Expand Down Expand Up @@ -75,6 +76,7 @@ import { CciComponent } from './cci/cci.component';
MatChipsModule,
MatTabsModule,
MatBadgeModule,
MatTooltipModule,
MomentModule,
TruncateModule,
AppRoutingModule
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { SearchComponent } from './search/search.component';
import { StigComponent } from './stig/stig.component';
import { RuleComponent } from './rule/rule.component';
import { ControlComponent } from './control/control.component';
Expand All @@ -9,12 +9,12 @@ import { CciComponent } from './cci/cci.component';
const routes: Routes = [
{
path: '',
redirectTo: 'home',
redirectTo: 'search',
pathMatch: 'full'
},
{
path: 'home',
component: HomeComponent,
path: 'search',
component: SearchComponent,
runGuardsAndResolvers: 'always'
},
{
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/cci/cci.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
</div>

<ng-template #loading>
<div>
<div class="loading">
<mat-spinner></mat-spinner> Loading...
</div>
</ng-template>
Expand Down
18 changes: 10 additions & 8 deletions frontend/src/app/control/control.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
</mat-card-title>
<mat-card-content>
<p>{{ control.text }}</p>
<p>Guidance:</p>
<textarea
cdkTextareaAutosize
class="full-width"
disabled
>{{ control.guidance }}
</textarea>
<ng-container *ngIf="control.guidance">
<p>Guidance:</p>
<textarea
cdkTextareaAutosize
class="full-width"
disabled
>{{ control.guidance }}
</textarea>
</ng-container>
<p>Confidentiality Impact Threshold:
<mat-chip
[class.impact-low]="control.confidentiality_threshold == 'LOW'"
Expand Down Expand Up @@ -51,7 +53,7 @@
</div>

<ng-template #loading>
<div>
<div class="loading">
<mat-spinner></mat-spinner> Loading...
</div>
</ng-template>
2 changes: 1 addition & 1 deletion frontend/src/app/rule/rule.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</div>

<ng-template #loading>
<div>
<div class="loading">
<mat-spinner></mat-spinner> Loading...
</div>
</ng-template>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
</ng-template>

<ng-template #loading>
<div>
<div class="loading">
<mat-spinner></mat-spinner> Loading...
</div>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { HomeComponent } from './home.component';
import { SearchComponent } from './search.component';

describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;
describe('SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HomeComponent ]
declarations: [ SearchComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { Cci, Control } from '../models/control';


@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class HomeComponent implements OnInit {
export class SearchComponent implements OnInit {

navigationSubscription;
searchForm: FormGroup;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/stig/stig.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</div>

<ng-template #loading>
<div>
<div class="loading">
<mat-spinner></mat-spinner> Loading...
</div>
</ng-template>
Loading

0 comments on commit cd3ad54

Please sign in to comment.