Commit Graph

286 Commits

Author SHA1 Message Date
Claudio Ortolina a265f2ae52 Address Credo warnings 2025-09-16 12:57:03 +03:00
Claudio Ortolina dae334b1b7 Scrobbled tracks CRUD
- Failing tests
- Warnings
2025-09-16 12:45:36 +03:00
Claudio Ortolina 6c98b164eb Update more MusicBrainz fixtures 2025-09-15 14:31:55 +03:00
Claudio Ortolina b2283a3e5e Fix fixture path 2025-09-15 14:20:43 +03:00
Claudio Ortolina 2ef4eb2d2e Refresh fixtures 2025-09-15 14:02:53 +03:00
Claudio Ortolina 1b89f244f8 Change test to avoid timing-related failures
When inserting twice, the second insertion is a no-op, due to the fact
that we use `on_conflict: :nothing` in the `Repo.insert/2` call.

This means that for fields generated client side like timestamps we
don't get the stored values, but the client side values (which haven't
been persisted).

In case of the test crossing the second boundary, the second asset would
be different because of the timestamps, and the test would fail.

This test is better. It tests that no matter how many times we insert
the same thing we always have one stored.
2025-09-15 07:42:03 +03:00
Claudio Ortolina 57deea2f4c Use healthcheck to test deployment is up 2025-09-15 07:28:42 +03:00
Claudio Ortolina ecb830b2fd Render [[Example]] links as links to the collection search 2025-09-13 15:35:02 +03:00
Claudio Ortolina 5aaa7708ee Add tests for On This Day functionality 2025-09-10 17:52:11 +03:00
Claudio Ortolina 0cb46e4ec7 Capture warning location 2025-09-04 08:31:44 +03:00
Claudio Ortolina 408899747e Add module to format numbers 2025-09-02 08:26:59 +03:00
Claudio Ortolina dc77d337c0 Migrate from embedded covers to assets 2025-09-01 16:20:41 +03:00
Claudio Ortolina cbf567a502 Support assets upserts 2025-09-01 15:06:00 +03:00
Claudio Ortolina bd12454884 Serve covers from assets 2025-09-01 14:44:17 +03:00
Claudio Ortolina 49c3a5cba2 Serve images from assets table 2025-09-01 13:46:32 +03:00
Claudio Ortolina db843bf061 Add convenience to store images 2025-09-01 11:59:16 +03:00
Claudio Ortolina a605610c9d Add base infra to store assets 2025-09-01 11:59:16 +03:00
Claudio Ortolina df4ca83aeb Add ability to rotate dominant colors 2025-08-27 21:11:13 +03:00
Claudio Ortolina c20e901d21 Show total results inside add record modal 2025-08-27 09:49:18 +03:00
Claudio Ortolina a934fad58d Setup CI healthcheck after deploy 2025-08-24 20:16:48 +03:00
Claudio Ortolina eec746e4ab Fix prod tests (Coolify returns a 302 for http->https) 2025-08-24 19:54:09 +03:00
Claudio Ortolina 1ca1dcf0f3 Fix signature of LastFm.Feed.update/1 2025-08-11 11:59:44 +03:00
Claudio Ortolina 64fdea7e9d Use LazyHTML to escape markup in test 2025-08-05 09:06:58 +03:00
Claudio Ortolina f37719f272 Rename ScrobbleRules.Worker to Worker.ApplyScrobbleRules 2025-07-07 20:14:32 +01:00
Claudio Ortolina aee08cc2bb Use Enum for scrobble rule type 2025-07-04 14:23:19 +01:00
Claudio Ortolina 93e10bc5d9 Remove excessively defensive coding 2025-07-03 21:18:21 +01:00
Claudio Ortolina b2bc5f07f7 Remove unnecessary code 2025-07-03 21:10:59 +01:00
Claudio Ortolina 024ba38b63 Use Ecto.UUID instead of hand-rolled type 2025-07-03 21:07:42 +01:00
Claudio Ortolina d91685b646 First rough implementation of scrobble rules (Claude)
This plan outlines the implementation of functionality to manage
arbitrary rules for fixing data in the `scrobbled_tracks` table. The
system will support two types of transformations:

1. **Album Rule**: Update the album MusicBrainz ID of all tracks
belonging to a specific album (identified by album title)
2. **Artist Rule**: Update the artist MusicBrainz ID of all tracks
belonging to a specific artist (identified by artist name)

- `scrobbled_tracks` table exists with the following structure:
  - `scrobbled_at_uts` (integer, primary key)
  - `musicbrainz_id` (string) - track MusicBrainz ID
  - `title` (string) - track title
  - `cover_url` (string)
  - `scrobbled_at_label` (string)
  - `artist` (map) - embedded artist data with `musicbrainz_id` and
`name`
  - `album` (map) - embedded album data with `musicbrainz_id` and
`title`
  - `last_fm_data` (map) - raw Last.fm API response

- `LastFm.Track` - Schema for scrobbled tracks
- `LastFm.Artist` - Embedded artist schema
- `LastFm.Album` - Embedded album schema

- Main navigation has Stats, Collection, Wishlist sections
- "More" dropdown contains development tools and utilities
- Uses Phoenix LiveView for interactive components

**File**:
`priv/repo/migrations/YYYYMMDDHHMMSS_create_scrobble_rules.exs`

Create `scrobble_rules` table with:

- `id` (primary key)
- `type` (string) - "album" or "artist"
- `match_value` (string) - album title or artist name to match
- `target_musicbrainz_id` (string) - MusicBrainz ID to set
- `enabled` (boolean) - whether rule is active
- `description` (text) - optional description for the rule
- `inserted_at` (timestamp)
- `updated_at` (timestamp)

Indexes:

- `type, match_value` (compound index for efficient matching)
- `enabled` (for filtering active rules)

**File**: `lib/music_library/scrobble_rules/scrobble_rule.ex`

Create Ecto schema with:

- Field definitions matching migration
- Validations:
  - `type` must be "album" or "artist"
  - `match_value` required and non-empty
  - `target_musicbrainz_id` required and non-empty
  - `enabled` defaults to true
- Changeset functions for create/update operations

**File**: `lib/music_library/scrobble_rules.ex`

Implement context functions:

- `list_scrobble_rules(opts \\ [])` - list all rules with optional
filtering
- `get_scrobble_rule!(id)` - get specific rule by ID
- `create_scrobble_rule(attrs)` - create new rule
- `update_scrobble_rule(rule, attrs)` - update existing rule
- `delete_scrobble_rule(rule)` - delete rule
- `change_scrobble_rule(rule, attrs \\ %{})` - create changeset
- `list_enabled_rules()` - get only enabled rules for worker
- `apply_album_rule(rule)` - apply album transformation
- `apply_artist_rule(rule)` - apply artist transformation
- `apply_all_rules()` - apply all enabled rules

**File**: `lib/music_library_web/components/layouts/app.html.heex`

Add new navigation link in the "More" dropdown:

- Icon: `hero-adjustments-horizontal`
- Label: "Scrobble Rules"
- Route: `/scrobble-rules`
- Position: After "Oban" link, before separator

**File**: `lib/music_library_web/router.ex`

Add new LiveView routes in the authenticated scope:

- `/scrobble-rules` - index page
- `/scrobble-rules/new` - create new rule
- `/scrobble-rules/:id/edit` - edit existing rule

**File**: `lib/music_library_web/live/scrobble_rules_live/index.ex`

Implement LiveView with:

- List view showing all rules with status indicators
- Create/Edit forms with validation
- Delete confirmation
- Filter controls (by type, enabled/disabled)
- Manual rule application triggers
- Real-time updates via PubSub

**Template**:
`lib/music_library_web/live/scrobble_rules_live/index.html.heex`

- Table layout with rule details
- Action buttons (Edit, Delete, Apply)
- Form components for create/edit
- Status indicators for enabled/disabled rules

**File**:
`lib/music_library_web/live/scrobble_rules_live/form_component.ex`

Create reusable form component:

- Type selection (album/artist)
- Match value input with validation
- Target MusicBrainz ID input
- Description textarea
- Enable/disable toggle
- Save/Cancel actions

Where possible, use the components available at
<https://docs.fluxonui.com/1.1.4/overview.html>.

**File**: `lib/music_library/scrobble_rules/worker.ex`

Implement Oban worker:

- Scheduled to run periodically (e.g., every 30 minutes)
- Fetches all enabled rules
- Applies each rule in sequence
- Logs application results
- Handles errors gracefully
- Tracks last application time

**File**: `test/music_library/scrobble_rules_test.exs`

- Test all context functions
- Test rule application logic
- Test edge cases and error handling

**File**: `test/music_library/scrobble_rules/scrobble_rule_test.exs`

- Test schema validations
- Test changeset functions
- Test constraint validations

**File**: `test/music_library_web/live/scrobble_rules_live_test.exs`

- Test LiveView interactions
- Test form submissions
- Test real-time updates
- Test navigation and routing

**File**: `test/music_library/scrobble_rules/worker_test.exs`

- Test job execution
- Test rule application
- Test error handling
- Test scheduling

- Ensure atomic operations when applying rules
- Validate MusicBrainz IDs before applying
- Log all transformations for audit trail

- Index optimization for rule matching
- Batch processing for large datasets
- Rate limiting for external API calls

- Clear feedback on rule application
- Preview mode to show what would change
- Bulk operations for managing multiple rules
- Export/import functionality for rule sets

- Input validation and sanitization
- Rate limiting on rule creation
- Audit logging for all changes

```sql
-- scrobble_rules table
CREATE TABLE scrobble_rules (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  type TEXT NOT NULL CHECK (type IN ('album', 'artist')),
  match_value TEXT NOT NULL,
  target_musicbrainz_id TEXT NOT NULL,
  enabled BOOLEAN NOT NULL DEFAULT TRUE,
  description TEXT,
  inserted_at TIMESTAMP NOT NULL,
  updated_at TIMESTAMP NOT NULL
);

CREATE INDEX idx_scrobble_rules_type_match ON scrobble_rules(type,
match_value);
CREATE INDEX idx_scrobble_rules_enabled ON scrobble_rules(enabled);
```

```
lib/music_library/
├── scrobble_rules/
│   ├── scrobble_rule.ex          # Schema
│   └── worker.ex                 # Oban worker
└── scrobble_rules.ex             # Context

lib/music_library_web/
└── live/
    └── scrobble_rules_live/
        ├── index.ex              # LiveView
        ├── index.html.heex       # Template
        └── form_component.ex     # Form component

priv/repo/migrations/
└── YYYYMMDDHHMMSS_create_scrobble_rules.exs

test/
├── music_library/
│   ├── scrobble_rules_test.exs
│   └── scrobble_rules/
│       ├── scrobble_rule_test.exs
│       └── worker_test.exs
└── music_library_web/
    └── live/
        └── scrobble_rules_live_test.exs
```

1. Database migration and schema
2. Context module with basic CRUD operations
3. LiveView interface for rule management
4. Navigation and routing updates
5. Periodic worker implementation
6. Comprehensive testing
7. Documentation and deployment

This plan ensures a robust, maintainable solution that integrates
seamlessly with the existing Music Library application architecture.
2025-07-03 16:47:08 +01:00
Claudio Ortolina 08bc50d5dd Use tabs for scrobble activity toggle 2025-06-25 12:05:23 +03:00
Claudio Ortolina 03e4c6195b Update dependencies
db_connection 2.7.0 => 2.8.0
ecto 3.13.1 => 3.13.2
ecto_sql 3.13.1 => 3.13.2
ecto_sqlite3 0.20.0 => 0.21.0 (minor)
phoenix_ecto 4.6.4 => 4.6.5
req 0.5.11 => 0.5.12

Restores old form of last_fm_test which had changed to accomodate a
change of default of Req.
2025-06-24 20:19:12 +03:00
Claudio Ortolina c53d09dd24 Update dependencies
req 0.5.10 => 0.5.11

Requires a change in test logic due to automatic reading of body.
2025-06-23 16:59:14 +03:00
Claudio Ortolina adc44e056e Reduce data size to speedup test
Suggested and executed by Claude
2025-06-14 21:15:26 +03:00
Claudio Ortolina 07d9db6011 Speed up tests by caching cover data
Suggested and executed by Claude
2025-06-14 21:15:06 +03:00
Claudio Ortolina 1d78f6c4dc Make test faster by deterministically reducing size 2025-06-14 20:04:09 +03:00
Claudio Ortolina c5e47cbc8c Display dominant colors from the db 2025-06-07 20:07:23 +01:00
Claudio Ortolina fd997e853f Update artists when refreshing MusicBrainz data 2025-06-04 15:57:25 +01:00
Claudio Ortolina 27c1cc0eda Import artist joinphrase 2025-06-03 18:16:47 +01:00
Claudio Ortolina 46681deca9 Improve track refresh logic 2025-06-02 21:00:44 +01:00
Claudio Ortolina d7060697b2 Format dates in release summary and label 2025-06-01 16:58:04 +01:00
Claudio Ortolina 1703f1cf70 Use stored scrobbled tracks 2025-05-31 21:44:54 +01:00
Claudio Ortolina 06511f89db Cache Last.fm tracks raw data when parsing api response 2025-05-30 16:52:07 +01:00
Claudio Ortolina ae34909c1f Apply quokka single_node rule 2025-05-28 19:50:06 +01:00
Claudio Ortolina 676c2e4290 Enable quokka with :module_directives rule 2025-05-28 19:41:57 +01:00
Claudio Ortolina cbf12e6026 Add documentation for LastFm.Session parsing 2025-05-26 23:24:31 +01:00
Claudio Ortolina 1298d711a3 Remove unnecessary selected_release_label/1 2025-05-25 08:56:24 +01:00
Claudio Ortolina b44693b022 Move Artist and ArtistInfo under Artists context 2025-05-23 20:11:57 +01:00
Claudio Ortolina 80ca186ba8 Render artist bio in a left-side sheet 2025-05-21 21:24:15 +01:00
Claudio Ortolina cc3cf02490 Show artist MusicBrainz ID (with link and copy button) 2025-05-15 16:59:12 +01:00
Claudio Ortolina f21a289008 Load release when opening collection show page
Includes small fixes to make both logic and tests more robust
2025-05-11 11:04:28 +01:00