-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from Facts-and-Files/fix/calculateMilesCorrectly
fix: calculate miles the same for user stats and campaign stats
- Loading branch information
Showing
5 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/database/migrations/2024_10_04_111700_create_anew_user_stats_view.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
// delete old view first | ||
DB::statement('DROP VIEW IF EXISTS user_stats_view'); | ||
|
||
DB::statement(' | ||
CREATE VIEW user_stats_view AS | ||
SELECT | ||
s.UserId, | ||
COUNT(DISTINCT s.ItemId) AS Items, | ||
SUM(CASE WHEN s.ScoreTypeId = 1 THEN s.Amount ELSE 0 END) AS Locations, | ||
SUM(CASE WHEN s.ScoreTypeId = 2 THEN s.Amount ELSE 0 END) AS ManualTranscriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 3 THEN s.Amount ELSE 0 END) AS Enrichments, | ||
SUM(CASE WHEN s.ScoreTypeId = 4 THEN s.Amount ELSE 0 END) AS Descriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 5 THEN s.Amount ELSE 0 END) AS HTRTranscriptions, | ||
SUM(s.Amount * st.Rate) AS Miles | ||
FROM | ||
Score s | ||
JOIN | ||
ScoreType st ON s.ScoreTypeId = st.ScoreTypeId | ||
GROUP BY | ||
UserId; | ||
'); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
DB::statement('DROP VIEW IF EXISTS user_stats_view'); | ||
|
||
DB::statement(' | ||
CREATE VIEW user_stats_view AS | ||
SELECT | ||
s.UserId, | ||
COUNT(DISTINCT s.ItemId) AS Items, | ||
SUM(CASE WHEN s.ScoreTypeId = 1 THEN s.Amount ELSE 0 END) AS Locations, | ||
SUM(CASE WHEN s.ScoreTypeId = 2 THEN s.Amount ELSE 0 END) AS ManualTranscriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 3 THEN s.Amount ELSE 0 END) AS Enrichments, | ||
SUM(CASE WHEN s.ScoreTypeId = 4 THEN s.Amount ELSE 0 END) AS Descriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 5 THEN s.Amount ELSE 0 END) AS HTRTranscriptions, | ||
ROUND(SUM(s.Amount * st.Rate) + 0.5, 0) AS Miles | ||
FROM | ||
Score s | ||
JOIN | ||
ScoreType st ON s.ScoreTypeId = st.ScoreTypeId | ||
GROUP BY | ||
UserId; | ||
'); | ||
} | ||
}; |
71 changes: 71 additions & 0 deletions
71
src/database/migrations/2024_10_04_111800_create_anew_campaign_stats_view.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
DB::statement('DROP VIEW IF EXISTS campaign_stats_view'); | ||
|
||
DB::statement(' | ||
CREATE VIEW campaign_stats_view AS | ||
SELECT | ||
c.CampaignId, | ||
sc.StoryId, | ||
s.ItemId, | ||
s.UserId, | ||
SUM(CASE WHEN s.ScoreTypeId = 1 THEN s.Amount ELSE 0 END) AS Locations, | ||
SUM(CASE WHEN s.ScoreTypeId = 2 THEN s.Amount ELSE 0 END) AS ManualTranscriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 3 THEN s.Amount ELSE 0 END) AS Enrichments, | ||
SUM(CASE WHEN s.ScoreTypeId = 4 THEN s.Amount ELSE 0 END) AS Descriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 5 THEN s.Amount ELSE 0 END) AS HTRTranscriptions, | ||
SUM(s.Amount * st.Rate) AS Miles | ||
FROM | ||
Campaign c | ||
JOIN | ||
StoryCampaign sc ON sc.CampaignId = c.CampaignId | ||
JOIN | ||
Item i ON i.StoryId = sc.StoryId | ||
JOIN | ||
Score s ON s.ItemId = i.ItemId AND s.Timestamp >= c.Start AND s.Timestamp <= c.End | ||
JOIN | ||
ScoreType st ON st.ScoreTypeId = s.ScoreTypeId | ||
GROUP BY | ||
c.CampaignId, sc.StoryId, s.ItemId, s.UserId; | ||
'); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
DB::statement('DROP VIEW IF EXISTS campaign_stats_view'); | ||
|
||
DB::statement(' | ||
CREATE VIEW campaign_stats_view AS | ||
SELECT | ||
c.CampaignId, | ||
sc.StoryId, | ||
s.ItemId, | ||
s.UserId, | ||
SUM(CASE WHEN s.ScoreTypeId = 1 THEN s.Amount ELSE 0 END) AS Locations, | ||
SUM(CASE WHEN s.ScoreTypeId = 2 THEN s.Amount ELSE 0 END) AS ManualTranscriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 3 THEN s.Amount ELSE 0 END) AS Enrichments, | ||
SUM(CASE WHEN s.ScoreTypeId = 4 THEN s.Amount ELSE 0 END) AS Descriptions, | ||
SUM(CASE WHEN s.ScoreTypeId = 5 THEN s.Amount ELSE 0 END) AS HTRTranscriptions, | ||
ROUND(SUM(s.Amount * st.Rate) + 0.5, 0) AS Miles | ||
FROM | ||
Campaign c | ||
JOIN | ||
StoryCampaign sc ON sc.CampaignId = c.CampaignId | ||
JOIN | ||
Item i ON i.StoryId = sc.StoryId | ||
JOIN | ||
Score s ON s.ItemId = i.ItemId AND s.Timestamp >= c.Start AND s.Timestamp <= c.End | ||
JOIN | ||
ScoreType st ON st.ScoreTypeId = s.ScoreTypeId | ||
GROUP BY | ||
c.CampaignId, sc.StoryId, s.ItemId, s.UserId; | ||
'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters