RethinkDB query OrderBy distances between central point and subtable of locations - rethinkdb

I'm fairly new to RethinkDB and am trying to solve a thorny problem.
I have a database that currently consists of two kinds of account, customers and technicians. I want to write a query that will produce a table of technicians, ordered by their distance to a given customer. The technician and customer accounts each have coordinate location attributes, and the technicians have a service area attribute in the form of a roughly circular polygon of coordinates.
For example, a query that returns a table of technicians whose service area overlaps the location of a given customer looks like this:
r.db('database').table('Account')
.filter(r.row('location')('coverage').intersects(r.db('database')
.table('Account').get("6aab8bbc-a49f-4a9d-80cc-88c95d0bae8d")
.getField('location').getField('point')))
From here I want to order the resulting subtable of technicians by their distance to the customer they're overlapping.

It's hard to work on this without a sample dataset so I can play around. I'm using my imagination.
Your Account table stores both of customer and technician
Technician document has field location.coverage
By using intersect, you can returns a list of technician who the coverage locations includes customer location.
To order it, we can pass a function into orderBy command. With each of technican, we get their point field using distance command, return that distance number, and using that to order.
r.db('database').table('Account')
.filter(
r.row('location')('coverage')
.intersects(
r.db('database').table('Account').get("6aab8bbc-a49f-4a9d-80cc-88c95d0bae8d")('location')('point')
)
)
.orderBy(function(technician) {
return technician('location')('point')
.distance(r.db('database').table('Account').get("6aab8bbc-a49f-4a9d-80cc-88c95d0bae8d")('location')('point'))
})
I hope it helps. If not, let's post some sample data here and we can try figure it out together.

Related

How I do this on Spring boot + sql

I'm having trouble solving this problem here that was sent to me as an exercise
Create a vehicle registration Rest API to store the vehicles used by the
company. The register must contain the following data:
Name
brand
model
Manufacturing date
Average fuel consumption within the city (KM/L)
Average fuel consumption on highways (KM/L)
Create an API to perform the expense forecast calculation.
It should receive the following information as a parameter:
gasoline price R$
Total km that will be traveled within the city
Total km to be traveled on highways
The return must be a ranked list of company vehicles taking into account
consideration of the amount spent on fuel
I'm having difficulty in how to add an "expense", then calculate this expense according to the variables, associate this expense with the foreign key, and finally show this expense.
This is the database (By the way, it is in Brazilian Portuguese):
car database
expense database
What I managed to do so far in the code was this: https://github.com/LuizMafraJNR/fluigcclean-api-0.0.1
If anyone can really help me, because I'm racking my brains and can't find proper ways to solve this, I'm still learning Spring boot and API creations, so I need someone to enlighten me
I will give an example of entry in the registration of a vehicle
{ "name":"Jetta", "brand":"Volkswagen", "model":"GLI", "dateManufacture":"12/12/2021", "ConsumoMedioCidade":10.9, "ConsumoMedioRodovia":14.7, }
So far so good, but now I want to register a "Spend" on this vehicle that I registered. I made the foreign key already linked to the primary key of the Car class, but I want to understand how to register an expense by selecting the vehicle ID.
After registering this expense in the vehicle I selected, I want to calculate it following these parameters
valorGasto = ((kmRodadosCidade /consumoMedioCidade) + (kmRodadosRodovia/consumoRodovia)) * valorGasolina
The parameter valorGasto is from the database, that is, I want it to take these values ​​from the database, calculate and return in this column and then make an endpoint to show the expense of this vehicle.

DM and hierarchies - dimensions for future use

My very first DM so be gentle..
Modeling a hierarchy with ERD as follows:
Responses are my facts. All the advice I've seen indicates creating a single dimension (say dim_event) and denormalizing event, department and organization into that dimension:
What if I KNOW that there will be future facts/reports that rely on an Organization dimension, or a Department dimension that do not involve this particular fact?
It makes more sense to me (from the OLTP world) to create individual dimensions for the major components and attach them to the fact. That way they could be reused as conformed dimensions.
This way for any updating dimension attributes there would be one dim table; if I had everything denormalized I could have org name in several dimension tables.
--Update--
As requested:
An "event" is an email campaign designed to gather response data from a specific subset of clients. They log in and we ask them a series of questions and score the answers.
The "response" is the set of scores we generate from the event.
So an "event" record may look like this:
name: '2019 test Event'
department: 'finance'
"response" records look something like this:
event: '2019 test Event'
retScore: 2190
balScore: 19.98
If your organization and department are tightly coupled (i.e. department implies organization as well), they should be denormalized and created as a single dimension. If department & organization do not have a hierarchical relationship, they would be separate dimensions.
Your Event would likely be a dim (degenerate) and a fact. The fact would point to the various dimensions that describe the Event and would contain the measures about what happened at the Event (retScore, balScore).
A good way to identify if you're dealing with a dim or a fact is to ask "What do I know before any thing happens?" I expect you'd know which orgs & depts are available. You may even know certain types of recurring events (blood drive, annual fundraiser), which could also be a separate dimension (event type). But you wouldn't have any details about a specific event, HR Fundraiser 2019 (fact), until one is scheduled.
A dimension represents the possibilities, but a fact record indicates something actually happens. My favorite analogy for this is a restaurant menu vs a restaurant order. The items on the menu can be referenced even if they've never been ordered. The menu is the dimension, the order is the fact.
Hope this helps.

Is Elasticsearch X-Pack able to return graph vertices across different types?

I have product type data loaded into Elasticsearch containing catalogue_number and name. I also have customer data loaded into Elasticsearch containing name and purchases (where purchases is an array of product numbers).
For example:
CATALOGUE_NUMBER, NAME
518, "Toilet Paper"
388, "Candy Bar"
263, "Carrots"
And, for customers:
NAME, PURCHASES
"Jack", [518, 388]
"John", [263]
"Bill", [263, 518]
Considering the relationship is many to one (i.e. customers purchase many items), am I able to use Kibana to view a graph linking purchases to specific customers, or is this out of scope?
My end goal is to have a graph showing product and customer as vertices and edges showing which products each customer purchases. I am very confused as to whether Elasticsearch is capable, or if I should move to a pure graph database such as Neo4J and Elasticsearch for searching only.
The Graph feature can draw out these connections if they share a common field name - the unique identity of a node is a field name and a term. Terms can be in different indices but as long as they share a common field name they are seen as the same node.
I'm not sure which business problem you are trying to solve (recommendations? Fraud?) but depending on what you are trying to achieve you may want to model things differently.
If you're interested in recommendations and people who-bought-X-also-bought-Y style suggestions then the people are unlikely to be interesting nodes to plot and you can just examine the "purchases" field which will draw out which products significantly co-occur.
For more detailed "forensic" type applications you may want to just have person->product links and not have product->product links in which case you would be forced to create more classical "edge-like" documents with only 2 nodes - a person ID and a product ID.

Cognos Report-need 2nd (Totals) Crosstab that is "aware" (filters) using the current Grouping

I am trying to create a report which groups on a column called "Legal Entity." When the output is directed to Excel, a separate tab will be created for each distinct entity in the query resultset.
For each Excel tab/Legal Entity, there will be two "sections." The first is a repeating section that breaks on a column "Funding Arrangement Type." After all of the Funding Arrangement Types are exhausted, there will be a single "Totals" grid which will summarize the data on the tab for the current Legal Entity. The data will be summarized across all Funding Arrangement Types within the current Legal Entity.
Because the Totals (lower) grid is really just a summarization of the same source query, Query1, I thought that I would also bind the Totals grid to it. However, if I do that, I get a run time error that tells me that I need to establish a Master-Detail relationship (If I decide to use a separate query for the Totals grid, the Totals grid "will not be aware" of the current Legal Entity/tab that must be considered when summarizing.)
Therefore, I continued with my guess at how the Master-Detail relationship should be defined. I made various attempts to link the two grids, including:
On all of the dimension (non-summarized) columns.
On Legal Entity
On Legal Entity and Funding Arrangement Type
Doing so affected previously correct totals reported in the upper cross tab results/
This Master-Detail approach is foreign and as a result I don't understand what it is doing.
I also tried to use a separate query, Query2, for the lower totals grid and adding a filter to filter SQL2 where SQL2. LegalEntity = SQL1.LegalEntity in an effort to get the totals grid to summarize within the current LeglEntity grouping. This resulted in a cross join error.
I’m a real noob with Cognos. Suggestions are welcomed. Thank you!
You can use mouse+scroll wheel to zoom in:
I was able to get it working by binding both grids to a single query and for both grids, establish a Master-Detail Replationship on Legal Entity. Prior to doing that, I added these columns to both grids and hide them, not sure if this was necessary.

Multiple entities in one UITableview

I have a data model with two entitys with relationship one to many. Lets call them City<-->>Person.
In my tableview Im currently displaying each City, seperated by sections (currently one cell in each section).
Is there any way for me to add each Person connected to the specific city in the corresponding section? (adding as many cells as number of persons connected to the city)
Im using two different fetchedResultsControllers to access the Entities but cant quite figure out how to do this.
Ultimately Id like each sections first cell to be a city, followed by all the persons in that city.
Any help or pointer in the right direction would be highly appreciated.
As Michal mentioned in comments. A cleaner design would be for your City entities to be displayed in the section headers (pretty easy) and have your person objects in the sections as rows.
To do that you need to add a unique identifier from the city as your section path of your NSFetchedResultsController. Since that attribute takes a key path you can specify something like city.name and it will resolve through the relationship from person to city and get that value.
Your only other option would be to manually fetch the city objects and build your UITableViewDatasource to talk to your own construct of data. Doable but far, far more difficult than what Michal suggested.

Resources