This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
skeletonapi.go
167 lines (139 loc) · 8.49 KB
/
skeletonapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package skeleton
import (
"context"
"net/http"
"github.com/DRK-Blutspende-BaWueHe/logcom-api/logcom"
"github.com/DRK-Blutspende-BaWueHe/skeleton/consolelog/repository"
"github.com/DRK-Blutspende-BaWueHe/skeleton/consolelog/service"
"github.com/DRK-Blutspende-BaWueHe/skeleton/server"
"github.com/gin-gonic/gin"
config2 "github.com/DRK-Blutspende-BaWueHe/skeleton/config"
"github.com/DRK-Blutspende-BaWueHe/skeleton/db"
"github.com/DRK-Blutspende-BaWueHe/skeleton/migrator"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
type SkeletonError error
// SkeletonCallbackHandlerV1 - must implement an EventHandler to react on Events triggered by Skeleton
type SkeletonCallbackHandlerV1 interface {
// HandleAnalysisRequests is called to inform and check about new analysis request.
// Returning a not nil will prevent the result being processed further.
// This method should be used e.g. to update standing caches regarding analysis requests whenever a request is received.
HandleAnalysisRequests(request []AnalysisRequest) error
// GetManufacturerTestList is called when the Skeleton requires a list of test names (strings)
// as known to be valid by the manufacturer of this instrument
GetManufacturerTestList(instrumentId uuid.UUID, protocolId uuid.UUID) ([]SupportedManufacturerTests, error)
// GetEncodingList is called when the Skeleton requires a list of supported encodings (strings)
// as known to be valid by the provided protocol
GetEncodingList(protocolId uuid.UUID) ([]string, error)
RevokeAnalysisRequests(request []AnalysisRequest)
}
// SkeletonAPI is the interface for accessing the skeleton driver capabilities
type SkeletonAPI interface {
SetCallbackHandler(eventHandler SkeletonCallbackHandlerV1)
GetCallbackHandler() SkeletonCallbackHandlerV1
// Log : (info) logging to ui console
Log(instrumentID uuid.UUID, msg string)
// Log : (error) logging to ui console
LogError(instrumentID uuid.UUID, err error)
// Log : (debug) logging to ui console
LogDebug(instrumentID uuid.UUID, msg string)
// GetAnalysisRequestWithNoResults - return those requests that have no results yet
//TODO maybe remove
GetAnalysisRequestWithNoResults(ctx context.Context, currentPage, itemsPerPage int) (requests []AnalysisRequest, maxPages int, err error)
GetAnalysisRequestsBySampleCode(ctx context.Context, sampleCode string, allowResending bool) ([]AnalysisRequest, error)
// GetAnalysisRequestsBySampleCodes - Return a list of AnalysisRequests that contains the sampleCodes
// Empty List if nothing is found. Error occurs only on Database-error
// if allowResending is true, returns all analysis requests for the sample code, without checking if they were already sent to the instrument
GetAnalysisRequestsBySampleCodes(ctx context.Context, sampleCodes []string, allowResending bool) (map[string][]AnalysisRequest, error)
GetRequestMappingsByInstrumentID(ctx context.Context, instrumentID uuid.UUID) ([]RequestMapping, error)
SaveAnalysisRequestsInstrumentTransmissions(ctx context.Context, analysisRequestIDs []uuid.UUID, instrumentID uuid.UUID) error
// SubmitAnalysisResult - Submit results to Skeleton and/or Cerberus,
//
// SubmitTypes:
// * <No Parameter given> = SubmitTypeBatchStoreAndSend (default)
// * SubmitTypeBatchStoreAndSend = Batch request and send with a 3 seconds delay
// * SubmitTypeInstantStoreAndSend = Instantly send the Result
// * SubmitTypeStoreOnly = Store the results and do not send to cerberus
// By default this function batches the transmissions by collecting them and
// use the batch-endpoint of cerberus for performance reasons
SubmitAnalysisResult(ctx context.Context, resultData AnalysisResult, submitTypes ...SubmitType) error
// GetInstrument returns all the settings regarding an instrument
// contains AnalyteMappings[] and RequestMappings[]
GetInstrument(ctx context.Context, instrumentID uuid.UUID) (Instrument, error)
// GetInstrumentByIP returns all the settings regarding an instrument
// contains AnalyteMappings[] and RequestMappings[]
GetInstrumentByIP(ctx context.Context, ip string) (Instrument, error)
// GetInstruments - Returns a list of instruments configured for this Driver class
// contains AnalyteMappings[] and RequestMappings[]
GetInstruments(ctx context.Context) ([]Instrument, error)
// FindAnalyteByManufacturerTestCode - Search for the analyte that is mapped (check ui for more info)
// Returns the mapping or model.EmptyAnalyteMapping
FindAnalyteByManufacturerTestCode(instrument Instrument, testCode string) AnalyteMapping
// FindResultEntities - Convienient: Lookup Instrument, AnalysisRequest and ResulteMapping for the analyte at once
FindResultEntities(ctx context.Context, InstrumentID uuid.UUID, SampleCode string, ManufacturerTestCode string) (Instrument, []AnalysisRequest, AnalyteMapping, error)
// FindResultMapping - Helper function to search for the RESULT mapping
// ResultMappings can be made via the ui to translate results
// e.g. "+" -> "pos" to be used in a pein datatype
FindResultMapping(searchValue string, mapping []ResultMapping) (string, error)
// RegisterProtocol - Registers
RegisterProtocol(ctx context.Context, id uuid.UUID, name string, description string, abilities []ProtocolAbility, settings []ProtocolSetting) error
SetOnlineStatus(ctx context.Context, id uuid.UUID, status InstrumentStatus) error
// Start - MUST BE CALLED ON STARTUP
// - migrates skeleton database
// - launches goroutines for analysis request/result processing
Start() error
}
func New(ctx context.Context, sqlConn *sqlx.DB, dbSchema string) (SkeletonAPI, error) {
config, err := config2.ReadConfiguration()
if err != nil {
return nil, err
}
authManager := NewAuthManager(&config,
NewRestyClient(context.Background(), &config, true))
internalApiRestyClient := NewRestyClientWithAuthManager(context.Background(), &config, authManager)
cerberusClient, err := NewCerberusClient(config.CerberusURL, internalApiRestyClient)
if err != nil {
return nil, err
}
deaClient, err := NewDEAClient(config.DeaURL, internalApiRestyClient)
if err != nil {
return nil, err
}
dbConn := db.CreateDbConnector(sqlConn)
manager := NewSkeletonManager(ctx)
instrumentCache := NewInstrumentCache()
analysisRepository := NewAnalysisRepository(dbConn, dbSchema)
instrumentRepository := NewInstrumentRepository(dbConn, dbSchema)
consoleLogRepository := repository.NewConsoleLogRepository(500)
analysisService := NewAnalysisService(analysisRepository, deaClient, cerberusClient, manager)
instrumentService := NewInstrumentService(&config, instrumentRepository, manager, instrumentCache, cerberusClient)
consoleLogSSEServer := server.NewConsoleLogSSEServer(service.NewConsoleLogSSEClientListener())
consoleLogService := service.NewConsoleLogService(consoleLogRepository, consoleLogSSEServer)
api := NewAPI(&config, authManager, analysisService, instrumentService, consoleLogService, consoleLogSSEServer)
logcom.Init(logcom.Configuration{
ServiceName: config.LogComServiceName,
LogComURL: config.LogComURL,
HeaderProvider: func(ctx context.Context) http.Header {
if ginCtx, ok := ctx.(*gin.Context); ok {
return ginCtx.Request.Header
}
return http.Header{}
},
})
return NewSkeleton(ctx, sqlConn, dbSchema, migrator.NewSkeletonMigrator(), api, analysisRepository, analysisService, instrumentService, consoleLogService, manager, cerberusClient, deaClient, config)
}
func NewForTest(ctx context.Context, config config2.Configuration, sqlConn *sqlx.DB, dbSchema string, cerberusClient Cerberus, deaClient DeaClientV1, authManager AuthManager) (SkeletonAPI, error) {
dbConn := db.CreateDbConnector(sqlConn)
manager := NewSkeletonManager(ctx)
instrumentCache := NewInstrumentCache()
analysisRepository := NewAnalysisRepository(dbConn, dbSchema)
instrumentRepository := NewInstrumentRepository(dbConn, dbSchema)
consoleLogRepository := repository.NewConsoleLogRepository(500)
analysisService := NewAnalysisService(analysisRepository, deaClient, cerberusClient, manager)
instrumentService := NewInstrumentService(&config, instrumentRepository, manager, instrumentCache, cerberusClient)
consoleLogSSEServer := server.NewConsoleLogSSEServer(service.NewConsoleLogSSEClientListener())
consoleLogService := service.NewConsoleLogService(consoleLogRepository, consoleLogSSEServer)
api := NewAPI(&config, authManager, analysisService, instrumentService, consoleLogService, consoleLogSSEServer)
return NewSkeleton(ctx, sqlConn, dbSchema, migrator.NewSkeletonMigrator(), api, analysisRepository, analysisService, instrumentService, consoleLogService, manager, cerberusClient, deaClient, config)
}