Plot a Ganglia graph using AngularJS - angularjs

I would like to plot a graph in AngularJS, using Highcharts, to be something like this:
This graph represents the load of a server in the last hour. So the datapoints given, contains a point and epoch time.
The data is received in a JSON format, [point,epochtime], as follows:
[
{
"ds_name": "a0",
"cluster_name": "",
"graph_type": "stack",
"host_name": "",
"metric_name": "1-min",
"color": "#BBBBBB",
"datapoints": [
[
0.58,
1604244900
],
[
0.59733333333,
1604244915
],
[
0.6,
1604244930
],
[
0.6,
1604244945
],
[
0.6,
1604244960
],
[
0.6,
1604244975
],
[
0.612,
1604244990
]
]
},
{
"ds_name": "a2",
"cluster_name": "",
"graph_type": "line",
"host_name": "",
"metric_name": "CPUs ",
"color": "#FF0000",
"datapoints": [
[
2,
1604244900
],
[
2,
1604244915
],
[
2,
1604244930
],
[
2,
1604244945
],
[
2,
1604244960
],
[
2,
1604244975
],
[
2,
1604244990
],
[
2,
1604245005
]
]
},
{
"ds_name": "a3",
"cluster_name": "",
"graph_type": "line",
"host_name": "",
"metric_name": "Procs",
"color": "#2030F4",
"datapoints": [
[
1,
1604244900
],
[
1,
1604244915
],
[
1,
1604244930
]
]
}
]
I posted here only part of the dataset, since it is too long, but I think you can understand the format.
How can I draw something similar using Highcharts?

The most important thing here is to properly parse the date. Since the names in the JSON are not the same as Highcharts requires you have to pass them manually to the right places as I did it in the demo below.
I also recommend using the keys property because the data should be declared as [x, y], in your case it is the other way around. You can change that by setting this property.
Notice that in the Highcharts there is no such series as stack, it is just column.
API: https://api.highcharts.com/highcharts/series.line.keys
Demo: https://jsfiddle.net/BlackLabel/246phxum/

So this is how I did it eventually, the most suitable types of charts are line, and area chart.
Highcharts.chart("container", {
chart: {
type: 'line'
},
xAxis: {
type: "datetime",
labels: {
formatter: function () {
return Highcharts.dateFormat('%H:%M:%S.%L', this.value);
}
}
},
series: [{
name: "A",
type: "area",
color: "#BB000B",
keys: ["y", "x"],
data: [
[0.58, 1604244900],
[0.59733333333, 1604244915],
[0.6, 1604244930],
[0.6, 1604244945],
[0.6, 1604244960],
[0.6, 1604244975],
[0.612, 1604244990]
]
},
{
name: "B",
type: "line",
color: "#FF00F0",
keys: ["y", "x"],
data: [
[2, 1604244900],
[2, 1604244915],
[2, 1604244930],
[2, 1604244945],
[2, 1604244960],
[2, 1604244975],
[2, 1604244990],
[2, 1604245005]
]
}, {
name: 'C',
keys: ['y', 'x'],
data: [
[1, 1604244900],
[1, 1604244915],
[1, 1604244930],
[1, 1604244945],
[1, 1604244960],
[1, 1604244975],
[1, 1604244990],
[1, 1604245005]
]
}
]
});

Related

How to Update Array dict Elements in mongodb based on another field

How can I update a value in a document based on applying functions to another field (which is in a different embedded document)?
With the sample data below, I want to
get the col field for the farm having id 12
multiply that by 0.025
add the current value of the statistic.crypt field
ensure the value is a double by converting it with $toDouble
store the result back into statistic.crypt
data:
{
"_id": {
"$oid": "6128c238c144326c57444227"
},
"statistic": {
"balance": 112570,
"diamond": 14,
"exp": 862.5,
"lvl": 76,
"mn_exp": 2.5,
"lvl_mn_exp": 15,
"coll_ms": 8047,
"all_exp": 67057.8,
"rating": 0,
"crypt": 0
},
"inventory": {
"farm": [{
"id": 12,
"col": 100,
"currency": "diamond",
"cost": 2,
"date": "2021-09-02 18:58:39"
}, {
"id": 14,
"col": 1,
"currency": "diamond",
"cost": 2,
"date": "2021-09-02 16:57:08"
}],
"items": []
},
...
}
My initial attempt is:
self.collection
.update_many({"inventory.farm.id": 12}, [{
"$set": {
"test": {
'$toDouble': {
"$sum": [
{'$multiply':["$inventory.farm.$[].col", 0.025]},
'$test'
]
}
} }
},])
This does not work as it applies to test rather than statistic.crypt, and I cannot figure out how to modify it to apply to statistic.crypt.
A field can be updated based on another in the following stages:
add a field containing the farm
set statistic.crypt to the result of the mathematical expression (applied to the newly embedded farm)
remove extra fields
In code:
self.collection.update_many({"inventory.farm.id": 12 }, [
{
$addFields: {
hh: {
$filter: {
input: "$inventory.farm",
as: "z",
cond: { $eq: ["$$z.id", 12] },
},
},
},
},
{
$set: {
"statistic.crypt": {
$toDouble: {
$sum: [
{
$multiply: [{ $first: "$hh.col" }, 0.025],
},
"statistic.crypt",
],
},
},
},
},
{
$project: {
id_pr: 1,
id_server: 1,
role: 1,
warns: 1,
id_clan: 1,
statistic: 1,
design: 1,
date: 1,
inventory: 1,
voice: 1,
},
},)

Rails how to extract data from nested JSON data

I am trying to parse out the values I need from this nested JSON data. I need to get from quotas is responders and I need to get from qualified is service_id, codes.
I tried first to get just the quotas but kept getting this error []': no implicit conversion of String into Integer
hash = JSON::parse(response.body)
hash.each do |data|
p data["quotas"]
end
Json data
{
"id": 14706,
"relationships" : [
{
"id": 538
}
]
"quotas": [
{
"id": 48894,
"name": "Test",
"responders": 6,
"qualified": [
{
"service_id": 12,
"codes": [
1,
2,
3,
6,
]
},
{
"service_id": 23,
"pre_codes": [
1,
2
]
}
]
}
]
}
I needed to convert your example into json. Then I could loop the quotas and output the values.
hash = JSON::parse(data.to_json)
hash['quotas'].each do |data|
p data["responders"]
data["qualified"].each do |responder|
p responder['service_id']
p responder['codes']
end
end
Hash in data variable (needed for the sample code to work):
require "json"
data = {
"id": 14706,
"relationships": [
{
"id": 538
}
],
"quotas": [
{
"id": 48894,
"name": "Test",
"responders": 6,
"qualified": [
{
"service_id": 12,
"codes": [
1,
2,
3,
6,
]
},
{
"service_id": 23,
"pre_codes": [
1,
2
]
}
]
}
]
}

Using $size and $addToSet to compare multiple arrays from the same cluster

Here is an example of the kind of documents I'm querying:
INPUT:
}
"_id": ObjectId("2786872873872"),
"data_shop" : {
"records_data" : [
{
"artist_name" : [
{
"val" : "BEYONCE",
},
],
"album_name" : [
{
"val" : "COUNTDOWN",
}
],
"qty" : [
0,
1,
2,
3
]
},
{
"artist_name" : [
{
"val" : "MUSE",
},
],
"album_name" : [
{
"val" : "THE RESISTANCE",
}
],
"qty" : [
0,
1,
2,
3,
3
]
}
],
},
}
}
"_id": ObjectId("2786872855555"),
"data_shop" : {
"records_data" : [
{
"artist_name" : [
{
"val" : "MAC MILLER",
},
],
"album_name" : [
{
"val" : "SWIMMING",
}
],
"qty" : [
0,
1,
2,
3,
]
},
{
"artist_name" : [
{
"val" : "DAFT PUNK",
},
],
"album_name" : [
{
"val" : "RANDOM ACCESS MEMORIES",
}
],
"qty" : [
0,
1,
2,
3,
4,
]
}
],
},
}
What I've done so far:
I'm trying to use both $size and $addtoSet in order to return the ObjectIds that have repeated numbers in the qty field. As you can see, only the first ObjectId has a repeated number (3) in the qty field.
This is what I've done so far:
db.mycollection.aggregate(
[
{$match: {"data_shop.records_data.qty.1": {$lte: 1}}},
{
$project: {Album_Cluster:"$data_shop.records_data"}
},
{
$unwind: "$Album_Cluster"
},
{
$project: {qty: "$Album_Cluster.qty"},
},
{
$project: {qty_size: {$size: "$qty"}, qty:1}
},
{ $match: {"qty_size.1": {$exists: false}, qty_size: {$gt: 1} }},
{$group:
{_id: "$_id",
totalSize: {$push: "$qty_size"},
realSize: {$addToSet: "$qty"},
}
},
],
{allowDiskUse: true}
)
And this is the result of the query above, in order to check the functionality of the query:
{"_id":ObjectId("2786872873872"), "totalSize": [4, 5], "realSize":[[0, 1, 2, 3]]}
{"_id":ObjectId("2786872855555"), "totalSize": [4, 5], "realSize":[[0, 1, 2, 3], [0, 1, 2, 3, 4]]}
I'm a little bit stuck at this part since I want to compare the total size of each array versus the real size of the array (by real size I mean non-repeating numbers)
OUTPUT
This is how the output of the query should look like:
{"_id":ObjectId("2786872873872"), "isRepeating": true}
{"_id":ObjectId("2786872855555"), "isRepeating": false}
EDIT:
I've improved my query in order to get this output schema:
db.mycollection.aggregate(
[
{$match: {"data_shop.records_data.qty.1": {$lte: 1}}},
{
$project: {Album_Cluster:"$data_shop.records_data"}
},
{
$unwind: "$Album_Cluster"
},
{
$project: {qty: "$Album_Cluster.qty"},
},
{
$project: {qty_size: {$size: "$qty"}, qty:1}
},
{$group:
{
_id: "$_id",
totalSize: {$addToSet: "$qty_size"},
realSize: {$addToSet: "$qty"},
}
},
{$unwind: "$realSize"},
{
$project:
{
totalSize:1,
real_count: {$size: "$realSize"}
}
},
{$unwind: "$totalSize"},
{
$group: {
_id: "$_id",
total_size: {$addToSet: "$totalSize"},
real_size: {$addToSet: "$real_count"}
}
},
],
{allowDiskUse: true}
)
And now I'm getting this as my output:
{"_id":ObjectId("2786872873872"), "total_size": [4, 5], "real_size":[4]}
{"_id":ObjectId("2786872855555"), "total_size": [4, 5], "real_size":[5, 4]}
Now my question is, does $in allow me to validate that [4, 5] is valid in [5, 4] so my output will be isRepeating = false?
Altought you can achieve this with a stack og unwind / group stages, it can be very expensive in resources consumption.
Unfortunaltely, the $addToSet oerator in available only in $group stage.
But... There's a trick, with the $setUnion operator.
$setUnion performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setUnion ignores the duplicate entries.
Knowing this, performing a $setUnion on your qty array, without any other array, will just... remove the duplicates.
Here's a implementation of this approach, using only 2 project stages
db.collection.aggregate([
{
$project: {
"data_shop.records_data": {
$map: {
input: "$data_shop.records_data",
as: "data",
in: {
qty_size: {
$size: "$$data.qty"
},
qty_size_unique: {
$size: {
$setUnion: [
"$$data.qty"
]
}
}
}
}
}
}
},
{
$project: {
isRepeating: {
$cond: {
if: {
$eq: [
"$data_shop.records_data.qty_size",
"$data_shop.records_data.qty_size_unique"
]
},
then: false,
else: true
}
}
}
}
])
It will return the expected output :
[
{
"_id": 1,
"isRepeating": true
},
{
"_id": 2,
"isRepeating": false
}
]

Flutter: Parse json arrays without name

I am getting json response from server as below.
[
[{
"ID": 1,
"Date": "11-09-2015",
"Balance": 1496693.00
}, {
"ID": 2,
"Date": "01-10-2015",
"Balance": 1496693.00
}],
[{
"ID": 1,
"Date": "03-09-2000",
"IntAmount": "003.00"
}],
[{
"EmployeeId": "000",
"DesignationName": "deg"
}],
[{
"LoanAmount": "00000.00",
"IntRate": "3.00",
"LoanNo": "56656"
}]
]
I can parse json array with name but in above json there are three arrays without name.
How to parse above json in three different arrays?
If you are positive that the data will always come in the stated format, then you can iterate through the result. See below for example:
main(List<String> args) {
// Define the array of data "object" like this
List<List<Map<String, dynamic>>> arrayOfData = [
[
{"ID": 1, "Date": "11-09-2015", "Balance": 1496693.00},
{"ID": 2, "Date": "01-10-2015", "Balance": 1496693.00}
],
[
{"ID": 1, "Date": "03-09-2000", "IntAmount": "003.00"}
],
[
{"EmployeeId": "000", "DesignationName": "deg"}
],
[
{"LoanAmount": "00000.00", "IntRate": "3.00", "LoanNo": "56656"}
]
];
/*
Iterate through the array of "objects" using forEach,
then, iterate through each resulting array using forEach
*/
arrayOfData.forEach((datasetArray) => datasetArray.forEach((dataset) => print(dataset)));
/*
============== RESULT ========
{ID: 1, Date: 11-09-2015, Balance: 1496693.0}
{ID: 2, Date: 01-10-2015, Balance: 1496693.0}
{ID: 1, Date: 03-09-2000, IntAmount: 003.00}
{EmployeeId: 000, DesignationName: deg}
{LoanAmount: 00000.00, IntRate: 3.00, LoanNo: 56656}
*/
}

XSD Setup for Arrays

I have a nested json array as follows:
When I try to post this I am getting the error messages:
{"RestFaultElement":{"summary":"Translation Failure.\nFailed to translate JSON to XML. java.io.IOException: Array start inside array\nThe incoming data does not conform to the NXSD schema. Please correct the problem.\n","code":null,"detail":null}}
"Options": {
"Co-ordinates": {
"X": "594752",
"Y": "714430"
},
"Language": "English",
"PageSize": "A4",
"Scale": "1,000",
"Orientation": "Landscape",
"GeometryData": {
"type": "polygon",
"rings": [
[
[
716102.6700605,
733105.7692149
],
[
716431.41550966,
733105.7692149
],
[
716431.41550966,
732878.22709315
],
[
716102.6700605,
732878.22709315
],
[
716102.6700605,
733105.7692149
]
]
],
"_ring": 0,
"spatialReference": {
"wkid": 2157,
"latestWkid": 2157
},
"cache": {
"_extent": {
"xmin": 717192,
"ymin": 733058.86,
"xmax": 717424,
"ymax": 733230.86,
"spatialReference": {
"wkid": 2157,
"latestWkid": 2157
}
},
"_partwise": "PARTWISE",
"_centroid": {
"type": "point",
"x": 717308.00000013,
"y": 733144.86000007,
"spatialReference": {
"wkid": 2157,
"latestWkid": 2157
}
}
}
}
The problem part of this seems to be with the "rings" array.
I believe that 1 of 2 things is happening:
1. The JSON syntax is wrong.
2. The xsd file is set up wrong.
The XSD segments which should accept this element is defined as:
<xsd:element type="xsd:decimal" name="rings"/>
Would this set up allow for an array like so to be passed to it?
Thank you.
You are missing brackets at start end end of your string. Try:
{"rings": [
[
[
716102.6700605,
733105.7692149
],
[
716431.41550966,
733105.7692149
],
[
716431.41550966,
732878.22709315
],
[
716102.6700605,
732878.22709315
],
[
716102.6700605,
733105.7692149
]
]
]}

Resources