-- PostgreSQL database dump
--
-\restrict CceG0LqeHWTFqDb6DQrSpNSpqyV7f4lvN0FfZf1Vzji48NH0WT6mIEhc9x3L4V9
+\restrict pXUPUyLkgUWquVqcqOb0Ffu3EcVPJGROwmyXhgPEICcDG5af5bsnhpnMAO9PMhG
-- Dumped from database version 17.6 (Debian 17.6-0+deb13u1)
-- Dumped by pg_dump version 17.6 (Debian 17.6-0+deb13u1)
);
+--
+-- Name: source_stats; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.source_stats (
+ id integer NOT NULL,
+ source_id integer NOT NULL,
+ ts timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
+ total_domains integer NOT NULL,
+ dead_domains integer NOT NULL
+);
+
+
+--
+-- Name: source_stats_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.source_stats_id_seq
+ AS integer
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: source_stats_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.source_stats_id_seq OWNED BY public.source_stats.id;
+
+
--
-- Name: sources; Type: TABLE; Schema: public; Owner: -
--
ALTER TABLE ONLY public.nameservers ALTER COLUMN id SET DEFAULT nextval('public.nameservers_id_seq'::regclass);
+--
+-- Name: source_stats id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.source_stats ALTER COLUMN id SET DEFAULT nextval('public.source_stats_id_seq'::regclass);
+
+
--
-- Name: sources id; Type: DEFAULT; Schema: public; Owner: -
--
ADD CONSTRAINT reports_pkey PRIMARY KEY (id);
+--
+-- Name: source_stats source_stats_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.source_stats
+ ADD CONSTRAINT source_stats_pkey PRIMARY KEY (id);
+
+
--
-- Name: sources sources_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
CREATE INDEX domains_list_id ON public.domains USING btree (list_id) WHERE (removed_at IS NULL);
+--
+-- Name: domains_list_id_added_at; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX domains_list_id_added_at ON public.domains USING btree (added_at DESC);
+
+
+--
+-- Name: domains_list_id_removed_at; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX domains_list_id_removed_at ON public.domains USING btree (removed_at DESC) WHERE (removed_at IS NOT NULL);
+
+
--
-- Name: domains_search; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX reports_open ON public.reports USING btree (name) WHERE (closed_at IS NULL);
+--
+-- Name: source_stats_unique; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX source_stats_unique ON public.source_stats USING btree (source_id, ts DESC);
+
+
--
-- Name: sources_unique; Type: INDEX; Schema: public; Owner: -
--
ADD CONSTRAINT reports_list_id FOREIGN KEY (list_id) REFERENCES public.lists(id);
+--
+-- Name: source_stats source_stats_source_id; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.source_stats
+ ADD CONSTRAINT source_stats_source_id FOREIGN KEY (source_id) REFERENCES public.sources(id);
+
+
--
-- Name: sources sources_list_id; Type: FK CONSTRAINT; Schema: public; Owner: -
--
-- PostgreSQL database dump complete
--
-\unrestrict CceG0LqeHWTFqDb6DQrSpNSpqyV7f4lvN0FfZf1Vzji48NH0WT6mIEhc9x3L4V9
+\unrestrict pXUPUyLkgUWquVqcqOb0Ffu3EcVPJGROwmyXhgPEICcDG5af5bsnhpnMAO9PMhG
# Store the total number of dead domains
self.dead_domains = self.backend.db.fetch_one(stmt)
+
+ # Store the stats history
+ self.backend.db.insert(
+ SourceStats,
+ source = self,
+ total_domains = self.total_domains,
+ dead_domains = self.dead_domains,
+ )
+
+
+class SourceStats(sqlmodel.SQLModel, table=True):
+ __tablename__ = "source_stats"
+
+ # ID
+ id: int = sqlmodel.Field(primary_key=True, exclude=True)
+
+ # Source ID
+ source_id: int = sqlmodel.Field(foreign_key="sources.id", exclude=True)
+
+ # Source
+ source: "Source" = sqlmodel.Relationship()
+
+ # Timestamp
+ ts: datetime.datetime = sqlmodel.Field(
+ sa_column_kwargs = { "server_default" : sqlmodel.text("CURRENT_TIMESTAMP") },
+ )
+
+ # Total Domains
+ total_domains: int
+
+ # Dead Domains
+ dead_domains: int