Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Console Script Aware #9433

Merged
merged 6 commits into from Dec 23, 2016
Merged

Make Console Script Aware #9433

merged 6 commits into from Dec 23, 2016

Conversation

bleskes
Copy link
Contributor

@bleskes bleskes commented Dec 9, 2016

At the moment, Console understands HTTP methods, URLs and JSON. However ES has long had the options to write inline scripts in its API and with the introduction of painless inline scripts become more and more popular. Sadly, writing them in JSON is a PITA as you must escape every single thing.

This PR adds the notion of literal strings to Console's flavor of JSON. This means that everything that is surrounded with """ will be JSON escaped before sending to ES. On top of that, if a literal string is found after "script" or "inline" tag it is treated as a script literal, which adds syntax highlighting and some Ace goodness. Script literal are a full blown Ace Mode, meaning we can extend it to do auto complete and similar in the future. Also, auto formatting works as well, automatically taking strings with more than 2 escaped characters and converting them literal strings. In similar fashion, the output pane also auto converts strings in the output.

This is how it all looks:

Script literal:

image

String literals as fields in documents:

image

To illustrate what happens when things are sent to ES, here is the Copy as Curl output of the last document:

curl -XPUT "http://localhost:9200/hockey/player/test" -d'
{
  "name": "player1",
  "description": "  some long and explicit text with new lines, \\slashes\\ and\n  {crazy} brackets :/",
  "field_chars": """/regex/g"""
}'

@bleskes
Copy link
Contributor Author

bleskes commented Dec 9, 2016

PS. These the keywords currently highlighted in script mode:

let painlessKeywords = (
  "def|int|long|byte|String|new|null|for|if|return|ctx"
);

@nik9000 @jdconrad it would be great if you can extend this list (just post it here and I'll update the code)

@nik9000
Copy link
Member

nik9000 commented Dec 9, 2016

(just post it here and I'll update the code)

Here is the master list: https://github.com/elastic/elasticsearch/blob/master/modules/lang-painless/src/main/antlr/PainlessLexer.g4#L43-L57

@nik9000
Copy link
Member

nik9000 commented Dec 9, 2016

Once we get this in I can modify the tool that extract CONSOLE syntax out of Elasticsearch's docs and we can use this in the docs. Much awesome!

@nik9000
Copy link
Member

nik9000 commented Dec 9, 2016

float and double and char are all missing primitives. That list I pasted above is really just "syntax". null and true and false are also in the list, just hiding below.

@jdconrad
Copy link

jdconrad commented Dec 9, 2016

@bleskes This is awesome! @nik9000 Do you think it would be worth somehow special casing all of the types in the whitelist as well? If we could somehow build this to do the recommendations for methods off of a class type that would be pretty amazing.

@nik9000
Copy link
Member

nik9000 commented Dec 9, 2016

If we could somehow build this to do the recommendations for methods off of a class type that would be pretty amazing.

That'd be cool. We could add an endpoint that you could use to look them up if we wanted to get fancy.

this.$outdent.autoOutdent(doc, row);
};

// this.createWorker = function (session) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this code isn't in use, just remove it rather than commenting it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left overs indeed. removed.

@@ -20,6 +21,9 @@ var Mode = function () {
this.$outdent = new MatchingBraceOutdent();
this.$behaviour = new CstyleBehaviour();
this.foldingRules = new CStyleFoldMode();
this.createModeDelegates({
"script-": ScriptMode
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the extra indents here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing

if (state != "double_q_string") {
if (state == "string_literal") {
// noop
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a noop, why not just if (state !== 'string_literal') {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

uiModules.get('app/sense').setupResizeCheckerForRootEditors($el, input, output);
// this may not exist if running from tests
let appSense = uiModules.get('app/sense');
if (appSense.setupResizeCheckerForRootEditors) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mock this in the tests rather than altering the code to accommodate the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the extra check and everything seems to work fine now. I think it's a left over from old times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it back. Things break. I mocked it instead.

this.$behaviour = new CstyleBehaviour();
this.foldingRules = new CStyleFoldMode();
};
oop.inherits(ScriptMode, TextMode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the oop module in ace, but you can now use native JS class syntax, which is a little easier to grok:

class ScriptMode extends TextMode {
  constructor() {
    this.$outdent = new MatchingBraceOutdent();
    this.$behaviour = new CstyleBehaviour();
    this.foldingRules = new CStyleFoldMode();
  }

  get HighlightRules() {
    return ScriptHighlightRules;
  }

  getNextLineIndent(state, line, tab) {
    let indent = this.$getIndent(line);
    const match = line.match(/^.*[\{\[]\s*$/);
    if (match) {
      indent += tab;
    }
    return indent;
  }

  // etc etc
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the acequire thing... I really have no idea what any of that is doing, so take what I just proposed with a grain of salt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current ace is a stand alone fully embeddable version of it that tries to isolate itself from the universe. It has it's own version of require that is fully served from a single big file. I think at some point we should move to an unpacked version of ace where it's easier to read the source and we just import ourselves. I rather not tackle it now though.


}).call(ScriptMode.prototype);

module.exports.ScriptMode = ScriptMode;
Copy link
Contributor

@epixa epixa Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use native JS exports now rather than CommonJS module exports, so your class definition can simply be:

export class ScriptMode extends TextMode {

And then you can remove module.exports.ScriptMode = ScriptMode; entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for the export tip. applied.

"def|int|long|byte|String|new|null|for|if|return|ctx"
);

var ScriptHighlightRules = function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same suggestion here about using native class syntax as I did in the previous file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took over the direct export

@epixa
Copy link
Contributor

epixa commented Dec 21, 2016

I added a couple of notes. PRs for new features on console are particular challenging because we haven't yet updated the console codebase to be consistent in terms of code style and technologies with the rest of Kibana. I created two issues in particular for this: #9603 #9604

We shouldn't block this PR on those issues, but I wanted to highlight this for both you @bleskes and for other reviewers as we have to tread the fine line between writing code that's consistent with Kibana and code that's consistent with Console.

@bleskes
Copy link
Contributor Author

bleskes commented Dec 23, 2016

thx @epixa . I addressed all your comments. Can you take another look?

@epixa
Copy link
Contributor

epixa commented Dec 23, 2016

@bleskes Looks like there's a linter failure in CI:

/var/lib/jenkins/workspace/elastic+kibana+pull-request+multijob-intake/src/core_plugins/console/public/tests/src/utils_tests.js
  6:6   error  A space is required after '{'   babel/object-curly-spacing
  6:64  error  A space is required before '}'  babel/object-curly-spacing

@epixa
Copy link
Contributor

epixa commented Dec 23, 2016

@bleskes How do I run the qunit tests for console?

@epixa
Copy link
Contributor

epixa commented Dec 23, 2016

Nevermind, I found it.

Edit: Just access http://localhost:5601/app/sense-tests for any future readers.

@epixa
Copy link
Contributor

epixa commented Dec 23, 2016

LGTM

@epixa epixa merged commit 5616a15 into elastic:master Dec 23, 2016
elastic-jasper added a commit that referenced this pull request Dec 23, 2016
Backports PR #9433

**Commit 1:**
Add console scripts

* Original sha: c645975
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T16:28:26Z

**Commit 2:**
Merge remote-tracking branch 'upstream/master' into console_scripts

* Original sha: 795fcc0
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T16:29:04Z

**Commit 3:**
Better handling of auto formatting

* Original sha: cf6e0b1
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T20:15:16Z

**Commit 4:**
Merge remote-tracking branch 'upstream/master' into console_scripts

* Original sha: 036b0f6
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T15:17:52Z

**Commit 5:**
review feedback

* Original sha: 361429e
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T16:16:32Z

**Commit 6:**
linting

* Original sha: 412633d
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T18:22:28Z
@bleskes
Copy link
Contributor Author

bleskes commented Dec 23, 2016

@bleskes Looks like there's a linter failure in CI:

That's peculiar because I thought you guys have linting setup as precommit. Anyway it works now :)

@epixa thx for the review. It seems you guys have a different process (as you merged it in) and it looks like you guys are also taking care of backporting? If so thanks for that as well :)

@epixa
Copy link
Contributor

epixa commented Dec 23, 2016

@bleskes To be honest, we do usually expect the submitter to merge/backport, but I didn't know what process you followed, so I treated this as I would a community submitted PR instead. I'll remember this for your future PRs though :P

@bleskes
Copy link
Contributor Author

bleskes commented Dec 23, 2016

@epixa ❤️

@bleskes bleskes deleted the console_scripts branch December 23, 2016 19:16
@epixa
Copy link
Contributor

epixa commented Dec 23, 2016

Oh, and as for the precommit hook: the task was broken for a little while and wasn't linting properly, but that was fixed recently. Your branch just didn't have the latest master, so the linter wasn't working.

epixa added a commit that referenced this pull request Dec 23, 2016
Backports PR #9433

**Commit 1:**
Add console scripts

* Original sha: c645975
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T16:28:26Z

**Commit 2:**
Merge remote-tracking branch 'upstream/master' into console_scripts

* Original sha: 795fcc0
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T16:29:04Z

**Commit 3:**
Better handling of auto formatting

* Original sha: cf6e0b1
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T20:15:16Z

**Commit 4:**
Merge remote-tracking branch 'upstream/master' into console_scripts

* Original sha: 036b0f6
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T15:17:52Z

**Commit 5:**
review feedback

* Original sha: 361429e
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T16:16:32Z

**Commit 6:**
linting

* Original sha: 412633d
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T18:22:28Z
epixa pushed a commit that referenced this pull request Dec 23, 2016
Backports PR #9433

**Commit 1:**
Add console scripts

* Original sha: c645975
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T16:28:26Z

**Commit 2:**
Merge remote-tracking branch 'upstream/master' into console_scripts

* Original sha: 795fcc0
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T16:29:04Z

**Commit 3:**
Better handling of auto formatting

* Original sha: cf6e0b1
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-09T20:15:16Z

**Commit 4:**
Merge remote-tracking branch 'upstream/master' into console_scripts

* Original sha: 036b0f6
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T15:17:52Z

**Commit 5:**
review feedback

* Original sha: 361429e
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T16:16:32Z

**Commit 6:**
linting

* Original sha: 412633d
* Authored by Boaz Leskes <b.leskes@gmail.com> on 2016-12-23T18:22:28Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants