github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/webdashboard/webdashboard.go (about)

     1  // Copyright 2020 Readium Foundation. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license
     3  // that can be found in the LICENSE file exposed on Github (readium) in the project repository.
     4  
     5  package webdashboard
     6  
     7  import (
     8  	"database/sql"
     9  	"errors"
    10  
    11  	"github.com/readium/readium-lcp-server/config"
    12  )
    13  
    14  // Publication status
    15  const (
    16  	StatusDraft      string = "draft"
    17  	StatusEncrypting string = "encrypting"
    18  	StatusError      string = "error"
    19  	StatusOk         string = "ok"
    20  )
    21  
    22  // ErrNotFound error trown when publication is not found
    23  var ErrNotFound = errors.New("informations not found")
    24  
    25  // WebDashboard interface for publication db interaction
    26  type WebDashboard interface {
    27  	GetDashboardInfos() (Dashboard, error)
    28  	GetDashboardBestSellers() ([5]BestSeller, error)
    29  }
    30  
    31  // Dashboard struct defines a publication
    32  type Dashboard struct {
    33  	PublicationCount int64 `json:"publicationCount"`
    34  	UserCount        int64 `json:"userCount"`
    35  	BuyCount         int64 `json:"buyCount"`
    36  	LoanCount        int64 `json:"loanCount"`
    37  }
    38  
    39  // BestSeller struct defines a best seller
    40  type BestSeller struct {
    41  	Title string `json:"title"`
    42  	Count int64  `json:"count"`
    43  }
    44  
    45  // DashboardManager helper
    46  type DashboardManager struct {
    47  	db               *sql.DB
    48  	dbGetBestSellers *sql.Stmt
    49  }
    50  
    51  // GetDashboardInfos a publication for a given ID
    52  func (dashManager DashboardManager) GetDashboardInfos() (Dashboard, error) {
    53  	//
    54  	var dash Dashboard
    55  
    56  	row := dashManager.db.QueryRow("SELECT COUNT(*) FROM publication")
    57  	row.Scan(&dash.PublicationCount)
    58  
    59  	row = dashManager.db.QueryRow("SELECT COUNT(*) FROM \"user\"")
    60  	row.Scan(&dash.UserCount)
    61  
    62  	row = dashManager.db.QueryRow(`SELECT COUNT(*) FROM purchase WHERE type="BUY"`)
    63  	row.Scan(&dash.BuyCount)
    64  
    65  	row = dashManager.db.QueryRow(`SELECT COUNT(*) FROM purchase WHERE type="LOAN"`)
    66  	row.Scan(&dash.LoanCount)
    67  
    68  	return dash, nil
    69  }
    70  
    71  // GetDashboardBestSellers a publication for a given ID
    72  func (dashManager DashboardManager) GetDashboardBestSellers() ([5]BestSeller, error) {
    73  	rows, err := dashManager.dbGetBestSellers.Query()
    74  	if err != nil {
    75  		return [5]BestSeller{}, err
    76  	}
    77  	defer rows.Close()
    78  
    79  	var bestSellers [5]BestSeller
    80  	i := 0
    81  
    82  	for rows.Next() {
    83  		var newBestSeller BestSeller
    84  		err := rows.Scan(&newBestSeller.Title, &newBestSeller.Count)
    85  		bestSellers[i] = newBestSeller
    86  		i++
    87  		if err != nil {
    88  			return bestSellers, err
    89  		}
    90  	}
    91  	return bestSellers, err
    92  }
    93  
    94  // Init publication manager
    95  func Init(db *sql.DB) (i WebDashboard, err error) {
    96  
    97  	driver, _ := config.GetDatabase(config.Config.FrontendServer.Database)
    98  	var dbGetBestSellers *sql.Stmt
    99  
   100  	if driver == "mssql" {
   101  		dbGetBestSellers, err = db.Prepare(
   102  			`SELECT TOP 5 pub.title, count(pur.id)
   103  				FROM purchase pur 
   104  				JOIN publication pub 
   105  				ON pur.publication_id = pub.id
   106  				GROUP BY pub.title
   107  				ORDER BY  Count(pur.id) DESC`)
   108  	} else {
   109  		dbGetBestSellers, err = db.Prepare(
   110  			`SELECT pub.title, count(pur.id)
   111  				FROM purchase pur 
   112  				JOIN publication pub 
   113  				ON pur.publication_id = pub.id
   114  				 GROUP BY pub.title
   115  				 ORDER BY  Count(pur.id) DESC LIMIT 5`)
   116  	}
   117  
   118  	i = DashboardManager{db, dbGetBestSellers}
   119  	return
   120  }