Commit 1795aaff authored by Nacim Goura's avatar Nacim Goura

add mapping elasticsearch for each user

parent fef1fde1
......@@ -4,6 +4,7 @@ import { check } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base';
import { Roles } from 'meteor/alanning:roles';
import formAccountSchema from '/imports/api/account/formAccountSchema';
import { defineConfig } from '/imports/api/config/methods';
/**
* add user account
......@@ -17,6 +18,11 @@ export function addAccount(user) {
throw new Meteor.Error('Error', 'Impossible de créer un compte!');
}
Roles.addUsersToRoles(id, 'gestionnaire');
console.log(user);
defineConfig({
userId: id,
listConfig: [],
});
return user;
}
......
......@@ -31,6 +31,7 @@ SimpleSchema.configCollection = new SimpleSchema({
type: String,
label: 'Nom de domaine',
regEx: SimpleSchema.RegEx.Url,
required: false,
autoValue() {
return this.value.replace('https', 'http');
},
......
......@@ -14,7 +14,10 @@ export function defineConfig(config) {
if (!config.listConfig) {
throw new Meteor.Error('Error', 'Impossible de définir les configurations');
}
console.log(config);
if (!config.userId) {
config.userId = Meteor.userId();
}
_.forEach(config.listConfig, (item) => {
item.forbiddenWord = item.forbiddenWordString.split(',');
});
......
......@@ -14,8 +14,9 @@ export default class crawlWebsite extends CrawlGeneric {
* @param data
* @returns {Promise}
*/
constructor(data) {
constructor(data, contextIndex) {
super();
this.contextIndex = contextIndex;
this.urlWebsite = data.urlWebsite;
this.config.crawl = _.find(this.config.listConfig, n => n.domain === data.nameConfig);
return this.start();
......@@ -75,7 +76,7 @@ export default class crawlWebsite extends CrawlGeneric {
if (toQueueUrl.match(/(pdf)/)) {
listPdf.push(toQueueUrl);
} else {
console.log(toQueueUrl);
// console.log(toQueueUrl);
crawl.queue(toQueueUrl);
}
}
......@@ -85,7 +86,7 @@ export default class crawlWebsite extends CrawlGeneric {
}
});
// if url has not parameter => parse data
if (!res.options.uri.match(/(#.*|\?.*)/g)) {
if (!/(#.*|\?.*)/g.test(res.options.uri)) {
this.parseData(res.$, res.options.uri, listPdf);
}
}
......@@ -108,7 +109,7 @@ export default class crawlWebsite extends CrawlGeneric {
* @param currentUrl
* @param listPdf
*/
parseData($, currentUrl, listPdf) {
async parseData($, currentUrl, listPdf) {
const body = $('body');
body.html(checkData.cleanHtml(body.html()));
const title = checkData.cleanText($('title').text());
......@@ -143,7 +144,16 @@ export default class crawlWebsite extends CrawlGeneric {
dataForIndex.listPdf = listPdf.join(' ');
}
this.listDataForIndex.push({
const _index = Meteor.settings.private.elasticsearch.esIndex;
const _type = this.config.userId;
const _id = currentUrl;
this.listDataForIndex.push(dataForIndex);
return this.contextIndex.indexOne(_index, _type, _id, dataForIndex);
/* this.listDataForIndex.push({
index: {
_index: Meteor.settings.private.elasticsearch.esIndex,
_type: this.config.userId,
......@@ -152,5 +162,6 @@ export default class crawlWebsite extends CrawlGeneric {
});
this.listDataForIndex.push(dataForIndex);
*/
}
}
......@@ -30,7 +30,6 @@ export function indexWebsite(data) {
export function indexApi(data) {
check(data, Object);
console.log(data);
formApiCrawlSchema.validate(data);
const index = new IndexApi();
return index.start(data);
......
......@@ -10,6 +10,11 @@ export default class IndexGeneric {
constructor() {
this.config = getConfig();
console.log(this.config);
}
indexOne(esIndex, esType, id, obj) {
return elastic.index(esIndex, esType, id, obj);
}
indexByBulk(data, hasFile) {
......@@ -35,7 +40,12 @@ export default class IndexGeneric {
const analyser = configElastic.analyser;
const mapping = configElastic.mapping;
await elastic.initAnalyzer(esIndex, analyser);
/**
* init seulement le mapping
* (chaque user à son propre type sur elastic)
* initialiser l'analyzer supprimerai egalement le mapping des autres
*/
// await elastic.initAnalyzer(esIndex, analyser);
await elastic.initMapping(esIndex, this.config.userId, mapping);
return elastic.createPipeline();
}
......
......@@ -11,14 +11,14 @@ export default class IndexWebsite extends IndexGeneric {
*/
async start(data) {
try {
const dataToIndex = await new CrawlWebsite(data);
const dataToIndex = await new CrawlWebsite(data, this);
/**
* dataToIndex index website page
* false no file
*/
const resultIndex = await this.indexByBulk(dataToIndex, false);
// const resultIndex = await this.indexByBulk(dataToIndex, false);
return {
message: `Indexation de ${resultIndex.items.length} liens pour ${data.urlWebsite} fini avec succès!`,
message: `Indexation de ${dataToIndex.length} liens pour ${data.urlWebsite} fini avec succès!`,
};
} catch (error) {
throw error;
......
......@@ -26,6 +26,14 @@ SimpleSchema.notifsCollection = new SimpleSchema({
},
userId: {
type: String,
autoValue() {
if (this.userId) {
return this.userId;
} else if (this.value) {
return this.value;
}
return 'non défini!';
},
},
});
......
......@@ -5,10 +5,10 @@ import _ from 'lodash';
import Search from '/imports/api/search/server/search';
import formSearchSchema from '/imports/api/search/formSearchSchema';
export async function searchWebsite(data) {
export async function searchWebsite(data, userId) {
check(data, Object);
formSearchSchema.validate(data);
const search = new Search();
const search = new Search(userId);
try {
const results = await search.searchWebsite(data.searchTerm);
return {
......@@ -21,9 +21,9 @@ export async function searchWebsite(data) {
}
}
export async function searchApi(data) {
export async function searchApi(data, userId) {
check(data, Object);
const search = new Search();
const search = new Search(userId);
try {
const results = await search.searchApi(data.searchTerm);
return {
......@@ -36,9 +36,9 @@ export async function searchApi(data) {
}
}
export async function searchDocument(data) {
export async function searchDocument(data, userId) {
check(data, Object);
const search = new Search();
const search = new Search(userId);
try {
const results = await search.searchDocument(data.searchTerm);
return {
......@@ -52,11 +52,12 @@ export async function searchDocument(data) {
}
export async function searchAll(data) {
const userId = Meteor.userId();
try {
return {
website: await searchWebsite(data),
api: await searchApi(data),
document: await searchDocument(data),
website: await searchWebsite(data, userId),
api: await searchApi(data, userId),
document: await searchDocument(data, userId),
};
} catch (error) {
console.log(error);
......@@ -66,7 +67,7 @@ export async function searchAll(data) {
export async function autoCompletion(term) {
check(term, String);
const search = new Search();
const search = new Search(Meteor.userId());
try {
const results = await search.autoComplete(term);
if (results.suggest) {
......
......@@ -2,10 +2,13 @@
import indexationElastic from '/imports/libs/elasticsearch/elasticsearch';
const esIndex = Meteor.settings.private.elasticsearch.esIndex;
const esType = Meteor.settings.private.elasticsearch.esType;
export default class Search {
constructor(userId) {
this.esType = userId;
}
/**
* search
* @param term
......@@ -67,7 +70,7 @@ export default class Search {
};
}
return indexationElastic.search(esIndex, esType, params);
return indexationElastic.search(esIndex, this.esType, params);
}
searchApi(term) {
......@@ -121,7 +124,7 @@ export default class Search {
};
}
return indexationElastic.search(esIndex, esType, params);
return indexationElastic.search(esIndex, this.esType, params);
}
searchDocument(term) {
......@@ -170,7 +173,7 @@ export default class Search {
};
}
return indexationElastic.search(esIndex, esType, params);
return indexationElastic.search(esIndex, this.esType, params);
}
/**
......
......@@ -239,10 +239,10 @@ export default {
async initMapping(esIndex, esType, obj) {
console.log('init mapping');
try {
const exists = await this.typeExists(esIndex, esType);
/* const exists = await this.typeExists(esIndex, esType);
if (exists) {
await this.mappingDelete(esIndex, esType);
}
}*/
await this.indexClose(esIndex);
await this.mappingCreate(esIndex, esType, obj);
return this.indexOpen(esIndex);
......
......@@ -32,6 +32,11 @@ adminSection.route('/account', {
navbar: 'navbarLayoutTpl',
});
},
triggersEnter: [(context, redirect) => {
if (Meteor.user() && Meteor.user().roles && Meteor.user().roles[0] !== 'admin') {
redirect('home');
}
}],
});
adminSection.route('/stat', {
......@@ -43,6 +48,11 @@ adminSection.route('/stat', {
navbar: 'navbarLayoutTpl',
});
},
triggersEnter: [(context, redirect) => {
if (Meteor.user() && Meteor.user().roles && Meteor.user().roles[0] !== 'admin') {
redirect('home');
}
}],
});
adminSection.route('/notif', {
......
......@@ -31,10 +31,15 @@ FlowRouter.route('/', {
sidebar: 'sidebarLayoutTpl',
});
},
triggersEnter: [(context, redirect) => {
if (!Meteor.userId()) {
redirect('login');
}
}],
});
FlowRouter.route('/demo', {
name: 'home',
name: 'demo',
action() {
BlazeLayout.render('mainLayoutTpl', {
main: 'demoTpl',
......@@ -56,7 +61,7 @@ FlowRouter.route('/logout', {
name: 'logout',
action() {
Meteor.logout();
FlowRouter.go('/');
FlowRouter.go('login');
displayNotif({
type: 'info',
title: 'Info : ',
......
......@@ -3,6 +3,7 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { Roles } from 'meteor/alanning:roles';
import { defineConfig } from "/imports/api/config/methods";
Meteor.startup(() => {
/**
......@@ -36,6 +37,10 @@ Meteor.startup(() => {
if (newUser) {
Roles.addUsersToRoles(newUser, 'admin');
defineConfig({
userId: newUser._id,
listConfig: [],
});
}
console.log('===================================================');
......
......@@ -10,12 +10,14 @@
<li>
<a href="/admin"><i class="fa fa-fw fa-etsy fa-20px" aria-hidden="true"></i> Indexation</a>
</li>
{{#if grantedAccessAdmin}}
<li>
<a href="/admin/account"><i class="fa fa-fw fa-user fa-20px" aria-hidden="true"></i> Comptes</a>
</li>
<li>
<a href="/admin/stat"><i class="fa fa-fw fa-bar-chart-o fa-20px" aria-hidden="true"></i> Statistiques</a>
</li>
{{/if}}
<li>
<a class="" href="/logout" ><i class="fa fa-fw fa-lock fa-20px"></i> Déconnexion</a>
</li>
......
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import './sidebar.html';
Template.sidebarLayoutTpl.helpers({
grantedAccessAdmin: () => !!(Meteor.user() && Meteor.user().roles && Meteor.user().roles[0] === 'admin'),
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment