Elasticsearch - Get locale specific date in Groovy script - datetime

I need to update existing documents of a particular type with a new field. This new field should be set to the local day name of a date given by a datetime field in the document. The date time field is in the format yyyy-MM-dd'T'HH:mm:ss, is in UTC but has no explicit timezone code.
I'd like to do this with Groovy but have no Java experience. I'm guessing I need to:
1) Set the datetime to UTC locale
2) Convert to local locale (set to Europe/London) in this case
3) Get the day name from converted date and set new field (dayname) value with it
If I use the following update_by_query:
POST /myindex/_update_by_query
{
"script": {
"inline": "ctx._source.dayname = Some_function(ctx._source.datetime)"
},
"query": {
"term": {
"_type": "myDocType"
}
}
}
Is there a simple way to do this with a some Groovy functions (replacing Some_function above).
Any hints would be very much appreciated!
UPDATE - Thanks to tim_yates i have the following Sense console code:
POST /rating/_update_by_query
{
"script": {
"inline": "ctx._source.day = Date.parse(\"yyyy-MM-dd'T'HH:mm:ss\", ctx._source.datetime, java.util.TimeZone.getTimeZone('UTC')).format('EEEE', java.util.TimeZone.getTimeZone('Europe/London'))"
},
"query": {
"term": {
"_type": "transaction"
}
}
}
Unfortunately this generates the following error message:
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "failed to run inline script [ctx._source.day = Date.parse(\"yyyy-MM-dd'T'HH:mm:ss\", ctx._source.datetime, java.util.TimeZone.getTimeZone('UTC')).format('EEEE', java.util.TimeZone.getTimeZone('Europe/London'))] using lang [groovy]"
}
],
"type": "script_exception",
"reason": "failed to run inline script [ctx._source.day = Date.parse(\"yyyy-MM-dd'T'HH:mm:ss\", ctx._source.datetime, java.util.TimeZone.getTimeZone('UTC')).format('EEEE', java.util.TimeZone.getTimeZone('Europe/London'))] using lang [groovy]",
"caused_by": {
"type": "missing_property_exception",
"reason": "No such property: java for class: 1209ff7fb16beac3a71ff2a276ac2225f7c4505b"
}
},
"status": 500
}
Though it does work if I remove reference to the getTimeZone - exact Sense console code as follows:
POST /rating/_update_by_query
{
"script": {
"inline": "ctx._source.day = Date.parse(\"yyyy-MM-dd'T'HH:mm:ss\", ctx._source.datetime).format('EEEE')"
},
"query": {
"term": {
"_type": "transaction"
}
}
}
I'm not sure why the getTimeZone method is failing. I've tried "TimeZone.getTimeZone" instead of "java.util.TimeZone.getTimeZone"

The Groovy code (for Java 7) you will need is going to be very similar to this:
Date.parse("yyyy-MM-dd'T'HH:mm:ss", ctx._source.datetime, TimeZone.getTimeZone('UTC'))
.format('EEEE', TimeZone.getTimeZone("Europe/London"))

OK, a bit more research allowed me to understand that tim_yates answer was correct, we just needed to whitelist the Java class required. We did this via:
nano $JAVA_HOME/lib/security/java.policy
and adding:
permission org.elasticsearch.script.ClassPermission "java.util.TimeZone";
...then restart the ES services.
Note this whitelists the class for all users, not just the ES user. See:
https://www.elastic.co/guide/en/elasticsearch/reference/2.2/modules-scripting-security.html

Related

Is there a script to show the added fields in response in Postman when using "additionalProperties": false?

i have added "additionalProperties": false to schema scripts in postman. The "additionalProperties": false, worked fine and im getting error message when new fields are newly added to the json response and not covered by schema. But is there a script to specify what fields are added in the json?
No, I think you cannot.
I have tried using itv4 directly instead of using it through pm...
like below... The best I have obtained is the number of fields that are exceeding... but not the name.
const schema = {
"type": "object",
"properties": {
"code": { "type": "string" }
},
"additionalProperties": false
};
console.log(tv4.validate(body, schema))
console.log(tv4.validateResult(body, schema))
console.log(tv4.validateMultiple(body, schema));
pm.test("Explicitly ", function () {
let Ajv = require('ajv');
let ajv = Ajv()
let result = ajv.validate(schema, jsonData);
pm.expect(result, JSON.stringify(ajv.errors)).to.be.true;
});
this will show the additional property there is a bug for this already :
https://github.com/postmanlabs/postman-app-support/issues/9276

pushsubscription gives options instead of keys when using requestSubscription()

I'm trying to follow this tutorial to enable push notifications in my PWA. According to the tutorial, I should get a pushSubscription object like this:
{
"endpoint": "https://fcm.googleapis.com/fcm/send/cbx2QC6AGbY:APA91bEjTzUxaBU7j-YN7ReiXV-MD-bmk2pGsp9ZVq4Jj0yuBOhFRrUS9pjz5FMnIvUenVqNpALTh5Hng7HRQpcUNQMFblTLTF7aw-yu1dGqhBOJ-U3IBfnw3hz9hq-TJ4K5f9fHLvjY",
"expirationTime": null,
"keys": {
"p256dh": "BOXYnlKnMkzlMc6xlIjD8OmqVh-YqswZdut2M7zoAspl1UkFeQgSLYZ7eKqKcx6xMsGK7aAguQbcG9FMmlDrDIA=",
"auth": "if-YFywyb4g-bFB1hO9WMw=="
}
}
However when I inspect my subscription object I get something in the form of
{
"endpoint": "https://fcm.googleapis.com/fcm/send/cbx2QC6AGbY:APA91bEjTzUxaBU7j-YN7ReiXV-MD-bmk2pGsp9ZVq4Jj0yuBOhFRrUS9pjz5FMnIvUenVqNpALTh5Hng7HRQpcUNQMFblTLTF7aw-yu1dGqhBOJ-U3IBfnw3hz9hq-TJ4K5f9fHLvjY",
"expirationTime": null,
"options": {
"applicationServerKey": ArrayBuffer(65),
"userVisibleOnly": true
}
}
note that I do not get the keys object and instead get an options object.
Have the properties of the pushSubscription object changed? If I use this to send notifications, will it still work?
When saving my pushSubscription object with Mongoose, I used keys instead of options
If I then inspect the object in my db (on MLab in my case) it looks like
{
"endpoint": "https://fcm.googleapis.com/fcm/send/cbx2QC6AGbY:APA91bEjTzUxaBU7j-YN7ReiXV-MD-bmk2pGsp9ZVq4Jj0yuBOhFRrUS9pjz5FMnIvUenVqNpALTh5Hng7HRQpcUNQMFblTLTF7aw-yu1dGqhBOJ-U3IBfnw3hz9hq-TJ4K5f9fHLvjY",
"expirationTime": null,
"keys": {
"p256dh": "BOXYnlKnMkzlMc6xlIjD8OmqVh-YqswZdut2M7zoAspl1UkFeQgSLYZ7eKqKcx6xMsGK7aAguQbcG9FMmlDrDIA=",
"auth": "if-YFywyb4g-bFB1hO9WMw=="
}
}
I guess that the object in my console is visualized differently, thus showing me the options parameter instead of keys.

No HTTP resource was found that matches the request URI (...) for all tables

Following this tutorial as a guide (OData/EntityFramework/Asp.Net).
I'm able to execute a simple GET command on the root.
{
"#odata.context": "http://localhost:49624/$metadata",
"value": [
{
"name": "Appointments",
"kind": "EntitySet",
"url": "Appointments"
},
......
{
"name": "Clients",
"kind": "EntitySet",
"url": "Clients"
}
]
}
But anything more complex than that gives me an error message. (I'm using a null routePrefix.)
http://localhost:49624/Services
Gives me:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:49624/Services'.",
"MessageDetail": "No type was found that matches the controller named 'Services'."
}
Here's my super simple GET
[EnableQuery]
public IQueryable<Service> Get()
{
return db.Services;
}
If it matters I'm using Postman to test these commands. Although I imagine that is a non-factor.
I have a database & a DbSet for every table. I have no idea why I can't access any of this.
WebApiConfig:
config.MapHttpAttributeRoutes();
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Appointment>("Appointments");
builder.EntitySet<Service>("Services");
builder.EntitySet<Employee>("Employees");
builder.EntitySet<Client>("Clients");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
I'm sorry if this is a basic question but I'm really new to all this and have been at this wall too long already haha.
Jan Hommes above pointed out above that the controller class needs to be pluralized (In my case ServiceController -> ServicesController)

Limit depth of data returned

Is it possible to limit the depth of data returned from Firebase database?
For example, if I want to get some data from a parent object without waiting for all of its children & sub-children, can I specify that I only want x levels of objects?
Firebase does not yet have this capability.
We do intend to add it, but don't have a timetable yet.
There appears to be a shallow option...
{
"message": {
"user": {
"name": "Chris"
},
"body": "Hello!"
}
}
// A request to /message.json?shallow=true
// would return the following:
{
"user": true,
"body": true
}
// A request to /message/body.json?shallow=true
// would simply return:
"Hello!"
From: https://firebase.google.com/docs/database/rest/retrieve-data

Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken

I'm attempting to build off of Mike Jansen's JIRA REST Client, and I'm trying to pull in JIRA version information. I'm new to JSON, so I'm not sure if it's just a formatting issue or what.
When debugging, I have the following token:
{[
{
"self": "https://<company>.atlassian.net/rest/api/2/version/10101",
"id": "10101",
"name": "2012.3",
"archived": false,
"released": false,
"releaseDate": "2012-10-08"
},
{
"self": "https://<company>.atlassian.net/rest/api/2/version/10200",
"id": "10200",
"name": "2012.4",
"archived": false,
"released": false
}
]}
and the following line of code
token.Children().Values<T>()
is throwing the following error
Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken
while trying to convert the two version tokens into the corresponding JiraVersion class:
using System;
namespace JiraRestClient
{
public class JiraVersion : JiraObjectBase, IJiraVersion
{
public JiraVersion(IJiraRestResponse jiraResponse) : base(jiraResponse) { }
public string Id { get { return Get<string>("Id", "id"); } }
public string Name { get { return Get<string>("Name", "name"); } }
}
}
Can somebody help me out?
Those of you familiar with JSON may have noticed quickly that it was, in fact, a problem with the formatting (the extra curly brackets enclosing the array). Like I said, I'm new to JSON, but the end result of my research is that the JsonWrapper.TryGetPath(...) method attempts to traverse the JObject tree and does not produce correctly formatted JSON when what's being retrieved is an array.
My solution was to simplify things by removing JSON.Net from the solution and depending only on RestSharp (just because it makes it so easy to make requests) and the System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(response.Content) approach.

Resources