OpenWhisk - character sets? - openwhisk

I recently started using OpenWhisk and love it.
Everything seems to work real nice, except I have run into some issue which might be related to character sets / encoding.
E.g. when I use "Scandinavian characters", like æ, ø, å, I see this in the OpenWhisk Web Editor when calling an action / trigger with payload like:
{
"station": "Rådhuset",
"no2": 8.7,
"pm10": 6.5,
"pm25": 2.2,
"time": 1461348000,
"id": "Rådhuset-1461348000"
}
I get the following result / response payload:
{
"notify": "Station R??dhuset != R���dhuset"
}
The main function in the action called looks like this:
var payload = params.payload || params;
var station = 'Rådhuset';
if (station == payload.station) {
...
} else
return whisk.done({notify : 'Station ' + station + ' != ' + payload.station});
When running the action without these characters, e.g. "Kirkeveien", everything works fine.
Has anyone else run into similar situation?!

There is a known defect with non-ASCII characters. https://github.com/openwhisk/openwhisk/issues/252
A possible workaround is to encode the string (base64 encoding for example).

try encoding:
var payload = params.payload || params;
var station = 'Rådhuset';
if (station == payload.station) {
...
} else
return whisk.done({notify : 'Station ' + encodeURIComponent(station) + ' != ' + encodeURIComponent(payload.station)});

Related

Discord.js Announce Command Slice Issue with Args

iv made an announce command for my bot, i have it outputting the contents of the message to a RichEmbed.
I have it set in IF statement
If users mentioned first then it removes the mention and sends it to the channel and displays embed with contents (args.slice(2))
ELSE just send the embed with args.slice(1) removing the command.
My problem is that they either both slice(1) or both slice(2) , despite me having them set differently
if (!user);
const embed = new Discord.MessageEmbed()
.setTitle(`${(args.slice(1).join(" "))}`, 'https://i.gyazo.com/898806671312f3585209cf0fd69341006.jpg')
.setColor(0x320b52)
.setTimestamp()
.setFooter('Requested by ' + message.author.tag, 'https://i.gyazo.com/898806671312f3585209cf0fd69341006.jpg')
message.channel.send(embed);
}
else {
message.channel.send("<#" + user.id + ">")
const embed = new Discord.MessageEmbed()
.setTitle(`${(args.slice(2).join(" "))}`, 'https://i.gyazo.com/898806671312f358509cf0fd69341006.jpg')
.setColor(0x320b52)
.setTimestamp()
.setFooter('Requested by ' + message.author.tag, 'https://i.gyazo.com/898806671312f358509cf0fd69341006.jpg')
message.channel.send(embed);
}
Replace the ; in your first line with a {:
if (!user) {

socketio ip: why is ipfix variable / username not changing

io.on('connection', function(socket){
var ip = socket.request.connection.remoteAddress;
var ipfix = ip; // made a copy of ip cause thought it may be a constant
for (var g = 0; g < ipfix.length; g++)
{
if (ipfix[g] == 'f' || ipfix[g] == ':')
{
ipfix[g] = '';
console.log("changed to nothing"); // logs to console
}
if (ipfix[g] == '.')
{
ipfix[g] = '0';
console.log("changed to zero"); // logs to console
}
}
var username = 'User#' + ipfix[9] + ipfix[8] + ipfix[11] + ipfix[13];
console.log(username); // Logs, no change to dots / f / :
}
The purpose of this is to remove dots, colons, and 'f' from ip
To me this seems perfectly reasonable, but everytime i've run it, it prints out username with no change to the values seen in IP, even though IP was copied into ipfix and iterated through.
I found a fix, unlike C, javascript strings are immutable - a solution is to use ipfix = ip.split(''); <-- turns into an array of characters.

Bing custom search apis returning only limited results from one location and full result from different location

I am trying to use Bing Custom Search's API for documents from Cognitive Services. The strange thing is that when I run it from India, it gives me more than a thousand results, but when I run it from a US server, it returns only 25 (sometimes 50 results). Here is the sample code for that:
var totalCount = 0;
var filetypes = new List<string> { "pdf", "docx", "doc" };
foreach (var filetype in filetypes)
{
var searchTerm = "microsoft%20.net%20resume+filetype%3a" + filetype;
Console.WriteLine("Searching for : " + filetype);
for (var i = 0; i < 40; i++)
{
var nextCount = 0;
var url = "https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search?" +
"q=" + searchTerm +
"&customconfig=" + customConfigId +
"&count=25" + "&offset=" + ((i * 25) + nextCount);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
var httpResponseMessage = client.GetAsync(url).Result;
var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
BingCustomSearchResponse response =
JsonConvert.DeserializeObject<BingCustomSearchResponse>(responseContent);
if (response.webPages == null || response.webPages.value.Length <= 0)
{
Console.WriteLine("response.webPages is null ");
break;
}
foreach (var webPage in response.webPages.value)
{
Console.WriteLine("name: " + webPage.name);
Console.WriteLine("url: " + webPage.url);
Console.WriteLine("displayUrl: " + webPage.displayUrl);
Console.WriteLine("snippet: " + webPage.snippet);
Console.WriteLine("dateLastCrawled: " + webPage.dateLastCrawled);
Console.WriteLine();
}
totalCount = totalCount + response.webPages.value.Length;
}
}
}
The subscription key I am using is a trial key.
I got the reason of this behavior. Actually it had nothing to do with region/country/market.
After looking into the response i got this message.
"Rate limit is exceeded. Try again in 1 seconds"
It means for after each call in the loop i have to wait for 1 second to give next call. Now need to know is this limit for trial subscription or this is kept for all calls to prevent DDOS attack or something.
May be from India it was working because may one iteraction is already taking one or more second.
Two things you can try: 1) In searchTerm, no need to use %20 and %3a, just use punctuations as you type in Bing, e.g. var searchTerm = "microsoft.net resume filetype::"+filetype, and 2) Enforce market by appending mkt=en-in (for India) or en-us (for US) in the query. You can do this by appending +"&mkt=en-in" at the end of url.
I presume for custom search you have selected domains (for both en-in and en-us markets) that return thousands of results for this query.

grafana scripted influxdb

I am using grafana 4 and influxDB.
I need to show a graph of say CPU usage for a certain host by building the parameters in the URL like this
http://my_grafana:3000/dashboard/script/scripted.js?name=CPULoad&host=ussd1
i am trying to use scripted dashboards for this but i cannot figure out how to tell scripted.js where to look for the data of CPULoad.
can anyone give me some pointers?
regards,
Martin
Well I found out how it works, but I have to say it is wierd that it is not documented anywhere and it involves a little modification to the source code...
A little bit of context first
I have a influxdb database called "Nagios". Inside this database, I have several series. a show series in influxdb shows the following
> show series
key
---
nagios.CPULoad,hostname=cbba.storage,state=OK
nagios.CPULoad,hostname=ussd1,state=OK
nagios.CPULoad,hostname=ussd2,state=OK
nagios.CPULoad,hostname=ussd3,state=OK
nagios.CPULoad,hostname=ussd4,state=OK
The structure of the data in series CPULoad is like this
> select * from "nagios.CPULoad" limit 1
name: nagios.CPULoad
time hostname load1 load15 load5 state
---- -------- ----- ------ ----- -----
1487867813000000000 cbba.storage 0 0 0 OK
My URL to scripted.js is as follows:
http://10.72.6.220:3000/dashboard/script/scripted.js?name=CPULoad&field=load1&hostname=ussd3
name indicates the series in influxDB I want to graph
field indicates which field to use
hostname indicates the host to choose
The SQL I want grafana scripted.js to build is as follows
SELECT mean("load1") FROM "nagios.CPULoad" WHERE "hostname" = 'ussd3' AND $timeFilter GROUP BY time($interval) fill(null)
The code to build inside scripted.js involves modifying the "targets" parameter in dashboard.rows structure, and it turns out to be like this (i found this out after going through the code)
targets: [
{
"measurement": "nagios." + ARGS.name,
"metric": ARGS.name,
"tags": {
"hostname": {
operator: "=" ,
value: ARGS.hostname
}
},
"select": [[{
type: "field",
params: [ARGS.field]
}, {
type: "mean",
params: []
}]],
},
],
Now, I dont know why, but I had to modify the code in order for the key "hostname" to be taken into account. In function renderTagCondition which I copy here for convenience
a.prototype.renderTagCondition = function(a, b, c) {
var d = ""
, e = a.operator
, f = a.value;
return b > 0 && (d = (a.condition || "AND") + " "),
e || (e = /^\/.*\/$/.test(f) ? "=~" : "="),
"=~" !== e && "!~" !== e ? (c && (f = this.templateSrv.replace(f, this.scopedVars)),
">" !== e && "<" !== e && (f = "'" + f.replace(/\\/g, "\\\\") + "'")) : c && (f = this.templateSrv.replace(f, this.scopedVars, "regex")),
d + '"' + a.key + '" ' + e + " " + f
}
the returned value
d + '"' + a.key + '" ' + e + " " + f
seems to be wrong... It should be
d + '"' + b + '" ' + e + " " + f
since b carries "hostname"
After all this, calling the URL I mentioned at the beginning it all worked out pretty well
Adding to #mquevedob answer above,
Change the tags object like this
"tags":
[
{
key: "jobId",
operator: "=" ,
value: "340"
}
]
This should work fine in Grafana when using InfluxDB.

Xcode is telling me to put in expected ',' separator

xcode is telling me to put in expected ',' separator. Below is my code. even when i put in what Xcode tells me I still get an error. Does anyone know what I could be doing wrong?
} else {
if let p = placemarks?[0] {
var subThoroughfare:String = ""
if (p.throughfare != nil {
subThoroughfare = p.subThoroughfare
} // there are telling me to put this ',' here
self.addressLabel.text = "\(subThoroughfare) \(p.thoroughfare) \n \(p.subLocality) \n \(p.subAdministrativeArea) \n \(p.postalCode) \n \(p.country)"
} // xcode is telling me to put this ',' here
} // xcode is telling me to put this ',' here
})
Change this portion of your code and check if it still shows the error:
if (p.throughfare != nil) {
subThoroughfare = p.subThoroughfare
}
If it still show the error please show the if part also in your code it will help in understanding the full code as there look like extra bracket at end also

Resources