Elasticsearch aggregation query not giving expected output - java

I am trying to do aggregation on documents which contains datetime and CPU time and server name. I want to find the avg CPU time on latest date. I have the following query which partially works it gives me the avg CPU time but not on latest date it just randomly chooses date.
client.prepareSearch("myindex").
setTypes("mytype").
setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(FilterBuilders.termFilter("server","x")))).
addAggregation(AggregationBuilders.avg("cpu_agg")
.field("dt_time").field("cpu_time"))
.get()
Please guide. I want avg cpu time on latest date say today's date. I am new to ElasticSearch. Thanks in advance.

client.prepareSearch("myindex").
setTypes("mytype").
setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(FilterBuilders.termFilter("server","x")))).
addAggregation(AggregationBuilders.avg("cpu_agg")
.field("dt_time").field("cpu_time"))
.get()
look at the portion (where field is set to dt_time at first and replaced to cpu_time), which means aggregation is build for cpu_time,
If you want to get cpu time in today's date then one way is use date filter ,
FilterBuilders.andFilter(FilterBuilders.termFilter("server","x"),FilterBuilders.rangeFilter("dt_time").to(to).from(from))))
For your problem, from = to = today_date (or last date)
so finally,
client.prepareSearch("myindex").
setTypes("mytype").
setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(FilterBuilders.termFilter("server", "x"), FilterBuilders.rangeFilter("dt_time").to("to").from("from")))).
addAggregation(AggregationBuilders.avg("cpu_agg")
.field("cpu_time"))
.get();

Related

Read Anylogic schedule exceptions from database table

How can I read a list of exception dates for a schedule from an Excel file without having to adopt each date from the file separately? I am trying to set up a shift-plan which takes into account holidays, etc. over the next 5 years. For this I have created an Excel table containing a list of holiday dates, which I would now like to use in my AnyLogic simulation. I tried the exceptions section of my schedule object, but didn't find a way to connect this section to my excel file. The only option I am getting here is to manually enter each date... Since this would be extremely tedious, I am looking for a workaround (Java Code?). Can someone help?
Suppose you have a scheduled object of type integer and a database table full of exception dates that you want the integer value of the schedule to be 0 for the entire day.
You can then programmatically add exceptions to your scheduling with the following code
List<Tuple> rows = selectFrom(db_table).list();
for (Tuple row : rows) {
Date exceptionDate = row.get( db_table.db_column );
schedule.addException(exceptionDate.getYear(), exceptionDate.getMonth(), exceptionDate.getDay(), exceptionDate.getHours(), exceptionDate.getMinutes(), exceptionDate.getSeconds(),
exceptionDate.getYear(), exceptionDate.getMonth(), exceptionDate.getDay()+1, exceptionDate.getHours(), exceptionDate.getMinutes(), exceptionDate.getSeconds(),
0, false);
}
The format for adding exceptions is a bit cumbersome but it is:
schedule.addException(startYear, startMonth, startDay, startHour, startMinute, startSecond, endYear, endMonth, endDay, endHour, endMinute, endSecond, value, annually);
The value object is the value of your schedule type
True, there is no build-in way for this. You need to code your schedule. This allows adding exceptions programmatically, see https://anylogic.help/anylogic/data/schedule.html#creating-and-initializing-schedule-from-code-on-model-startup
Specifically:

compare timestamp from auto_now format of django in java

I am working on a django and java project in which I need to compare the time in django to the time in current time in java.
I am storing the enbled_time in models as :
enabled_time = models.DateTimeField(auto_now = True, default=timezone.now())
The time gets populated in the db in the form :
2017-02-26 14:54:02
Now in my java project a cron is running which checks whether enabled_time plus an expiry time is greater than the current time something as:
Long EditedTime = db.getEnabledTime() + (expiryTime*60*1000); //expiryTime is in mins
if (System.currentTimeMillis() - EditedTime > 0) {
//do something
}
Here db is the database entity for that table.
But db.getEnabledTime() gives a result '2017'. What am I doing wrong?
PS: I am storing time as Long which seems unsuitable to me. Can someone suggest which datatype should I choose or does it work fine?

Calculate difference between 2 times in MongoDB

I have a field called Last Modified At with value like '2016/04/12 20:24:18'. It is not an ISO Date but a normal String value stored via a java process in MongoDb.
I am trying to write a shell script to calculate the difference between '2016/04/12 20:24:18' and say '2016/04/12 16:24:18'. The difference could be either in days or hours or mins or secs. I tried couple of things including converting to ISO dates but it doesnt work out. Is there an easy way to find out like Oracle.
Any help would be appreciated?
Thanks,
Ram
I'm not exactly sure how you plan on running the shell script, but it is possible in the mongo shell to parse dates (using Javascript) and calculate time between two dates as you have asked. Assume we have a document in the things database as follows:
{
"_id" : ObjectId("570f1e528163383227ace761"),
"lastModifiedAt" : "2016/04/12 20:24:18"
}
We can run the following script to get the difference between the lastModifiedDate of a document and a hard-coded date, such as 2016/04/12 16:24:18:
db.things.find({}).forEach(function(thing) {
var date1 = new Date(thing.lastModifiedAt);
var date2 = new Date('2016/04/12 16:24:18');
var dateDiff = date1.getTime() - date2.getTime();
printjson({_id:thing._id,lastModifiedAt:thing.lastModifiedAt,dateDiff:dateDiff});
});
This results in:
{
"_id" : ObjectId("570f1e528163383227ace761"),
"lastModifiedAt" : "2016/04/12 20:24:18",
"dateDiff" : 14400000
}
where dateDiff is milliseconds and 14400000 milliseconds = 4 hours.
If you provide more information on how you plan on making this call, and where the second date is coming from I would be happy to expand upon this answer.

IBM ContentManager v8 - Query Syntax to search documents by its creation date

We are using IBM ContentManager v8. I am looking for the correct syntax to find all documents uploaded to CM, after a specific date. I would like to use the document or object creation time stamp, which is an internal attribute of every document uploaded to CM.
Our backend database for CM is DB2.
From the example, I see that we can use
/<item_type> [ICMCHECKEDOUT/#ICMCHKOUTTS > "2013-11-20-12.00.00.000000"]
for example, to find all documents that were checked out after the specified date.
Is there an internal attribute for the time on which the object was added to CM.? If yes, what is the syntax.?
FYI:- DKLobICM.getCreatedTimestamp() method returns the exact time the object was added to CM. But I need a way to use this timestamp in my query.
Rgds,
Raj.
For the record:
/<item_type>[#SYSROOTATTRS.CREATETS>"2013-11-20-12.00.00.000000"]
will give you the expected results.
Try This
/<item_type>[#AttributeRef="xxxxx" AND #CREATETS BETWEEN "2015-06-01-00.00.00.000" AND "2015-12-31-00.00.00.0000"]

Trouble when inserting time and date to db using Java

Here's my code:
uprs.moveToInsertRow();
uprs.updateString("Sender", myName);
uprs.updateString("Receiver", withWho);
uprs.updateString("Message", myMessage);
//uprs.updateString("Time",****);
uprs.insertRow();
Things go perfectly when adding the non-time data into the database.
But I am struggling how to add the current date and time into the database.(Ms SQL 2008)
Is there any expert can tell me how to organize the code about getting the current time and date and insert them into the db?
The data type of Time in my db is "datetime".
Thank you!
For a date/time type you should use updateTimestamp instead of updateString

Categories

Resources