Skip to content

Commit f9ee05c

Browse files
Nihal SrinivasuNihal Srinivasu
Nihal Srinivasu
authored and
Nihal Srinivasu
committed
Updated from old org
1 parent a0a90d9 commit f9ee05c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1948
-0
lines changed

Diff for: ad-devops/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ad-devops
2+
This repo will contain all DevOps code for the project Ad-pulse.

Diff for: ad-manager-svc/.github/workflows/build_push.yaml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Build and Push to Docker Hub
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
10+
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
11+
12+
jobs:
13+
bump-version:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
20+
- name: Bump version
21+
run: |
22+
old_version=$(cat version.txt)
23+
regex="([0-9]+)\.([0-9]+)\.([0-9]+)"
24+
if [[ $old_version =~ $regex ]]; then
25+
major="${BASH_REMATCH[1]}"
26+
minor="${BASH_REMATCH[2]}"
27+
patch="${BASH_REMATCH[3]}"
28+
new_patch=$((patch + 1))
29+
new_version="${major}.${minor}.${new_patch}"
30+
echo "Old version: $old_version"
31+
echo "New version: $new_version"
32+
echo $new_version > version.txt
33+
else
34+
echo "Invalid version format: $old_version"
35+
fi
36+
37+
- name: Commit changes
38+
run: |
39+
git config --global user.name "GitHub Actions"
40+
git config --global user.email "[email protected]"
41+
git add version.txt
42+
git commit -m "Bump version to $new_version"
43+
git push https://${{ secrets.PAT }}@github.com/${{ github.repository }} HEAD:main
44+
build:
45+
needs: bump-version
46+
runs-on: ubuntu-latest
47+
48+
steps:
49+
- name: Checkout code
50+
uses: actions/checkout@v2
51+
52+
- name: Set up Docker Buildx
53+
uses: docker/setup-buildx-action@v1
54+
55+
- name: Login to Docker Hub
56+
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
57+
58+
- name: Build and push Docker image
59+
run: |
60+
docker buildx create --use
61+
docker buildx build --platform linux/amd64,linux/arm64 --push -t adpulse18/$(basename ${{ github.repository }}):$(cat version.txt) .

Diff for: ad-manager-svc/.gitignore

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

Diff for: ad-manager-svc/Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Python runtime as the base image
2+
FROM python:3.9
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Upgrade pip
8+
RUN pip install --no-cache-dir --upgrade pip
9+
10+
# Copy the requirements file into the container
11+
COPY requirements.txt .
12+
13+
# Install the Python dependencies
14+
RUN pip install --no-cache-dir -r requirements.txt
15+
16+
# Copy the rest of the application code into the container
17+
COPY . .
18+
19+
# Set the command to run the application
20+
CMD [ "python", "run.py" ]

Diff for: ad-manager-svc/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ad-manager-svc

Diff for: ad-manager-svc/app/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Flask
2+
from config import Config
3+
4+
def create_app():
5+
app = Flask(__name__)
6+
app.config.from_object(Config)
7+
8+
from app.cache import cache_blueprint
9+
app.register_blueprint(cache_blueprint)
10+
11+
return app

Diff for: ad-manager-svc/app/cache/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .cache_routes import cache_blueprint
2+
from .cache_job import fetch_and_cache_active_campaigns
3+
4+
__all__ = ['cache_blueprint', 'fetch_and_cache_active_campaigns']

Diff for: ad-manager-svc/app/cache/cache_job.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import json
2+
from flask import current_app
3+
from config.redis import get_redis_client
4+
from app.models.campaign import Campaign
5+
from app.models.ad import Ad
6+
from config.db import create_session
7+
8+
redis_client = get_redis_client()
9+
10+
def fetch_and_cache_active_campaigns():
11+
12+
with current_app.app_context():
13+
14+
session = create_session()
15+
16+
redis_client.delete('C*')
17+
18+
active_campaigns = session.query(Campaign).filter(Campaign.campaignstate == 'ACTIVE').all()
19+
20+
session.close()
21+
22+
cache_active_campaigns(active_campaigns)
23+
24+
def cache_active_campaigns(active_campaigns):
25+
for campaign in active_campaigns:
26+
key = campaign.campaignid
27+
campaign_json = {key: getattr(campaign, key) for key in campaign.__dict__.keys() if key != '_sa_instance_state'}
28+
campaign_json['startdate'] = campaign_json['startdate'].isoformat() if campaign_json['startdate'] else None
29+
campaign_json['enddate'] = campaign_json['enddate'].isoformat() if campaign_json['enddate'] else None
30+
campaign_json['createdat'] = campaign_json['createdat'].isoformat() if campaign_json['createdat'] else None
31+
campaign_json['updatedat'] = campaign_json['updatedat'].isoformat() if campaign_json['updatedat'] else None
32+
value = json.dumps(campaign_json)
33+
redis_client.set(key, value)
34+
35+
def fetch_and_cache_active_ads():
36+
37+
with current_app.app_context():
38+
39+
session = create_session()
40+
41+
redis_client.delete('campaigns:*')
42+
43+
active_ads = session.query(Ad).filter(Ad.adstate == 'ACTIVE').all()
44+
45+
session.close()
46+
47+
cache_active_ads(active_ads)
48+
49+
def cache_active_ads(active_ads):
50+
for ad in active_ads:
51+
key = f'campaign:{ad.campaignid}'
52+
ad_json = {key: getattr(ad, key) for key in ad.__dict__.keys() if key != '_sa_instance_state'}
53+
ad_json['startdate'] = ad_json['startdate'].isoformat() if ad_json['startdate'] else None
54+
ad_json['enddate'] = ad_json['enddate'].isoformat() if ad_json['enddate'] else None
55+
ad_json['createdat'] = ad_json['createdat'].isoformat() if ad_json['createdat'] else None
56+
ad_json['updatedat'] = ad_json['updatedat'].isoformat() if ad_json['updatedat'] else None
57+
value = json.dumps(ad_json)
58+
redis_client.hset(key, ad.adid, value)

Diff for: ad-manager-svc/app/cache/cache_routes.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from time import sleep
2+
from flask import jsonify, Blueprint
3+
from config.redis import get_redis_client
4+
import json
5+
from app.cache.cache_job import fetch_and_cache_active_campaigns, fetch_and_cache_active_ads
6+
7+
cache_blueprint = Blueprint('cache', __name__)
8+
9+
redis_client = get_redis_client()
10+
11+
@cache_blueprint.route('/cache/campaigns', methods=['GET'])
12+
def get_cache():
13+
fetch_and_cache_active_campaigns()
14+
# sleep(5)
15+
all_keys = redis_client.keys('C*')
16+
cache_data = []
17+
for key in all_keys:
18+
value = redis_client.get(key)
19+
cache_data.append(json.loads(value))
20+
return jsonify(cache_data)
21+
22+
@cache_blueprint.route('/cache/ads', methods=['GET'])
23+
def get_cached_ads():
24+
25+
fetch_and_cache_active_ads()
26+
ads_by_campaign = {}
27+
28+
# Iterate over each key in the Redis database
29+
for key in redis_client.scan_iter("campaign:*"):
30+
campaign_id = key.split(':')[1]
31+
32+
# Get all ad IDs and their corresponding data for the campaign
33+
ad_data = redis_client.hgetall(key)
34+
35+
# Convert ad data from bytes to string and then to dictionary
36+
ad_data_dict = {ad_id: json.loads(ad_data[ad_id]) for ad_id in ad_data}
37+
38+
# Add the ad data to the ads_by_campaign dictionary
39+
ads_by_campaign[campaign_id] = ad_data_dict
40+
41+
return jsonify(ads_by_campaign)

Diff for: ad-manager-svc/app/enums/States.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from sqlalchemy import Enum
2+
3+
class States(Enum):
4+
CREATED = 'CREATED'
5+
ACTIVE = 'ACTIVE'
6+
INACTIVE = 'INACTIVE'
7+
EXPIRED = 'EXPIRED'
8+
EXHAUSTED = 'EXHAUSTED'

Diff for: ad-manager-svc/app/models/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)