Skip to content

Commit

Permalink
[template] Improve last articles from week (#4)
Browse files Browse the repository at this point in the history
* Add changelogs

* Add author name to the latest articles from week

* Add count of days since last article in DB
  • Loading branch information
Zhbert authored Aug 19, 2024
1 parent 2aa9a8e commit 5ada57c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelogs/v0.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Changelog v0.2.0

* The date of generation and a link to the utility have been added to the header of the document. ([#3](https://github.com/Zhbert/colligendis/pull/3))
13 changes: 8 additions & 5 deletions internal/common/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package structs

import (
"colligendis/internal/db_service/domain"
"time"
)

Expand All @@ -46,11 +47,13 @@ type HabrArticle struct {
}

type StatsArticle struct {
Id int
Name string
Date time.Time
Views int
Growth int
Id int
Name string
Date time.Time
Views int
Growth int
Author domain.HabrAuthor
DayBefore int
}

type TemplateStruct struct {
Expand Down
12 changes: 12 additions & 0 deletions internal/date_service/date_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package date_service

import (
"time"
)

func GetDaysBefore(dateOfArticle time.Time, todayDate time.Time) int {

diff := todayDate.Sub(dateOfArticle).Hours() / 24

return int(diff)
}
19 changes: 19 additions & 0 deletions internal/db_service/habr_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"colligendis/cmd/common"
"colligendis/internal/common/structs"
"colligendis/internal/common/text"
"colligendis/internal/date_service"
"colligendis/internal/db_service/domain"
"errors"
"gorm.io/driver/sqlite"
Expand Down Expand Up @@ -135,6 +136,22 @@ func getAuthor(db *gorm.DB, name string, flags *common.ColligendisFlags) domain.
}
}

func getAuthorByID(id uint) domain.HabrAuthor {
var author domain.HabrAuthor

db, err := gorm.Open(sqlite.Open("colligendis.db"),
&gorm.Config{Logger: logger.Default.LogMode(getLogger())})
if err != nil {
log.Fatal("Error opening db!")
} else {
result := db.Where("id = ?", id).First(&author)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
log.Println("There is no such author")
}
}
return author
}

func getHubs(db *gorm.DB, hubs []string, flags *common.ColligendisFlags) []domain.HabrHub {
createHubsIfNotExists(db, hubs, flags)
var hubsForDB []domain.HabrHub
Expand Down Expand Up @@ -339,6 +356,8 @@ func GetArticlesFormLastPeriod(dt time.Time, getAll bool, global bool) (int, []s
stat.Id = i
stat.Name = text.CleanText(a.Name)
stat.Date = a.DateOfPublication
stat.Author = getAuthorByID(a.Author.ID)
stat.DayBefore = date_service.GetDaysBefore(a.DateOfPublication, time.Now())
if len(stats) > 1 {
stat.Views = stats[1].Views
stat.Growth = stats[1].Views - stats[0].Views
Expand Down
1 change: 1 addition & 0 deletions templates/tex/preamble
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
\usepackage[left=10mm, top=10mm, right=10mm, bottom=15mm, footskip=7mm]{geometry} % настройки полей документа
\usepackage[active]{srcltx}
\usepackage{indentfirst}
\usepackage{awesomebox}
\usepackage{listings}
\usepackage{float}
\usepackage{longtable}
Expand Down
7 changes: 4 additions & 3 deletions templates/tex/stats.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
\subsubsection{Опубликованные на этой неделе статьи}
\begin{table}[H]
\begin{threeparttable}
\begin{tabularx}{\textwidth}{|X|l|l|}
\begin{tabularx}{\textwidth}{|X|l|l|l|l|}
\hline
\textbf{Название статьи} & \textbf{Дата публикации} & \textbf{Кол-во просмотров} \\ \hline
\textbf{Название статьи} & \textbf{Автор} & \textbf{Дата публикации} & \textbf{Дней назад*} & \textbf{Кол-во просмотров} \\ \hline
{{ range .LatestArticlesFromWeek }}
{{ .Name }} & {{ .Date.Format "2006-01-02" }} & {{ .Growth }} \\ \hline
{{ .Name }} & {{ .Author.Name }} & {{ .Date.Format "2006-01-02" }} & {{ .DayBefore }} & {{ .Growth }} \\ \hline
{{ end }}
\end{tabularx}
\notebox{\textbf{*} — с даты генерации страницы. \newline Дата генерации страницы может отличаться от даты последней загрузки статистики в БД.}
\caption{Статьи, опубликованные на этой неделе}
\end{threeparttable}
\end{table}
Expand Down

0 comments on commit 5ada57c

Please sign in to comment.