Extracting Array inside a Property in Azure Stream Analytics - arrays

I have had no luck so far extracting certain values in a wide format out of a JSON string via a stream analytics job.
The JSON has the following format:
{"devicename":"demo","msgtime":"2018-04-13T11:00:00.0000000Z",
"payload":[{"Sensor":"one","Value":1.817,"Unit":"W"},
{"Sensor":"two","Value":0.481,"Unit":"W"},
{"Sensor":"three","Value":0.153,"Unit":"W"}]}}
I am trying to get it in the following format:
name one two three
demo 1.817 0.481 0.153
… … … …
I tried getting the values with "Cross APPLY GetPropertyValues(input)", but I can't get them in a wide format.

try code like below
SELECT
localInput.devicename,
udf.getValue('one', localInput.payload) as One,
udf.getValue('two', localInput.payload) as Two,
udf.getValue('three', localInput.payload) as Three
FROM localInput;
function main(identifier, arr) {
var result = null;
if (Object.prototype.toString.call(arr) == "[object Array]") {
for (i = 0; i < arr.length; i++) {
if (arr[i].type == identifier) {
result = arr[i].value;
}
}
}
return result;
}

Related

Create a XLS file dynamically with XLSX in Angular

I'm creating a pickList where there's a list of attributes that the user can choose to create its own personal report. But I'm having quite some trouble on creating this XLS, because of two things.
First my XLSX.utils.json_to_sheet is ignoring blank fields (""), and I can't have that because it will desorganize the whole report.
Second: because its a dynamic xls, I can't know how many columns there will be created...
Here's what I've done so far, regarding the xls creation part:
exportTable() {
this.createObjectWithColumnsSelected(); //ignore this, i'm only creating a reference object based on the user columns choices
this.manipulateExportableData(); //manipulating the whole table data, to be formated accordly the reference object
const worksheet = XLSX.utils.json_to_sheet(this.customersTable);
this.changeHeaderWorksheet(worksheet);
}
converte(): string[] {
const alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
return alph.split('');
}
changeHeaderWorksheet(worksheet: WorkSheet): void {
const alphabet = this.converte();
for (let i = 0; i < this.cols.length; i++) {
if (i >= 26){
console.log(this.cols[i].header);
worksheet[`A${alphabet[i]}1`] = this.cols[i].header;// I think this is quite wrong
}else{
console.log(this.cols[i].header);
worksheet[`${alphabet[i]}1`] = this.cols[i].header; //I think this is quite wrong
}
}
}

How to seach emoji with proper text via Entity Framework Core

Here is my code:
var emoji = "⭐";
var query = myContext.Products.Where(x => x.Name.Contains(emoji));
var queryString = query.ToQueryString();
var list = query.ToList();
Query returns all table records. If I replace contains to equal works great, but I have to search something like this:
"this is my emoji ⭐"
This is the SQL query:
DECLARE #__emoji_0 nvarchar(4000) = N'⭐'
SELECT [p].[Id], [p].[Name], [p].[Quantity]
FROM [Products] AS [p]
WHERE (#__emoji_0 LIKE N'') OR (CHARINDEX(#__emoji_0, [p].[Name]) > 0)
Is any way to do this in EF Core or raw SQL?
Your main issue is the fact that emojis and strings are represented differently.
Before you can search the emojis you will need to decide how are you gonna unify them both in search query and db.
First of all emojis are a pair of chars.What does that mean? Here as a quote from the Microsoft docs:
"🐂".Length = 2
s[0] = '�' ('\ud83d')
s[1] = '�' ('\udc02')
These examples show that the value of string.Length, which indicates the number of char instances, doesn't necessarily indicate the number of displayed characters. A single char instance by itself doesn't necessarily represent a character.
The char pairs that map to a single character are called surrogate pairs. To understand how they work, you need to understand Unicode and UTF-16 encoding.
Having this in mind I would go as follows:
Define a method which will convert emojis to a UTF16 string[] which will keep the two surrogate chars representation.
internal static string[] EmojiToUtf16Pair(string emoji)
{
string[] arr = new string[2];
for (int i = 0; i < emoji.Length; i++)
{
arr[i] = emoji[i].ToString();
}
return arr;
}
This could be use when you persist emojis in DB. Depending on how you decide to persist the emojis in DB some modification could be done for that method e.g. to return concatenated string or something like that.
I am not sure when, but for some reason you could use another method to do the reverse operation -> UTF16 to Emoji
internal static string UTF16PairToEmoji(string[] codes)
{
var test = string.Empty;
foreach (var i in codes)
{
test += i;
}
var result = test.ToString();
return result;
}
Here is all the code example:
class Program
{
static void Main()
{
var str = "🚴";
var utf16 = string.Join("",EmojiToUtf16Pair(str));
Console.WriteLine(utf16);
var testEmpoji = UTF16PairToEmoji(EmojiToUtf16Pair(str));
Console.WriteLine(testEmpoji);
}
internal static string[] EmojiToUtf16Pair(string emoji)
{
string[] arr = new string[2];
for (int i = 0; i < emoji.Length; i++)
{
arr[i] = emoji[i].ToString();
}
return arr;
}
internal static string UTF16PairToEmoji(string[] codes)
{
var test = string.Empty;
foreach (var i in codes)
{
test += i;
}
var result = test.ToString();
return result;
}
}
emoji ef-core db-query
You have to use like command
SELECT * FROM emoticon where emoji_utf like '👨🏫';
with EF in .net core
Emoticon emoticon=db_context.Emoticons.Where(a=>EF.Functions.Like(a.EmojiUtf,"%"+item.emojiString+"%" ) ).FirstOrDefault();

Is there anyway to dynamically access an object from a Utility Class in EXTJS?

My code structure is as follows->
MyApp-> app -> model, view, controller, store, utils
I have the following utility class created for my validation errors:
Ext.define('MyApp.shared.utils.GlobalStrings', {
AmountLimitViolation: "Amount Limit Violated",
DurationLimitViolation: "Duration Limit Violated"
});
I am trying to get the strings dynamically in the following way, but they keep coming up undefined:
Ext.define('MyApp.controller.ViewController', {
extend: 'Ext.grid.Panel',
violationCheck: function (tradelimit, tradelimitViolations){
let me = this,
tradeViolationMessage = "";
if(tradelimit > 0){
for(i = 0; i < tradelimitViolations.length; i++){
violationString = tradelimitViolations[i] + "Violation";
tradeViolationMessage += GlobalStrings.violationString;
}
}
}
});
tradelimitViolations is a string that would contain something like: ("AmountLimit", "DurationLimit")
So is there any method I could use to get AmountLimitViolation and DurationLimitViolation dynamically without having to resort to using a long list of conditional statements? Because I'm just showing two of the many violation errors I have.
The problem is how you are "indexing" into GlobalStrings. The dot operator uses the text to the right as the key, but you have a variable holding your key.
Try this:
for(i = 0; i < tradelimitViolations.length; i++){
violationString = tradelimitViolations[i] + "Violation";
tradeViolationMessage += GlobalStrings[violationString];
}

Flutter Save JSON Array to SharedPreferences

How to save JSON array into SharedPreferences?
I've tried some code like this below, but i got some error :
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type
'Text' is not a subtype of type 'String'
for (var x = 0; x < dynamicwidget.length - 1; x++) {
_listOrder={
"id_product": dataJSON[x]["product_id"],
"order_count": dynamicwidget[x].controller.text,
};
}
String json = jsonEncode(_listOrder);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(categoryname, json);
I would recommend you to use something like this (local storage implementation) which is made for saving and loading JSON.
From the code snippet itself i can't tell you exactly what is causing this exception but basically everywhere a String is beeing expected but you provided an actual Text widget. It could be the categoryname variable. What kind of controller do you get from your dynamicWidget instance? Are you sure controller.text returns a String?
Another note: you are re-assigning _listOrder with every for loop instead of adding information. You have to use an empty array which will be filled with every loop.
Thanks all, for contributing this question.
I've solved this question and makes some change.
_listOrder.clear();
for (var x = 0; x < dynamicwidget.length; x++) {
int jml = int.parse(dynamicwidget[x].controller.text == ""? "0": dynamicwidget[x].controller.text);
if (jml > 0) {
_listOrder.add({
"id_product": dataJSON[x]["product_id"],
"order_count": dynamicwidget[x].controller.text,
});
}
}
if (_listOrder.length > 0) {
SharedPreferences prefs = await SharedPreferences.getInstance();
Map<String, dynamic> _collOrder = Map<String, dynamic>();
String sessjson = prefs.getString("orderlist");
if (sessjson != null) {
_collOrder = json.decode(sessjson);
_collOrder.remove(categoryname);
}
_collOrder[categoryname] = _listOrder;
prefs.setString("orderlist", json.encode(_collOrder));
}

randdusing/ng-cordova-bluetoothle, parsing ble advertisement ionic

using the randdusing bluetoothle plugin for ionic app, need to read the advertisement.
The ble scan returns with Start Scan Success :
{"address":"14::30:c6:60:e8;9f","name":null,"rssi":-50,"advertisement":"AgEGG/9SVgIADSw5YTNlMTQAAAJlAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;=","status":"scanResult"}
query: need to decipher this json data and convert this advertisement data into array containing hex values of advertisement data? The advertisement data seems to be base64 encoded. Please advice.
I made for this purpose a little helper function as shown below. The key is the $cordovaBluetoothLE.encodedStringToBytes as you can see in docs https://github.com/randdusing/ng-cordova-bluetoothle.
var encodedToByteString = function encodedToByteString(input) {
var val = $cordovaBluetoothLE.encodedStringToBytes(input);
var byteStr = "";
for (var i = 0; i < val.length; i++) {
var byte = val[i].toString(16);
if (byte.length == 1) byte = "0" + byte;
byteStr += byte;
}
return byteStr;
};
The same goes for the opposite operation - that is sending data. You first need to get your hex-string into an array of bytes and then encode it via $cordovaBluetoothLE.bytesToEncodedString(value).

Resources