update_metadata.php 2.07 KB
<?php
include(realpath(dirname(__FILE__) . "/config.php"));
include(CLASSPATH . "EpnTapMgr.php");
$metadata_json_path = EpnTapDataPath . "metadata.json";

if(!is_dir(EpnTapDataPath)) {
	mkdir(EpnTapDataPath);
}
	
$EpnTapMgr = new EpnTapMgr;

$services = $EpnTapMgr->getServices();
if(array_key_exists('error', $services)) {
	error_log('Can not get services: ' . $services['error']);
	exit(1);
}

$metadata = Array();
foreach($services as $service) {

	$query = "SELECT TOP 100000
		dataproduct_type as dp_type,
		target_class as t_class,
		target_name as t_name,
		COUNT(granule_uid) as nb_res,
		MIN(time_min) as time_min,
		MAX(time_max) as time_max
		FROM " . $service['table_name'] . " GROUP BY dp_type, t_class, t_name";

	$service_results = $EpnTapMgr->request($service['access_url'], $query);
	if(! array_key_exists('error', $service_results)) {
		$current_service_id = $EpnTapMgr->generateServiceId($service);
		$new_metadata_array = array_map('format_metadata', $service_results);

        $old_nb = count($metadata);
		foreach ($new_metadata_array as $new_metadata) {
			$metadata = array_merge($metadata, $new_metadata);
		}
        $new_nb = count($metadata);

        print("Service $current_service_id added " . ($new_nb-$old_nb) . " items on metadata, total = " . $new_nb . ".\n");
	}
}

$servicesJsonFile = fopen($metadata_json_path, "w");
fwrite($servicesJsonFile, json_encode($metadata, JSON_PARTIAL_OUTPUT_ON_ERROR));
fclose($servicesJsonFile);

$json_error = json_last_error();
if($json_error != 0) {
	error_log("json error: " . $json_error);
}

function format_metadata($serv) {
    global $EpnTapMgr, $current_service_id;

	$time_min = $EpnTapMgr->JDTodate($serv['time_min']);
	$time_max = $EpnTapMgr->JDTodate($serv['time_max']);
	$metadata_list = Array();

	foreach (explode('#', $serv['dp_type']) as $dp_type) {
		foreach (explode('#', $serv['t_class']) as $t_class) {
			foreach (explode('#', $serv['t_name']) as $t_name) {
				array_push($metadata_list, Array($dp_type, $t_class, $t_name, $current_service_id, $serv['nb_res'], $time_min, $time_max));
			}
		}
	}

	return $metadata_list;
}
?>