Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add code formatter #1507

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[flake8]
ignore = C901, W504
max-line-length = 125
exclude = .git,__pycache__,old,build,dist,*/migrations/*.py
extend-ignore = C901, W504, E203, E701
max-line-length = 130
exclude = .git,__pycache__,old,build,dist,*migrations*,*snapshots*
max-complexity = 10
per-file-ignores =
/**/tests/*_mock_data.py: E501
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Python lint check

on:
push:
branches:
- develop
pull_request:


jobs:
pre_commit_checks:
name: 🚴 Pre-Commit checks 🚴
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@main
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@main
with:
cache: 'poetry'
- run: poetry install
- uses: pre-commit/action@main
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
default_language_version:
python: python3

# NOTE: Update in .flake8 pyproject.toml as well
exclude: |
(?x)^(
\.git|
__pycache__|
.*snap_test_.*\.py|
.+\/.+\/migrations\/.*
)

repos:
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.8.10
3.12
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.8-slim-buster as base
FROM python:3.12-slim-bullseye as base

LABEL maintainer="Deep Dev dev@thedeep.io"

Expand Down
2 changes: 1 addition & 1 deletion apps/analysis/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class AnalysisConfig(AppConfig):
name = 'analysis'
name = "analysis"
144 changes: 77 additions & 67 deletions apps/analysis/dataloaders.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
from promise import Promise
from collections import defaultdict

from django.utils.functional import cached_property
from django.db import models
from django.utils.functional import cached_property
from promise import Promise

from utils.graphene.dataloaders import DataLoaderWithContext, WithContextMixin

from .models import (
Analysis,
AnalysisPillar,
AnalysisReport,
AnalysisReportContainer,
AnalysisReportContainerData,
AnalysisReportSnapshot,
AnalysisReportUpload,
AnalyticalStatement,
AnalyticalStatementEntry,
DiscardedEntry,
TopicModelCluster,
AnalysisReportUpload,
AnalysisReportContainerData,
AnalysisReportContainer,
AnalysisReportSnapshot,
)


class AnalysisPublicationDatesLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalyticalStatementEntry.objects.filter(
analytical_statement__analysis_pillar__analysis__in=keys,
).order_by().values('analytical_statement__analysis_pillar__analysis').annotate(
published_on_min=models.Min('entry__lead__published_on'),
published_on_max=models.Max('entry__lead__published_on'),
).values_list(
'published_on_min',
'published_on_max',
'analytical_statement__analysis_pillar__analysis'
qs = (
AnalyticalStatementEntry.objects.filter(
analytical_statement__analysis_pillar__analysis__in=keys,
)
.order_by()
.values("analytical_statement__analysis_pillar__analysis")
.annotate(
published_on_min=models.Min("entry__lead__published_on"),
published_on_max=models.Max("entry__lead__published_on"),
)
.values_list("published_on_min", "published_on_max", "analytical_statement__analysis_pillar__analysis")
)
_map = {}
for start_date, end_date, _id in qs:
Expand All @@ -45,17 +47,13 @@ def batch_load_fn(self, keys):

class AnalysisAnalyzedEntriesLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
_map = Analysis.get_analyzed_entries([
Analysis(id=key) for key in keys
])
_map = Analysis.get_analyzed_entries([Analysis(id=key) for key in keys])
return Promise.resolve([_map.get(key, 0) for key in keys])


class AnalysisAnalyzedLeadsLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
_map = Analysis.get_analyzed_sources([
Analysis(id=key) for key in keys
])
_map = Analysis.get_analyzed_sources([Analysis(id=key) for key in keys])
return Promise.resolve([_map.get(key, 0) for key in keys])


Expand Down Expand Up @@ -88,57 +86,71 @@ def batch_load_fn(self, keys):

class AnalysisPillarsAnalyzedEntriesLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisPillar.objects\
.filter(id__in=keys)\
qs = (
AnalysisPillar.objects.filter(id__in=keys)
.annotate(
dragged_entries=models.functions.Coalesce(
models.Subquery(
AnalyticalStatement.objects.filter(
analysis_pillar=models.OuterRef('pk')
).order_by().values('analysis_pillar').annotate(count=models.Count(
'entries',
distinct=True,
filter=models.Q(entries__lead__published_on__lte=models.OuterRef('analysis__end_date'))))
.values('count')[:1],
AnalyticalStatement.objects.filter(analysis_pillar=models.OuterRef("pk"))
.order_by()
.values("analysis_pillar")
.annotate(
count=models.Count(
"entries",
distinct=True,
filter=models.Q(entries__lead__published_on__lte=models.OuterRef("analysis__end_date")),
)
)
.values("count")[:1],
output_field=models.IntegerField(),
), 0),
),
0,
),
discarded_entries=models.functions.Coalesce(
models.Subquery(
DiscardedEntry.objects.filter(
analysis_pillar=models.OuterRef('pk')
).order_by().values('analysis_pillar__analysis').annotate(count=models.Count(
'entry',
distinct=True,
filter=models.Q(entry__lead__published_on__lte=models.OuterRef('analysis__end_date'))))
.values('count')[:1],
DiscardedEntry.objects.filter(analysis_pillar=models.OuterRef("pk"))
.order_by()
.values("analysis_pillar__analysis")
.annotate(
count=models.Count(
"entry",
distinct=True,
filter=models.Q(entry__lead__published_on__lte=models.OuterRef("analysis__end_date")),
)
)
.values("count")[:1],
output_field=models.IntegerField(),
), 0),
analyzed_entries=models.F('dragged_entries') + models.F('discarded_entries'),
).values_list('id', 'analyzed_entries')
_map = {
_id: count
for _id, count in qs
}
),
0,
),
analyzed_entries=models.F("dragged_entries") + models.F("discarded_entries"),
)
.values_list("id", "analyzed_entries")
)
_map = {_id: count for _id, count in qs}
return Promise.resolve([_map.get(key, 0) for key in keys])


class AnalysisStatementAnalyzedEntriesLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalyticalStatement.objects.filter(id__in=keys).annotate(
count=models.Count('entries', distinct=True)
).values('id', 'count')
_map = {
_id: count
for _id, count in qs
}
qs = (
AnalyticalStatement.objects.filter(id__in=keys)
.annotate(count=models.Count("entries", distinct=True))
.values("id", "count")
)
_map = {_id: count for _id, count in qs}
return Promise.resolve([_map.get(key, 0) for key in keys])


class AnalysisTopicModelClusterEntryLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = TopicModelCluster.entries.through.objects.filter(
topicmodelcluster__in=keys,
).select_related('entry').order_by('topicmodelcluster', 'entry_id')
qs = (
TopicModelCluster.entries.through.objects.filter(
topicmodelcluster__in=keys,
)
.select_related("entry")
.order_by("topicmodelcluster", "entry_id")
)
_map = defaultdict(list)
for cluster_entry in qs:
_map[cluster_entry.topicmodelcluster_id].append(cluster_entry.entry)
Expand All @@ -151,10 +163,7 @@ def batch_load_fn(self, keys):
qs = AnalysisReportUpload.objects.filter(
id__in=keys,
)
_map = {
item.pk: item
for item in qs
}
_map = {item.pk: item for item in qs}
return Promise.resolve([_map.get(key, []) for key in keys])


Expand All @@ -173,7 +182,7 @@ class OrganizationByAnalysisReportLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReport.organizations.through.objects.filter(
analysisreport__in=keys,
).select_related('organization')
).select_related("organization")
_map = defaultdict(list)
for item in qs:
_map[item.analysisreport_id].append(item.organization)
Expand Down Expand Up @@ -204,13 +213,14 @@ def batch_load_fn(self, keys):

class LatestReportSnapshotByAnalysisReportLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
qs = AnalysisReportSnapshot.objects.filter(
report__in=keys,
).order_by('report_id', '-published_on').distinct('report_id')
_map = {
snapshot.report_id: snapshot
for snapshot in qs
}
qs = (
AnalysisReportSnapshot.objects.filter(
report__in=keys,
)
.order_by("report_id", "-published_on")
.distinct("report_id")
)
_map = {snapshot.report_id: snapshot for snapshot in qs}
return Promise.resolve([_map.get(key) for key in keys])


Expand Down
Loading
Loading