blob: 5be2bc005c13e2f3d2fd3f2b79433fe7f8fcbf91 [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
31 resultatcoviddescripcio = 'Positiu PCR' AND
32 abscodi in(".implode(",", array_map(function($abs) { return "'".$abs."'"; }, $area["abs"])).")
33 GROUP BY data
34 ORDER BY data ASC
35 LIMIT 50000");
36
37 // Obtenim el nombre d'habitants a les ABS
38 $habitants = habitants($area["abs"]);
39 if ($habitants === null) {
40 echo "[Fatal error] Failed getting population for ".$area["name"].".\n";
41 continue;
42 }
43
44 // Generem les dades
45 $summary = generateSummary($data, $habitants);
46
47 // Les escribim en un fitxer
48 $file = tmpfile();
49 $fileName = stream_get_meta_data($file)['uri'];
50
51 foreach ($summary as $row)
52 fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]."\n");
53
54 // Cridem al gnuplot perquè generi la gràfica
55 shell_exec("gnuplot -c generateCustomGraph.gnu \"".escapeshellcmd($area["name"])."\" \"".escapeshellcmd($area["codename"])."\" \"".escapeshellcmd($fileName)."\"");
56 rename("/tmp/covid19graphgenerator-area-".$area["codename"]."-graph.png", __DIR__."/../output/area-".$area["codename"]."-graph.png");
57 rename("/tmp/covid19graphgenerator-area-".$area["codename"]."-graph.svg", __DIR__."/../output/area-".$area["codename"]."-graph.svg");
58
59 fclose($file);
60 }
61} else {
62 echo "[Warning] The config/customAreas.php file doesn't define the customAreas field.";
63}