Working prototype with cache

This commit is contained in:
aunefyren 2021-10-05 10:38:06 +02:00
parent a595f139f9
commit c702e44502

View file

@ -69,6 +69,8 @@ if($config->use_cache) {
} else { } else {
$tautulli_data = array(); $tautulli_data = array();
} }
} else {
$tautulli_data = array();
} }
// UPDATE THE CACHE // UPDATE THE CACHE
@ -224,10 +226,12 @@ function check_cache() {
global $id; global $id;
$path = "../config/cache.json"; $path = "../config/cache.json";
if(!file_exists($path)) { if(!file_exists($path)) {
fopen($path, "w"); fopen($path, "w");
} }
$cache = json_decode(file_get_contents($path));
$cache = json_decode(file_get_contents($path), True);
if(!empty($cache)) { if(!empty($cache)) {
return $cache; return $cache;
@ -238,27 +242,7 @@ function check_cache() {
function update_cache($result) { function update_cache($result) {
global $config; global $config;
$cache = json_decode(file_get_contents("../config/cache.json")); $save = json_encode($result);
$decode_result = json_decode($result);
$found = False;
if(!empty($cache)) {
for($i = 0; $i < count($cache); $i++) {
if($cache[$i]->user->id == $decode_result->user->id && !$found) {
$cache[$i] = $decode_result;
$found = True;
break;
}
}
} else {
$cache = array();
}
if(!$found) {
array_push($cache, $decode_result);
}
$save = json_encode($cache);
file_put_contents("../config/cache.json", $save); file_put_contents("../config/cache.json", $save);
return True; return True;
} }
@ -284,6 +268,9 @@ function log_activity($id, $message) {
} }
function tautulli_get_wrapped_dates($id, $array) { function tautulli_get_wrapped_dates($id, $array) {
$time_start = microtime(true);
global $connection; global $connection;
global $config; global $config;
global $arrContextOptions; global $arrContextOptions;
@ -294,41 +281,68 @@ function tautulli_get_wrapped_dates($id, $array) {
for ($i = 0; $loop_time <= $end_loop_date; $i++) { for ($i = 0; $loop_time <= $end_loop_date; $i++) {
$current_loop_date = date('Y-m-d', $loop_time); $current_loop_date = date('Y-m-d', $loop_time);
$now = new DateTime('NOW');
$then = new DateTime($current_loop_date);
if($then > $now) {
break;
}
$found_date = False;
for($j = 0; $j < count($array); $j++) { for($j = 0; $j < count($array); $j++) {
if($array[$j]["date"] == $current_loop_date) { if($array[$j]["date"] == $current_loop_date) {
continue; $found_date = True;
break;
} }
} }
if($found_date) {
continue;
}
$url = $connection . "/api/v2?apikey=" . $config->tautulli_apikey . "&cmd=get_history&order_column=date&order_dir=desc&include_activity=0&length=" . $config->tautulli_length . "&start_date=" . $current_loop_date; $url = $connection . "/api/v2?apikey=" . $config->tautulli_apikey . "&cmd=get_history&order_column=date&order_dir=desc&include_activity=0&length=" . $config->tautulli_length . "&start_date=" . $current_loop_date;
if($config->ssl) { if($config->ssl) {
$response = json_decode(file_get_contents($url, false, stream_context_create($arrContextOptions))); $response = json_decode(file_get_contents($url, false, stream_context_create($arrContextOptions)), True);
} else { } else {
$response = json_decode(file_get_contents($url)); $response = json_decode(file_get_contents($url), True);
} }
$temp = $response->response->data->data; $temp = $response["response"]["data"]["data"];
if(!empty($temp)) { $temp_clean = array();
for($j = 0; $j < count($temp); $j++) { for($j = 0; $j < count($temp); $j++) {
if($temp[$j]->media_type == "movie" || $temp[$j]->media_type == "episode" || $temp[$j]->media_type == "track") { if($temp[$j]["media_type"] == "movie" || $temp[$j]["media_type"] == "episode" || $temp[$j]["media_type"] == "track") {
array_push($array, array("date" => $current_loop_date, "data" => $temp[$j])); array_push($temp_clean, $temp[$j]);
}
} }
} }
array_push($array, array("date" => $current_loop_date, "data" => $temp_clean));
$loop_time += 86400; $loop_time += 86400;
} }
// Sort data by date // Sort data by date
$date = array_column($array, 'date'); $date = array_column($array, 'date');
array_multisort($date, SORT_DESC, $array); array_multisort($date, SORT_ASC, $array);
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;
//execution time of the script
echo '<b>Data Downloading Execution Time:</b> '.$execution_time.' Mins';
// if you get weird results, use number_format((float) $execution_time, 10)
return $array; return $array;
} }
function data_get_user_stats_loop($id, $array_data) { function data_get_user_stats_loop($id, $array) {
$time_start = microtime(true);
global $connection;
global $config;
global $arrContextOptions;
$movies = array(); $movies = array();
$movies_percent_complete = array(); $movies_percent_complete = array();
@ -337,26 +351,27 @@ function data_get_user_stats_loop($id, $array_data) {
for ($i = 0; $i < count($array); $i++) { for ($i = 0; $i < count($array); $i++) {
for($d = 0; $d < count($array->data); $d++) { for($d = 0; $d < count($array[$i]["data"]); $d++) {
if($config->get_user_movie_stats && $array[$i]->data[$d]->media_type == "movie" && $array[$i]->data[$d]->user_id == $id) { if($config->get_user_movie_stats && $array[$i]["data"][$d]["media_type"] == "movie" && $array[$i]["data"][$d]["user_id"] == $id) {
if($array[$i]->data[$d]->date > $config->wrapped_end) { if($array[$i]["data"][$d]["date"] > $config->wrapped_end) {
continue; continue;
} else if ($array[$i]->data[$d]->date < $config->wrapped_start) { } else if ($array[$i]["data"][$d]["date"] < $config->wrapped_start) {
break; break;
} }
$duration = $array[$i]->data[$d]->duration; $duration = $array[$i]["data"][$d]["duration"];
$percent_complete = $array[$i]["data"][$d]["percent_complete"];
if($duration > 300) { if($duration > 300) {
array_push($movies_percent_complete, $array[$i]->data[$d]->percent_complete); array_push($movies_percent_complete, $percent_complete);
} }
$title = $array[$i]->data[$d]->full_title; $title = $array[$i]["data"][$d]["full_title"];
$year = $array[$i]->data[$d]->year; $year = $array[$i]["data"][$d]["year"];
$percent_complete = $array[$i]->data[$d]->percent_complete; $percent_complete = $array[$i]["data"][$d]["percent_complete"];
$paused_counter = $array[$i]->data[$d]->paused_counter; $paused_counter = $array[$i]["data"][$d]["paused_counter"];
$found = False; $found = False;
@ -375,15 +390,15 @@ function data_get_user_stats_loop($id, $array_data) {
} }
} }
if($config->get_user_show_stats && $array[$i]->data[$d]->media_type == "episode" && $array[$i]->data[$d]->user_id == $id) { if($config->get_user_show_stats && $array[$i]["data"][$d]["media_type"] == "episode" && $array[$i]["data"][$d]["user_id"] == $id) {
if($array[$i]->data[$d]->date > $config->wrapped_end) { if($array[$i]["data"][$d]["date"] > $config->wrapped_end) {
continue; continue;
} else if ($array[$i]->data[$d]->date < $config->wrapped_start) { } else if ($array[$i]["data"][$d]["date"] < $config->wrapped_start) {
break; break;
} }
$title = $array[$i]->data[$d]->grandparent_title; $title = $array[$i]["data"][$d]["grandparent_title"];
$duration = $array[$i]->data[$d]->duration; $duration = $array[$i]["data"][$d]["duration"];
$found = False; $found = False;
for ($j = 0; $j < count($shows); $j++) { for ($j = 0; $j < count($shows); $j++) {
@ -401,29 +416,38 @@ function data_get_user_stats_loop($id, $array_data) {
} }
} }
if($config->get_user_music_stats && $array[$i]->data[$d]->media_type == "track" && $array[$i]->data[$d]->user_id == $id) { if($config->get_user_music_stats && $array[$i]["data"][$d]["media_type"] == "track" && $array[$i]["data"][$d]["user_id"] == $id) {
if($array[$i]->data[$d]->date > $config->wrapped_end) { if($array[$i]["data"][$d]["date"] > $config->wrapped_end) {
continue; continue;
} else if ($array[$i]->data[$d]->date < $config->wrapped_start) { } else if ($array[$i]["data"][$d]["date"] < $config->wrapped_start) {
break; break;
} }
if($array[$i]->data[$d]->title == "" || $array[$i]->data[$d]->grandparent_title == "" || $array[$i]->data[$d]->parent_title == "") { $title = $array[$i]["data"][$d]["title"];
$parent_title = $array[$i]["data"][$d]["parent_title"];
$grandparent_title = $array[$i]["data"][$d]["grandparent_title"];
$duration = $array[$i]["data"][$d]["duration"];
$year = $array[$i]["data"][$d]["year"];
$rating_key = $array[$i]["data"][$d]["rating_key"];
$parent_rating_key = $array[$i]["data"][$d]["parent_rating_key"];
$grandparent_rating_key = $array[$i]["data"][$d]["grandparent_rating_key"];
if($title == "" || $grandparent_title == "" || $parent_title == "") {
continue; continue;
} }
$found = False; $found = False;
for ($j = 0; $j < count($tracks); $j++) { for ($j = 0; $j < count($tracks); $j++) {
if($array[$i]->data[$d]->title == $tracks[$j]["title"] && $array[$i]->data[$d]->parent_title == $tracks[$j]["parent_title"] && $array[$i]->data[$d]->grandparent_title == $tracks[$j]["grandparent_title"]) { if($title == $tracks[$j]["title"] && $parent_title == $tracks[$j]["parent_title"] && $grandparent_title == $tracks[$j]["grandparent_title"]) {
$tracks[$j]["plays"] = intval($tracks[$j]["plays"]) + 1; $tracks[$j]["plays"] = intval($tracks[$j]["plays"]) + 1;
$tracks[$j]["duration"] = intval($tracks[$j]["duration"]) + $array[$i]->data[$d]->duration; $tracks[$j]["duration"] = intval($tracks[$j]["duration"]) + intval($duration);
break; break;
} }
} }
if(!$found) { if(!$found) {
array_push($tracks, array("title" => $array[$i]->data[$d]->title, "parent_title" => $array[$i]->data[$d]->parent_title, "grandparent_title" => $array[$i]->data[$d]->grandparent_title, "plays" => 1, "duration" => $array[$i]->data[$d]->duration, "year" => $array[$i]->data[$d]->year, "rating_key" => $array[$i]->data[$d]->rating_key, "parent_rating_key" => $array[$i]->data[$d]->parent_rating_key, "grandparent_rating_key" => $array[$i]->data[$d]->grandparent_rating_key)); array_push($tracks, array("title" => $title, "parent_title" => $parent_title, "grandparent_title" => $grandparent_title, "plays" => 1, "duration" => $duration, "year" => $year, "rating_key" => $rating_key, "parent_rating_key" => $parent_rating_key, "grandparent_rating_key" => $grandparent_rating_key));
} }
} }
} }
@ -489,6 +513,15 @@ function data_get_user_stats_loop($id, $array_data) {
$return_music = array(); $return_music = array();
} }
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;
//execution time of the script
echo '<b>Data Processing Execution Time:</b> '.$execution_time.' Mins';
// if you get weird results, use number_format((float) $execution_time, 10)
return array("movies" => $return_movies, "shows" => $return_shows, "music" => $return_music); return array("movies" => $return_movies, "shows" => $return_shows, "music" => $return_music);
} }