github.com/Kindred87/Obsidian@v0.0.0-20210809203756-86936424b848/server/definitions.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sync"
     7  
     8  	"github.com/Kindred87/Obsidian/datasource"
     9  	"github.com/Kindred87/Obsidian/generation"
    10  	"github.com/Kindred87/Obsidian/retrieval/html"
    11  
    12  	"github.com/Kindred87/Obsidian/api"
    13  )
    14  
    15  type ObsidianServer struct {
    16  	api.UnimplementedObsidianServer
    17  
    18  	mu sync.Mutex
    19  }
    20  
    21  func NewServer() *ObsidianServer {
    22  	s := &ObsidianServer{}
    23  	return s
    24  }
    25  
    26  // AddDatasource creates or overwrites a datasource configuration file.
    27  func (s *ObsidianServer) AddDatasource(ctx context.Context, req *api.DatasourceAddRequest) (*api.Response, error) {
    28  	logRequest(fmt.Sprintf("datasource addition with alias %s", req.Alias()))
    29  
    30  	err := datasource.Add(req, req.OverwriteExisting())
    31  	if err != nil {
    32  		logError(err)
    33  		return nil, err
    34  	}
    35  
    36  	logProcessed()
    37  
    38  	return &api.Response{Message: "Added " + req.Alias()}, nil
    39  }
    40  
    41  // CheckForDatasource determines whether a file matching a datasource specifications exists.
    42  func (s *ObsidianServer) CheckForDatasource(ctx context.Context, check *api.CheckDatasourceRequest) (*api.CheckDatasourceResponse, error) {
    43  	logRequest(fmt.Sprintf("datasource check for alias %s", check.DatasourceAlias))
    44  
    45  	result, path, err := datasource.Find(check.DatasourceAlias)
    46  	if err != nil {
    47  		logError(err)
    48  		return nil, err
    49  	}
    50  
    51  	logProcessed()
    52  
    53  	if result {
    54  		fmt.Print("Located datasource.\n\n")
    55  		return &api.CheckDatasourceResponse{Message: "Datasource located", FilePath: path}, nil
    56  	} else {
    57  		fmt.Print("Failed to locate datasource.\n\n")
    58  		return &api.CheckDatasourceResponse{Message: "Datasource could not be located"}, nil
    59  	}
    60  }
    61  
    62  // HTMLNodeHelper lists nodes containing target values and their parents to assist in query structuring.
    63  func (s *ObsidianServer) HTMLNodeHelper(ctx context.Context, req *api.HTMLNodeRequest) (*api.HTMlNodeResponse, error) {
    64  	if req.Help {
    65  		logRequest("Help for HTMLNodeHelper")
    66  		logProcessed()
    67  		return &api.HTMlNodeResponse{Message: api.HTMLNodeRequestHelp()}, nil
    68  	}
    69  
    70  	logRequest(fmt.Sprintf("HTMLNodeHelper for alias %s", req.DatasourceAlias))
    71  
    72  	logActionAndTargets("Searching for nodes containing", req.ValuesToFind)
    73  
    74  	nodeLists, err := html.HierarchyFor(req.DatasourceAlias, req.ValuesToFind, int(req.ParentLimit))
    75  	if err != nil {
    76  		logError(err)
    77  		return nil, err
    78  	}
    79  
    80  	logProcessed()
    81  	return &api.HTMlNodeResponse{Message: []string{"Success"}, NodeList: htmlNodeHelperStringConv(nodeLists)}, nil
    82  }
    83  
    84  // HTMLSearch identifies collections of HTML nodes related to the collection of nodes described in the given request.
    85  func (s *ObsidianServer) HTMLSearch(ctx context.Context, req *api.HTMLSearchRequest) (*api.HTMLSearchResponse, error) {
    86  	logRequest("HTML search for alias " + req.DatasourceAlias)
    87  
    88  	nList, err := html.CollectionSiblings(req.DatasourceAlias, req.SearchGroupsAsStrings())
    89  	if err != nil {
    90  		logError(err)
    91  		return nil, err
    92  	}
    93  
    94  	err = generation.NodesToCsv(nList)
    95  	if err != nil {
    96  		logError(err)
    97  		return nil, err
    98  	}
    99  
   100  	logProcessed()
   101  
   102  	return &api.HTMLSearchResponse{
   103  		Message: []string{"Success", fmt.Sprintf("%d records processed", len(nList))},
   104  		Results: htmlSearchStringConv(nList)}, nil
   105  }