Alright, one last time.
I don’t have access to the Plex source code but I can tell you it’s extremely likely the “recently added TV” thing works like this:
- Determine which items were added recently
- Take that list of items and fetch metadata
- Generate the display (sounds like an XML file is produced and fed to Plex/Web for this)
The only item that has to change is #1. Most likely, #1 is already just creating a list of IDs, and then those IDs are populated with metadata information.
My suggestion all along has been to adjust only step 1, because that is the only thing that exhibits a problem. It doesn’t require rewriting large amounts of code. It really doesn’t require a ton of discussion about the whole chain of stuff going on, either. The only thing that matters here is the first query that determines which items were added most recently.
For some reason the reaction of people here has been “well what about steps 2 and 3?” Ok? What about them? This is a simple bugfix. Fix the query in step 1. Done. You merging 1 and 2 already with a super complex query? Great! Adjust this query to add your joins and columns. Done.
@random.server said:
@honkdazzle You’re so focused on grouping that you conveniently managed to skip over in your SQL example the point of contention I mentioned in my original rebuttal; plex code returns a single episode or aggregates multiple episodes (or seasons if need be) of the same show.
No, the query I posted solved that problem. I don’t think you ever ran it. One thing I did notice was missing was season grouping, but only because I thought Plex deprecated it. I fixed it below.
Pull x number of most recent episodes and corresponding date based on a shows season (to keep it simple), dump that into a temp table (id,parent_id,date,season_items(1)). Use a basic aggregation query for seasons like you so graciously typed out for us that’s limited to the max min date range in previous query, update previous temp table’s season_items. Pull the ID or parent id from the table based on whether season_item>1. Apply those consolidated ID’s into another query to feed the XML parser.
This is not helpful – neither your solution nor your attitude. First you didn’t understand or actually test/try what I did, then you came up with a weird and convoluted solution instead of just using the tools you’re provided in the language.
I may have not been clear enough since I only included “series ID” as a column in the example, and I also realized Plex does in fact still bucketize by individual season if only episodes from that season were recently added, so I’m updating it one last time. The rest is just fetching metadata. And once again a properly designed piece of software isn’t going to go and write complicated custom joins on every query to do a common task; it’s going to generate a single list of IDs and then fetch all the metadata at once, so don’t ramble on about how I am not thinking about the metadata part.
“But the page has to be rendered in a browser! You are wrong because you didn’t write me a web browser too!”
Also note there’s a lot of extra stuff here to help you understand what’s going on. This can be pared down considerably; the type column for example is purely visual. Still fast though and covers everything. Unfortunately the formatting is being eaten by the forums but you can copy/paste this into your editor of choice to clean it up.
select
(case when new_seasons > 1 then series_id else (case when new_items > 1 then season_id else episode_id end) end) as item_id,
(case when new_seasons > 1 then ‘multi-season’ else (case when new_items > 1 then ‘single-season’ else ‘single-episode’ end) end) as type,
new_items,new_seasons,last_created_at,title from (
select st.title as title,
count(1) as new_items,
count(distinct ss.id) as new_seasons,
max(i.created_at) as last_created_at,
i.id as episode_id,
ss.id as season_id,
ss.parent_id as series_id
from
metadata_items i
join metadata_items ss on ss.id=i.parent_id
join metadata_items st on st.id=ss.parent_id
where i.parent_id not null and
i.library_section_id=2 and
i.metadata_type=4 and
i.created_at > datetime(‘now’, ‘-7 days’)
group by series_id
order by last_created_at desc
);
Here’s the output on a sample server:
For comparison here is that server’s “Recently Added TV” dash view:
They match perfectly, with the exception of Adventure Time because it was victim to the episode cutoff (there are two new episodes in the last 7 days, not one). You can also see that Girls was grouped into a series, whereas Clarence was properly grouped into only one season. But the important thing is that this query doesn’t limit by some arbitrary number of episodes like Plex is doing now. It’s purely by time (last 7 days) so it won’t run into display issues from episode starvation caused by bulk imports.
I don’t think I have it in me to explain any further man. I was just really insulted by your disrespectful know-it-all attitude. Also your off-the-cuff idea of a solution is inefficient and doesn’t solve the same problem. I hope this is useful to someone. Plex engs, easy fix if you just mess with your query and do a group by.
I’ll leave you with this quote, brought to you by Storage Spaces:
@honkdazzle you don’t know enough Sql or coding in general if you think that is the solution. To do it in one call, be able to group the resulting data into sets, and show the most recent episode if there was no sets; they’d have to just query a number (how many would probably be dependant on speed) of items, then reduce that number by matching a similar metric (seasons, show).