I love data! For the most part, I enjoy programming not so much for the code itself, but for the fact that code operates on a huge variety of data and variables. I love optimizing data, structuring and logically organizing it. Maybe I should have gone into Data Science, but I know my limitations — I lack a strong mathematical foundation and knowledge of statistical analysis tools
That said, I love watching all kinds of charts, analyzing them, and finding patterns. Take GitLab activity data, for example. It’s only stored for 2–3 years, which is inconvenient for me — I’d like to track activity across years. That’s exactly why I created the following script. Completely primitive code that simply fetches data and converts it to CSV. Run it regularly, and you’ll be able to have such a nice infographic too
Why bother? Well… I can immediately see all 9 years of working at the company (yes, I didn’t come up with the script right away, so the first year is lost forever). I can see when I burned out, when I recovered and came back to the company, overtime periods and vacations. Anyway, it’s just an interesting part of my life
<?php /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
require_once __DIR__.'/vendor/autoload.php';
// const GITLAB_API_URI = ;
// const GITLAB_API_TOKEN = ;
// const GITLAB_USER_ID = ;
try {
$client = new Gitlab\Client();
$client->setUrl(GITLAB_API_URI);
$client->authenticate(GITLAB_API_TOKEN, Gitlab\Client::AUTH_HTTP_TOKEN);
$eventsArray = [];
$page = 1;
$fetched = 0;
while (!isset($events) || !empty($events)) {
$events = $client->users()->events(GITLAB_USER_ID, [
'page' => $page,
'per_page' => 50,
]);
$fetched += \count($events);
foreach ($events as $event) {
$creationDate = new \DateTimeImmutable($event['created_at']);
$createdAt = $creationDate->format('Y-m-d');
$eventsArray[$createdAt] = ($eventsArray[$createdAt] ?? 0) + 1;
unset($creationDate);
}
echo \sprintf('Page: %d', $page).PHP_EOL;
$page++;
}
echo \sprintf('Fetched: %d', $fetched).PHP_EOL;
// \file_put_contents(
// __DIR__.'/events.json',
// \json_encode(\array_map(static function ($value, $key) {
// return ['date' => $key, 'count' => $value];
// }, \array_values($eventsArray), \array_keys($eventsArray)))
// );
$eventsCSV = 'date,value'.PHP_EOL;;
foreach ($eventsArray as $date => $count) {
$eventsCSV .= $date.','.$count.PHP_EOL;
}
\file_put_contents('events_fetched.csv', $eventsCSV);
return 0;
} catch (\Throwable $throwable) {
echo \sprintf('Error: %s', $throwable->getMessage());
return 1;
}
{
"type": "project",
"license": "CC-BY-4.0",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=7.4",
"ext-json": "*",
"m4tthumphrey/php-gitlab-api": "^11.4",
"guzzlehttp/guzzle": "^7.2",
"http-interop/http-factory-guzzle": "^1.0"
}
}