Is it possible to add a Relative Time Field to a Kibana Datatable - elasticsearch

I would like to add a field to a datatable which shows the elapsed time based on another date field. Specifically, I have a field which provides the event date and time, and need another field which shows that event happened 1 day 2 hours and 15 minutes ago. When they look at the same visualization a minute later the field should update to 1 day 2 hours and 16 minutes ago.
I know that I can have users select the time range they would like to focus on, but I'm attempting to duplicate functionality from a previous (non-ELK) solution which offered this field.

So here's what I came up with:
long different = (System.currentTimeMillis() - doc['BeginDate'].value);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
different = different % secondsInMilli;
if(elapsedSeconds < 1) {
different.toString() + " milliseconds";
} else if (elapsedSeconds == 1) {
elapsedSeconds.toString() + " second, " + different.toString() + " milliseconds";
} else if (elapsedMinutes < 1) {
elapsedSeconds.toString() + " seconds, " + different.toString() + " milliseconds";
} else if (elapsedMinutes == 1) {
elapsedMinutes.toString() + " minute, " + elapsedSeconds.toString() + " seconds";
} else if (elapsedHours < 1) {
elapsedMinutes.toString() + " minutes, " + elapsedSeconds.toString() + " seconds";
} else if (elapsedHours == 1) {
elapsedHours.toString() + " hour, " + elapsedMinutes.toString() + " minutes";
} else if (elapsedDays < 1) {
elapsedHours.toString() + " hours, " + elapsedMinutes.toString() + " minutes";
} else if (elapsedDays == 1) {
elapsedDays.toString() + " day, " + elapsedHours.toString() + " hours";
} else {
elapsedDays.toString() + " days, " + elapsedHours.toString() + " hours";
}
I added a scripted field to the Index Pattern, chose "painless" for language, "date" for Type, "String" for Format, and Transform is "- none -".
It isn't pretty, but it appears to work. I'm certain that it could be improved greatly, but it appears to fit my needs. Hope this helps someone else.

Related

Sending very long strings from MetaTrader 4 to Python backend using ZeroMQ Push/Pull Pattern

I have a quite weird issue related to ZeroMQ messaging between MetaTrader 4 and a python backend.
The below shown code creates three strings which should be forwarded to a ZeroMQ PUSH socket. However, it only sends the live_trades string via the socket. Both the account_info and the historical_trades strings are not sent.
While debugging the weirdness only increased:
When I print the historical_trades string, it is shown in the MT4 expert journal, but not in the MT4 log editor
When I add Print( historical_trades ) before or after the pushSocket.send(StringFormat("%s", historical_trades, true)); it sends the string.
Possible issues which I actually exclude:
The PUSH socket is too slow: I decreased the timer to 10 seconds and more, same result
MT4 itself is too slow to create the strings: It prints them correctly, so it is fast enough
The ZeroMQ SNDHWM ( HighWaterMark ) blocks the EA/socket from sending multiple strings in no time: Already increased it to 100 which should be more than sufficient
Any other ideas I cannot verify with debugging:
The MT4 Print() function does some other stuff behind the scenes which enables the EA to send the string?
Maybe there is a maximum string size in MT4 which blocks the string creation loop (my string is around 35.000 characters long). But then why does it work when I just Print() the string once within the onTimer() function?...
Any chance that the account related information is not accessible on weekends?
Or it is just my code which might be buggy..
Any ideas much appreciated!
int OnInit()
{
//---
EventSetTimer(2); // Set Second Timer as push intervall
context.setBlocky(false);
// Send data to PULL_PORT that consumer is listening to.
Print("Connecting MT4 Dashex Feeder to Dashboard on Port " + IntegerToString(PUSH_PORT) + "..");
pushSocket.connect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));
pushSocket.setSendHighWaterMark(100);
pushSocket.setLinger(0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
Print("Disconnecting MT4 Dashex Feeder on Port " + IntegerToString(PUSH_PORT) + "..");
pushSocket.disconnect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));
// Shutdown ZeroMQ Context
context.shutdown();
context.destroy(0);
EventKillTimer();
}
//---
//+------------------------------------------------------------------+
//| Expert timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
/*
1.) Account information
*/
string account_info = "";
account_info = account_info +
"account_info|" +
version + "|" +
DID + "|" +
IntegerToString(AccountNumber()) + "|" +
AccountInfoString(ACCOUNT_COMPANY) + "|" +
IntegerToString(AccountInfoInteger(ACCOUNT_LEVERAGE)) + "|" +
DoubleToString(AccountInfoDouble(ACCOUNT_BALANCE),2) + "|" +
DoubleToString(AccountInfoDouble(ACCOUNT_PROFIT),2) + "|" +
DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY),2) + "|" +
DoubleToString(AccountInfoDouble(ACCOUNT_MARGIN),2) + "|" +
DoubleToString(AccountInfoDouble(ACCOUNT_MARGIN_FREE),2) + "|" +
DoubleToString(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),2) + "|" +
AccountInfoString(ACCOUNT_CURRENCY) + "|" +
IntegerToString(IsDemo()) + "|" +
pushSocket.send(StringFormat("%s", account_info, true));
Print("Pushing Account Information To Dashex.Finance Dashboard For Account No. " + IntegerToString(AccountNumber()));
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
/*
2.) Pending and running trades
*/
int live_orders = OrdersTotal();
string live_trades = "";
for(int i=live_orders; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)==false) continue;
live_trades = live_trades +
"live_trades|" +
version + "|" +
DID + "|" +
IntegerToString(AccountNumber()) + "|" +
IntegerToString(OrderTicket()) + "|" +
TimeToString(OrderOpenTime(), TIME_DATE|TIME_SECONDS) + "|" +
TimeToString(OrderCloseTime(), TIME_DATE|TIME_SECONDS) + "|" +
IntegerToString(OrderType()) + "|" +
DoubleToString(OrderLots(),2) + "|" +
OrderSymbol() + "|" +
DoubleToString(OrderOpenPrice(),5) + "|" +
DoubleToString(OrderClosePrice(),5) + "|" +
DoubleToString(OrderStopLoss(),5) + "|" +
DoubleToString(OrderTakeProfit(),5) + "|" +
DoubleToString(OrderCommission(),2) + "|" +
DoubleToString(OrderSwap(),2) + "|" +
DoubleToString(OrderProfit(),2) + "|" +
"<" + OrderComment() + ">|";
}
pushSocket.send(StringFormat("%s", live_trades, true));
Print("Pushing Live Trades To Dashex.Finance Dashboard For Account No. " + IntegerToString(AccountNumber()));
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
/*
3.) History Trades
*/
int hstTotal = OrdersHistoryTotal();
string historical_trades = "";
Print(hstTotal);
for(int i=hstTotal; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;
historical_trades = historical_trades +
"historical_trades|" +
version + "|" +
DID + "|" +
IntegerToString(AccountNumber()) + "|" +
IntegerToString(OrderTicket()) + "|" +
TimeToString(OrderOpenTime(), TIME_DATE|TIME_SECONDS) + "|" +
TimeToString(OrderCloseTime(), TIME_DATE|TIME_SECONDS) + "|" +
IntegerToString(OrderType()) + "|" +
DoubleToString(OrderLots(),2) + "|" +
OrderSymbol() + "|" +
DoubleToString(OrderOpenPrice(),5) + "|" +
DoubleToString(OrderClosePrice(),5) + "|" +
DoubleToString(OrderStopLoss(),5) + "|" +
DoubleToString(OrderTakeProfit(),5) + "|" +
DoubleToString(OrderCommission(),2) + "|" +
DoubleToString(OrderSwap(),2) + "|" +
DoubleToString(OrderProfit(),2) + "|" +
"<" + OrderComment() + ">|";
}
pushSocket.send(StringFormat("%s", historical_trades, true));
Print("Pushing History Trades To Dashex.Finance Dashboard For Account No. " + IntegerToString(AccountNumber()));
Sleep(1);
}
MetaTrader 4 log.editor:
MetaTrader 4 console:
Q : Any chance that the account related information is not accessible on weekends?
This has an easy proof: do a Comment( aStringUnderTEST ); + check all account / Broker-related items.
Q : Maybe there is a maximum string size in MT4 which blocks the string creation loop...?
This is, except a case of an MT4-Build-X.Y.Z release-bug awaiting a BugFIX, very low probability hypothesis.
Yet it has an easy proof: do a loop of growing string-lengths and test until what size the processing keeps working. The indirectly proved size-limit will help you track the root cause, MT4 not being the SPoF here, is it?
Q : The MT4 Print function does some other stuff behind the scenes which enables the EA to send the string?
The MT4 Support ought be contacted if this is to get confirmed or denied. The software is licensed as an as-is product, so do not expect any rapid response or a rocket science to take place if you try to drill down to the bolts and nuts, inside their closed and sealed product.

Update statements taking too long

I have to update a column value (real time value) for all the rows in a table
I am using simple update statement in a loop
for (i = 1; i < totalRows; i++)
{
using (var command = new SqlCommand("update [dbo].[real] set rtv= " + engValues[i] + ",[DateTimeR] = '" + DateTime.Now + "' where [ID]='" + i+ "' ",connection))
{
command.ExecuteNonQuery();
}
}
It takes around 10 to 15 seconds to update the entire table which contains only about 700 records!
However i need to get it done every second?

strange behaviour when xcode evaluates variables at runtime

I get the error "expression was too complex to be solved in reasonable time" for the following code.
I saw other threads regarding the error with much complexer expressions then mine and it was stated, that the compiler is buggy.
Is this also true for the following simple code ?
Problem:
let value1:Int = 1;
let value2:Int = 3;
var sql="The Number 2 is in "
+ " between " + String(iFrom) + " and " + String(iTo) + ".";
but this works
let value1:Int = 1;
let value2:Int = 3;
var sql="The Number 2 is in "
+ " between " + String(iFrom) + " and " + String(iTo);
The difference is only the "." at the end of concatenation.
If its a bug:
The process SourceKitService is running at max and slowes everything down. Can compilation at runtime be disabled ?
One thing you could try that I've had some success with is to try specifically typing your sql variable.
var sql: String ="The Number 2 is in "
+ " between " + String(iFrom) + " and " + String(iTo) + ".";

If a user write "text" more than two times a error pop up

if (question == "What is the market?") {
post = "You can find it # " + store.market + ".";
response.sendfile("src/index.html");
}
I got a input text box that when a user writes Name? The computer says "My name is (me.name).
This code is in the server.js
But what i would like to do.. If a user write Name? like 3 times or so. it will come up a error that says You have asked me to many times or something in that way.
i tryd with for (var i=0;i < 3; ++i)
textbox = "Error"
But i cant get it to work. Im not the best programmer but im trying. Can it be done with a simple loop or do i need something more?
Try this:
var n=0, a=0, d=0;
if (question == "Name?") {
if(n<3)
answ = "I am " + me.name + ".";
else
answ = "You asked me that " + n + " times.";
response.sendfile("public/index.html");
n++;
} else if (question == "Age?") {
if(a<3)
answ = "I am " + me.age + " years old.";
else
answ = "You asked me that " + a + " times.";
response.sendfile("public/index.html");
a++;
} else if (question == "Do you have a name?") {
if(d<3)
answ = "My name is:" + me.Name";
else
answ = "You've asked me that " + d + " times.";
response.sendfile("public/index.html");
d++;
}
Basically you're keeping track in a,n, and d how many times they asked the question, and changing you're response accordingly.

Ace editor, inaccurate horizontal scroll, how to fix?

Using Ace editor, works OK, except: and start and end of line, if there is at least one line of the document that doesn't fit the screen, horizontal scroll is incomplete. For caret at start of line, once the view is h-scrolled, it won't scroll back fully when caret is at start of line, and thus the caret won't show. Annoying. And the same for caret at end of line (although it seems it doesn't scroll at all, rather than not flush right, so might be different bugs. Anybody know how to fix? And what versions are affected?
(Oh, right, forgot to mention: the gutter is enabled.)
(Edit II: using Google Chrome 18.0.1025.162)
(E#3: forgot to mention: using Shift+scroll wheel I can manually fix it, scrolling full left. (E4: ...and that's just a clue; not a solution. One should not have to do any extraneous manual mousing.))
(Edit#N: managed to hide the gutter: "editor_loaded.renderer.setShowGutter(false);". Problem persists.)
Well, as requested (by 'WarFox'), here's what I did to get around the scrolling issues in version 0.2.0, straight out of local repo. HTH, YMMV. No guarantees for modifying this or any version, of course. The code for this.scrollCursorIntoView is modified to be:
this.scrollCursorIntoView = function() {
var log = function (s) {
// console.log("### scrollCursorIntoView ###: " + s);
};
// log("(scrollCursorIntoView...)");
function loge(expr) {
var value = eval(expr);
log("" + expr + " => " + value);
}
// the editor is not visible
if (this.$size.scrollerHeight === 0)
return;
var pos = this.$cursorLayer.getPixelPosition();
var left = pos.left + this.$padding;
log("left = " + left);
var top = pos.top;
log("top = " + top);
if (this.scrollTop > top) {
this.scrollToY(top);
}
if (this.scrollTop + this.$size.scrollerHeight < top + this.lineHeight) {
this.scrollToY(top + this.lineHeight - this.$size.scrollerHeight);
}
var scrollLeft = this.scroller.scrollLeft;
var left_ = left - this.characterWidth;
log("(scrollLeft > left): " + scrollLeft + " > " + left);
log("(scrollLeft > left_): " + scrollLeft + " > " + left_);
if (scrollLeft > left_) {
this.scrollToX(left_);
} else {
log("NOT (scrollLeft > left): " + scrollLeft + " > " + left);
log("NOT (scrollLeft > left_): " + scrollLeft + " > " + left_);
}
loge("scrollLeft");
log("scrollLeft = " + scrollLeft);
log("this.$size.scrollerWidth = " + this.$size.scrollerWidth);
log("left = " + left);
log("this.characterWidth = " + this.characterWidth);
var right_side_scroll_yes = scrollLeft + this.$size.scrollerWidth < left + this.characterWidth;
if (right_side_scroll_yes) {
log("(right side scroll...)");
//loge("this.layerConfig.width");
if (left > this.layerConfig.width) {
log("right #1");
log("this.layerConfig.width = " + this.layerConfig.width);
this.$desiredScrollLeft = left + 2 * this.characterWidth;
this.scrollToX(this.$desiredScrollLeft);
} else {
log("right #2");
var tmp = Math.round(left + this.characterWidth - this.$size.scrollerWidth);
loge("tmp");
this.scrollToX(tmp);
}
} else {
log("NOT (right_side_scroll_yes): " + scrollLeft + " > " + left);
}
};
Obviously, the logging calls are not necessary for anything but debugging.

Resources