How I do this on Spring boot + sql - spring

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.

Related

NEAR Marketplace - Timed Auction Functionality

We're building a marketplace in NEAR. We have two smart contracts for token and marketplace. Same as NEAR example.
In our platform, we have successfully implemented below features:
Token mint (used contract method: nft_mint)
Token listing for sale at Fixed Price in marketplace (used contract methods: storage_deposit nft_approve)
Token purchase (used contract method: offer)
Apply creator royalty at mint time.
Apply service fee (Transaction fee of Marketplace) on each sale.
Everything is working fine.
Now, we would like to implement the Timed Auction Functionality
Creator can create NFT’s to be sold as an auction so that buyers can bid on them to get a higher price.
Method:- Sell to the highest bidder: where the highest bid wins at the end.
Making a bid on this kind of auction is like making an offer on a fixed-price listing. The seller can choose to accept it at any time, but the buyer will near of the amount of bid to create a bid.
Questions:
How we can implement the Timed Auction Functionality with Marketplace
Contract?
Do we have any documentation or example for the
Timed Auction Functionality?
Example of OpenSea's Timed Auction Functionality:
You can't do that directly from within a contract since the contract does not run all the time you will have to call function trough something like https://cron.cat/ or your own.
Contracts perceive time trough current execution so when a function is called env::block_timestamp_ms() within said fn would give us the current time of execution, you can subtract time from it to get some time in the past. We can do manual checks like is the time stamp past this than do that or the less accurate is the blockheight above certain threshold but all this will have to be executed within a function that needs to be called.
I will preface this post by saying that this is how I would go about solving this in a simple way. There are more complex solutions out there but I'll try and keep it relatively simple. (scroll to the bottom for a TLDR).
Here's a solution to the timed auction functionality. In the marketplace example that you posted, what you'll want to do is store two extra fields in the sale object indicating the desired length of the auction as well as the time when the sale was listed.
Whenever NFTs are put for sale (in the nft_on_approve function), you'll need to add the current block_timestamp (which will give you the number of nanoseconds since the Unix epoch). You'll also need to store the desired length of the auction which is up to you as to how you would like to implement that. It can be time in nanoseconds, milliseconds, seconds, days, either since the Unix epoch or the actual desired duration of the auction.
Once your sale object contains both these new fields, all you would need to do is whenever an offer is made, check if the current block timestamp is within the desired auction length. If it is, proceed, if it's not panic. Since the marketplace can't constantly be polling to see if the auction is finished, a common approach is to use the functionality I described above:
You either bid on the NFT before the timestamp or you didn't. If you did, your bid is accepted, if you don't, it panics.
At this point, there is no way for the marketplace to automatically transfer the NFT to the highest bidder the second the auction closes (due to the marketplace's inability to poll). My suggestion here would be to have a claim function that the highest bidder must call if they want the NFT transferred to them. You can get spicy with this and have timeouts whereby if it isn't claimed within 5 days, the original owner can claim it back but for the sake of simplicity, let's not get into that.
As for the bidding mechanic, there's many different ways you can do this. I'll go over one of the more simple approaches. Currently, when a sale is listed with sale conditions, the NFT can only be purchased for more than or equal to the price as shown here. What you'll want to do is store active bids in the sale object.
You'll need to store both the current highest bidder as well as their bid amount. This will be initialized to empty when the NFT is listed and then once an offer is made for less than the desired list price, it's considered a bid. Bids will only be added if they are less than the desired price and greater than the highest current bid. If the offer is more than the list price, the NFT is automatically sold and the purchase is processed.
Combining everything I've outlined so far in a TLDR:
What you would need to add as a minimum PoC to the sales object:
Store desired length of auction (in whatever format you want)
Store date of listing
Store current highest bidder
Store current highest bid
Listing mechanics:
When a sale is listed, set the date of listing and the desired length of the auction.
Offer mechanics:
make sure the attached deposit is greater than the current highest bid (if none, make sure it's >= 0).
If the attached deposit is greater than the list price, process the purchase. If it's not, add it as the current highest bid.
Make sure that the any offers are made within the desired length of the auction. If they aren't panic, if they are, you're good to go.
If somebody is outbid, make sure you refund them for their deposit!
Claim mechanics:
Claims can only be made if the auction time has passed. Only the highest bidder can claim the NFT. Once claimed, the purchase is processed.

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.

Need help in choosing right caching strategy

We car planning to store prices data to Memcache. prices are subject to car variant and location(city). This is how it is stored in the database.
variant, city, price
21, 48, 40000
Now the confusion is that how do we store this data into Memcache.
Possibility 1 : We store each price in separate cache object and do a multiget if the price of all variant belongs to a model need to be displayed on a single page.
Possibility 2 : We store prices at the model, city level. Prices of all variants of a model will be stored in a single object. This object will be slightly heavy but multiget wouldn't be required.
Need your help in taking the right decision.
TLDR: It all depends on how you want to expose the feature to your end users, and what the query pattern looks like.
For example:
If your flow is that a user can see all the variant prices on a detail page for a city, then you could use <city_id>_<car_model_id> as the key, and store all data for variants against that key (Possibility 2).
If the flow is that a user can see prices of all variants across cities on a single page, then you would need the key as <car_model_id> and store all data as Json against this key
If the flow is that a user can see prices of one variant at a time only for every city, then you would use the key <city_id>_<car_variant_id> and store prices.
One thing to definitely keep in mind is the frequency with which you may have to refresh the cache/ perform upserts, which in the case of cars should be infrequent (who changes the prices of a car every day/second). So, I would have gone with option 1 above (Possibility 2 as described by you).

RethinkDB query OrderBy distances between central point and subtable of locations

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.

Prolog With Java Query

Hi Somebody can Help me with this problem, i need the follow tables in prolog and i need todo some query to this table like the follow.
2.1 Get the plate, number of doors, model, reference and name of the owner
vehicles having a given displacement.
2.2. Get the plate, reference number and owner name doors of the vehicles that
have affiliate contract type and a given model.
2.3. Get the plate, date of revision, ID of the owner of the vehicles with revisions in a
given state and insurer corresponds to a given.
2.4. Get the model plate, ID of owner and contact number of the vehicles with a
and number of gates given speed below a given amount.
2.5. Get the plate, reference, cedula Owners of older vehicles (his model is
lower).
in this paste are the tables
http://pastebin.com/BBue2nFE
please it's somebody can help me Thanks.
Sorry, your posting is hard to understand.
Is your problem that you have data in a relational database and need to access it from Prolog? If so, we need to know which Prolog you are using and which database.

Resources