Cypher aggregation and collect - neo4j

Given the following result in my cypher query, where people are a collection:
[
{
"people": [
{
"id": 24749,
"matches": 1
},
{
"id": 26026,
"matches": 1
},
{
"id": 26223,
"matches": 1
},
{
"id": 25121,
"matches": 1
},
{
"id": 24632,
"matches": 1
},
{
"id": 25708,
"matches": 1
},
{
"id": 25182,
"matches": 1
},
{
"id": 24826,
"matches": 1
},
{
"id": 26186,
"matches": 1
},
{
"id": 27001,
"matches": 1
},
{
"id": 24243,
"matches": 1
},
{
"id": 27255,
"matches": 1
},
{
"id": 27145,
"matches": 1
},
{
"id": 24126,
"matches": 1
},
{
"id": 27463,
"matches": 1
},
{
"id": 24069,
"matches": 1
},
{
"id": 25210,
"matches": 1
},
{
"id": 24994,
"matches": 1
},
{
"id": 27331,
"matches": 1
},
{
"id": 25793,
"matches": 1
},
{
"id": 27312,
"matches": 1
},
{
"id": 26206,
"matches": 1
},
{
"id": 24252,
"matches": 1
},
{
"id": 24714,
"matches": 2
},
{
"id": 24612,
"matches": 1
},
{
"id": 26964,
"matches": 1
},
{
"id": 27101,
"matches": 1
},
{
"id": 26730,
"matches": 1
},
{
"id": 27211,
"matches": 1
},
{
"id": 24783,
"matches": 2
},
{
"id": 25336,
"matches": 1
},
{
"id": 24128,
"matches": 1
},
{
"id": 26186,
"matches": 1
},
{
"id": 25125,
"matches": 2
},
{
"id": 24069,
"matches": 3
},
{
"id": 24607,
"matches": 1
},
{
"id": 27055,
"matches": 1
},
{
"id": 25336,
"matches": 3
},
{
"id": 24128,
"matches": 2
},
{
"id": 26716,
"matches": 1
},
{
"id": 27331,
"matches": 1
},
{
"id": 24069,
"matches": 1
}
]
}
]
How can I (with cypher) iterate the people collection and find the ones with the same "id", sum the "matches" items together and then add a new item called "duplicates" or similar.
Example result I'm trying to get:
[
{
"people": [
{
"id": 24069,
"matches": 5, // all the "matches" of the duplicate 24069's added together
"duplicates": 3 // how may times the id 24069 was found in the collection called people
},
// etc...
]
}
]

The query:
match (p:people) return distinct(p.id),sum(p.matches)
should probably give you what you need

This is kinda complicated, as the simplest way is to tear your collection apart and reassemble it. It's a lot of steps, but here is the Cypher to do that as well as comments on what each step is doing.
// initial matching (whatever you match on to get your collection)
MATCH (n:People) WITH collect(n) as people
// Tear people collection apart for possessing
UNWIND people as p
// Reduce on id
WITH COLLECT(DISTINCT p.id) as id, p.matches as m
// For each id, sum and count the matches
WITH {id:id, matches:SUM(m), duplicates:COUNT(m)} as people
// Recollect rows into collection
RETURN COLLECT(people) as people

Related

How to add a temporary price change of InAppPurchase with /v1/inAppPurchasePriceSchedules

Like Steve with his question, i have problem with this new API :
Unknown Error trying to add Pricing to a In App Purchase String
I followed Guide from Apple :
https://developer.apple.com/documentation/appstoreconnectapi/app_store/in-app_purchase/managing_in-app_purchases
I can change price from inAppPurchase but now i'm stuck with "Promotional Price".
For example, my inApp Puchase have a product tiers 8 at 9.99€ and from 1 November 2022 to 30 November i want a different price (product tiers 4 at 4.99€). The first of December, price will go back to 9.99€.
When testing inAppPurchasePriceSchedules POST, i have 409 error (Conflict) or 500 error.
Here one of payload i test :
{
"data": {
"relationships": {
"inAppPurchase": {
"data": {
"id": 1592386688,
"type": "inAppPurchases"
}
},
"manualPrices": {
"data": [{
"type": "inAppPurchasePrices",
"id": "${prices-id}"
}, {
"type": "inAppPurchasePrices",
"id": "${prices-id-1}"
}
]
}
},
"type": "inAppPurchasePriceSchedules"
},
"included": [{
"attributes": {
"startDate": "2022-11-01"
},
"id": "${prices-id}",
"relationships": {
"inAppPurchasePricePoint": {
"data": {
"id": "eyJzIjoiMTU5MjM4NjY4OCIsInQiOiJGUkEiLCJwIjoiMyJ9", //Tiers 5
"type": "inAppPurchasePricePoints"
}
},
"inAppPurchaseV2": {
"data": {
"id": "1592386688",
"type": "inAppPurchases"
}
}
}
}, {
"attributes": {
"startDate": "2022-12-01"
},
"id": "${prices-id-1}",
"relationships": {
"inAppPurchasePricePoint": {
"data": {
"id": "eyJzIjoiMTU5MjM4NjY4OCIsInQiOiJGUkEiLCJwIjoiNSJ9", //Tiers 10
"type": "inAppPurchasePricePoints"
}
},
"inAppPurchaseV2": {
"data": {
"id": "1592386688",
"type": "inAppPurchases"
}
}
},
"type": "inAppPurchasePrices"
}
]
}
I tried with 3 manualPrices, with "{$price1}" or "{$price2}" as id.
I tried with id from current price...
I'm missing something, I must be close by...
Update
after using Fiddler on appstoreconnect.apple.com and adding / removing promotion i was able to create this payload :
{
"data": {
"type": "inAppPurchasePriceSchedules",
"relationships": {
"inAppPurchase": {
"data": {
"type": "inAppPurchases",
"id": "1592386688"
}
},
"manualPrices": {
"data": [{
"type": "inAppPurchasePrices",
"id": "${price1}"
}, {
"type": "inAppPurchasePrices",
"id": "${price2}"
}, {
"type": "inAppPurchasePrices",
"id": "${price3}"
}
]
}
}
},
"included": [{
"type": "inAppPurchasePrices",
"id": "${price1}",
"attributes": {
"startDate": null
},
"relationships": {
"inAppPurchaseV2": {
"data": {
"type": "inAppPurchases",
"id": "1592386688"
}
},
"inAppPurchasePricePoint": {
"data": {
"type": "inAppPurchasePricePoints",
"id": "eyJzIjoiMTU5MjM4NjY4OCIsInQiOiJGUkEiLCJwIjoiNSJ9"
}
}
}
}, {
"type": "inAppPurchasePrices",
"id": "${price2}",
"attributes": {
"startDate": "2022-11-01"
},
"relationships": {
"inAppPurchaseV2": {
"data": {
"type": "inAppPurchases",
"id": "1592386688"
}
},
"inAppPurchasePricePoint": {
"data": {
"type": "inAppPurchasePricePoints",
"id": "eyJzIjoiMTU5MjM4NjY4OCIsInQiOiJGUkEiLCJwIjoiMyJ9"
}
}
}
}, {
"type": "inAppPurchasePrices",
"id": "${price3}",
"attributes": {
"startDate": "2022-12-01"
},
"relationships": {
"inAppPurchaseV2": {
"data": {
"type": "inAppPurchases",
"id": "1592386688"
}
},
"inAppPurchasePricePoint": {
"data": {
"type": "inAppPurchasePricePoints",
"id": "eyJzIjoiMTU5MjM4NjY4OCIsInQiOiJGUkEiLCJwIjoiNSJ9"
}
}
}
}
]
}
This one worked !
Thanks
Guldil

Twilio IVR Speech Recognition

I'm new to developing IVR with twilio studio so I started with the basic template and even that's not working.
This is the log:
LOG
Split Based On...
DETAIL
Input evaluated to 'Sales.' from '{{widgets.gather_input.SpeechResult}}'
Transitioning to 'say_play_1' because 'Sales.' did not match any expression
The split is set to "Equal to" sales which then connects the call to a number. It's obviously recognizing the correct speech input but still not working. Any ideas?
{
"description": "IVR",
"states": [
{
"name": "Trigger",
"type": "trigger",
"transitions": [
{
"event": "incomingMessage"
},
{
"next": "gather_input",
"event": "incomingCall"
},
{
"event": "incomingRequest"
}
],
"properties": {
"offset": {
"x": 250,
"y": 50
}
}
},
{
"name": "gather_input",
"type": "gather-input-on-call",
"transitions": [
{
"next": "split_key_press",
"event": "keypress"
},
{
"next": "split_speech_result",
"event": "speech"
},
{
"event": "timeout"
}
],
"properties": {
"voice": "alice",
"speech_timeout": "auto",
"offset": {
"x": 290,
"y": 250
},
"loop": 1,
"hints": "support,sales",
"finish_on_key": "",
"say": "Hello, how can we direct your call? Press 1 for sales, or say sales. To reach support, press 2 or say support.",
"language": "en",
"stop_gather": false,
"gather_language": "en-US",
"profanity_filter": "false",
"timeout": 5
}
},
{
"name": "split_key_press",
"type": "split-based-on",
"transitions": [
{
"event": "noMatch"
},
{
"next": "connect_call_to_sales",
"event": "match",
"conditions": [
{
"friendly_name": "1",
"arguments": [
"{{widgets.gather_input.Digits}}"
],
"type": "equal_to",
"value": "1"
}
]
},
{
"next": "connect_call_to_support",
"event": "match",
"conditions": [
{
"friendly_name": "2",
"arguments": [
"{{widgets.gather_input.Digits}}"
],
"type": "equal_to",
"value": "2"
}
]
}
],
"properties": {
"input": "{{widgets.gather_input.Digits}}",
"offset": {
"x": 100,
"y": 510
}
}
},
{
"name": "split_speech_result",
"type": "split-based-on",
"transitions": [
{
"next": "say_play_1",
"event": "noMatch"
},
{
"next": "connect_call_to_sales",
"event": "match",
"conditions": [
{
"friendly_name": "sales",
"arguments": [
"{{widgets.gather_input.SpeechResult}}"
],
"type": "equal_to",
"value": "sales"
}
]
},
{
"next": "connect_call_to_support",
"event": "match",
"conditions": [
{
"friendly_name": "support",
"arguments": [
"{{widgets.gather_input.SpeechResult}}"
],
"type": "equal_to",
"value": "support"
}
]
}
],
"properties": {
"input": "{{widgets.gather_input.SpeechResult}}",
"offset": {
"x": 510,
"y": 510
}
}
},
{
"name": "connect_call_to_sales",
"type": "connect-call-to",
"transitions": [
{
"event": "callCompleted"
}
],
"properties": {
"offset": {
"x": 100,
"y": 750
},
"caller_id": "{{contact.channel.address}}",
"noun": "number",
"to": "12222222",
"timeout": 30
}
},
{
"name": "connect_call_to_support",
"type": "connect-call-to",
"transitions": [
{
"event": "callCompleted"
}
],
"properties": {
"offset": {
"x": 520,
"y": 750
},
"caller_id": "{{contact.channel.address}}",
"noun": "number",
"to": "12222222",
"timeout": 30
}
},
{
"name": "say_play_1",
"type": "say-play",
"transitions": [
{
"next": "gather_input",
"event": "audioComplete"
}
],
"properties": {
"offset": {
"x": 710,
"y": 200
},
"loop": 1,
"say": "not valid choice."
}
}
],
"initial_state": "Trigger",
"flags": {
"allow_concurrent_calls": true
}
}
Twilio developer evangelist here.
That is weird behaviour, mainly because the log says "evaluated to 'Sales.'". Split widgets conditions are not case-sensitive and should trim leading and following white-space. For some reason this appears to have a capital "S" and a full-stop.
I would suggest a couple of things. Firstly, raise a ticket with Twilio support to look into why the condition didn't match correctly.
Then, try some of the other conditions. When I generate a new IVR template in Studio, the conditions use "Matches any of" instead of "Equal to". You might also try "Contains".
So, experiment with the ways you can match the operators, but get support involved to drill down into why it didn't work in the first place.
The period is needed after the word for some reason...I just put both, for example:
"1, 1., Uno, Uno., Una, Una., Uno, Uno., Español, Español., Español, Español., Español, Español."

How to sum items in a List in Dart

What's the best way to sum all the "amounts" of all "expenses", for each user?
I've tried a few different things but I can't quite get it right. It should return 2 values: 20.0 and 24.90
[
{
"id": 3,
"company": {
"id": 2
},
"user": {
"id": 3,
"first_name": "Fred",
"last_name": "Smith",
"email": "asdfasf",
"is_suspended": false,
"vendor_id": "FS-100",
"username": "etytyurtyu",
"expense": [
{
"id": 7,
"date": "2019-12-14T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyAccount",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL8",
"URL9"
],
"user": {
"id": 3
},
"expense_type": {
"id": 4
},
"booking": {
"id": "HI-3565346"
}
}
]
}
},
{
"id": 2,
"company": {
"id": 2
},
"user": {
"id": 2,
"first_name": "Pierre",
"last_name": "XXXMar",
"email": "asdfasdfads",
"is_suspended": false,
"vendor_id": "PM-100",
"username": "asdfas",
"expense": [
{
"id": 2,
"date": "2019-12-16T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 5,
"date": "2019-12-08T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 3,
"date": "2019-12-17T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyCard",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL5",
"URL6"
],
"user": {
"id": 2
},
"expense_type": {
"id": 12
},
"booking": {
"id": "HI-3565346"
}
}
]
}
}
]
Thanks
You would first map the expenses amounts, then fold it into a single value, like this :
double sum = expenses.map((expense) => expense.amount).fold(0, (prev, amount) => prev + amount);
List item
For the sake of completeness: In 2021 you can replace the .fold(0, (prev, amount) => prev + amount) part by just calling .sum on the list. Just import import 'package:collection/collection.dart'; first –
tmaihoff

Geojson gets distorted when trying to render in vega

I try to render a geojson in vega.
I found this example which work fine:
How to read geojson with vega
however, when trying to replace the geojson with one of mine, the features get completely distorted.
{"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"width": 500,
"height": 600,
"autosize": "none",
"signals": [
{
"name": "translate0",
"update": "width / 2"
},
{
"name": "translate1",
"update": "height / 2"
}
],
"projections": [
{
"name": "projection",
"type": "mercator",
"scale": 1000,
"rotate": [
0,
0,
0
],
"center": [
17,
-3
],
"translate": [
{
"signal": "translate0"
},
{
"signal": "translate1"
}
]
}
],
"data": [
{
"name": "drc",
"url": "https://gist.githubusercontent.com/thomas-maschler/ef9891ef03ed4cf3fb23a4378dab485e/raw/47f3632d2135b9a783eeb76d0091762b70677c0d/drc.geojson",
"format": {
"type": "json",
"property": "features"
}
}
],
"marks": [
{
"type": "shape",
"from": {
"data": "drc"
},
"encode": {
"update": {
"strokeWidth": {
"value": 0.5
},
"stroke": {
"value": "darkblue"
},
"fill": {
"value": "lightblue"
},
"fillOpacity": {
"value": 0.5
}
},
"hover": {
"fill": {
"value": "#66C2A5"
},
"strokeWidth": {
"value": 2
},
"stroke": {
"value": "#FC8D62"
}
}
},
"transform": [
{
"type": "geoshape",
"projection": "projection"
}
]
}
]
}
Here is what they are suppose to look like
https://gist.github.com/thomas-maschler/ef9891ef03ed4cf3fb23a4378dab485e
What am I getting wrong?
Thanks,
Thomas
Not sure what happened. It seems your geojson was corrupt, but not really as I eventually could parse it in www.mapshaper.org. I reduced the file to 35% and then it parsed normally:
Vega-lite spec below (compile to Vega code in the editor if needed):
{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"width": 700,
"height": 500,
"config": {"view": {"stroke": "transparent"}},
"layer": [
{
"data": {
"url": "https://gist.githubusercontent.com/mattijn/2ce897c2020a6e5b7ae6baf03dffe179/raw/564b6d484657864dcb77d0bb18db00fc7dc7668d/drc.geojson",
"format": {"type": "json", "property": "features"}
},
"mark": {"type": "geoshape", "stroke": "white", "strokeWidth": 1},
"encoding": {"color": {"value": "#bcbcbc"}}
}
]
}

How to read geojson with vega

It sounds super simple, but I can't get how can I use geojson, not topojson, for my polygons.
that's my current attempt:
"data": [
{
"name": "nabs",
"url": "both_boundaries.geojson",
"format": {"type": "json"},
"transform": [
{
"type": "geopath", "projection": "mercator",
"scale": 74, "center": [-73.99,40.72]
}
]
}
]
You have to parse the features using property within your format:
"format": {"type": "json", "property":"features"},
Full example spec:
{"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"width": 500,
"height": 600,
"autosize": "none",
"signals": [
{
"name": "translate0",
"update": "width / 2"
},
{
"name": "translate1",
"update": "height / 2"
}
],
"projections": [
{
"name": "projection",
"size": {"signal": "[width, height]"},
"fit": {"signal": "data('netherlands')"}
}
],
"data": [
{
"name": "netherlands",
"url": "https://raw.githubusercontent.com/mattijn/datasets/master/NL_outline_geo.json",
"format": {
"type": "json",
"property": "features"
}
}
],
"marks": [
{
"type": "shape",
"from": {
"data": "netherlands"
},
"encode": {
"update": {
"strokeWidth": {
"value": 0.5
},
"stroke": {
"value": "darkblue"
},
"fill": {
"value": "lightblue"
},
"fillOpacity": {
"value": 0.5
}
},
"hover": {
"fill": {
"value": "#66C2A5"
},
"strokeWidth": {
"value": 2
},
"stroke": {
"value": "#FC8D62"
}
}
},
"transform": [
{
"type": "geoshape",
"projection": "projection"
}
]
}
]
}

Resources