Skip to content

Commit

Permalink
feat: поправлена валидация, sitesProcessing перенесен в store
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Dec 22, 2018
1 parent e9b583b commit 71b7cf3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 177 deletions.
2 changes: 1 addition & 1 deletion assets/js/presets/columns.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
'engine',
'meta_year',
'meta_visitors',
'files_size',
'meta_client_priority',
]
},
cron: {
Expand Down
100 changes: 1 addition & 99 deletions assets/js/validate.conf.js
Original file line number Diff line number Diff line change
@@ -1,99 +1 @@
module.exports = {
lighthouse_interactive: {
warn: { min: 3000, max: 8000 }, // ps: 3900 warn
error: { min: 8001 }
},
lighthouse_speed_index: {
warn: { min: 3500, max: 8000 }, // ps: 3200 ok, 3900 warn, 8500 fail
error: { min: 8001 }
},
lighthouse_first_contentful_paint: {
warn: { min: 3000, max: 5000 }, // ps: 2400 warn, lh: 2700 ok
error: { min: 5001 }
},
lighthouse_first_cpu_idle: {
warn: { min: 3000, max: 5000 },
error: { min: 5001 }
},
lighthouse_first_meaningful_paint: {
warn: { min: 2000, max: 4999 }, // ps: 5000 fail
error: { min: 5000 }
},
lighthouse_input_latency: {
warn: { min: 50, max: 100 },
error: { min: 101 }
},
lighthouse_performance: {
warn: { min: 50, max: 79 },
error: { max: 50 }
},
lighthouse_pwa: {
warn: { min: 50, max: 79 },
error: { max: 50 }
},
lighthouse_accessibility: {
warn: { min: 50, max: 79 },
error: { max: 50 }
},
lighthouse_best_practices: {
warn: { min: 50, max: 79 },
error: { max: 50 }
},
lighthouse_seo: {
warn: { min: 50, max: 79 },
error: { max: 50 }
},
lighthouse_all: {
warn: { min: 50, max: 79 },
error: { max: 50 }
},
lighthouse_errors_in_console: {
warn: { min: 1, max: 2 },
error: { min: 3 }
},
lighthouse_metwork_requests: {
warn: { min: 50, max: 100 },
error: { min: 100 }
},
lighthouse_mainthread_work_breakdown: {
warn: { min: 2000, max: 4999 },
error: { min: 5000 }
},
lighthouse_total_byte_weight: {
warn: { min: 2000, max: 4999 },
error: { min: 5000 }
},
yandex_maps: {
error: { min: 1 }
},
envy: {
warn: { min: 1 }
},
jivosite: {
warn: { min: 1 }
},
meta_client_priority: {
warn: { min: 2 },
error: { min: 3 }
},
https: {
warn: { max: 0 }
},
meta_visitors: {
warn: { max: 499 },
error: { max: 99 }
},
meta_year: {
warn: {
min: new Date().getFullYear() - 4,
max: new Date().getFullYear() - 2
},
error: {
max: new Date().getFullYear() - 5
}
},
meta_yandex_sqi: {
warn: { max: 100 },
error: { max: 50 }
}
};
module.exports = {};
2 changes: 1 addition & 1 deletion components/SiteDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default {
);
// console.log(fieldName + ' validateClass: ', info.validateClass);
if (info.type == "boolean") {
info.valueText = info.value ? "да" : "нет";
info.valueText = parseInt(info.value) ? "да" : "нет";
}
groups[groupName].fields.push(info);
}
Expand Down
68 changes: 1 addition & 67 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
<style lang="scss" src="./index.scss"></style>

<script>
import moment from "moment";
import Toolbar from "~/components/Toolbar";
import SiteDetails from "~/components/SiteDetails";
import columnPresets from "~/assets/js/presets/columns.conf";
Expand Down Expand Up @@ -246,70 +245,6 @@ export default {
});
},
// дополняет колонки sites.json
sitesProcessing(sites) {
if (!sites) return [];
const sitesData = sites.map(s => {
let site = { ...s };
// should be before site_info flatten
if (
site.engine != "default" &&
(!site.site_info || (site.site_info && !site.site_info.engine))
) {
if (!site.site_info) site.site_info = {};
site.site_info.engine = site.engine;
}
// flatten site_info
for (let i in site.site_info) {
site[i] = site.site_info[i];
if (i == "files_size") site[i] = Math.round(site[i] / 1024);
if (i == "git_size") site[i] = Math.round(site[i] / 1024);
if (i == "updated_time")
site[i] = moment.unix(site[i]).format("YYYY-MM-DD HH:mm:ss");
}
delete site.site_info;
// flatten meta
if (site.meta) {
for (let i in site.meta) {
const ln = "meta_" + i;
site[ln] = site.meta[i];
}
delete site.meta;
}
// flatten lighthouse
if (site.lighthouse) {
// console.log(site.lighthouse);
for (let i in site.lighthouse) {
// вложенный объект
if (i == "scores") {
for (let s in site.lighthouse.scores) {
const ln = "lighthouse_" + s.split("-").join("_");
site[ln] = site.lighthouse.scores[s];
}
} else {
const ln = "lighthouse_" + i.split("-").join("_");
if (ln == "lighthouse_total_byte_weight") {
site.lighthouse[i] = Math.round(site.lighthouse[i] / 1024);
}
site[ln] = site.lighthouse[i];
}
}
delete site.lighthouse;
}
site.prod = site.prod ? 1 : 0;
site.https = site.https ? 1 : 0;
site.errors = site.error ? 1 : 0;
site.id = site.domain;
site.time = parseInt(site.time);
return site;
});
return sitesData;
},
// выдает класс валидации по домену сайта и имени колонки
getColumnValidateClass(props, domain, column) {
return this.$store.getters.getColumnValidateClass(props, domain, column);
Expand All @@ -335,8 +270,7 @@ export default {
// data init
const sitesJson = await this.$axios.$get(this.$store.state.sitesJsonUrl);
this.$store.commit("tests", sitesJson.tests);
const sitesData = this.sitesProcessing(sitesJson.sites);
this.$store.commit("sites", sitesData);
this.$store.dispatch("sites", sitesJson.sites);
this.$store.dispatch("q", this.$route.query["q"]);
this.fieldsInit();
Expand Down
107 changes: 98 additions & 9 deletions store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import pjson from '~/package.json';
import jsonQuery from 'json-query';
import dateformat from 'dateformat';
import validateMap from '~/assets/js/validate.conf';
import moment from "moment";

export const state = () => ({
// data
Expand All @@ -24,6 +25,71 @@ export const state = () => ({
});

export const getters = {
// дополняет колонки sites.json
sitesProcessing(state) {
return sites => {
if (!sites) return [];
const sitesData = sites.map(s => {
let site = { ...s };
// should be before site_info flatten
if (
site.engine != 'default' &&
(!site.site_info || (site.site_info && !site.site_info.engine))
) {
if (!site.site_info) site.site_info = {};
site.site_info.engine = site.engine;
}

// flatten site_info
for (let i in site.site_info) {
site[i] = site.site_info[i];
if (i == 'files_size') site[i] = Math.round(site[i] / 1024);
if (i == 'git_size') site[i] = Math.round(site[i] / 1024);
if (i == 'updated_time') site[i] = moment.unix(site[i]).format('YYYY-MM-DD HH:mm:ss');
}
delete site.site_info;

// flatten meta
if (site.meta) {
for (let i in site.meta) {
const ln = 'meta_' + i;
site[ln] = site.meta[i];
}
delete site.meta;
}

// flatten lighthouse
if (site.lighthouse) {
// console.log(site.lighthouse);
for (let i in site.lighthouse) {
// вложенный объект
if (i == 'scores') {
for (let s in site.lighthouse.scores) {
const ln = 'lighthouse_' + s.split('-').join('_');
site[ln] = site.lighthouse.scores[s];
}
} else {
const ln = 'lighthouse_' + i.split('-').join('_');
if (ln == 'lighthouse_total_byte_weight') {
site.lighthouse[i] = Math.round(site.lighthouse[i] / 1024);
}
site[ln] = site.lighthouse[i];
}
}
delete site.lighthouse;
}

site.prod = site.prod ? 1 : 0;
site.https = site.https ? 1 : 0;
site.errors = site.error ? 1 : 0;
site.id = site.domain;
site.time = parseInt(site.time);
return site;
});
return sitesData;
};
},

/* lastUpdated(state) {
let date = Math.max.apply(Math, state.songs.map(song => new Date(song.created)));
return dateformat(new Date(date), 'dd.mm.yyyy');
Expand All @@ -49,7 +115,15 @@ export const getters = {
};
},

// выдает класс валидации по домену сайта и имени колонки
/** выдает класс валидации по домену сайта и имени колонки
* сначала проверяется на условия pass, если не проходит по условиям,
* то смотрится warn, если не подходит, засчитывается fail
* В условиях могут быть объекты: validate = { min: 1, max: 1 }
* или значения: validate = 1
* или условие warn: validate = { min: 80, warning: { max: 50 } }
* Можно делать валидацию без fail, только pass и warn:
* validate = { min: 1, warning: { max: 0} }
*/
getColumnValidateClass(state) {
return (props, domain, column) => {
const site = state.filteredSites.find(site => site.domain == domain);
Expand All @@ -61,10 +135,16 @@ export const getters = {

// проверяет, попадает ли значение под лимиты
const isFits = (value, rules) => {
if (Number.isInteger(rules)) return value != rules;
if ('max' in rules && value > rules.max) return false;
if ('min' in rules && value < rules.min) return false;
return true;
let valid = true;
if (Number.isInteger(rules)) {
return value == rules;
}
if ('max' in rules && value > rules.max) valid = false;
if ('min' in rules && value < rules.min) valid = false;
if (!('min' in rules) && !('max' in rules)) {
valid = false;
}
return valid;
};

let r;
Expand All @@ -75,10 +155,13 @@ export const getters = {

// validate map
// проверка на соответствие из массива
if (Number.isInteger(r)) r = { error: r };
if ('error' in r && isFits(site[column], r.error)) result = 'fail';
else if ('warn' in r && isFits(site[column], r.warn)) result = 'warn';
else result = 'pass';
// if (Number.isInteger(r)) r = { pass: r };
result = 'fail';
if (isFits(site[column], r)) result = 'pass';
else if (typeof r == 'object') {
// if ('error' in r && isFits(site[column], r.error)) result = 'fail';
if ('warning' in r && isFits(site[column], r.warning)) result = 'warn';
}

if (!result && site.tests) {
const test = state.tests[column];
Expand Down Expand Up @@ -133,6 +216,12 @@ export const mutations = {
};

export const actions = {
// дополняет sites и сохраняет
sites({ commit, state, getters }, sites) {
const sitesData = getters.sitesProcessing(sites);
commit('sites', sitesData);
},

// фильтрует sites на основе q
filterSites({ commit, state }) {
const q = state.q.toLowerCase();
Expand Down

0 comments on commit 71b7cf3

Please sign in to comment.