This is a basic idea to tag jobs

Basic rules for job tagging are stored in "rules.json", and this file calls this file and performs a comparison with the meta.json files
This commit is contained in:
AmritanshuV 2024-08-15 12:35:07 +02:00 committed by GitHub
parent 649d50812b
commit 9fdcdfaa4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,18 +2,21 @@
// All rights reserved. // All rights reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package tagger package main
import ( import (
"bufio" "bufio"
"embed" "embed"
"encoding/json"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/ClusterCockpit/cc-backend/internal/repository" "github.com/ClusterCockpit/cc-backend/internal/repository"
"github.com/ClusterCockpit/cc-backend/pkg/log" "github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/ClusterCockpit/cc-backend/pkg/schema" "github.com/ClusterCockpit/cc-backend/pkg/schema"
"github.com/diegoholiveira/jsonlogic"
) )
const tagType = "app" const tagType = "app"
@ -28,6 +31,7 @@ type appInfo struct {
type AppTagger struct { type AppTagger struct {
apps []appInfo apps []appInfo
jsonRule map[string]interface{} // Store a single JSON rule
} }
func (t *AppTagger) Register() error { func (t *AppTagger) Register() error {
@ -56,17 +60,57 @@ func (t *AppTagger) Register() error {
return nil return nil
} }
// LoadRule function to load JSON logic rule from rules.json
func (t *AppTagger) LoadRule() error {
file, err := os.Open("rules.json")
if err != nil {
return fmt.Errorf("error opening rules file: %v", err)
}
defer file.Close()
var rule map[string]interface{}
decoder := json.NewDecoder(file)
if err := decoder.Decode(&rule); err != nil {
return fmt.Errorf("error decoding rule: %v", err)
}
t.jsonRule = rule
return nil
}
func (t *AppTagger) Match(job *schema.Job) { func (t *AppTagger) Match(job *schema.Job) {
r := repository.GetJobRepository() r := repository.GetJobRepository()
meta, err := r.FetchMetadata(job) meta, err := r.FetchMetadata(job)
if err != nil { if err != nil {
log.Error("cannot fetch meta data") log.Error("cannot fetch meta data")
return
} }
// Prepare the data for JSON logic evaluation
data := map[string]interface{}{
"metaData": meta,
"statistics": job.Statistics,
}
jobscript, ok := meta["jobScript"] jobscript, ok := meta["jobScript"]
if ok { if ok {
id := job.ID id := job.ID
out: out:
// Apply JSON logic rule
if t.jsonRule != nil {
result, err := jsonlogic.Apply(t.jsonRule, data)
if err != nil {
log.Errorf("error applying JSON logic: %v", err)
} else if match, ok := result.(bool); ok && match {
tag := "detectedApp" // Define a tag for detected apps
if !r.HasTag(id, tagType, tag) {
r.AddTagOrCreate(id, tagType, tag)
break out
}
}
}
// Original app matching logic remains for backward compatibility (if needed).
for _, a := range t.apps { for _, a := range t.apps {
tag := a.tag tag := a.tag
for _, s := range a.strings { for _, s := range a.strings {
@ -82,3 +126,25 @@ func (t *AppTagger) Match(job *schema.Job) {
log.Infof("Cannot extract job script for job: %d on %s", job.JobID, job.Cluster) log.Infof("Cannot extract job script for job: %d on %s", job.JobID, job.Cluster)
} }
} }
func main() {
appTagger := &AppTagger{}
// Load the JSON logic rule
if err := appTagger.LoadRule(); err != nil {
log.Fatalf("Failed to load rules: %v", err)
}
// Assume you have a job to process
var job *schema.Job
// Fetch or define the job data here
// Apply the matching logic
appTagger.Match(job)
}
//
//why are we using json logics, cannot we simply use read input and apply if/else logic to it??
//the first part is to give the json logic an input
//making rules for taggingthe data