Skip to content

Commit

Permalink
feat: add search_analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Aug 1, 2024
1 parent b8b09fa commit cf2c5f1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- migrate:up
create table search_analytics (
query text primary key,
occurences int not null default 0,
created_at timestamptz default current_timestamp not null,
updated_at timestamptz default current_timestamp not null
);

create trigger search_analytics_moddatetime
before update on search_analytics
for each row
execute procedure moddatetime (updated_at);

-- migrate:down
drop trigger search_analytics_moddatetime on search_analytics;
drop table search_analytics;
30 changes: 29 additions & 1 deletion apps/backend/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ CREATE TABLE public.schema_migrations (
);


--
-- Name: search_analytics; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.search_analytics (
query text NOT NULL,
occurences integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


--
-- Name: analytics analytics_foreign_id_table_name_day_key; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -404,6 +416,14 @@ ALTER TABLE ONLY public.schema_migrations
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);


--
-- Name: search_analytics search_analytics_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.search_analytics
ADD CONSTRAINT search_analytics_pkey PRIMARY KEY (query);


--
-- Name: package_type_fun_signature_documentation; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -474,6 +494,13 @@ CREATE TRIGGER package_release_moddatetime BEFORE UPDATE ON public.package_relea
CREATE TRIGGER package_type_fun_signature_moddatetime BEFORE UPDATE ON public.package_type_fun_signature FOR EACH ROW EXECUTE FUNCTION public.moddatetime('updated_at');


--
-- Name: search_analytics search_analytics_moddatetime; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER search_analytics_moddatetime BEFORE UPDATE ON public.search_analytics FOR EACH ROW EXECUTE FUNCTION public.moddatetime('updated_at');


--
-- Name: package_module package_module_package_release_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -539,4 +566,5 @@ INSERT INTO public.schema_migrations (version) VALUES
('20240517083006'),
('20240518232212'),
('20240521174525'),
('20240521204341');
('20240521204341'),
('20240801164720');
16 changes: 16 additions & 0 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ pub fn upsert_most_recent_hex_timestamp(db: pgo.Connection, latest: Time) {
})
}

pub fn upsert_search_analytics(db: pgo.Connection, query: String) {
"INSERT INTO search_analytics (query)
VALUES ($1)
ON CONFLICT (query) DO UPDATE
SET occurences = search_analytics.occurences + 1
RETURNING *"
|> pgo.execute(db, [pgo.text(query)], dynamic.dynamic)
|> result.map_error(error.DatabaseError)
|> result.try(fn(response) {
let err = "Upsert search analytics failed"
response.rows
|> list.first()
|> result.replace_error(error.UnknownError(err))
})
}

pub fn upsert_hex_user(db: pgo.Connection, owner: hexpm.PackageOwner) {
let username = pgo.text(owner.username)
let email = pgo.nullable(pgo.text, owner.email)
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn empty_json() {

fn search(query: String, ctx: Context) {
wisp.log_notice("Searching for " <> query)
let _ = queries.upsert_search_analytics(ctx.db, query) |> io.debug

let exact_type_searches =
option.then(ctx.type_search_subject, fn(subject) {
Expand Down

0 comments on commit cf2c5f1

Please sign in to comment.