Skip to content

Commit

Permalink
Merge pull request #26 from KAnanev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KAnanev committed Aug 4, 2023
2 parents 46fae82 + 16ff899 commit 66e63ea
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
# we want to test our package on several versions of Python
python-version: [3.11]
python-version: ['3.10', '3.11']
services:
postgres:
image: postgres:latest
Expand Down
58 changes: 58 additions & 0 deletions page_analyzer/services/sql_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
GET_ITEMS = """SELECT
json_build_object(
'id', urls.id,
'name', urls.name,
'created_at', urls.created_at,
'url_checks', COALESCE(json_agg(json_build_object(
'id', latest_url_checks.id,
'url_id', latest_url_checks.url_id,
'status_code', latest_url_checks.status_code,
'h1', latest_url_checks.h1,
'title', latest_url_checks.title,
'description', latest_url_checks.description,
'created_at', latest_url_checks.created_at
)) FILTER (WHERE latest_url_checks.id IS NOT NULL), '[]'::json)
) AS result
FROM urls
LEFT JOIN LATERAL (
SELECT *
FROM url_checks
WHERE url_checks.url_id = urls.id
ORDER BY url_checks.created_at DESC
LIMIT 1
) AS latest_url_checks ON true
GROUP BY urls.id;"""
GET_JSON_BY_ID = """SELECT
json_build_object(
'id', urls.id,
'name', urls.name,
'created_at', urls.created_at,
'url_checks', COALESCE(json_agg(json_build_object(
'id', url_checks.id,
'url_id', url_checks.url_id,
'status_code', url_checks.status_code,
'h1', url_checks.h1,
'title', url_checks.title,
'description', url_checks.description,
'created_at', url_checks.created_at
)) FILTER (WHERE url_checks.id IS NOT NULL) , '[]'::json)
) AS result
FROM urls
LEFT JOIN url_checks ON urls.id = url_checks.url_id
WHERE urls.id = (%s)
GROUP BY urls.id;"""
GET_JSON_BY_URL = """SELECT
json_build_object(
'id', id,
'name', name,
'created_at', created_at
) AS result
FROM urls WHERE name = (%s)"""
INSERT_ITEM_RETURN_JSON = """INSERT INTO
urls (name, created_at)
VALUES (%s,%s)
RETURNING json_build_object(
'id', id,
'name', name,
'created', created_at
) AS result;"""
68 changes: 6 additions & 62 deletions page_analyzer/services/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,12 @@

from page_analyzer.models import URLModel, URLSModel
from page_analyzer.services.db import PostgresDB

GET_ITEMS = """SELECT
json_build_object(
'id', urls.id,
'name', urls.name,
'created_at', urls.created_at,
'url_checks', COALESCE(json_agg(json_build_object(
'id', latest_url_checks.id,
'url_id', latest_url_checks.url_id,
'status_code', latest_url_checks.status_code,
'h1', latest_url_checks.h1,
'title', latest_url_checks.title,
'description', latest_url_checks.description,
'created_at', latest_url_checks.created_at
)) FILTER (WHERE latest_url_checks.id IS NOT NULL), '[]'::json)
) AS result
FROM urls
LEFT JOIN LATERAL (
SELECT *
FROM url_checks
WHERE url_checks.url_id = urls.id
ORDER BY url_checks.created_at DESC
LIMIT 1
) AS latest_url_checks ON true
GROUP BY urls.id;"""

GET_JSON_BY_ID = """SELECT
json_build_object(
'id', urls.id,
'name', urls.name,
'created_at', urls.created_at,
'url_checks', COALESCE(json_agg(json_build_object(
'id', url_checks.id,
'url_id', url_checks.url_id,
'status_code', url_checks.status_code,
'h1', url_checks.h1,
'title', url_checks.title,
'description', url_checks.description,
'created_at', url_checks.created_at
)) FILTER (WHERE url_checks.id IS NOT NULL) , '[]'::json)
) AS result
FROM urls
LEFT JOIN url_checks ON urls.id = url_checks.url_id
WHERE urls.id = (%s)
GROUP BY urls.id;"""

GET_JSON_BY_URL = """SELECT
json_build_object(
'id', id,
'name', name,
'created_at', created_at
) AS result
FROM urls WHERE name = (%s)"""

INSERT_ITEM_RETURN_JSON = """INSERT INTO
urls (name, created_at)
VALUES (%s,%s)
RETURNING json_build_object(
'id', id,
'name', name,
'created', created_at
) AS result;"""
from page_analyzer.services.sql_query import (
GET_ITEMS,
GET_JSON_BY_ID,
GET_JSON_BY_URL,
INSERT_ITEM_RETURN_JSON
)


class URLService:
Expand Down
Loading

0 comments on commit 66e63ea

Please sign in to comment.