โŒˆโŒ‹ โŽ‡ branch:  freshcode


Artifact [fe378b28b9]

Artifact fe378b28b9493faf675b8d99c528acabb08b473e:

  • File cron.daily/header_stats.php — part of check-in [840066990b] at 2022-06-05 21:30:31 on branch trunk — update to GraphQL query (user: mario size: 3126)

<?php
/**
 * title: statistics
 * description: generate statistics, visitors, projects, autoupdate, etc. for header
 * version: 0.3
 * depends:
 * category: template
 * type: cron
 * x-cron: 11 23,5,16 * * *
 *
 * Approximate weekly numbers by multiplication.
 *
 */

chdir(dirname(__DIR__)); 
include("config.php");


// get CloudFlare visitor stats
// https://developers.cloudflare.com/analytics/graphql-api/migration-guides/zone-analytics/
$ZONE_ID = "e373b910a645ebccd6035d4b38a2fe4c";
$date = new DateTime("");
$date->sub(new DateInterval("P7D"));
$date = $date->format("Y-m-d");
$payload = <<<GRAPHQL
{
  viewer {
    zones(filter: {zoneTag: $ZONE_ID}) {
      httpRequests1dGroups(orderBy: [date_ASC], limit: 100, filter:{date_geq: "$date"}) {
        dimensions {
          date
        }
        sum {
          bytes
          cachedBytes
          cachedRequests
          countryMap {
            bytes
            requests
            threats
            clientCountryName
          }
          encryptedBytes
          encryptedRequests
          pageViews
          requests
          threats
        }
        uniq {
          uniques
        }
      }
    }
  }
}
GRAPHQL;
$json = curl()
   #->url("https://api.cloudflare.com/client/v4/user/tokens/verify")
   ->url("https://api.cloudflare.com/client/v4/graphql")
   ->httpheader([
       "X-Auth-Email: " . CLOUDFLARE_EMAIL,
       "X-Auth-Key: " . CLOUDFLARE_KEY,
       "Authorization: Bearer " . COUDFLARE_TKN2,
       "Content-Type: application/json",
   ])
   ->postfields(json_encode(["query"=>$payload]))
   ->exec();
$stats = json_decode($json);
$stats = $stats->data->viewer->zones[0]->httpRequests1dGroups;
#print_r($stats);
#exit();
$s_visitors = 0;
$s_pageviews = 0;
foreach ($stats as $ds) {
    $s_visitors += $ds->uniq->uniques;
    $s_pageviews += $ds->sum->requests;
}


// number of projects
$s_num_proj = db("SELECT COUNT(name) AS cnt FROM (SELECT name FROM release GROUP BY name)")->cnt;

// releases
$s_num_vers = db("SELECT COUNT(name) AS cnt FROM (SELECT DISTINCT name, version FROM release WHERE version != ?)", "")->cnt;

// autoupdating entries
$s_num_auto = db("SELECT COUNT(name) AS cnt FROM (SELECT name FROM release WHERE autoupdate_module != ? GROUP BY name)", "none")->cnt;
$s_num_auto = round($s_num_auto / $s_num_proj * 100, 1);

// admin infos
$s_flags = db("SELECT COUNT(reason) AS cnt FROM flags")->cnt;
$s_col = $s_flags ? "style=color:red" : "";
$s_spool = count($a_incoming = array_filter(glob("incoming/??*"), "is_file"));

#-- general freshcode stats
file_put_contents("template/stats.htm",
"
      <li> <var>$s_num_proj</var> projects
      <li> <var>$s_num_vers</var> releases
      <li> <var>$s_num_auto%</var> auto updating
      <li> <var>$s_visitors</var> visitors/wk
      <li> <var>$s_pageviews</var> recent pageviews
");

#-- moderator addendum
$a_incoming = join(", ", array_map("basename", $a_incoming));
file_put_contents("template/stats.admin.htm",
"
      <li> <var $s_col>$s_flags</var> <a href='/admin' style=color:grey>flags</a> ยท <span title='$a_incoming'><var>$s_spool</var> incoming</span>
"
);