move constants to a file

This commit is contained in:
Jake McDermott 2018-07-30 18:10:45 -04:00
parent 86a1f9c0ad
commit 4c3370bd34
11 changed files with 133 additions and 97 deletions

View File

@ -1,10 +1,12 @@
const API_PAGE_SIZE = 200;
const PAGE_SIZE = 50;
const ORDER_BY = 'counter';
import {
API_MAX_PAGE_SIZE,
OUTPUT_ORDER_BY,
OUTPUT_PAGE_SIZE,
} from './constants';
const BASE_PARAMS = {
page_size: PAGE_SIZE,
order_by: ORDER_BY,
order_by: OUTPUT_ORDER_BY,
page_size: OUTPUT_PAGE_SIZE,
};
const merge = (...objs) => _.merge({}, ...objs);
@ -77,7 +79,7 @@ function JobEventsApiService ($http, $q) {
return $q.resolve(this.cache.last);
}
const params = merge(this.params, { page: 1, order_by: `-${ORDER_BY}` });
const params = merge(this.params, { page: 1, order_by: `-${OUTPUT_ORDER_BY}` });
return $http.get(this.endpoint, { params })
.then(({ data }) => {
@ -86,8 +88,8 @@ function JobEventsApiService ($http, $q) {
let rotated = results;
if (count > PAGE_SIZE) {
rotated = results.splice(count % PAGE_SIZE);
if (count > OUTPUT_PAGE_SIZE) {
rotated = results.splice(count % OUTPUT_PAGE_SIZE);
if (results.length > 0) {
rotated = results;
@ -112,7 +114,7 @@ function JobEventsApiService ($http, $q) {
const [low, high] = range;
const params = merge(this.params, { counter__gte: [low], counter__lte: [high] });
params.page_size = API_PAGE_SIZE;
params.page_size = API_MAX_PAGE_SIZE;
return $http.get(this.endpoint, { params })
.then(({ data }) => {
@ -127,7 +129,7 @@ function JobEventsApiService ($http, $q) {
});
};
this.getLastPageNumber = () => Math.ceil(this.state.count / PAGE_SIZE);
this.getLastPageNumber = () => Math.ceil(this.state.count / OUTPUT_PAGE_SIZE);
this.getMaxCounter = () => this.state.maxCounter;
}

View File

@ -0,0 +1,30 @@
export const API_MAX_PAGE_SIZE = 200;
export const API_ROOT = '/api/v2/';
export const EVENT_START_TASK = 'playbook_on_task_start';
export const EVENT_START_PLAY = 'playbook_on_play_start';
export const EVENT_START_PLAYBOOK = 'playbook_on_start';
export const EVENT_STATS_PLAY = 'playbook_on_stats';
export const HOST_STATUS_KEYS = ['dark', 'failures', 'changed', 'ok', 'skipped'];
export const JOB_STATUS_COMPLETE = ['successful', 'failed', 'unknown'];
export const JOB_STATUS_INCOMPLETE = ['canceled', 'error'];
export const JOB_STATUS_UNSUCCESSFUL = ['failed'].concat(JOB_STATUS_INCOMPLETE);
export const JOB_STATUS_FINISHED = JOB_STATUS_COMPLETE.concat(JOB_STATUS_INCOMPLETE);
export const OUTPUT_ELEMENT_CONTAINER = '.at-Stdout-container';
export const OUTPUT_ELEMENT_TBODY = '#atStdoutResultTable';
export const OUTPUT_MAX_LAG = 120;
export const OUTPUT_ORDER_BY = 'counter';
export const OUTPUT_PAGE_CACHE = true;
export const OUTPUT_PAGE_LIMIT = 5;
export const OUTPUT_PAGE_SIZE = 50;
export const OUTPUT_SCROLL_DELAY = 100;
export const OUTPUT_SCROLL_THRESHOLD = 0.1;
export const OUTPUT_SEARCH_DOCLINK = 'https://docs.ansible.com/ansible-tower/3.3.0/html/userguide/search_sort.html';
export const OUTPUT_SEARCH_FIELDS = ['changed', 'created', 'failed', 'host_name', 'stdout', 'task', 'role', 'playbook', 'play'];
export const OUTPUT_SEARCH_KEY_EXAMPLES = ['host_name:localhost', 'task:set', 'created:>=2000-01-01'];
export const OUTPUT_EVENT_LIMIT = OUTPUT_PAGE_LIMIT * OUTPUT_PAGE_SIZE;
export const WS_PREFIX = 'ws';

View File

@ -1,6 +1,8 @@
/* eslint camelcase: 0 */
const EVENT_START_TASK = 'playbook_on_task_start';
const EVENT_START_PLAY = 'playbook_on_play_start';
import {
EVENT_START_PLAY,
EVENT_START_TASK,
} from './constants';
let $compile;
let $q;

View File

@ -18,16 +18,15 @@ import SearchComponent from '~features/output/search.component';
import StatsComponent from '~features/output/stats.component';
import HostEvent from './host-event/index';
const Template = require('~features/output/index.view.html');
import {
API_ROOT,
OUTPUT_ORDER_BY,
OUTPUT_PAGE_SIZE,
WS_PREFIX,
} from './constants';
const MODULE_NAME = 'at.features.output';
const PAGE_CACHE = true;
const PAGE_LIMIT = 5;
const PAGE_SIZE = 50;
const ORDER_BY = 'counter';
const WS_PREFIX = 'ws';
const API_ROOT = '/api/v2/';
const Template = require('~features/output/index.view.html');
function resolveResource (
$state,
@ -80,23 +79,16 @@ function resolveResource (
}
const params = {
page_size: PAGE_SIZE,
order_by: ORDER_BY,
};
const config = {
params,
pageCache: PAGE_CACHE,
pageLimit: PAGE_LIMIT,
page_size: OUTPUT_PAGE_SIZE,
order_by: OUTPUT_ORDER_BY,
};
if (job_event_search) { // eslint-disable-line camelcase
const query = qs.encodeQuerysetObject(qs.decodeArr(job_event_search));
Object.assign(config.params, query);
Object.assign(params, query);
}
Events.init(`${API_ROOT}${related}`, config.params);
Events.init(`${API_ROOT}${related}`, params);
Wait('start');
const promise = Promise.all([new Resource(['get', 'options'], [id, id]), Events.fetch()])

View File

@ -1,5 +1,5 @@
/* eslint camelcase: 0 */
const PAGE_LIMIT = 5;
import { OUTPUT_PAGE_LIMIT } from './constants';
function PageService ($q) {
this.init = (storage, api, { getScrollHeight }) => {
@ -150,7 +150,7 @@ function PageService ($q) {
const pageCount = this.state.head - this.state.tail;
if (pageCount >= PAGE_LIMIT) {
if (pageCount >= OUTPUT_PAGE_LIMIT) {
this.chain = this.chain
.then(() => this.popBack())
.then(() => {
@ -185,7 +185,7 @@ function PageService ($q) {
const pageCount = this.state.head - this.state.tail;
if (pageCount >= PAGE_LIMIT) {
if (pageCount >= OUTPUT_PAGE_LIMIT) {
this.chain = this.chain
.then(() => this.popFront())
.then(() => {

View File

@ -1,20 +1,22 @@
import Ansi from 'ansi-to-html';
import Entities from 'html-entities';
const ELEMENT_TBODY = '#atStdoutResultTable';
const EVENT_START_TASK = 'playbook_on_task_start';
const EVENT_START_PLAY = 'playbook_on_play_start';
const EVENT_STATS_PLAY = 'playbook_on_stats';
import {
EVENT_START_PLAY,
EVENT_STATS_PLAY,
EVENT_START_TASK,
OUTPUT_ELEMENT_TBODY,
} from './constants';
const EVENT_GROUPS = [
EVENT_START_TASK,
EVENT_START_PLAY
EVENT_START_PLAY,
];
const TIME_EVENTS = [
EVENT_START_TASK,
EVENT_START_PLAY,
EVENT_STATS_PLAY
EVENT_STATS_PLAY,
];
const ansi = new Ansi();
@ -33,7 +35,7 @@ function JobRenderService ($q, $sce, $window) {
this.init = ({ compile, toggles }) => {
this.parent = null;
this.record = {};
this.el = $(ELEMENT_TBODY);
this.el = $(OUTPUT_ELEMENT_TBODY);
this.hooks = { compile };
this.createToggles = toggles;

View File

@ -1,11 +1,13 @@
const ELEMENT_CONTAINER = '.at-Stdout-container';
const ELEMENT_TBODY = '#atStdoutResultTable';
const DELAY = 100;
const THRESHOLD = 0.1;
import {
OUTPUT_ELEMENT_CONTAINER,
OUTPUT_ELEMENT_TBODY,
OUTPUT_SCROLL_DELAY,
OUTPUT_SCROLL_THRESHOLD,
} from './constants';
function JobScrollService ($q, $timeout) {
this.init = ({ next, previous }) => {
this.el = $(ELEMENT_CONTAINER);
this.el = $(OUTPUT_ELEMENT_CONTAINER);
this.timer = null;
this.position = {
@ -37,7 +39,7 @@ function JobScrollService ($q, $timeout) {
$timeout.cancel(this.timer);
}
this.timer = $timeout(this.register, DELAY);
this.timer = $timeout(this.register, OUTPUT_SCROLL_DELAY);
};
this.register = () => {
@ -76,10 +78,10 @@ function JobScrollService ($q, $timeout) {
if (downward) {
current += this.getViewableHeight();
if (current >= height || ((height - current) / height) < THRESHOLD) {
if (current >= height || ((height - current) / height) < OUTPUT_SCROLL_THRESHOLD) {
return true;
}
} else if (current <= 0 || (current / height) < THRESHOLD) {
} else if (current <= 0 || (current / height) < OUTPUT_SCROLL_THRESHOLD) {
return true;
}
@ -177,7 +179,7 @@ function JobScrollService ($q, $timeout) {
};
this.isLocked = () => this.state.locked;
this.isMissing = () => $(ELEMENT_TBODY)[0].clientHeight < this.getViewableHeight();
this.isMissing = () => $(OUTPUT_ELEMENT_TBODY)[0].clientHeight < this.getViewableHeight();
}
JobScrollService.$inject = ['$q', '$timeout'];

View File

@ -1,8 +1,10 @@
const templateUrl = require('~features/output/search.partial.html');
import {
OUTPUT_SEARCH_DOCLINK,
OUTPUT_SEARCH_FIELDS,
OUTPUT_SEARCH_KEY_EXAMPLES,
} from './constants';
const searchKeyExamples = ['host_name:localhost', 'task:set', 'created:>=2000-01-01'];
const searchKeyFields = ['changed', 'created', 'failed', 'host_name', 'stdout', 'task', 'role', 'playbook', 'play'];
const searchKeyDocLink = 'https://docs.ansible.com/ansible-tower/3.3.0/html/userguide/search_sort.html';
const templateUrl = require('~features/output/search.partial.html');
let $state;
let qs;
@ -50,7 +52,7 @@ function reloadQueryset (queryset, rejection = strings.get('search.REJECT_DEFAUL
const isFilterable = term => {
const field = term[0].split('.')[0].replace(/^-/, '');
return (searchKeyFields.indexOf(field) > -1);
return (OUTPUT_SEARCH_FIELDS.indexOf(field) > -1);
};
function removeSearchTag (index) {
@ -94,9 +96,9 @@ function JobSearchController (_$state_, _qs_, _strings_, { subscribe }) {
vm = this || {};
vm.strings = strings;
vm.examples = searchKeyExamples;
vm.fields = searchKeyFields;
vm.docLink = searchKeyDocLink;
vm.examples = OUTPUT_SEARCH_KEY_EXAMPLES;
vm.fields = OUTPUT_SEARCH_FIELDS;
vm.docLink = OUTPUT_SEARCH_DOCLINK;
vm.relatedFields = [];
vm.clearSearch = clearSearch;

View File

@ -1,7 +1,8 @@
/* eslint camelcase: 0 */
const PAGE_SIZE = 50;
const PAGE_LIMIT = 5;
const EVENT_LIMIT = PAGE_LIMIT * PAGE_SIZE;
import {
OUTPUT_EVENT_LIMIT,
OUTPUT_PAGE_SIZE,
} from './constants';
/**
* Check if a range overlaps another range
@ -266,7 +267,7 @@ function SlidingWindowService ($q) {
return this.chain;
};
this.getNext = (displacement = PAGE_SIZE) => {
this.getNext = (displacement = OUTPUT_PAGE_SIZE) => {
const [head, tail] = this.getRange();
const tailRoom = this.getMaxCounter() - tail;
@ -276,14 +277,14 @@ function SlidingWindowService ($q) {
let headDisplacement = 0;
if (newTail - head > EVENT_LIMIT) {
headDisplacement = (newTail - EVENT_LIMIT) - head;
if (newTail - head > OUTPUT_EVENT_LIMIT) {
headDisplacement = (newTail - OUTPUT_EVENT_LIMIT) - head;
}
return this.move([head + headDisplacement, tail + tailDisplacement]);
};
this.getPrevious = (displacement = PAGE_SIZE) => {
this.getPrevious = (displacement = OUTPUT_PAGE_SIZE) => {
const [head, tail] = this.getRange();
const headRoom = head - 1;
@ -293,8 +294,8 @@ function SlidingWindowService ($q) {
let tailDisplacement = 0;
if (tail - newHead > EVENT_LIMIT) {
tailDisplacement = tail - (newHead + EVENT_LIMIT);
if (tail - newHead > OUTPUT_EVENT_LIMIT) {
tailDisplacement = tail - (newHead + OUTPUT_EVENT_LIMIT);
}
return this.move([newHead, tail - tailDisplacement]);
@ -332,12 +333,12 @@ function SlidingWindowService ($q) {
this.getFirst = () => this.clear()
.then(() => this.api.getFirst())
.then(events => this.pushFront(events))
.then(() => this.moveTail(PAGE_SIZE));
.then(() => this.moveTail(OUTPUT_PAGE_SIZE));
this.getLast = () => this.clear()
.then(() => this.api.getLast())
.then(events => this.pushBack(events))
.then(() => this.moveHead(-PAGE_SIZE));
.then(() => this.moveHead(-OUTPUT_PAGE_SIZE));
this.getTailCounter = () => {
const tail = Math.max(...Object.keys(this.records));
@ -360,7 +361,7 @@ function SlidingWindowService ($q) {
this.getRange = () => [this.getHeadCounter(), this.getTailCounter()];
this.getRecordCount = () => Object.keys(this.records).length;
this.getCapacity = () => EVENT_LIMIT - this.getRecordCount();
this.getCapacity = () => OUTPUT_EVENT_LIMIT - this.getRecordCount();
}
SlidingWindowService.$inject = ['$q'];

View File

@ -1,14 +1,15 @@
/* eslint camelcase: 0 */
const JOB_START = 'playbook_on_start';
const JOB_END = 'playbook_on_stats';
const PLAY_START = 'playbook_on_play_start';
const TASK_START = 'playbook_on_task_start';
const HOST_STATUS_KEYS = ['dark', 'failures', 'changed', 'ok', 'skipped'];
const COMPLETE = ['successful', 'failed', 'unknown'];
const INCOMPLETE = ['canceled', 'error'];
const UNSUCCESSFUL = ['failed'].concat(INCOMPLETE);
const FINISHED = COMPLETE.concat(INCOMPLETE);
import {
EVENT_START_PLAYBOOK,
EVENT_STATS_PLAY,
EVENT_START_PLAY,
EVENT_START_TASK,
HOST_STATUS_KEYS,
JOB_STATUS_COMPLETE,
JOB_STATUS_INCOMPLETE,
JOB_STATUS_UNSUCCESSFUL,
JOB_STATUS_FINISHED,
} from './constants';
function JobStatusService (moment, message) {
this.dispatch = () => message.dispatch('status', this.state);
@ -62,11 +63,11 @@ function JobStatusService (moment, message) {
};
this.createHostStatusCounts = status => {
if (UNSUCCESSFUL.includes(status)) {
if (JOB_STATUS_UNSUCCESSFUL.includes(status)) {
return { failures: 1 };
}
if (COMPLETE.includes(status)) {
if (JOB_STATUS_COMPLETE.includes(status)) {
return { ok: 1 };
}
@ -92,7 +93,7 @@ function JobStatusService (moment, message) {
let changed = false;
if (!this.active && !(data.event === JOB_END)) {
if (!this.active && !(data.event === EVENT_STATS_PLAY)) {
this.active = true;
this.setJobStatus('running');
changed = true;
@ -105,22 +106,22 @@ function JobStatusService (moment, message) {
changed = true;
}
if (data.event === JOB_START) {
if (data.event === EVENT_START_PLAYBOOK) {
this.setStarted(this.state.started || data.created);
changed = true;
}
if (data.event === PLAY_START) {
if (data.event === EVENT_START_PLAY) {
this.state.counts.plays++;
changed = true;
}
if (data.event === TASK_START) {
if (data.event === EVENT_START_TASK) {
this.state.counts.tasks++;
changed = true;
}
if (data.event === JOB_END) {
if (data.event === EVENT_STATS_PLAY) {
this.setStatsEvent(data);
changed = true;
}
@ -193,9 +194,9 @@ function JobStatusService (moment, message) {
this.setJobStatus = status => {
const isExpectingStats = this.isExpectingStatsEvent();
const isIncomplete = INCOMPLETE.includes(status);
const isFinished = FINISHED.includes(status);
const isAlreadyFinished = FINISHED.includes(this.state.status);
const isIncomplete = JOB_STATUS_INCOMPLETE.includes(status);
const isFinished = JOB_STATUS_FINISHED.includes(status);
const isAlreadyFinished = JOB_STATUS_FINISHED.includes(this.state.status);
if (isAlreadyFinished) {
return;

View File

@ -1,7 +1,9 @@
/* eslint camelcase: 0 */
const PAGE_SIZE = 50;
const MAX_LAG = 120;
const JOB_END = 'playbook_on_stats';
import {
EVENT_STATS_PLAY,
OUTPUT_MAX_LAG,
OUTPUT_PAGE_SIZE,
} from './constants';
function OutputStream ($q) {
this.init = ({ bufferAdd, bufferEmpty, onFrames, onStop }) => {
@ -28,7 +30,7 @@ function OutputStream ($q) {
this.lag = 0;
this.chain = $q.resolve();
this.factors = this.calcFactors(PAGE_SIZE);
this.factors = this.calcFactors(OUTPUT_PAGE_SIZE);
this.setFramesPerRender();
};
@ -47,7 +49,7 @@ function OutputStream ($q) {
};
this.setFramesPerRender = () => {
const index = Math.floor((this.lag / MAX_LAG) * this.factors.length);
const index = Math.floor((this.lag / OUTPUT_MAX_LAG) * this.factors.length);
const boundedIndex = Math.min(this.factors.length - 1, index);
this.framesPerRender = this.factors[boundedIndex];
@ -96,7 +98,7 @@ function OutputStream ($q) {
this.chain = this.chain
.then(() => {
if (data.event === JOB_END) {
if (data.event === EVENT_STATS_PLAY) {
this.state.ending = true;
this.counters.final = data.counter;
}
@ -104,7 +106,7 @@ function OutputStream ($q) {
const [minReady, maxReady] = this.updateCounterState(data);
const count = this.hooks.bufferAdd(data);
if (count % PAGE_SIZE === 0) {
if (count % OUTPUT_PAGE_SIZE === 0) {
this.setFramesPerRender();
}