Filter data by using nodejs - javascript

from one API I am getting these type of response. I need to get latitude, longitude of single entity. How can I filter this data by using javascript (node js).
header {
gtfs_realtime_version: "1.0"
incrementality: FULL_DATASET
timestamp: 1501132018
}
entity {
id: "1"
vehicle {
trip {
trip_id: "141.180717.42.1145"
schedule_relationship: SCHEDULED
route_id: "4T.C.141"
}
position {
latitude: -29.77179
longitude: 151.11717
bearing: 90.00001
}
timestamp: 1501131987
congestion_level: UNKNOWN_CONGESTION_LEVEL
stop_id: "23604"
vehicle {
id: "141"
label: "11:45am (141) Grafton City - Moree Town"
}
}
}
entity {
id: "2"
vehicle {
trip {
trip_id: "511.160717.90.1416"
schedule_relationship: SCHEDULED
route_id: "4T.C.511"
}
position {
latitude: -32.09544
longitude: 148.06787
bearing: 313.0
}
timestamp: 1501131974
congestion_level: UNKNOWN_CONGESTION_LEVEL
stop_id: "28211"
vehicle {
id: "511"
label: "02:16pm (511) Dubbo - Bourke"
}
}
}
All these data are of transportation API. If it would have been in JSON then we can get it by using foreach and by using its index.

Use javascript map function and extract only those fields which you want.

Related

Is there any way to aggregate a model based on date range (i.e monthly aggregation) on dynamo db with graphql?

I am using amplify cli having dynamo database along with GraphQL API. I have a sales model which keep sales record i.e sale amount along with the date. I want to aggregate the sales amount for each month in a year to show the sales statistics on a graph which show total sales made in each month over the year. Currently I am using GraphQL queries to query/search data from the backend.
Here is the model that I am using.
type Sales #model #searchable #auth(rules: [{ allow: private }]) {
id: ID!
userID: String! #index(name: "byAdded", sortKeyFields: ["createdAt"])
createdAt: AWSDateTime!
saleAmount: Float
}
Here is the example usage of graphQL query that I am currently using to get a aggregated amount for the whole year.
await API.graphql(
graphqlOperation(searchSales, {
filter: {
and: [
{ userID: { eq: userId } },
{ createdAt: { range:["2022-01-01", "2022-12-31"] } },
],
},
aggregates: {
type: 'sum',
field: 'saleAmount',
name: 'totalSales',
},
}),
);
Currenty Query result:
{"aggregateItems": [{"name": "totalSales", "result": {"value": 4608} }], "nextToken": "WyIwZTUyNmY2JkMjUiXQ==", "total": 7}
I want to have a result like following:
aggregations : { jan: 234, feb: 343, march: 3465, ...... }

Openweathermap API call responding with wrong json

I am using the hourly forecast API call from Openweathe map to build a weather app. I can correctly fetch the URL for the call but the structure of the JSON that shows seems not to match the one provided as an example. I can't access data.list or either data.sys or data.city.name. While I can access data.name.
Here is the example JSON
{
cod: "200",
message: 0.0208,
cnt: 96,
list: [
{
dt: 1553709600,
main: {
temp: 286.44,
temp_min: 286.258,
temp_max: 286.44,
pressure: 1015.82,
sea_level: 1015.82,
grnd_level: 1002.193,
humidity: 100,
temp_kf: 0.18
},
weather: [
{
id: 500,
main: "Rain",
description: "light rain",
icon: "10d"
}
],
clouds: {
all: 86
},
wind: {
speed: 5.51,
deg: 202.816
},
rain: {
1h: 0.812
},
sys: {
pod: "d"
},
dt_txt: "2019-03-27 18:00:00"
},
...
],
city: {
id: 420006353,
name: "Mountain View",
coord: {
lat: 37.3894,
lon: -122.0833
},
country: "US"
}
}
and here is the code I use to manage the call
function call(city,country,zip) {
let countryCode = list.getCode(country);
let API;
if (city!="" && country!="") {
API = 'http://api.openweathermap.org/data/2.5/weather?q='+city.toLowerCase()+','+countryCode.toLowerCase()+'&APPID=1a9b84b61d1a8d6fdbb52fa2800ef894';
}
else if(country!="" && zip!=""){
API ='http://api.openweathermap.org/data/2.5/forecast/hourly?zip='+zip.toString()+','+countryCode.toLowerCase()+'&APPID=1a9b84b61d1a8d6fdbb52fa2800ef894';
}
else{
alert("Please type your city or Zip code");
}
return API;
}
function getPlace() {
let city = cityInput.value;
let country = countryInput.value;
let zip = zipInput.value;
let APICall=call(city,country,zip);
fetch(APICall).then(response=>{
return response.json();
}).then(data=>{
let cityName= data.name;
settler(cityName);
})
}
I should be able to access data.list but
alert(data.list.lenght) gives me an error saying that can not define length of undefined. Same for alert(data.city.name) that says that can not access name property of undefined.
The weather API docs correctly show the data you should expect for weather query:
https://openweathermap.org/current
Your hourly query should work if you hit the correct endpoint (pro) which your (exposed; you should fix that) API key is not valid for (AFAICT).
The pricing page indicates that hourly data is not available for free.

Adding object in array of object in javascript

I have an Array object in my code:
cities =[{city_name:'NY',X:'1',Y:'2'}]
I'm trying to get new city info using ajax and add to cities array.
I get the response from ajax and make exact object like above, but when I try to push new object into array nothing happen.
My full code:
<script>
cities = [{
svgPath: targetSVG,
title: "Romania",
latitude: 44.4333000183,
longitude: 26.1000003815,
}
, {
svgPath: targetSVG,
title: "China",
latitude: 39.8897018433,
longitude: 115.275001526,
}
, {
svgPath: targetSVG,
title: "Taiwan",
latitude: 24.8047008514,
longitude: 120.9713974,
}
];
function push_test() {
temp = {
arc: -0.85,
alpha: 0.9,
latitudes: 1,
longitudes: 2
};
cities.push(temp);
}
</script>
<button onclick='push_test()'>test</button>

CouchDB sorting by linked doc property

I have the following data:
payment1: {
id: "payment1",
categoryId: "category1"
}
payment2: {
id: "payment2",
categoryId: "category2"
}
payment3: {
id: "payment3",
categoryId: "category2"
}
category1: {
id: "category1",
name: "Food"
}
category2: {
id: "category2",
name: "Leisure"
}
What I need is a view for sorting the payment data by category NAME. Is it possible in CouchDB?
Thanks in advance!
Ivan
What is possible is a bit different from what you want.
Here is the map:
function (o) {
if (o.categoryId) {
emit(o.categoryId);
} else {
emit(o.id);
}
}
You'll call it with /mydb/_design/mydesign/_view/myview?include_docs=true and get:
{"rows":[
{"key":"category1", "doc":{"id":"payment1","categoryId":"category1"},
{"key":"category1", "doc":{"id":"category1","name":"Food"},
{"key":"category2", "doc":{"id":"payment2","categoryId":"category2"},
{"key":"category2", "doc":{"id":"payment3","categoryId":"category2"},
{"key":"category2", "doc":{"id":"category2","name":"Leisure"}
]}
In other words, payments are grouped by category, they are also joined with category's name. However, they are sorted according category ID but not category's name.
What you want would require two different sorts (i.e. "chained map reduce").

MongoDB aggregate() .$unwind

I have some data that looks like this (not real data):
{
_id:'cust04',
name:'Diarmuid Rellis',
address:'Elysium, Passage East',
county:'Waterford',
phone:'051-345786',
email:'dreil#drarch.com',
quotations:[
{
_id:'quot03',
supplier_ref:'A2006',
date_received: new Date('2013-05-12T00:00:00'),
date_returned: new Date('2013-05-15T00:00:00'),
supplier_price:35000.00,
customer_price:35000.00,
orders:[
{
_id:'ord03',
order_date: new Date('2013-05-20T00:00:00'),
del_date: new Date('2013-08-12T00:00:00'),
del_address:'Elysium, Passage East, Co. Waterford',
status:'BALPAID'
}
]
},
{
_id:'quot04',
supplier_ref:'A2007',
date_received: new Date('2013-08-10T00:00:00'),
date_returned: new Date('2013-08-12T00:00:00'),
supplier_price:29600.00,
customer_price:29600.00,
orders:[
{
_id:'ord04',
order_date: new Date('2014-03-20T00:00:00'),
del_date: new Date('2014-05-12T00:00:00'),
del_address:'Elysium, Passage East, Co. Waterford',
status:'INPROD'
}
]
}
]
}
I am trying to unwind the quotations and orders arrays, and get a projection of all orders in production which include the customer name, supplier_ref and order date for each.
Here is my query:
db.customers.aggregate([
{ $unwind: "$quotations" },
{ $unwind: "$quotations.orders" },
{ $match: { 'quotations.orders.status': 'INPROD' } },
{
$project: {
name: 1,
supplier_ref: "$quotations.supplier_ref",
order_id: "$quotations.orders._id",
order_date: "$quotations.orders.order_date"
}
},
{
$group: {
_id: "$order_id"
}
}
], function (err, results) {
console.log(results);
})
The query runs successfully, but just gives the order ids, not any of the other fields required. What am I missing ?
EDIT
I am hoping for a result like:
"result": [
{
"_id" : "orderid01",
"name" : "Joe Bloggs",
"supplier_ref" : "A1234",
"date_ordered" : "2012-04-14"
},
{
"_id" : "orderid02",
"name" : "Joe Bloggs",
"supplier_ref" : "A1235",
"date_ordered" : "2012-04-16"
}
]
When I add an extra field to my 'group' function, like so:
$group: {
_id: "$order_id",
supplier_ref: "$supplier_ref"
}
I get the error: "the group aggregate field 'supplier_ref' must be defined as an expression inside an object". Do I have to associate it with the result object in some way ?
Removing the group function altogether produced the results I wanted.

Categories