Update: Improve performance by requesting only data that is needed and parallelising calls to Plex where possible

pull/30/head 2.0.10
Juraj Nyíri 3 years ago
parent c64d5ad94d
commit 1b3d3f8572

@ -18675,12 +18675,7 @@ class Plex {
this.requestTimeout = 10000; this.requestTimeout = 10000;
this.sections = []; this.sections = [];
this.init = async () => { this.init = async () => {
await this.getClients(); await Promise.all([this.getSections(), this.getClients(), this.getServerID()]);
/*
setInterval(() => {
this.getClients();
}, 30000);
*/
}; };
this.getClients = async () => { this.getClients = async () => {
const url = this.authorizeURL(`${this.getBasicURL()}/clients`); const url = this.authorizeURL(`${this.getBasicURL()}/clients`);
@ -18719,6 +18714,9 @@ class Plex {
return this.sections; return this.sections;
}; };
this.getSectionData = async (sectionID) => { this.getSectionData = async (sectionID) => {
return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]);
};
this.getSectionDataWithoutProcessing = async (sectionID) => {
const bulkItems = 50; const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`);
url += `&sort=${this.sort}`; url += `&sort=${this.sort}`;
@ -18759,7 +18757,7 @@ class Plex {
const sections = await this.getSections(); const sections = await this.getSections();
const sectionsRequests = []; const sectionsRequests = [];
lodash.forEach(sections, section => { lodash.forEach(sections, section => {
sectionsRequests.push(this.getSectionData(section.key)); sectionsRequests.push(this.getSectionDataWithoutProcessing(section.key));
}); });
return this.exportSectionsData(await Promise.all(sectionsRequests)); return this.exportSectionsData(await Promise.all(sectionsRequests));
}; };
@ -20954,6 +20952,9 @@ class PlexMeetsHomeAssistant extends HTMLElement {
await this.playController.init(); await this.playController.init();
} }
await this.plex.init(); await this.plex.init();
const plexAllSections = await this.plex.getSections();
const getOnDeck = async () => {
if (this.plex) {
try { try {
const onDeck = await this.plex.getOnDeck(); const onDeck = await this.plex.getOnDeck();
this.data.Deck = onDeck.Metadata; this.data.Deck = onDeck.Metadata;
@ -20966,6 +20967,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
const getContinueWatching = async () => {
if (this.plex) {
try { try {
const continueWatching = await this.plex.getContinueWatching(); const continueWatching = await this.plex.getContinueWatching();
this.data['Continue Watching'] = continueWatching.Metadata; this.data['Continue Watching'] = continueWatching.Metadata;
@ -20978,6 +20983,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
const getWatchNext = async () => {
if (this.plex) {
try { try {
const watchNext = await this.plex.getWatchNext(); const watchNext = await this.plex.getWatchNext();
this.data['Watch Next'] = watchNext.Metadata; this.data['Watch Next'] = watchNext.Metadata;
@ -20990,6 +20999,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
const getRecentyAdded = async () => {
if (this.plex) {
try { try {
const recentlyAdded = await this.plex.getRecentyAdded(); const recentlyAdded = await this.plex.getRecentyAdded();
this.data['Recently Added'] = recentlyAdded.Metadata; this.data['Recently Added'] = recentlyAdded.Metadata;
@ -21015,12 +21028,38 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); }
// eslint-disable-next-line @typescript-eslint/camelcase };
this.data.serverID = serverID; let sectionKey = 0;
lodash.forEach(plexAllSections, (section) => {
if (lodash.isEqual(section.title, this.config.libraryName)) {
sectionKey = section.key;
return false;
}
return true;
});
const loadDataRequests = [];
if (sectionKey) {
loadDataRequests.push(this.plex.getSectionData(sectionKey));
}
if (lodash.isEqual(this.config.libraryName, 'Deck')) {
loadDataRequests.push(getOnDeck());
}
else if (lodash.isEqual(this.config.libraryName, 'Continue Watching')) {
loadDataRequests.push(getContinueWatching());
}
else if (lodash.isEqual(this.config.libraryName, 'Watch Next')) {
loadDataRequests.push(getWatchNext());
}
else if (lodash.isEqual(this.config.libraryName, 'Recently Added')) {
loadDataRequests.push(getRecentyAdded());
}
const [plexSections] = await Promise.all(loadDataRequests);
if (plexSections && sectionKey) {
lodash.forEach(plexSections, section => { lodash.forEach(plexSections, section => {
this.data[section.title1] = section.Metadata; this.data[section.title1] = section.Metadata;
}); });
}
if (this.data[this.config.libraryName] === undefined) { if (this.data[this.config.libraryName] === undefined) {
this.error = `Library name ${this.config.libraryName} does not exist.`; this.error = `Library name ${this.config.libraryName} does not exist.`;
} }

@ -36,12 +36,7 @@ class Plex {
} }
init = async (): Promise<void> => { init = async (): Promise<void> => {
await this.getClients(); await Promise.all([this.getSections(), this.getClients(), this.getServerID()]);
/*
setInterval(() => {
this.getClients();
}, 30000);
*/
}; };
getClients = async (): Promise<Record<string, any>> => { getClients = async (): Promise<Record<string, any>> => {
@ -74,7 +69,7 @@ class Plex {
return this.serverInfo; return this.serverInfo;
}; };
getSections = async (): Promise<any> => { getSections = async (): Promise<Array<Record<string, any>>> => {
if (_.isEmpty(this.sections)) { if (_.isEmpty(this.sections)) {
const url = this.authorizeURL(`${this.getBasicURL()}/library/sections`); const url = this.authorizeURL(`${this.getBasicURL()}/library/sections`);
const sectionsData = await axios.get(url, { const sectionsData = await axios.get(url, {
@ -86,6 +81,10 @@ class Plex {
}; };
getSectionData = async (sectionID: number): Promise<any> => { getSectionData = async (sectionID: number): Promise<any> => {
return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]);
};
private getSectionDataWithoutProcessing = async (sectionID: number): Promise<any> => {
const bulkItems = 50; const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`);
url += `&sort=${this.sort}`; url += `&sort=${this.sort}`;
@ -137,7 +136,7 @@ class Plex {
const sections = await this.getSections(); const sections = await this.getSections();
const sectionsRequests: Array<Promise<any>> = []; const sectionsRequests: Array<Promise<any>> = [];
_.forEach(sections, section => { _.forEach(sections, section => {
sectionsRequests.push(this.getSectionData(section.key)); sectionsRequests.push(this.getSectionDataWithoutProcessing(section.key));
}); });
return this.exportSectionsData(await Promise.all(sectionsRequests)); return this.exportSectionsData(await Promise.all(sectionsRequests));
}; };

@ -319,7 +319,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
await this.playController.init(); await this.playController.init();
} }
await this.plex.init(); await this.plex.init();
const plexAllSections = await this.plex.getSections();
const getOnDeck = async (): Promise<void> => {
if (this.plex) {
try { try {
const onDeck = await this.plex.getOnDeck(); const onDeck = await this.plex.getOnDeck();
this.data.Deck = onDeck.Metadata; this.data.Deck = onDeck.Metadata;
@ -330,7 +333,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
const getContinueWatching = async (): Promise<void> => {
if (this.plex) {
try { try {
const continueWatching = await this.plex.getContinueWatching(); const continueWatching = await this.plex.getContinueWatching();
this.data['Continue Watching'] = continueWatching.Metadata; this.data['Continue Watching'] = continueWatching.Metadata;
@ -341,7 +347,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
const getWatchNext = async (): Promise<void> => {
if (this.plex) {
try { try {
const watchNext = await this.plex.getWatchNext(); const watchNext = await this.plex.getWatchNext();
this.data['Watch Next'] = watchNext.Metadata; this.data['Watch Next'] = watchNext.Metadata;
@ -352,7 +361,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
const getRecentyAdded = async (): Promise<void> => {
if (this.plex) {
try { try {
const recentlyAdded = await this.plex.getRecentyAdded(); const recentlyAdded = await this.plex.getRecentyAdded();
this.data['Recently Added'] = recentlyAdded.Metadata; this.data['Recently Added'] = recentlyAdded.Metadata;
@ -376,13 +388,38 @@ class PlexMeetsHomeAssistant extends HTMLElement {
throw err; throw err;
} }
} }
}
};
let sectionKey: number | false = 0;
_.forEach(plexAllSections, (section: Record<string, any>) => {
if (_.isEqual(section.title, this.config.libraryName)) {
sectionKey = section.key;
return false;
}
return true;
});
const loadDataRequests = [];
if (sectionKey) {
loadDataRequests.push(this.plex.getSectionData(sectionKey));
}
if (_.isEqual(this.config.libraryName, 'Deck')) {
loadDataRequests.push(getOnDeck());
} else if (_.isEqual(this.config.libraryName, 'Continue Watching')) {
loadDataRequests.push(getContinueWatching());
} else if (_.isEqual(this.config.libraryName, 'Watch Next')) {
loadDataRequests.push(getWatchNext());
} else if (_.isEqual(this.config.libraryName, 'Recently Added')) {
loadDataRequests.push(getRecentyAdded());
}
const [plexSections] = await Promise.all(loadDataRequests);
const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); if (plexSections && sectionKey) {
// eslint-disable-next-line @typescript-eslint/camelcase
this.data.serverID = serverID;
_.forEach(plexSections, section => { _.forEach(plexSections, section => {
this.data[section.title1] = section.Metadata; this.data[section.title1] = section.Metadata;
}); });
}
if (this.data[this.config.libraryName] === undefined) { if (this.data[this.config.libraryName] === undefined) {
this.error = `Library name ${this.config.libraryName} does not exist.`; this.error = `Library name ${this.config.libraryName} does not exist.`;

Loading…
Cancel
Save