I am trying to create a query where data from table analytics value per column will be grouped and counted, I have zero knowledge in grouping a value of columns and counting them and doing this on my end is really hard.
So I have an example query here:
So I have an example query here:
query getAnalytics($from: timestamptz, $to: timestamptz, $user_id: String) {
data:analytics(where: { created_at: {_gte: $from, _lte: $to}, user_id: {_eq: $user_id}}) { id user_id visitor_id type os device country browser created_at }
}
it get a sample data:
{
"data": {
"data": [
{
"id": 9,
"user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
"visitor_id": "null",
"type": "visit",
"os": "Windows 10",
"device": "Desktop",
"country": "Japan",
"browser": "Chrome",
"created_at": "2023-01-07T04:36:38.086468+00:00"
},
{
"id": 10,
"user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
"visitor_id": "null",
"type": "visit",
"os": "Windows 10",
"device": "Desktop",
"country": "Japan",
"browser": "Chrome",
"created_at": "2023-01-07T04:36:38.489139+00:00"
},
{
"id": 11,
"user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
"visitor_id": "null",
"type": "visit",
"os": "Windows 10",
"device": "Desktop",
"country": "Japan",
"browser": "Chrome",
"created_at": "2023-01-07T04:43:53.903306+00:00"
},
{
"id": 12,
"user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
"visitor_id": "null",
"type": "visit",
"os": "Windows 10",
"device": "Desktop",
"country": "Japan",
"browser": "Chrome",
"created_at": "2023-01-07T04:43:54.325736+00:00"
},
{
"id": 13,
"user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
"visitor_id": "null",
"type": "visit",
"os": "Windows 10",
"device": "Desktop",
"country": "Japan",
"browser": "Chrome",
"created_at": "2023-01-07T04:45:17.899966+00:00"
},
]
}
}
Sorry, I wasn't able to provide a tested code because most of my structure didn't work and the only thing that I know in counting things up is aggregate with condition and count or join, But things grouping dynamically with columns value and counting them up, I have zero idea.
Is it possible to group the value of columns and count them, you know for analytics purposes.
sample output structure that I want to happen:
{
"user_id": "user_2Jy9jmL8OIpyb1DpTGYrrtjS77P",
"visitor_id": {
"null": 41,
"user_2JzAMaOW8j01rpsl98McXwBlCkv": 30
}
"type": {
"visit": 71,
"shares": 0,
"clicks": 0,
},
"os": {
"Windows 10": 28,
"iOS 13.2.3": 36,
"null": 7,
},
"device": {
"Desktop": 60,
"Mobile": 10,
"null": 7,
},
"country": {
"Japan": 60,
"null": 7,
},
"browser": {
"Chrome": 28,
"Mobile Safari": 36,
"null": 7,
}
}
I am clueless and cant find in docs or other article the thing/related to things I want to happen
GraphQL is a query language for APIs (https://graphql.org/).
GraphQL it is not SQL and have not a group method. For each query we have output object format. In your case it is (approximately)
type Analytic {
id: ID!
user_id: String
visitor_id: String
type: String
os: String
device: String
country: String
browser: String
created_at: DateTime
}
Result of your query it is List of Analytic. Combining schema-defined types must already be done on the client. Or ask to add an object with analytics to the scheme in the format you need.
Related
I am attempting to scope an answer to a User or Project via a polymorphic relationship via multiple levels but I am having some trouble getting my head around it.
It's tricky to explain, but I will show you what I have at the moment, minus the Answer model/relationship.
I have a Category and a Role model. These are in a belongsTo relationship (a role belongs to a category).
I also have a Question model, this also has a belongsTo relationship with a Role (a question belongs to a role).
I have created a roleables polymorphic relationship, allowing me to assign roles to either a User or a Project.
All of this is working as expected. I am able to return the assigned roles via my endpoint user/1/roles or project/1/roles, and in this endpoint, I am able to return the questions for that role (see example output below).
{
"data": [
{
"id": 20,
"name": "Actor",
"entity_id": 1,
"category_id": 3,
"questions": [
{
"id": 20,
"role_id": 20,
"field_type": "text",
"field_name": "Height",
"field_default_value": null,
"field_validation_rules": "required,string,max:5",
"field_options": false,
"order": 0
},
{
"id": 21,
"role_id": 20,
"field_type": "text",
"field_name": "Weight",
"field_default_value": null,
"field_validation_rules": "required,string,max:5",
"field_options": false,
"order": 1
},
{
"id": 22,
"role_id": 20,
"field_type": "select",
"field_name": "Race",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": [
{
"id": "white",
"name": "White"
},
{
"id": "mixed",
"name": "Mixed"
}
],
"order": 2
},
{
"id": 23,
"role_id": 20,
"field_type": "select",
"field_name": "Gender",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": [
{
"id": "male",
"name": "Male"
},
{
"id": "female",
"name": "Female"
},
{
"id": "other",
"name": "Other"
}
],
"order": 3
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 23,
"name": "Singer",
"entity_id": 1,
"category_id": 3,
"questions": [
{
"id": 26,
"role_id": 23,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 32,
"name": "Console Operator",
"entity_id": 1,
"category_id": 5,
"questions": [
{
"id": 35,
"role_id": 32,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 60,
"name": "Composer",
"entity_id": 1,
"category_id": 7,
"questions": [
{
"id": 63,
"role_id": 60,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
}
]
}
The problem that I am trying to get my head around is Answers to the questions. Just a little background, which may help me explain this in writing:
In the front-end, I have a page in which the user can update their information, on this page they also have the ability to assign themselves some roles. When the click on a role the list of questions is displayed and they have the ability to answer them.
The same is also true for a Project. A user can create projects, and have the ability to assigned roles and answer the relevant questions.
So, I guess I will need to create a new table in the database to store the answers.
The question is how/what relationship should this new answers table have to the question, user/project in order to get all the roles, questions and any answers scoped to the entity (user/project) and it's roles?
I would love the each role in the output from my user/1/roleendpoint to look more like the following (notice the new answers key in questions):
{
"id": 60,
"name": "Composer",
"entity_id": 1,
"category_id": 7,
"questions": [
{
"id": 63,
"role_id": 60,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0,
"answers": { ... }
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
}
I was expecting entities such DATE, TIME, DURATION & No Of People Joining the Call in below JSON.
Now i got entities back such as DATE,TIME and DURATION correctly but for No Of People there is problem for me.
I am getting four entities as NUMBER so now i am confused as how to pick exact entity that represent No Of People. Ideally it is No. 6, but i am not getting on which basis i should decide that 6 is the No Of People
{
"query": "book audio bridge tomorrow for 6 people for 30 mins starts at 5:30 PM",
"topScoringIntent": {
"intent": "BookAudioBridge",
"score": 0.9895838
},
"intents": [
{
"intent": "BookAudioBridge",
"score": 0.9895838
},
{
"intent": "ListBooking",
"score": 0.00677821552
}
],
"entities": [
{
"entity": "tomorrow",
"type": "builtin.datetimeV2.date",
"startIndex": 18,
"endIndex": 25,
"resolution": {
"values": [
{
"timex": "2018-06-21",
"type": "date",
"value": "2018-06-21"
}
]
}
},
{
"entity": "30 mins",
"type": "builtin.datetimeV2.duration",
"startIndex": 44,
"endIndex": 50,
"resolution": {
"values": [
{
"timex": "PT30M",
"type": "duration",
"value": "1800"
}
]
}
},
{
"entity": "5:30 pm",
"type": "builtin.datetimeV2.time",
"startIndex": 62,
"endIndex": 68,
"resolution": {
"values": [
{
"timex": "T17:30",
"type": "time",
"value": "17:30:00"
}
]
}
},
{
"entity": "6",
"type": "builtin.number",
"startIndex": 31,
"endIndex": 31,
"resolution": {
"value": "6"
}
},
{
"entity": "30",
"type": "builtin.number",
"startIndex": 44,
"endIndex": 45,
"resolution": {
"value": "30"
}
},
{
"entity": "5",
"type": "builtin.number",
"startIndex": 62,
"endIndex": 62,
"resolution": {
"value": "5"
}
},
{
"entity": "30",
"type": "builtin.number",
"startIndex": 64,
"endIndex": 65,
"resolution": {
"value": "30"
}
}
]
}
You could build a composite entity of number and simple entity for "people" so that the entity that comes back indicates 6 as number and simple entity name as people.
I am trying to persist some objects that have a composite id. If I am only sending an array with one element it works fine, but it the array has more than one it throws an exception when saving the first one. public boolean
addParamsToChart(List<ChartParams> chartParams, Long chartId) {
List<ChartParams> chartParamsList = new ArrayList<>();
for(ChartParams chartParam: chartParams) {
ChartParamsId id = new ChartParamsId();
// id.setChartId(chartId);
id.setChartId(chartParam.getChart().getId());
id.setParamId(chartParam.getParam().getId());
id.setContextSourceId(chartParam.getContextSource().getId());
chartParam.setChartParamsId(id);
if(chartParamsRepository.save(chartParams) !=null) {
chartParamsList.add(chartParam);
}
}
if(chartParamsList.size()!=chartParams.size()) {
// something went wrong, delete previous inserted
deleteChartParams(chartParamsList);
chartRepository.delete(chartId);
return false;
}
return true;
}
[{
"chart": {
"id": 49,
"cv": {
"id": 1,
"name": "Money",
"category": {
"id": 1,
"name": "Euros"
},
"definition": "\"European curreny.\" [EU:euro]",
"enabled": true,
"cvid": "CC:1010"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Euro saving charts"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 3,
"name": "euro",
"internal": "eu",
"abbreviatedName": "eu"
} }]
But for this having two objects inside instead of one is not working, throwing an exception at the first save.
[{
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 6,
"name": "stock",
"internal": "st",
"abbreviatedName": "st"
} }, {
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 7,
"name": "Sold stock",
"internal": "st_sold",
"abbreviatedSequence": "st_sold"
} }]
The exception I got is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class eu.stocks.chart.chartParams.ChartParams
I am trying to hit a query string on a nested field abc.answer which does not use any analyzer. Here is the query I am using:
{
"query": {
"nested":{
"path": "abc",
"query":{
"query_string": {
"query": "photo AND delhi",
"fields": ["answer"]
}
},
"inner_hits": {
"explain": true
}
}
},
"explain" : true,
"sort" : [ {
"_score" : { }
} ]
}
I want to search for the document which has both dtp operator and bangalore in the nested field abc. But it is not showing any results.
My nested field structure :
"abc": [
{
"updatedAt": 1452073190000,
"questionId": 1,
"labelOriginal": "name",
"createdAt": 1452073190000,
"answerOriginal": "wexe",
"answer": "wexe",
"answerId": 0,
"label": "name",
"question": "what is your name?"
},
{
"updatedAt": 1452073190000,
"questionId": 2,
"labelOriginal": "mobile",
"createdAt": 1452073190000,
"answerOriginal": "9000000000",
"answer": "9000000000",
"answerId": 0,
"label": "mobile",
"question": "What is your mobile number?"
},
{
"updatedAt": 1452073190000,
"questionId": 3,
"labelOriginal": "email id",
"createdAt": 1452073190000,
"answerOriginal": "legallss#yahoo.com",
"answer": "legallss#yahoo.com",
"answerId": 0,
"label": "email Id",
"question": "What is your e-mail id ?"
},
{
"updatedAt": 1452073190000,
"questionId": 4,
"labelOriginal": "current role",
"createdAt": 1452073190000,
"answerOriginal": "dtp operator",
"answer": "DTP Operator",
"answerId": 597,
"label": "current role",
"question": "What is your current role?"
},
{
"updatedAt": 1452073190000,
"questionId": 5,
"labelOriginal": "city",
"createdAt": 1452073190000,
"answerOriginal": "bangalore",
"answer": "Bangalore",
"answerId": 23,
"label": "city",
"question": "Which city do you live in ?"
},
{
"updatedAt": 1452073190000,
"questionId": 6,
"labelOriginal": "locality",
"createdAt": 1452073190000,
"answerOriginal": "80 ft. road",
"answer": "80 Ft. Road",
"answerId": 0,
"label": "locality",
"question": "Which locality do you live in ?"
},
{
"updatedAt": 1452073190000,
"questionId": 13,
"labelOriginal": "job type",
"createdAt": 1452073190000,
"answerOriginal": "part time jobs",
"answer": "Part Time Jobs",
"answerId": 64,
"label": "Job Type",
"question": "Are you comfortable with working Full Time or Part Time?"
},
{
"labelOriginal": "userDesiredCity",
"answerOriginal": "bangalore",
"answer": "Bangalore",
"answerId": 23,
"label": "userDesiredCity"
}
You don't need a nested functionality for this kind of query, but the flat structure of a simple array.
Make your rsa field to have include_in_parent: true:
"rsa": {
"type": "nested",
"include_in_parent": true,
"properties": {
Re-index the test documents and then use a query like this one (no nested query):
"query": {
"query_string": {
"query": "dtp operator AND bangalore",
"fields": [
"rsa.answer"
]
}
}
I have the following json:
{
"release": {
"genres": {
"genre": "Electronic"
},
"identifiers": {
"identifier": [
{
"description": "Text",
"value": "5 709498 101026",
"type": "Barcode"
},
{
"description": "String",
"value": 5709498101026,
"type": "Barcode"
}
]
},
"status": "Accepted",
"videos": {
"video": [
{
"title": "Future 3 - Renaldo",
"duration": 446,
"description": "Future 3 - Renaldo",
"src": "http://www.youtube.com/watch?v=hpc9aQpnUjc",
"embed": true
},
{
"title": "Future 3 - Silver M from album We are the Future / 1995 Denmark / Archivos de Kraftwerkmusik",
"duration": 461,
"description": "Future 3 - Silver M from album We are the Future / 1995 Denmark / Archivos de Kraftwerkmusik",
"src": "http://www.youtube.com/watch?v=nlcHRI8iV4g",
"embed": true
},
{
"title": "Future 3 - Bubbles At Dawn",
"duration": 710,
"description": "Future 3 - Bubbles At Dawn",
"src": "http://www.youtube.com/watch?v=ABBCyvGMOFw",
"embed": true
}
]
},
"labels": {
"label": {
"catno": "APR 010CD",
"name": "April Records"
}
},
"companies": {
"company": {
"id": 26184,
"catno": "",
"name": "Voices Of Wonder",
"entity_type_name": "Published By",
"resource_url": "http://api.discogs.com/labels/26184",
"entity_type": 21
}
},
"styles": {
"style": [
"Abstract",
"IDM",
"Downtempo"
]
},
"formats": {
"format": {
"text": "",
"name": "CD",
"qty": 1,
"descriptions": {
"description": "Album"
}
}
},
"country": "Denmark",
"id": 5375,
"released": "1995-00-00",
"artists": {
"artist": {
"id": 5139,
"anv": "",
"name": "Future 3",
"role": "",
"tracks": "",
"join": ""
}
},
"title": "We Are The Future 3",
"master_id": 638422,
"tracklist": {
"track": [
{
"position": 1,
"duration": "8:04",
"title": "Future 3"
},
{
"position": 2,
"duration": "7:38",
"title": "Silver M"
},
{
"position": 3,
"duration": "7:27",
"title": "Renaldo"
},
{
"position": 4,
"duration": "6:04",
"title": "B.O.Y.D."
},
{
"position": 5,
"duration": "6:12",
"title": "Fumble"
},
{
"position": 6,
"duration": "6:12",
"title": "Dawn"
},
{
"position": 7,
"duration": "11:54",
"title": "Bubbles At Dawn"
},
{
"position": 8,
"duration": "6:03",
"title": "D.A.W.N. At 6"
},
{
"position": 9,
"duration": "8:50",
"title": 4684351684651
}
]
},
"data_quality": "Needs Vote",
"extraartists": {
"artist": [
{
"id": 2647642,
"anv": "",
"name": "Danesadwork",
"role": "Cover",
"tracks": "",
"join": ""
},
{
"id": 2647647,
"anv": "",
"name": "Djon Edvard Petersen",
"role": "Photography By",
"tracks": "",
"join": ""
},
{
"id": 114164,
"anv": "",
"name": "Anders Remmer",
"role": "Written-By",
"tracks": "",
"join": ""
},
{
"id": 435979,
"anv": "",
"name": "Jesper Skaaning",
"role": "Written-By",
"tracks": "",
"join": ""
},
{
"id": 15691,
"anv": "",
"name": "Thomas Knak",
"role": "Written-By",
"tracks": "",
"join": ""
}
]
},
"notes": "© 1995 April Records APS ℗ 1995 April Records APS"
}
}
I am trying to get those titles which end with 'At Dawn'.
I am using the following command
r.db("discogs1").table("releases").filter(function(doc){ return doc('release')('title').match('At Dawn$')})
But I get errors as follows:
RqlRuntimeError: Expected type STRING but found NUMBER in:r.db("discogs1").table("releases").filter(function(var_24) { return var_24("release")("title").match("At Dawn$"); })
I tried different combinations but I can't seem to get it to work
It seems that some of your documents don't have a row('release')('title') property that is a string. Some of them are numbers, so when you try to call .match on them, they throw an error because .match only works on strings.
To see if this is true, try the following:
r.db("discogs1").table("releases")
.filter(r.row('release')('title').typeOf().ne('STRING'))
.count()
Ideally, the result of this should be 0, since no document should have a title property that's not a string. If it's higher than 0, that's why you're getting an error.
If you want to only get documents where the title is a string, you can do the following:
r.db("discogs1").table("releases")
.filter(r.row('release')('title').typeOf().eq('STRING'))
.filter(function(doc){ return doc('release')('title').match('At Dawn$')})
This query will work, because it will filter our all documents where the title is not a string.
If you want to coerce all title into strings, you can do the following:
r.db("discogs1").table("releases")
.filter(r.row('release')('title').typeOf().ne('STRING'))
.merge(function (row) {
return {
'title': row('title').coerceTo('string')
}
})
If you want to delete all documents where the title is not a string, you can do this:
r.db("discogs1").table("releases")
.filter(r.row('release')('title').typeOf().ne('STRING'))
.delete()