File size: 6,522 Bytes
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations

import asyncio
import os
import re
import sqlite3
from collections.abc import Callable
from datetime import UTC, date, datetime, timedelta
from enum import StrEnum
from pathlib import Path


class ResearchTopic(StrEnum):
    ARTISTS_RELEASES = "artists_releases"
    AUDIO_RADIO = "audio_radio"
    VIDEO = "video"
    SHOP_DOWNLOADS = "shop_downloads"
    STUDIO_SERVICES = "studio_services"
    CONTACT_BOOKING = "contact_booking"
    PRIVACY = "privacy"
    OTHER = "other"


class ResearchIntent(StrEnum):
    INFORMATION = "information"
    NAVIGATION = "navigation"
    AVAILABILITY = "availability"
    CONSUME = "consume"
    DOWNLOAD_PURCHASE = "download_purchase"
    CONTACT_COLLABORATE = "contact_collaborate"
    SUPPORT = "support"
    OTHER = "other"


TOPIC_RULES = (
    (ResearchTopic.PRIVACY, ("datenschutz", "privacy", "speichern", "erinnerung", "memory")),
    (
        ResearchTopic.CONTACT_BOOKING,
        ("kontakt", "contact", "booking", "buchen", "einreichen", "email", "zusammenarbeit"),
    ),
    (
        ResearchTopic.STUDIO_SERVICES,
        ("2b studio", "studio", "service", "grafik", "produktion", "mixing", "mastering"),
    ),
    (
        ResearchTopic.SHOP_DOWNLOADS,
        ("shop", "merch", "kaufen", "bestellen", "download", "wav", "mp3"),
    ),
    (ResearchTopic.VIDEO, ("2b tube", "video", "clip", "visual", "ansehen", "anschauen")),
    (
        ResearchTopic.AUDIO_RADIO,
        ("2bfy", "radio", "musik", "song", "anhören", "hoeren", "stream", "playlist"),
    ),
    (
        ResearchTopic.ARTISTS_RELEASES,
        ("artist", "roster", "release", "album", "ep", "track", "evil crow", "ktn", "zmureo"),
    ),
)

INTENT_RULES = (
    (ResearchIntent.SUPPORT, ("fehler", "funktioniert nicht", "problem", "hilfe", "support")),
    (
        ResearchIntent.CONTACT_COLLABORATE,
        ("kontakt", "contact", "booking", "buchen", "einreichen", "mitmachen", "zusammenarbeiten"),
    ),
    (
        ResearchIntent.DOWNLOAD_PURCHASE,
        ("download", "herunterladen", "kaufen", "bestellen", "preis", "kosten"),
    ),
    (
        ResearchIntent.CONSUME,
        ("anhören", "hoeren", "abspielen", "ansehen", "anschauen", "streamen"),
    ),
    (ResearchIntent.NAVIGATION, ("wo finde", "link", "seite", "öffnen", "oeffnen", "hin")),
    (
        ResearchIntent.AVAILABILITY,
        ("verfügbar", "verfuegbar", "wann", "neu", "aktuell", "online", "live"),
    ),
    (ResearchIntent.INFORMATION, ("was", "wer", "wie", "warum", "welche", "info", "erklär")),
)


class ResearchWriteError(RuntimeError):
    pass


def _normalized(text: str) -> str:
    return " " + re.sub(r"[^\wäöüß]+", " ", text.casefold()).strip() + " "


def _matches(normalized: str, terms: tuple[str, ...]) -> bool:
    return any(f" {_normalized(term).strip()} " in normalized for term in terms)


def classify_research(text: str) -> tuple[ResearchTopic, ResearchIntent]:
    normalized = _normalized(text)
    topic = next(
        (category for category, terms in TOPIC_RULES if _matches(normalized, terms)),
        ResearchTopic.OTHER,
    )
    intent = next(
        (category for category, terms in INTENT_RULES if _matches(normalized, terms)),
        ResearchIntent.OTHER,
    )
    return topic, intent


def _utc_day() -> date:
    return datetime.now(UTC).date()


SCHEMA = """
CREATE TABLE IF NOT EXISTS research_daily_counts (
    day_utc TEXT NOT NULL,
    dimension TEXT NOT NULL CHECK (dimension IN ('topic', 'intent')),
    category TEXT NOT NULL,
    count INTEGER NOT NULL CHECK (typeof(count) = 'integer' AND count > 0),
    PRIMARY KEY (day_utc, dimension, category),
    CHECK (day_utc GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'),
    CHECK (
        (dimension = 'topic' AND category IN (
            'artists_releases', 'audio_radio', 'video', 'shop_downloads',
            'studio_services', 'contact_booking', 'privacy', 'other'
        )) OR
        (dimension = 'intent' AND category IN (
            'information', 'navigation', 'availability', 'consume',
            'download_purchase', 'contact_collaborate', 'support', 'other'
        ))
    )
) WITHOUT ROWID;
"""


class ResearchStore:
    def __init__(
        self,
        database_path: str,
        retention_days: int = 90,
        utc_day: Callable[[], date] = _utc_day,
    ) -> None:
        self._path = Path(database_path).expanduser().resolve()
        self._retention_days = retention_days
        self._utc_day = utc_day
        self._prepare()

    def _prepare(self) -> None:
        try:
            self._path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
            os.chmod(self._path.parent, 0o700)
            descriptor = os.open(self._path, os.O_CREAT | os.O_WRONLY, 0o600)
            os.close(descriptor)
            os.chmod(self._path, 0o600)
            with sqlite3.connect(self._path) as connection:
                connection.execute("PRAGMA trusted_schema = OFF")
                connection.executescript(SCHEMA)
            os.chmod(self._path, 0o600)
        except (OSError, sqlite3.Error) as error:
            raise ResearchWriteError("Could not initialize anonymous research counters.") from error

    async def increment(self, topic: ResearchTopic, intent: ResearchIntent) -> None:
        try:
            await asyncio.to_thread(self._increment_sync, topic, intent)
        except (OSError, sqlite3.Error) as error:
            raise ResearchWriteError("Could not update anonymous research counters.") from error

    def _increment_sync(self, topic: ResearchTopic, intent: ResearchIntent) -> None:
        current_day = self._utc_day()
        cutoff = current_day - timedelta(days=self._retention_days - 1)
        rows = (("topic", topic.value), ("intent", intent.value))
        with sqlite3.connect(self._path, timeout=5) as connection:
            connection.execute("BEGIN IMMEDIATE")
            connection.execute(
                "DELETE FROM research_daily_counts WHERE day_utc < ?",
                (cutoff.isoformat(),),
            )
            connection.executemany(
                """
                INSERT INTO research_daily_counts (day_utc, dimension, category, count)
                VALUES (?, ?, ?, 1)
                ON CONFLICT(day_utc, dimension, category)
                DO UPDATE SET count = count + 1
                """,
                ((current_day.isoformat(), dimension, category) for dimension, category in rows),
            )
            connection.commit()