Doctrine ArrayCollection fixed size - doctrine-orm

I'm working with a database with an entity of type Store. Each store has opening hours for each day of the week defined in an OpeningHour entity. A store can only have a maximum of 7 OpeningHour entities. This is fine since I can control that in the business logic.
However, the opening hours for a store may or may not be defined explicitly. This means that when I update the opening hours I first need to check if there are opening hours for that specific day already defined. If they exist I need to update otherwise I need to add a new OpeningHour entity to the ArrayCollection.
To get the opening hours for a store I do the following:
$openingHours = $store->getOpeningHours();
The $openingHours variable should now have a collection of not more than 7 entities (but might be less).
To update the opening hours I have a simple data structure that looks something like this:
$values = array(
array(
'day' => "Monday",
'start' => "00:00:00",
'end' => "00:00:00",
)
);
The $values data structure can have up to 7 entries for each of the 7 days. My OpeningHour entity has the same properties as this data structure.
My question is, how can I easily check within the $openingHours ollection if a entity with a specific day name already exist?

inside Store:
public function hasOpeningHoursOnDay($day) {
return $this->openingHours->filter(function($key, $element) use ($day) {
return $element->getDay() === $day;
})->count() > 0;
}

Related

How to implement a do while loop in Power Query and read the last row of a table which is updated dynamically

I am trying to import data from sharepoint rest API using the document id of all the documents. My objective is to start from the smallest document id and move on until there are no more documents.
I have designed a custom function and i am calling it by passing the Document Id which i am starting from 0. This function return me a table containing 500 documents whose Doc Id is greater than the Document Id which i am passing.
#"Output" =Table.AddColumn(Termset,"c", each GetList( try List.Max(Output[c.DocId]) otherwise LastDocID))
So my data is updated in the Output table. My problem here is that it is returning the same set of 500 recs again and again. Which is possibly because the value of List.Max(Output[c.DocId] is not changing (i want this value to be the last document id which is returned from GetList function) . I am trying here to do someting like a do while loop.
do{
Output=GetList(LastDocID)
LastDocId=List.Max(Output[DocId])
}while(there_are_no_more_docs)
Is there any way in Power Query that i can dynamically change the value of LastDocId which i am passing to the GetList function. The method which i tried below does not seem to be working as it is not able to read the contents of the Output table after every function call.
Note: I am using Termset as pages to put a check on the total documents being read. It is a list whose value starts from 0 and increments by 500 until it is less than the total number of docs in Sharepoint.
I would really appreciate if somebody can help me here.
You need to look at List.Generate to generate all of your page requests and then compute the last document id from the list of results.
Use a loop like the following to fetch all pages from the REST API:
listOfPages = List.Generate(
() => FetchPage(null),
(page) => page <> null,
(page) => FetchPage(page)
)

exercising ARP on a page with plenty of items

I have an apex form composed of around 50~ text fields and radio buttons.
I tried to create a page process for automated row processing. It works fine but only if my table has a column for each page item.
even though the page items are plenty, actually the question behind them is the same. So what I really want to perform is to collect those data row by row. i.e.
instead of
1 True False True foo
I'd like to store my data like this
1 True foo
2 True goo
3 False hoo
50 False zoo
Since I couldn't find a way to customize ARP, I decided to do it with some manual work.
However I still have this feeling that my efort is futile. I can't help but thinking there must be some other, wiser solutions than being have to create insert/update statements for entire page. I mean I can't be the only one who came up with this need, right?
Thank you very much in advance.
I hope you can achieve your requirement using APEX_COLLECTION. It stores the data temporarily that are associated with the session of the user currently logged into the application. below is the sample code for your reference.
begin
if not apex_collection.collection_exists('SAMPLE_COLLECTION') then
APEX_COLLECTION.CREATE_OR_TRUNCATE_COLLECTION(p_collection_name => 'SAMPLE_COLLECTION');
apex_collection.add_member(
p_collection_name => 'SAMPLE_COLLECTION',
p_c001 => :P1_TEST_ITEM1,
p_c002 => :P1_TEST_ITEM2,
p_c003 => :P1_TEST_ITEM3,
p_c004 => :P1_TEST_ITEM4,
p_c005 => :P1_TEST_ITEM5,
);
end if;
end;
You can select the data stored using the below SELECT statement.
select c001, c002,c003,c004, c005
from apex_collections where collection_name = 'SAMPLE_COLLECTION'
Please refer this link to explore more on APEX_COLLECTION

dynamodb - scan items where map contains a key

I have a table that contains a field (not a key field), called appsMap, and it looks like this:
appsMap = { "qa-app": "abc", "another-app": "xyz" }
I want to scan all rows whose appsMap contains the key "qa-app" (the value is not important, just the key). I tried something like this but it doesn't work in the way I need:
FilterExpression = '#appsMap.#app <> :v',
ExpressionAttributeNames = {
"#app": "qa-app",
"#appsMap": "appsMap"
},
ExpressionAttributeValues = {
":v": { "NULL": True }
},
ProjectionExpression = "deviceID"
What's the correct syntax?
Thanks.
There is a discussion on the subject here:
https://forums.aws.amazon.com/thread.jspa?threadID=164470
You might be missing this part from the example:
ExpressionAttributeValues: {":name":{"S":"Jeff"}}
However, just wanted to echo what was already being said, scan is an expensive procedure that goes through every item and thus making your database hard to scale.
Unlike with other databases, you have to do plenty of setup with Dynamo in order to get it to perform at it's great level, here is a suggestion:
1) Convert this into a root value, for example add to the root: qaExist, with possible values of 0|1 or true|false.
2) Create secondary index for the newly created value.
3) Make query on the new index specifying 0 as a search parameter.
This will make your system very fast and very scalable regardless of how many records you get in there later on.
If I understand the question correctly, you can do the following:
FilterExpression = 'attribute_exists(#0.#1)',
ExpressionAttributeNames = {
"#0": "appsMap",
"#1": "qa-app"
},
ProjectionExpression = "deviceID"
Since you're not being a bit vague about your expectations and what's happening ("I tried something like this but it doesn't work in the way I need") I'd like to mention that a scan with a filter is very different than a query.
Filters are applied on the server but only after the scan request is executed, meaning that it will still iterate over all data in your table and instead of returning you each item, it applies a filter to each response, saving you some network bandwidth, but potentially returning empty results as you page trough your entire table.
You could look into creating a GSI on the table if this is a query you expect to have to run often.

Multiple access to static data in a django app

I'm building an application and I'm having trouble making a choice about how is the best way to access multiple times to static data in a django app. My experience in the field is close to zero so I could use some help.
The app basically consists in a drag & drop of foods. When you drag a food to a determined place(breakfast for example) differents values gets updated: total breakfast calories, total day nutrients(Micro/Macro), total day calories, ...That's why I think the way I store and access the data it's pretty important performance speaking.
This is an excerpt of the json file I'm currently using:
foods.json
{
"112": {
"type": "Vegetables",
"description": "Mushrooms",
"nutrients": {
"Niacin": {
"unit": "mg",
"group": "Vitamins",
"value": 3.79
},
"Lysine": {
"units": "g",
"group": "Amino Acids",
"value": 0.123
},
... (+40 nutrients)
"amount": 1,
"unit": "cup whole",
"grams": 87.0 }
}
I've thought about different options:
1) JSON(The one I'm currently using):
Every time I drag a food to a "droppable" place, I call a getJSON function to access the food data and then update the corresponding values. This file has a 2mb size, but it surely will increase as I add more foods to it. I'm using this option because it was the most quickest to begin to build the app but I don't think it's a good choice for the live app.
2) RDBMS with normalized fields:
I could create two models: Food and Nutrient, each food has 40+ nutrients related by a FK. The problem I see with this is that every time a food data request is made, the app will hit the db a lot of times to retrieve it.
3) RDBMS with picklefield:
This is the option I'm actually considering. I could create a Food models and put the nutrients in a picklefield.
4) Something with Redis/Django Cache system:
I'll dive more deeply into this option. I've read some things about them but I don't clearly know if there's some way to use them to solve the problem I have.
Thanks in advance,
Mariano.
This is a typical use case for a relational database. More or less normalized form is the proper way most of the time.
I wrote this data model up from the top of my head, according to your example:
CREATE TABLE unit(
unit_id integer PRIMARY KEY
,unit text NOT NULL
,metric_unit text NOT NULL
,atomic_amount numeric NOT NULL
);
CREATE TABLE food_type(
food_type_id integer PRIMARY KEY
,food_type text NOT NULL
);
CREATE TABLE nutrient_type(
nutrient_type_id integer PRIMARY KEY
,nutrient_type text NOT NULL
);
CREATE TABLE food(
food_id serial PRIMARY KEY
,food text NOT NULL
,food_type_id integer REFERENCES food_type(food_type_id) ON UPDATE CASCADE
,unit_id integer REFERENCES unit(unit_id) ON UPDATE CASCADE
,base_amount numeric NOT NULL DEFAULT 1
);
CREATE TABLE nutrient(
nutrient_id serial PRIMARY KEY
,nutrient text NOT NULL
,metric_unit text NOT NULL
,base_amount numeric NOT NULL
,calories integer NOT NULL DEFAULT 0
);
CREATE TABLE food_nutrient(
food_id integer references food (food_id) ON UPDATE CASCADE ON DELETE CASCADE
,nutrient_id integer references nutrient (nutrient_id) ON UPDATE CASCADE
,amount numeric NOT NULL DEFAULT 1
,CONSTRAINT food_nutrient_pkey PRIMARY KEY (food_id, nutrient_id)
);
CREATE TABLE meal(
meal_id serial PRIMARY KEY
,meal text NOT NULL
);
CREATE TABLE meal_food(
meal_id integer references meal(meal_id) ON UPDATE CASCADE ON DELETE CASCADE
,food_id integer references food (food_id) ON UPDATE CASCADE
,amount numeric NOT NULL DEFAULT 1
,CONSTRAINT meal_food_pkey PRIMARY KEY (meal_id, food_id)
);
This is definitely not, how it should work:
every time a food data request is made, the app will hit the db a lot
of times to retrieve it.
You should calculate / aggregate all values you need in a view or function and hit the database only once per request, not many times.
Simple example to calculate the calories of a meal according to the above model:
SELECT sum(n.calories * fn.amount * f.base_amount * u.atomic_amount * mf.amount)
AS meal_calories
FROM meal_food mf
JOIN food f USING (food_id)
JOIN unit u USING (unit_id)
JOIN food_nutrient fn USING (food_id)
JOIN nutrient n USING (nutrient_id)
WHERE mf.meal_id = 7;
You can also use materialized views. For instance, store computed values per food in a table and update it automatically if underlying data changes. Most likely, those rarely change (but are still easily updated this way).
I think the flat file version you are using comes in last place. Every time it is requested it is being read from top to bottom. For the size I think this comes in last place. The cache system would provide the best performance, but the RDBMS would be the easiest to manage/extend, plus your queries will automatically be cached.

How to update a stream with the response from another stream where the sink type is "http-response"

Am trying to enrich my input stream with an additional attribute which gets populated via "http-response" response sink.
I have tried using the "join" with window attribute and with "every" keyword to merge two streams and inserting the resulting merged stream into another stream to enrich it.
The window attributes (window.time(1 sec) or window.length(1)) and "every" keyword works well when the incoming events are coming at a regular interval of 1 sec or more.
When (say for example 10 or 100) events are sent at the same time(within a second). Then the result of the merge is not in expected terms.
The one with "window" attribute (join)
**
from EventInputStreamOne#window.time(1 sec) as i
join EventInputStreamTwo as s
on i.variable2 == s.variable2
select i.variable1 as variable1, i.variable2 as variable2, s.variable2 as variable2
insert into EventOutputStream;
**
The one with the "every" keyword
**
from every e1=EventInputStream,e2=EventResponseStream
select e1.variable1 as variable1, e1.variable2 as variable2, e2.variable3 as variable3
insert into EventOutputStream;
**
Is there any better way to merge the two streams in order to update a third stream?
To get the original request attributes, you can use custom mapping as follows,
#source(type='http-call-response', sink.id='source-1'
#map(type='json',#attributes(name='name', id='id', volume='trp:volume', price='trp:price')))
define stream responseStream(name String, id int, headers String, volume long, price float);
Here, the request attributes can be accessed with trp:attributeName, in this sample only name is from the response, price and volume is from the request.
The syntax in your 'every' keyword approach isn't quite right. Have you tried something like this:
from every (e1 = event1) -> e2=event2[e1.variable == e2.variable]
select e1.variable1, e2.variable1, e2.variable2
insert into outputEvent;
This document might help.