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

retornando média por membro #138

Merged
merged 3 commits into from
Sep 13, 2024
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ func (c *Client) GetAllAgencyCollection(agency string) ([]models.AgencyMonthlyIn
}
return collections, nil
}

func (c *Client) GetAveragePerCapita(agency string, year int) (*models.PerCapitaData, error) {
avg, err := c.Db.GetAveragePerCapita(agency, year)
if err != nil {
return nil, fmt.Errorf("GetAveragePerCapita() error: %w", err)
}
return avg, nil
}
24 changes: 14 additions & 10 deletions models/monthlyInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,20 @@ type GeneralMonthlyInfo struct {
}

type AnnualSummary struct {
Year int `json:"year,omitempty"` // Year of the data
AverageCount int `json:"average_count,omitempty"` // Average number of employees
TotalCount int `json:"total_count,omitempty"` // Total number of employees
BaseRemuneration float64 `json:"base_remuneration,omitempty"` // Statistics (Max, Min, Median, Total)
OtherRemunerations float64 `json:"other_remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
Discounts float64 `json:"discounts,omitempty"` // Statistics (Max, Min, Median, Total)
Remunerations float64 `json:"remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
NumMonthsWithData int `json:"months_with_data,omitempty"`
Package *Backup `json:"package,omitempty"`
ItemSummary ItemSummary `json:"item_summary,omitempty"`
Year int `json:"year,omitempty"` // Year of the data
AverageCount int `json:"average_count,omitempty"` // Average number of employees
TotalCount int `json:"total_count,omitempty"` // Total number of employees
BaseRemuneration float64 `json:"base_remuneration,omitempty"` // Statistics (Max, Min, Median, Total)
OtherRemunerations float64 `json:"other_remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
BaseRemunerationPerCapita float64 `json:"base_remuneration_member,omitempty"`
OtherRemunerationsPerCapita float64 `json:"other_remunerations_member,omitempty"`
DiscountsPerCapita float64 `json:"discounts_member,omitempty"`
RemunerationsPerCapita float64 `json:"remunerations_member,omitempty"`
Discounts float64 `json:"discounts,omitempty"` // Statistics (Max, Min, Median, Total)
Remunerations float64 `json:"remunerations,omitempty"` // Statistics (Max, Min, Median, Total)
NumMonthsWithData int `json:"months_with_data,omitempty"`
Package *Backup `json:"package,omitempty"`
ItemSummary ItemSummary `json:"item_summary,omitempty"`
}

type RemmunerationSummary struct {
Expand Down
10 changes: 10 additions & 0 deletions models/perCapitaData.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type PerCapitaData struct {
AgencyID string `json:"orgao,omitempty"`
Year int `json:"ano,omitempty"`
BaseRemuneration float64 `json:"remuneracao_base,omitempty"`
OtherRemunerations float64 `json:"outras_remuneracoes,omitempty"`
Discounts float64 `json:"descontos,omitempty"`
Remunerations float64 `json:"remuneracoes,omitempty"`
}
15 changes: 15 additions & 0 deletions repo/database/database_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 37 additions & 25 deletions repo/database/dto/annuaISummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@ import (
)

type AnnualSummaryDTO struct {
Year int `gorm:"column:ano"`
AverageCount int `gorm:"column:media_num_membros"`
TotalCount int `gorm:"column:total_num_membros"`
BaseRemuneration float64 `gorm:"column:remuneracao_base"`
OtherRemunerations float64 `gorm:"column:outras_remuneracoes"`
Discounts float64 `gorm:"column:descontos"`
Remunerations float64 `gorm:"column:remuneracoes"`
NumMonthsWithData int `gorm:"column:meses_com_dados"`
ItemSummary ItemSummary `gorm:"embedded"`
Year int `gorm:"column:ano"`
AverageCount int `gorm:"column:media_num_membros"`
TotalCount int `gorm:"column:total_num_membros"`
BaseRemuneration float64 `gorm:"column:remuneracao_base"`
OtherRemunerations float64 `gorm:"column:outras_remuneracoes"`
Discounts float64 `gorm:"column:descontos"`
Remunerations float64 `gorm:"column:remuneracoes"`
BaseRemunerationPerCapita float64 `gorm:"column:remuneracao_base_membro"`
OtherRemunerationsPerCapita float64 `gorm:"column:outras_remuneracoes_membro"`
DiscountsPerCapita float64 `gorm:"column:descontos_membro"`
RemunerationsPerCapita float64 `gorm:"column:remuneracoes_membro"`
NumMonthsWithData int `gorm:"column:meses_com_dados"`
ItemSummary ItemSummary `gorm:"embedded"`
}

func NewAnnualSummaryDTO(ami models.AnnualSummary) *AnnualSummaryDTO {
return &AnnualSummaryDTO{
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
BaseRemunerationPerCapita: ami.BaseRemunerationPerCapita,
OtherRemunerationsPerCapita: ami.OtherRemunerationsPerCapita,
DiscountsPerCapita: ami.DiscountsPerCapita,
RemunerationsPerCapita: ami.RemunerationsPerCapita,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
ItemSummary: ItemSummary{
FoodAllowance: ami.ItemSummary.FoodAllowance,
BonusLicense: ami.ItemSummary.BonusLicense,
Expand All @@ -41,14 +49,18 @@ func NewAnnualSummaryDTO(ami models.AnnualSummary) *AnnualSummaryDTO {

func (ami *AnnualSummaryDTO) ConvertToModel() *models.AnnualSummary {
return &models.AnnualSummary{
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
Year: ami.Year,
AverageCount: ami.AverageCount,
TotalCount: ami.TotalCount,
BaseRemuneration: ami.BaseRemuneration,
OtherRemunerations: ami.OtherRemunerations,
BaseRemunerationPerCapita: ami.BaseRemunerationPerCapita,
OtherRemunerationsPerCapita: ami.OtherRemunerationsPerCapita,
DiscountsPerCapita: ami.DiscountsPerCapita,
RemunerationsPerCapita: ami.RemunerationsPerCapita,
Discounts: ami.Discounts,
Remunerations: ami.Remunerations,
NumMonthsWithData: ami.NumMonthsWithData,
ItemSummary: models.ItemSummary{
FoodAllowance: ami.ItemSummary.FoodAllowance,
BonusLicense: ami.ItemSummary.BonusLicense,
Expand Down
27 changes: 27 additions & 0 deletions repo/database/dto/perCapitaDataDTO.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dto

import "github.com/dadosjusbr/storage/models"

type PerCapitaData struct {
AgencyID string `gorm:"column:orgao"`
Year int `gorm:"column:ano"`
BaseRemuneration float64 `gorm:"column:salario"`
OtherRemunerations float64 `gorm:"column:beneficios"`
Discounts float64 `gorm:"column:descontos"`
Remunerations float64 `gorm:"column:remuneracao"`
}

func (PerCapitaData) TableName() string {
return "media_por_membro"
}

func (a *PerCapitaData) ConvertToModel() *models.PerCapitaData {
return &models.PerCapitaData{
AgencyID: a.AgencyID,
Year: a.Year,
BaseRemuneration: a.BaseRemuneration,
OtherRemunerations: a.OtherRemunerations,
Discounts: a.Discounts,
Remunerations: a.Remunerations,
}
}
22 changes: 22 additions & 0 deletions repo/database/init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ create table remuneracoes
constraint pk_remuneracoes primary key (id, id_contracheque, orgao, mes, ano),
constraint fk_remuneracoes foreign key (id_contracheque, orgao, mes, ano) references contracheques(id, orgao, mes, ano) on delete cascade
);

CREATE MATERIALIZED VIEW public.media_por_membro
TABLESPACE pg_default
AS SELECT media_por_membro.orgao,
media_por_membro.ano,
avg(media_por_membro.salario) AS salario,
avg(media_por_membro.beneficios) AS beneficios,
avg(media_por_membro.descontos) AS descontos,
avg(media_por_membro.remuneracao) AS remuneracao
FROM ( SELECT c.orgao,
c.ano,
c.nome_sanitizado,
count(*) AS num_meses,
avg(c.salario) AS salario,
avg(c.beneficios) AS beneficios,
avg(c.descontos) AS descontos,
avg(c.remuneracao) AS remuneracao
FROM contracheques c
GROUP BY c.orgao, c.ano, c.nome_sanitizado) media_por_membro
WHERE media_por_membro.num_meses > 1
GROUP BY media_por_membro.orgao, media_por_membro.ano
WITH DATA;
1 change: 1 addition & 0 deletions repo/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ type Interface interface {
GetAllAgencyCollection(agency string) ([]models.AgencyMonthlyInfo, error)
GetPaychecks(agency models.Agency, year int) ([]models.Paycheck, error)
GetPaycheckItems(agency models.Agency, year int) ([]models.PaycheckItem, error)
GetAveragePerCapita(agency string, year int) (*models.PerCapitaData, error)
}
25 changes: 21 additions & 4 deletions repo/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (p *PostgresDB) GetAnnualSummary(agency string) ([]models.AnnualSummary, er
var dtoAmis []dto.AnnualSummaryDTO
agency = strings.ToLower(agency)
query := `
ano,
coletas.ano,
id_orgao,
TRUNC(AVG((sumario -> 'membros')::text::int)) AS media_num_membros,
SUM((sumario -> 'membros')::text::int) AS total_num_membros,
Expand All @@ -323,10 +323,16 @@ func (p *PostgresDB) GetAnnualSummary(agency string) ([]models.AnnualSummary, er
SUM(CAST(sumario -> 'resumo_rubricas' ->> 'auxilio_saude' AS DECIMAL)) AS auxilio_saude,
SUM(CAST(sumario -> 'resumo_rubricas' ->> 'outras' AS DECIMAL)) AS outras,
SUM(CAST(sumario -> 'resumo_rubricas' ->> 'ferias' AS DECIMAL)) AS ferias,
COUNT(*) AS meses_com_dados`
m := p.db.Model(&dtoAgmi).Select(query)
COUNT(*) AS meses_com_dados,
MAX(media_por_membro.salario) AS remuneracao_base_membro,
MAX(media_por_membro.beneficios) AS outras_remuneracoes_membro,
MAX(media_por_membro.descontos) AS descontos_membro,
MAX(media_por_membro.remuneracao) AS remuneracoes_membro`

join := `LEFT JOIN media_por_membro on coletas.ano = media_por_membro.ano and coletas.id_orgao = media_por_membro.orgao`
m := p.db.Model(&dtoAgmi).Select(query).Joins(join)
m = m.Where("id_orgao = ? AND atual = TRUE AND (procinfo::text = 'null' OR procinfo IS NULL) ", agency)
m = m.Group("ano, id_orgao").Order("ano ASC")
m = m.Group("coletas.ano, id_orgao").Order("coletas.ano ASC")
if err := m.Scan(&dtoAmis).Error; err != nil {
return nil, fmt.Errorf("error getting annual monthly info: %q", err)
}
Expand Down Expand Up @@ -549,3 +555,14 @@ func (p *PostgresDB) GetPaycheckItems(agency models.Agency, year int) ([]models.
}
return results, nil
}

func (p *PostgresDB) GetAveragePerCapita(agency string, ano int) (*models.PerCapitaData, error) {
var dtoAvg dto.PerCapitaData
m := p.db.Model(&dto.PerCapitaData{})
m = m.Where("orgao = ? AND ano = ?", agency, ano)
if err := m.Find(&dtoAvg).Error; err != nil {
return nil, fmt.Errorf("error getting average per capita: %q", err)
}
avg := dtoAvg.ConvertToModel()
return avg, nil
}
Loading
Loading