Sales and Music Charts

L3
ModelContextProtocolPostgresChinook

Create monthly sales dashboard and top music charts system for tracking business performance and trending content.

Created by Lingxiao Du
2025-08-12
Reporting And AnalyticsStatistical AggregationSchema Design

Model Ranking

Click on the dots to view the trajectory of each task run
Model
Run Results
Pass@4
Pass^4
Avg Time
Avg Turns
Input Tokens
Output Tokens
Total Tokens
OpenAI
gpt-5
4
/4
107.7s
7.0
19,930
7,530
27,460
Qwen
qwen-3-coder
2
/4
88.9s
19.3
113,618
2,318
115,936
Gemini
gemini-2-5-pro
1
/4
84.8s
6.0
24,783
8,467
33,250
Claude
claude-4-1-opus
0
/1
--
424.2s
29.0
199,197
7,297
206,494
Claude
claude-4-sonnet
0
/4
154.1s
24.3
198,263
4,444
202,707
DeepSeek
deepseek-chat
0
/4
347.3s
29.8
261,914
4,302
266,216
Grok
grok-4
0
/4
91.4s
12.0
-
-
-
MoonshotAI
k2
0
/4
165.9s
26.8
212,775
3,329
216,103
OpenAI
o3
0
/4
67.9s
5.3
14,043
4,043
18,086

Task State

Table "Album" { "AlbumId" int4 [pk, not null] "Title" varchar(160) [not null] "ArtistId" int4 [not null] Indexes { ArtistId [type: btree, name: "IFK_AlbumArtistId"] } } Table "Artist" { "ArtistId" int4 [pk, not null] "Name" varchar(120) } Table "Customer" { "CustomerId" int4 [pk, not null] "FirstName" varchar(40) [not null] "LastName" varchar(20) [not null] "Company" varchar(80) "Address" varchar(70) "City" varchar(40) "State" varchar(40) "Country" varchar(40) "PostalCode" varchar(10) "Phone" varchar(24) "Fax" varchar(24) "Email" varchar(60) [not null] "SupportRepId" int4 Indexes { SupportRepId [type: btree, name: "IFK_CustomerSupportRepId"] } } Table "Employee" { "EmployeeId" int4 [pk, not null] "LastName" varchar(20) [not null] "FirstName" varchar(20) [not null] "Title" varchar(30) "ReportsTo" int4 "BirthDate" timestamp "HireDate" timestamp "Address" varchar(70) "City" varchar(40) "State" varchar(40) "Country" varchar(40) "PostalCode" varchar(10) "Phone" varchar(24) "Fax" varchar(24) "Email" varchar(60) Indexes { ReportsTo [type: btree, name: "IFK_EmployeeReportsTo"] } } Table "Genre" { "GenreId" int4 [pk, not null] "Name" varchar(120) } Table "Invoice" { "InvoiceId" int4 [pk, not null] "CustomerId" int4 [not null] "InvoiceDate" timestamp [not null] "BillingAddress" varchar(70) "BillingCity" varchar(40) "BillingState" varchar(40) "BillingCountry" varchar(40) "BillingPostalCode" varchar(10) "Total" numeric(10,2) [not null] Indexes { CustomerId [type: btree, name: "IFK_InvoiceCustomerId"] } } Table "InvoiceLine" { "InvoiceLineId" int4 [pk, not null] "InvoiceId" int4 [not null] "TrackId" int4 [not null] "UnitPrice" numeric(10,2) [not null] "Quantity" int4 [not null] Indexes { InvoiceId [type: btree, name: "IFK_InvoiceLineInvoiceId"] TrackId [type: btree, name: "IFK_InvoiceLineTrackId"] } } Table "MediaType" { "MediaTypeId" int4 [pk, not null] "Name" varchar(120) } Table "Playlist" { "PlaylistId" int4 [pk, not null] "Name" varchar(120) } Table "PlaylistTrack" { "PlaylistId" int4 [not null] "TrackId" int4 [not null] Indexes { (PlaylistId, TrackId) [type: btree, name: "PK_PlaylistTrack"] TrackId [type: btree, name: "IFK_PlaylistTrackTrackId"] } } Table "Track" { "TrackId" int4 [pk, not null] "Name" varchar(200) [not null] "AlbumId" int4 "MediaTypeId" int4 [not null] "GenreId" int4 "Composer" varchar(220) "Milliseconds" int4 [not null] "Bytes" int4 "UnitPrice" numeric(10,2) [not null] Indexes { AlbumId [type: btree, name: "IFK_TrackAlbumId"] GenreId [type: btree, name: "IFK_TrackGenreId"] MediaTypeId [type: btree, name: "IFK_TrackMediaTypeId"] } } Ref "FK_AlbumArtistId":"Artist"."ArtistId" < "Album"."ArtistId" Ref "FK_CustomerSupportRepId":"Employee"."EmployeeId" < "Customer"."SupportRepId" Ref "FK_EmployeeReportsTo":"Employee"."EmployeeId" < "Employee"."ReportsTo" Ref "FK_InvoiceCustomerId":"Customer"."CustomerId" < "Invoice"."CustomerId" Ref "FK_InvoiceLineInvoiceId":"Invoice"."InvoiceId" < "InvoiceLine"."InvoiceId" Ref "FK_InvoiceLineTrackId":"Track"."TrackId" < "InvoiceLine"."TrackId" Ref "FK_PlaylistTrackPlaylistId":"Playlist"."PlaylistId" < "PlaylistTrack"."PlaylistId" Ref "FK_PlaylistTrackTrackId":"Track"."TrackId" < "PlaylistTrack"."TrackId" Ref "FK_TrackAlbumId":"Album"."AlbumId" < "Track"."AlbumId" Ref "FK_TrackGenreId":"Genre"."GenreId" < "Track"."GenreId" Ref "FK_TrackMediaTypeId":"MediaType"."MediaTypeId" < "Track"."MediaTypeId"

Instruction



Verify

*.py
Python
"""
Verification script for PostgreSQL Task 1: Monthly Sales Dashboard and Music Charts
"""

import os
import sys
import psycopg2
from decimal import Decimal

def rows_match(actual_row, expected_row):
    """
    Compare two rows with appropriate tolerance.
    For Decimal types: allows 0.01 tolerance
    For other types: requires exact match
    """
    if len(actual_row) != len(expected_row):
        return False
    
    for actual, expected in zip(actual_row, expected_row):
        if isinstance(actual, Decimal) and isinstance(expected, Decimal):
            if abs(float(actual) - float(expected)) > 0.01:
                return False
        elif actual != expected:
            return False
    
    return True

def get_connection_params() -> dict:
    """Get database connection parameters."""
    return {
        "host": os.getenv("POSTGRES_HOST", "localhost"),
        "port": int(os.getenv("POSTGRES_PORT", 5432)),
        "database": os.getenv("POSTGRES_DATABASE"),
        "user": os.getenv("POSTGRES_USERNAME"),
        "password": os.getenv("POSTGRES_PASSWORD")
    }

def verify_monthly_sales_results(conn) -> bool:
    """Verify the monthly sales summary results."""
    with conn.cursor() as cur:
        # Get actual results from the created table
        cur.execute("""
            SELECT year_month, total_invoices, total_revenue, 
                   total_tracks_sold, average_invoice_value, unique_customers
            FROM monthly_sales_summary 
            ORDER BY year_month
        """)
        actual_results = cur.fetchall()
        
        # Execute ground truth query
        cur.execute("""
            WITH invoice_metrics AS (
            SELECT
                DATE_TRUNC('month', i."InvoiceDate") AS ym,
                COUNT(*)::INT                       AS total_invoices,
                SUM(i."Total")::DECIMAL             AS total_revenue,
                AVG(i."Total")::DECIMAL             AS average_invoice_value,
                COUNT(DISTINCT i."CustomerId")::INT AS unique_customers
            FROM "Invoice" i
            GROUP BY 1
            ),
            track_metrics AS (         
            SELECT
                DATE_TRUNC('month', i."InvoiceDate") AS ym,
                SUM(il."Quantity")::INT              AS total_tracks_sold
            FROM "Invoice" i
            JOIN "InvoiceLine" il ON il."InvoiceId" = i."InvoiceId"
            WHERE il."Quantity" > 0                
            GROUP BY 1
            )
            SELECT
            TO_CHAR(im.ym, 'YYYY-MM')          AS year_month,
            im.total_invoices,
            im.total_revenue,
            COALESCE(tm.total_tracks_sold, 0)  AS total_tracks_sold,
            im.average_invoice_value,
            im.unique_customers
            FROM invoice_metrics im
            LEFT JOIN track_metrics tm USING (ym)
            ORDER BY year_month;
        """)
        expected_results = cur.fetchall()

        if len(actual_results) != len(expected_results):
            print(f"❌ Expected {len(expected_results)} monthly sales records, got {len(actual_results)}")
            return False

        mismatches = 0
        for i, (actual, expected) in enumerate(zip(actual_results, expected_results)):
            if not rows_match(actual, expected):
                if mismatches < 5:  # Only show first 5 mismatches
                    print(f"❌ Monthly sales row {i+1} mismatch: expected {expected}, got {actual}")
                mismatches += 1

        if mismatches > 0:
            print(f"❌ Total monthly sales mismatches: {mismatches}")
            return False

        print(f"✅ Monthly sales results are correct ({len(actual_results)} records)")
        return True

def verify_music_charts_results(conn) -> bool:
    """Verify the music charts results."""
    with conn.cursor() as cur:
        # Get actual results from the created table
        cur.execute("""
            SELECT chart_type, rank_position, item_id, item_name, total_revenue
            FROM top_music_charts
            ORDER BY chart_type, rank_position
        """)
        actual_results = cur.fetchall()

        # Execute ground truth queries for each chart type
        cur.execute("""
            WITH track_stats AS (
            SELECT
                'top_tracks'::varchar AS chart_type,
                t."TrackId"           AS item_id,
                t."Name"              AS item_name,
                SUM(il."UnitPrice" * il."Quantity")::DECIMAL AS total_revenue,
                SUM(il."Quantity")::INT                      AS total_quantity
            FROM "Track" t
            JOIN "InvoiceLine" il ON il."TrackId" = t."TrackId"
            GROUP BY t."TrackId", t."Name"
            HAVING SUM(il."Quantity") > 0
            ),
            track_ranked AS (
            SELECT
                chart_type, item_id, item_name, total_revenue,
                ROW_NUMBER() OVER (ORDER BY total_quantity DESC, item_name, item_id) AS rank_position
            FROM track_stats
            ),
            album_rev AS (
            SELECT
                'top_albums'::varchar AS chart_type,
                a."AlbumId"           AS item_id,
                a."Title"             AS item_name,
                SUM(il."UnitPrice" * il."Quantity")::DECIMAL AS total_revenue
            FROM "Album" a
            JOIN "Track" t        ON t."AlbumId"  = a."AlbumId"
            JOIN "InvoiceLine" il ON il."TrackId" = t."TrackId"
            GROUP BY a."AlbumId", a."Title"
            HAVING SUM(il."UnitPrice" * il."Quantity") > 0
            ),
            album_ranked AS (
            SELECT
                chart_type, item_id, item_name, total_revenue,
                ROW_NUMBER() OVER (ORDER BY total_revenue DESC, item_name, item_id) AS rank_position
            FROM album_rev
            ),
            artist_rev AS (
            SELECT
                'top_artists'::varchar AS chart_type,
                ar."ArtistId"          AS item_id,
                ar."Name"              AS item_name,
                SUM(il."UnitPrice" * il."Quantity")::DECIMAL AS total_revenue
            FROM "Artist" ar
            JOIN "Album"  a       ON a."ArtistId" = ar."ArtistId"
            JOIN "Track"  t       ON t."AlbumId"  = a."AlbumId"
            JOIN "InvoiceLine" il ON il."TrackId" = t."TrackId"
            GROUP BY ar."ArtistId", ar."Name"
            HAVING SUM(il."UnitPrice" * il."Quantity") > 0
            ),
            artist_ranked AS (
            SELECT
                chart_type, item_id, item_name, total_revenue,
                ROW_NUMBER() OVER (ORDER BY total_revenue DESC, item_name, item_id) AS rank_position
            FROM artist_rev
            )
            SELECT chart_type, rank_position, item_id, item_name, total_revenue
            FROM (
            SELECT * FROM track_ranked  WHERE rank_position <= 10
            UNION ALL
            SELECT * FROM album_ranked  WHERE rank_position <= 10
            UNION ALL
            SELECT * FROM artist_ranked WHERE rank_position <= 10
            ) x
            ORDER BY chart_type, rank_position;
        """)
        expected_results = cur.fetchall()

        if len(actual_results) != len(expected_results):
            print(f"❌ Expected {len(expected_results)} music chart records, got {len(actual_results)}")
            return False

        mismatches = 0
        for i, (actual, expected) in enumerate(zip(actual_results, expected_results)):
            if not rows_match(actual, expected):
                if mismatches < 5:  # Only show first 5 mismatches
                    print(f"❌ Music chart row {i+1} mismatch: expected {expected}, got {actual}")
                mismatches += 1

        if mismatches > 0:
            print(f"❌ Total music chart mismatches: {mismatches}")
            return False

        print(f"✅ Music chart results are correct ({len(actual_results)} records)")
        return True

def main():
    """Main verification function."""
    print("=" * 50)

    # Get connection parameters
    conn_params = get_connection_params()

    if not conn_params["database"]:
        print("❌ No database specified")
        sys.exit(1)

    try:
        # Connect to database
        conn = psycopg2.connect(**conn_params)

        # Verify results
        success = verify_monthly_sales_results(conn) and verify_music_charts_results(conn)

        conn.close()

        if success:
            print("\n🎉 Task verification: PASS")
            sys.exit(0)
        else:
            print("\n❌ Task verification: FAIL")
            sys.exit(1)

    except psycopg2.Error as e:
        print(f"❌ Database error: {e}")
        sys.exit(1)
    except Exception as e:
        print(f"❌ Verification error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()