Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul public dashboard backend implementation #51

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _pages/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@section('content')
<section class="py-16 px-4 text-center bg-gray-100 dark:bg-slate-900">
<h2 class="text-2xl md:text-3-xl lg:text-4xl font-black text-slate-700 dark:text-slate-200 px-3 my-3">
Statistics
HydePHP Statistics
</h2>
<p class="text-lg prose dark:prose-invert text-center mx-auto">
Data compiled {{ Carbon\Carbon::now() }}.
Expand Down
75 changes: 9 additions & 66 deletions app/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,35 @@
*/
class StatisticsController
{
public object $framework;

public object $views;

public object $linesOfCode;
/** @var object{packagistInstalls: string, githubViews: string, githubClones: string, githubViewsWeekly: string, githubClonesWeekly: string} */
public object $stats;

public int $linesOfCode;

public function __construct()
{
$this->framework = $this->getFrameworkStats();
unset($this->framework->versions);

$this->views = $this->getViewStats();
$this->stats = $this->getStats();

$this->linesOfCode = $this->getLinesOfCode();
}

protected function getFrameworkStats()
protected function getStats()
{
// Mock
// $frameworkStats = '{"downloads":{"total":2497,"monthly":2290,"daily":76},"versions":["dev-master","v0.23.2-beta","v0.23.1-beta","v0.23.0-beta","v0.22.0-beta","v0.21.6-beta","v0.21.5-beta","v0.21.4-beta","v0.21.3-beta","v0.21.2-beta","v0.21.1-beta","v0.21.0-beta","v0.20.0-beta","v0.19.0-beta","v0.18.0-beta","v0.17.0-beta","v0.16.1-beta","v0.16.0-beta","v0.15.0-beta","v0.14.0-beta","v0.13.0-beta","v0.12.0-beta","v0.11.0-beta","v0.10.0-beta","v0.9.0-beta","v0.8.1-beta","v0.8.0-beta","v0.7.5-alpha","v0.7.4-alpha","v0.7.3-alpha","v0.7.2-alpha","v0.7.1-alpha","v0.7.0-alpha","v0.6.2-alpha","v0.6.1-alpha","v0.6.0-alpha","v0.5.3-alpha","v0.5.2-alpha","v0.5.1-alpha","v0.5.0-alpha","v0.4.3-alpha","v0.4.2-alpha","v0.4.1-alpha","v0.4.0-alpha","v0.3.1-alpha","v0.3.0-alpha","dev-update-changelog","dev-refactor-documentation-sidebar-internals","dev-analysis-KZEPOj","dev-analysis-BMOd0g","dev-analysis-OMWvDR","dev-refactor-docs-layout","dev-refactor-script-interactions","dev-analysis-vQEZPr","dev-analysis-L3GZVm","dev-280-feature-hide-the-hyde-install-command-once-it-has-been-run"],"average":"daily","date":"2022-03-21"}';
// $frameworkStats = json_decode($frameworkStats);

// return $frameworkStats;

$response = Http::withHeaders([
'Accept' => 'application/json',
'User-Agent' => 'HydeDocsCI/dev-master (Twitter contact: @CodeWithCaen)',
])->get('https://packagist.org/packages/hyde/framework/stats.json');
])->get('https://analytics.hydephp.com/api/stats');

return $response->object();
}

protected function getViewStats()
protected function getLinesOfCode()
{
$response = Http::withHeaders([
'Accept' => 'application/json',
'User-Agent' => 'HydeDocsCI/dev-master (Twitter contact: @CodeWithCaen)',
])->get('https://git.desilva.se/GitHubAnalyticsExplorer/table.json');

$collection = $response->collect();

$views = new Collection();

foreach ($collection as $item) {
if ($item['type'] === 'traffic/views') {
$views->push((object) $item);
}
}

$views = $views->sortBy('bucket');

$oldest = $views->first();
$newest = $views->last();

// Calculate the number of weeks between the oldest and newest view (the date is string)
$weeks = ((strtotime($newest->bucket) - strtotime($oldest->bucket)) / (60 * 60 * 24 * 7));

$object = new \stdClass();
$object->oldest = $oldest;
$object->newest = $newest;
$object->weeks = $weeks;
$object->views = $views;
$object->weekly = (int) ($views->sum('total') / $weeks);

return $object;
}

protected function getLinesOfCode()
{
try {
$response = Http::withHeaders([
'Accept' => 'application/json',
'User-Agent' => 'HydeDocsCI/dev-master (Twitter contact: @CodeWithCaen)',
])->get('https://tokei.ekzhang.com/b1/github/hydephp/develop');
} catch (\Throwable $th) {
// Return last known count
$object = new \stdClass();
$object->total = 87513;
return $object;
}

$object = new \stdClass();

$object->total = $response->object()->lines;
])->get('https://analytics.hydephp.com/api/stats/lines_of_code');

return $object;
return $response->json('data.total');
}
}
10 changes: 5 additions & 5 deletions resources/views/components/statistics.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="bg-white text-slate-900 p-4 pb-8 rounded-lg shadow-xl h-full relative">
<h3 class="font-bold text-2xl text-center mb-3 pb-3">Total Downloads</h3>
<strong id="counter-framework-downloads-total" class="text-5xl font-extrabold text-transparent bg-clip-text bg-gradient-to-br from-purple-500 to-red-600">
{{ ($statistics->framework->downloads->total) }}
{{ number_format($statistics->stats->data->packagistInstalls) }}
</strong>
<span class="opacity-75 hover:opacity-100 transition-opacity absolute top-4 right-4 cursor-help text-lg" title="Total downloads for the Hyde/Framework package, counted by packagist.org {{ Carbon\Carbon::now() }}">&#9432;</span>
</div>
Expand All @@ -18,18 +18,18 @@
<div class="bg-white text-slate-900 p-4 pb-8 rounded-lg shadow-xl h-full relative">
<h3 class="font-bold text-2xl text-center mb-3 pb-3">Weekly GitHub Views</h3>
<strong id="weekly-views-total" class="text-5xl font-extrabold text-transparent bg-clip-text bg-gradient-to-br from-purple-500 to-red-600">
{{ $statistics->views->weekly }}
{{ number_format($statistics->stats->data->githubViewsWeekly) }}
</strong>
<span class="opacity-75 hover:opacity-100 transition-opacity absolute top-4 right-4 cursor-help text-lg" title="Average weekly data for hydephp/framework, hydephp/hyde, and hydephp/hydefront, with data between {{ $statistics->views->oldest->bucket }} {{ $statistics->views->newest->bucket }}">&#9432;</span>
<span class="opacity-75 hover:opacity-100 transition-opacity absolute top-4 right-4 cursor-help text-lg" title="Average weekly data for hydephp/framework, hydephp/hyde, and hydephp/hydefront, based on the last week.">&#9432;</span>
</div>
</div>
<div class="my-4 px-4 w-full md:w-1/2 xl:w-1/3 max-w-md h-auto">
<div class="bg-white text-slate-900 p-4 pb-8 rounded-lg shadow-xl h-full relative">
<h3 class="font-bold text-2xl text-center mb-3 pb-3">Lines of Code</h3>
<strong id="counter-lines-total" class="text-5xl font-extrabold text-transparent bg-clip-text bg-gradient-to-br from-purple-500 to-red-600">
{{ ($statistics->linesOfCode->total) }}
{{ number_format($statistics->linesOfCode) }}
</strong>
<span class="opacity-75 hover:opacity-100 transition-opacity absolute top-4 right-4 cursor-help text-lg" title="Total lines of code in the Hyde monorepo, counted by tokei {{ Carbon\Carbon::now() }}">&#9432;</span>
<span class="opacity-75 hover:opacity-100 transition-opacity absolute top-4 right-4 cursor-help text-lg" title="Total lines of code in the Hyde monorepo, counted {{ Carbon\Carbon::now() }}">&#9432;</span>
</div>
</div>
</div>
Expand Down
Loading