Добавил БД

This commit is contained in:
2025-05-20 17:29:10 +03:00
parent dca5697b43
commit 040ca140c8
8 changed files with 138 additions and 2 deletions

3
.gitignore vendored
View File

@@ -58,6 +58,9 @@ cover/
# Django stuff:
*.log
*.db
*.sqlite3
*.sql
local_settings.py
db.sqlite3
db.sqlite3-journal

0
__init__.py Normal file
View File

1
classes/__init__.py Normal file
View File

@@ -0,0 +1 @@
from .classes import Activity, Tag

View File

@@ -7,9 +7,9 @@ from enums import ClockifyProjects, VetroProjects
@dataclass
class Activity:
id: str
task_id: int
vetro_project_id: VetroProjects
vetro_project_title: str
task_id: int
task_title: str
tag_id: str
description: str

View File

@@ -1,6 +1,8 @@
import argparse
from pydantic_settings import BaseSettings
from db.db import Database
from .clockify import ClockifyConfig # noqa: F401
from .redmine import RedmineConfig # noqa: F401
@@ -12,6 +14,8 @@ class Config(BaseSettings):
CLOCKIFY_USER_ID: str
CLOCKIFY_WORKSPACE_ID: str
DB_NAME: str = "RedmineTracker.db"
DATETIME_FORMAT: str = "%Y-%m-%dT%H:%M:%SZ"
VETRO_PROJECTS: dict = {
@@ -57,3 +61,4 @@ def configure_argument_parser():
arg_parser = configure_argument_parser()
config = Config()
db = Database(config.DB_NAME)

0
db/__init__.py Normal file
View File

126
db/db.py Normal file
View File

@@ -0,0 +1,126 @@
import logging
import sqlite3 as sq
from classes import Activity, Tag
class Database:
def __init__(self, db_name: str):
try:
self.con = sq.connect(db_name)
self.con.cursor().executescript('''CREATE TABLE IF NOT EXISTS "activities" (
"id" VARCHAR NOT NULL UNIQUE,
"vetro_project_id" VARCHAR NOT NULL,
"vetro_project_title" VARCHAR NOT NULL,
"task_id" VARCHAR NOT NULL,
"description" VARCHAR NOT NULL,
"task_title" VARCHAR NOT NULL,
"tag_id" VARCHAR NOT NULL,
"project_id" VARCHAR NOT NULL,
"time_spent" NUMERIC NOT NULL,
"date_start" DATETIME NOT NULL,
"date_end" DATETIME,
"is_tracked" BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY("id"),
FOREIGN KEY ("tag_id") REFERENCES "tags"("id")
ON UPDATE NO ACTION ON DELETE NO ACTION
);
CREATE TABLE IF NOT EXISTS "tags" (
"id" VARCHAR NOT NULL UNIQUE,
"title" VARCHAR NOT NULL,
PRIMARY KEY("id")
);
CREATE TABLE IF NOT EXISTS "vetro_projects" (
"id" VARCHAR NOT NULL UNIQUE,
"title" VARCHAR NOT NULL,
PRIMARY KEY("id"),
FOREIGN KEY ("id") REFERENCES "activities"("vetro_project_id")
ON UPDATE NO ACTION ON DELETE NO ACTION
);
CREATE TABLE IF NOT EXISTS "time_entry" (
"id" INTEGER NOT NULL UNIQUE,
"activity_id" VARCHAR NOT NULL,
"task_id" INTEGER NOT NULL,
"date" DATETIME NOT NULL,
"time_spent" NUMERIC NOT NULL,
"description" VARCHAR NOT NULL,
"activity_type" INTEGER NOT NULL CHECK(activity_type in (9, 8, 57, 58, 60)),
PRIMARY KEY("id"),
FOREIGN KEY ("activity_id") REFERENCES "activities"("id")
ON UPDATE NO ACTION ON DELETE NO ACTION,
FOREIGN KEY ("task_id") REFERENCES "activities"("task_id")
ON UPDATE NO ACTION ON DELETE NO ACTION
);
''')
except Exception as e:
raise(e)
def insert_activities(self, activities: list[Activity]):
cur = self.con.cursor()
for act in activities:
try:
cur.execute(
"INSERT INTO activities VALUES(?, ?, ? ,? ,? ,? ,? ,? ,? ,? ,? ,?)",
(
act.id,act.vetro_project_id,
act.vetro_project_title,
act.task_id,
act.description,
act.task_title,
act.tag_id,
act.project_id,
act.time_spent,
act.date_start,
act.date_end,
act.is_tracked
)
)
self.con.commit()
except Exception as e:
logging.error(e)
# Доделать
def insert_time_entries(self, activities: list[Activity]):
cur = self.con.cursor()
for act in activities:
try:
cur.execute(
"INSERT INTO time_entry VALUES(?, ?, ? ,? ,? ,? ,? ,? ,? ,? ,? ,?)",
(
act.id,act.vetro_project_id,
act.vetro_project_title,
act.task_id,
act.description,
act.task_title,
act.tag_id,
act.project_id,
act.time_spent,
act.date_start,
act.date_end,
act.is_tracked
)
)
self.con.commit()
except Exception as e:
logging.error(e)
def insert_tags(self, tags: list[Tag]):
cur = self.con.cursor()
for tag in tags:
try:
cur.execute(
"INSERT INTO tags VALUES(?, ?)",
(
tag.id,
tag.title
)
)
self.con.commit()
except Exception as e:
logging.error(e)

View File

@@ -2,7 +2,7 @@ import logging
import sys
from api import ClockifyAPI, RedmineAPI
from config import arg_parser, config
from config import arg_parser, config, db
from utils import format_date, str_to_date, today_start, today_end
@@ -37,6 +37,7 @@ def main():
"page-size": args.page_size,
},
)
db.insert_activities(activities)
# tracked_activities = red.track_activities(activities)
# clock.mark_tracked(tracked_activities)
for activity in activities: