blob: a4e942cd782504a7625a5118080203ac2c455581 [file] [log] [blame]
avm99963c3b457a2020-07-21 14:23:15 +02001<?php
2if (php_sapi_name() != "cli")
3 exit();
4
5require_once(__DIR__."/../config/config.php");
6require_once(__DIR__."/includes/generation.php");
7
8// Funció que retorna el nombre d'habitants a les ABS donades via una
9// API de la Generalitat de Catalunya
10function habitants($abs) {
11 $data = query("SELECT sum(poblacio_oficial) AS habitants
12 WHERE
13 abs_codi in(".implode(",", array_map(function($abs) { return "'".$abs."'"; }, $abs)).") AND
14 any = 2020", "ftq4-h9vk");
15
16 return ($data[0]["habitants"] ?? null);
17}
18
19if (isset($conf["customAreas"])) {
20 // A cada ciutat
21 foreach ($conf["customAreas"] as $area) {
22 // Si no hi ha cap ABS configurada no fem res
23 if (count($area["abs"]) == 0) {
24 echo "[Warning] There aren't any ABS configured for ".$area["name"].".\n";
25 continue;
26 }
27
28 // Demanem una llista del nombre de casos cada dia
29 $data = query("SELECT data, sum(numcasos) AS sum_numcasos
30 WHERE
avm99963678022a2021-01-05 16:02:39 +010031 (
32 resultatcoviddescripcio = 'Positiu PCR' OR
33 resultatcoviddescripcio = 'Positiu TAR'
34 ) AND
avm99963c3b457a2020-07-21 14:23:15 +020035 abscodi in(".implode(",", array_map(function($abs) { return "'".$abs."'"; }, $area["abs"])).")
36 GROUP BY data
37 ORDER BY data ASC
38 LIMIT 50000");
39
40 // Obtenim el nombre d'habitants a les ABS
41 $habitants = habitants($area["abs"]);
42 if ($habitants === null) {
43 echo "[Fatal error] Failed getting population for ".$area["name"].".\n";
44 continue;
45 }
46
47 // Generem les dades
48 $summary = generateSummary($data, $habitants);
49
50 // Les escribim en un fitxer
51 $file = tmpfile();
52 $fileName = stream_get_meta_data($file)['uri'];
53
avm9996373e13142020-08-31 01:59:30 +020054 $i = 0;
55 foreach ($summary as $row) {
56 fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]." ".$i."\n");
57 ++$i;
58 }
avm99963c3b457a2020-07-21 14:23:15 +020059
60 // Cridem al gnuplot perquè generi la gràfica
61 shell_exec("gnuplot -c generateCustomGraph.gnu \"".escapeshellcmd($area["name"])."\" \"".escapeshellcmd($area["codename"])."\" \"".escapeshellcmd($fileName)."\"");
62 rename("/tmp/covid19graphgenerator-area-".$area["codename"]."-graph.png", __DIR__."/../output/area-".$area["codename"]."-graph.png");
63 rename("/tmp/covid19graphgenerator-area-".$area["codename"]."-graph.svg", __DIR__."/../output/area-".$area["codename"]."-graph.svg");
64
65 fclose($file);
66 }
67} else {
68 echo "[Warning] The config/customAreas.php file doesn't define the customAreas field.";
69}