Last active
April 16, 2018 11:06
-
-
Save matryer/3adf57d2798fd2db27d95976746e14ba to your computer and use it in GitHub Desktop.
Classify log items using Classificationbox by Machine Box - https://machinebox.io/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| logclass | |
| 1. Run Classificationbox (see https://machinebox.io/docs/classificationbox) | |
| 2. Create a model and train it two classes with IDs: "noise" and "interesting" | |
| 3. Execute this command and pipe logs through it | |
| Any logs that are not "noise" are passed through, others will be dimmed. | |
| */ | |
| func main() { | |
| if err := run(); err != nil { | |
| log.Fatalln(err) | |
| } | |
| } | |
| func run() error { | |
| var ( | |
| cbAddr = flag.String("cb", "http://localhost:8080", "Classificationbox address") | |
| modelID = flag.String("model", "", "ID of the model to use") | |
| in = flag.String("in", "", "input file (default stdin)") | |
| out = flag.String("out", "", "output file (default stdout)") | |
| ) | |
| flag.Parse() | |
| if *modelID == "" { | |
| return errors.New("missing model ID") | |
| } | |
| cb := classificationbox.New(*cbAddr) | |
| ctx, cancel := context.WithCancel(context.Background()) | |
| defer cancel() | |
| if err := boxutil.WaitForReady(ctx, cb); err != nil { | |
| return err | |
| } | |
| var r io.Reader | |
| if *in != "" { | |
| inFile, err := os.Open(*in) | |
| if err != nil { | |
| return errors.Wrap(err, "in") | |
| } | |
| defer inFile.Close() | |
| r = inFile | |
| } else { | |
| r = os.Stdin | |
| } | |
| var w io.Writer | |
| if *out != "" { | |
| outFile, err := os.Create(*out) | |
| if err != nil { | |
| return errors.Wrap(err, "out") | |
| } | |
| defer outFile.Close() | |
| w = outFile | |
| } else { | |
| w = os.Stdout | |
| } | |
| s := bufio.NewScanner(r) | |
| for s.Scan() { | |
| req := classificationbox.PredictRequest{ | |
| Limit: 1, // we just need the top class | |
| Inputs: []classificationbox.Feature{ | |
| classificationbox.FeatureText("logitem", s.Text()), | |
| }, | |
| } | |
| ctxPredict, cancelPredict := context.WithTimeout(ctx, 250*time.Millisecond) | |
| resp, err := cb.Predict(ctxPredict, *modelID, req) | |
| if err != nil { | |
| cancelPredict() | |
| return err | |
| } | |
| cancelPredict() | |
| if len(resp.Classes) > 0 { | |
| if resp.Classes[0].ID != "noise" { | |
| io.WriteString(w, `\e[2m`) // dim text | |
| } else { | |
| io.WriteString(w, `\e[0m`) // normal text | |
| } | |
| if _, err := io.WriteString(w, s.Text()); err != nil { | |
| return err | |
| } | |
| } | |
| } | |
| if err := s.Err(); err != nil { | |
| return errors.Wrap(err, "scanner") | |
| } | |
| return nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment