Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshettyin committed Jan 12, 2025
1 parent 77b4c07 commit 84e0fa0
Show file tree
Hide file tree
Showing 30 changed files with 2,857 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pg-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
# Keep environment variables out of version control
.env
logs
dist
18 changes: 18 additions & 0 deletions pg-worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:20-alpine3.19

WORKDIR /var/kafka

COPY package.json ./
COPY package-lock.json ./

RUN npm install

COPY . .

RUN npm run build

# Add a health check to verify the application is running
# HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
# CMD curl --fail http://localhost:8080/health || exit 1

CMD [ "npm", "run", "start" ]
Empty file added pg-worker/env.example
Empty file.
29 changes: 29 additions & 0 deletions pg-worker/fixtures/mvs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Create a materialized view for total visits per code
CREATE MATERIALIZED VIEW public.total_visits_per_code AS
SELECT
code,
COUNT(*) AS total_visits
FROM
public."ClickAnalytics"
GROUP BY
code;

-- Optional: Create an index on the materialized view for faster lookups
CREATE INDEX idx_total_visits_per_code ON public.total_visits_per_code (code);


CREATE OR REPLACE FUNCTION refresh_total_visits_per_code()
RETURNS TRIGGER AS $$
BEGIN
-- Refresh the materialized view
PERFORM pg_notify('refresh_total_visits', 'Refreshing total visits view');
REFRESH MATERIALIZED VIEW public.total_visits_per_code;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;


CREATE TRIGGER refresh_view_on_clickanalytics_update
AFTER INSERT OR UPDATE OR DELETE ON public."ClickAnalytics"
FOR EACH STATEMENT
EXECUTE FUNCTION refresh_total_visits_per_code();
Loading

0 comments on commit 84e0fa0

Please sign in to comment.