Skip to content

Commit 6a4dd52

Browse files
author
Alban Jubert
committed
Show a progress bar while data indexation
Added badge notification when the index is out of sync Documentation fix
1 parent 5893c81 commit 6a4dd52

File tree

11 files changed

+187
-126
lines changed

11 files changed

+187
-126
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ If your Elasticsearch index becomes out of sync with your sites contents, you ca
106106

107107
## Elasticsearch plugin Roadmap
108108

109-
* Handel dependencies update
110-
* Handel multi-sites configurations
109+
* Handle dependencies update
110+
* Handle multi-sites configurations
111111
* Detect need for re-indexation
112112

113113
Brought to you by ![LHS Logo](resources/img/lhs.png) [La Haute Société](https://www.lahautesociete.com)

src/Elasticsearch.php

+9-32
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,7 @@ class Elasticsearch extends Plugin
6666
// Public Methods
6767
// =========================================================================
6868

69-
/**
70-
* Set our $plugin static property to this class so that it can be accessed via
71-
* Elasticsearch::$plugin
72-
*
73-
* Called after the plugin class is instantiated; do any one-time initialization
74-
* here such as hooks and events.
75-
*
76-
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
77-
* you do not need to load it in your init() method.
78-
*
79-
*/
69+
8070
public function init()
8171
{
8272
parent::init();
@@ -165,35 +155,22 @@ function (RegisterComponentTypesEvent $event) {
165155
}
166156
);
167157

168-
// Do something after we're installed
169158
Event::on(
170159
Plugins::class,
171-
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
160+
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
172161
function (PluginEvent $event) {
173162
if ($event->plugin === $this) {
174-
// We were just installed
163+
$this->elasticsearch->reindexAll();
175164
}
176165
}
177166
);
178167

179-
/**
180-
* Logging in Craft involves using one of the following methods:
181-
*
182-
* Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use.
183-
* Craft::info(): record a message that conveys some useful information.
184-
* Craft::warning(): record a warning message that indicates something unexpected has happened.
185-
* Craft::error(): record a fatal error that should be investigated as soon as possible.
186-
*
187-
* Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log`
188-
*
189-
* It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets
190-
* the category to the method (prefixed with the fully qualified class name) where the constant appears.
191-
*
192-
* To enable the Yii debug toolbar, go to your user account in the AdminCP and check the
193-
* [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel
194-
*
195-
* http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
196-
*/
168+
// Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, function(RegisterUserPermissionsEvent $event) {
169+
// $event->permissions[\Craft::t('elasticsearch', 'Elasticsearch')] = [
170+
// 'some-thing' => ['label' => \Craft::t('elasticsearch', 'Something')],
171+
// ];
172+
// });
173+
197174
Craft::info(
198175
Craft::t(
199176
'elasticsearch',

src/assetbundles/elasticsearch/dist/js/Elasticsearch.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
* @package Elasticsearch
1010
* @since 1.0.0
1111
*/
12+
13+

src/assetbundles/elasticsearchutility/dist/js/Elasticsearch.js

+88
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,91 @@
99
* @package Elasticsearch
1010
* @since 1.0.0
1111
*/
12+
13+
(function($) {
14+
15+
Craft.ElasticsearchUtility = Garnish.Base.extend(
16+
{
17+
$trigger: null,
18+
$form: null,
19+
20+
init: function(formId) {
21+
this.$form = $('#' + formId);
22+
this.$trigger = $('input.submit', this.$form);
23+
this.$status = $('.utility-status', this.$form);
24+
25+
this.addListener(this.$form, 'submit', 'onSubmit');
26+
},
27+
28+
onSubmit: function(ev) {
29+
ev.preventDefault();
30+
31+
if (!this.$trigger.hasClass('disabled')) {
32+
if (!this.progressBar) {
33+
this.progressBar = new Craft.ProgressBar(this.$status);
34+
}
35+
else {
36+
this.progressBar.resetProgressBar();
37+
}
38+
39+
this.progressBar.$progressBar.removeClass('hidden');
40+
41+
this.progressBar.$progressBar.velocity('stop').velocity(
42+
{
43+
opacity: 1
44+
},
45+
{
46+
complete: $.proxy(function() {
47+
var postData = Garnish.getPostData(this.$form),
48+
params = Craft.expandPostArray(postData);
49+
50+
var data = {};
51+
52+
Craft.postActionRequest(params.action, data, $.proxy(function(response, textStatus) {
53+
if (response && response.error) {
54+
alert(response.error);
55+
}
56+
57+
this.updateProgressBar();
58+
59+
setTimeout($.proxy(this, 'onComplete'), 300);
60+
61+
}, this),
62+
{
63+
complete: $.noop
64+
});
65+
66+
}, this)
67+
});
68+
69+
if (this.$allDone) {
70+
this.$allDone.css('opacity', 0);
71+
}
72+
73+
this.$trigger.addClass('disabled');
74+
this.$trigger.trigger('blur');
75+
}
76+
},
77+
78+
updateProgressBar: function() {
79+
var width = 100;
80+
this.progressBar.setProgressPercentage(width);
81+
},
82+
83+
onComplete: function() {
84+
if (!this.$allDone) {
85+
this.$allDone = $('<div class="alldone" data-icon="done" />').appendTo(this.$status);
86+
this.$allDone.css('opacity', 0);
87+
}
88+
89+
this.progressBar.$progressBar.velocity({opacity: 0}, {
90+
duration: 'fast', complete: $.proxy(function() {
91+
this.$allDone.velocity({opacity: 1}, {duration: 'fast'});
92+
this.$trigger.removeClass('disabled');
93+
this.$trigger.trigger('focus');
94+
}, this)
95+
});
96+
}
97+
});
98+
99+
})(jQuery);

src/console/controllers/ElasticsearchController.php

+4-49
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,10 @@
1111
namespace lhs\elasticsearch\console\controllers;
1212

1313
use lhs\elasticsearch\Elasticsearch;
14-
15-
use Craft;
1614
use yii\console\Controller;
17-
use yii\helpers\Console;
1815

1916
/**
20-
* Elasticsearch Command
21-
*
22-
* The first line of this class docblock is displayed as the description
23-
* of the Console Command in ./craft help
24-
*
25-
* Craft can be invoked via commandline console by using the `./craft` command
26-
* from the project root.
27-
*
28-
* Console Commands are just controllers that are invoked to handle console
29-
* actions. The segment routing is plugin-name/controller-name/action-name
30-
*
31-
* The actionIndex() method is what is executed if no sub-commands are supplied, e.g.:
32-
*
33-
* ./craft elasticsearch/elasticsearch
34-
*
35-
* Actions must be in 'kebab-case' so actionDoSomething() maps to 'do-something',
36-
* and would be invoked via:
37-
*
38-
* ./craft elasticsearch/elasticsearch/do-something
17+
* Allow various operation for elastisearch index
3918
*
4019
* @author Alban Jubert
4120
* @package Elasticsearch
@@ -47,36 +26,12 @@ class ElasticsearchController extends Controller
4726
// =========================================================================
4827

4928
/**
50-
* Handle elasticsearch/elasticsearch console commands
51-
*
52-
* The first line of this method docblock is displayed as the description
53-
* of the Console Command in ./craft help
29+
* Reindex Craft entries into elasticsearch
5430
*
5531
* @return mixed
5632
*/
57-
public function actionIndex()
33+
public function actionReindexAll()
5834
{
59-
$result = 'something';
60-
61-
echo "Welcome to the console ElasticsearchController actionIndex() method\n";
62-
63-
return $result;
64-
}
65-
66-
/**
67-
* Handle elasticsearch/elasticsearch/do-something console commands
68-
*
69-
* The first line of this method docblock is displayed as the description
70-
* of the Console Command in ./craft help
71-
*
72-
* @return mixed
73-
*/
74-
public function actionDoSomething()
75-
{
76-
$result = 'something';
77-
78-
echo "Welcome to the console ElasticsearchController actionDoSomething() method\n";
79-
80-
return $result;
35+
return Elasticsearch::$plugin->elasticsearch->reindexAll();
8136
}
8237
}

src/controllers/ElasticsearchController.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,37 @@ class ElasticsearchController extends Controller
5252
// =========================================================================
5353

5454
/**
55-
* Handle a request going to our plugin's actionDoSomething URL,
56-
* e.g.: actions/elasticsearch/elasticsearch/do-something
55+
* Test the elasticsearch connection
5756
*
5857
* @return mixed
5958
*/
6059
public function actionTestConnection()
6160
{
6261
if(Elasticsearch::$plugin->elasticsearch->testConnection() === true) {
63-
Craft::$app->session->setFlash('notice', Craft::t('elasticsearch', 'Successfully connected to {http_address}', ['http_address' => $this->module->settings->http_address]));
62+
Craft::$app->session->setNotice(Craft::t('elasticsearch', 'Successfully connected to {http_address}', ['http_address' => $this->module->settings->http_address]));
6463
}
6564
else {
66-
Craft::$app->session->setFlash('error', Craft::t('elasticsearch', 'Could not establish connection with {http_address}', ['http_address' => $this->module->settings->http_address]));
65+
Craft::$app->session->setError(Craft::t('elasticsearch', 'Could not establish connection with {http_address}', ['http_address' => $this->module->settings->http_address]));
6766
}
6867

6968
return $this->redirect(UrlHelper::cpUrl('utilities/elasticsearch-utilities'));
7069
}
7170

71+
/**
72+
* Reindex Craft entries into elasticsearch (called from utility panel)
73+
*
74+
* @return \yii\web\Response
75+
*/
7276
public function actionReindexAll()
7377
{
7478
Elasticsearch::$plugin->elasticsearch->reindexAll();
75-
Craft::$app->session->setFlash('notice', Craft::t('elasticsearch', 'Elasticsearch indexing in progress...'));
79+
80+
if (\Craft::$app->getRequest()->getAcceptsJson()) {
81+
return $this->asJson([
82+
'success' => true
83+
]);
84+
}
85+
7686
return $this->redirect(UrlHelper::cpUrl('utilities/elasticsearch-utilities'));
7787
}
7888
}

src/models/Settings.php

-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Craft;
1414
use craft\base\Model;
15-
use lhs\elasticsearch\Elasticsearch;
1615

1716
/**
1817
* Elasticsearch Settings Model
@@ -67,12 +66,4 @@ public function rules()
6766
[['auth_username', 'auth_password'], 'trim']
6867
];
6968
}
70-
71-
public function afterValidate()
72-
{
73-
if (Elasticsearch::$plugin->elasticsearch->testConnection() !== true) {
74-
$this->addError('http_address', Craft::t('elasticsearch', 'Could not establish connection with {http_address}', ['http_address' => $this->http_address]));
75-
}
76-
parent::afterValidate();
77-
}
7869
}

0 commit comments

Comments
 (0)