update_metadata.php
2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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;
}
?>