<commit_before>import { IMethodObserver, ObserveMethod } from "../annotation/observe-method"
import { GetLogger } from "../annotation/get-logger"
import { ILogger } from "../service/i-logger"
import { RVConfig } from "../config/config"
import * as $ from "jquery";

export class RVLogger implements ILogger {
    error(message?: any, ...optionalParams: any[]) {
        throw new Error('Method not implemented.');
    }

    warn(message?: any, ...optionalParams: any[]) {
        throw new Error('Method not implemented.');
    }

    info(message?: any, ...optionalParams: any[]) {
        throw new Error('Method not implemented.');
    }

    debug(toLog: string) {
        console.log("will send to log: ", toLog)
    }

    setClassName(className: string): void {
    }


}

class RVMethodObserver implements IMethodObserver {
    @GetLogger(RVLogger)
    private logger: ILogger

    static clientId: string = null
    static logNumber: number = 0
    static buffer: any[] = []

    private arguments: string = ""
    private result: string = ""
    private startTime: number = 0
    private executionTimeMillis = 0
    private methodName: string = ""

    methodNameCalled(methodName: string): void {
        this.methodName = methodName
    }

    methodCalled(that, ...args: any[]): void {
        this.arguments = args[0].join(", ")
        this.startTime = new Date().getTime()
    }

    methodReturns(that, ...args: any[]): void {
        this.executionTimeMillis = new Date().getTime() - this.startTime
        this.result = args.join(", ")
        this.log()
    }

    log(): void {
        RVMethodObserver.logNumber++;
        if (RVMethodObserver.clientId === null) {
            RVMethodObserver.clientId = this.generateClientId()
        }
        let date = new Date().toDateString()
        let result = {
            timestamp: date,
            clientId: RVMethodObserver.clientId,
            logNumber: RVMethodObserver.logNumber,
            methodName: this.methodName,
            arguments: this.arguments,
            result: this.result,
            executionTimeMS: this.executionTimeMillis
        }
        this.logger.debug(JSON.stringify(result))
        //RVMethodObserver.buffer.push(JSON.stringify(result))
        RVMethodObserver.buffer.push(result)
        if (RVMethodObserver.buffer.length >= RVConfig.BATCH_SIZE) {
            console.log("send A", JSON.stringify(RVMethodObserver.buffer))
            console.log("send B", RVMethodObserver.buffer)
            this.postToServer(JSON.stringify(RVMethodObserver.buffer))
            RVMethodObserver.buffer = []
        }
    }

    postToServer(message: string): void {
        //var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
        let r
        if ((<any>window).XMLHttpRequest) {
            r = new XMLHttpRequest();
        } else {
            r = new ActiveXObject("Microsoft.XMLHTTP");
        }
        r.open("POST", RVConfig.SERVER_ENDPOINT, true)
        r.setRequestHeader("Content-type", "application/json")
        r.send(message)
    }

    // TODO find a better strategy to compute a unique client ID
    generateClientId(): string {
        let rand1 = Math.floor(Math.random() * 1000) + 1
        let rand2 = Math.floor(Math.random() * 1000) + 1
        return "" + new Date().getTime() + "_" + rand1 + "_" + rand2
    }
}

/*
 @RVMethod({
 withArgs:boolean,
 withResult: boolean,
 setLogger: ILogger,
 clientID: string,
 logNumber: number,
 execTime: number
 })
 */
export function RVMethod() {
    return ObserveMethod(new RVMethodObserver())
}<commit_msg>changed date format and buffer-flush on unload<commit_after>import { IMethodObserver, ObserveMethod } from "../annotation/observe-method"
import { GetLogger } from "../annotation/get-logger"
import { ILogger } from "../service/i-logger"
import { RVConfig } from "../config/config"
import * as $ from "jquery";

export class RVLogger implements ILogger {
    error(message?: any, ...optionalParams: any[]) {
        throw new Error('Method not implemented.');
    }

    warn(message?: any, ...optionalParams: any[]) {
        throw new Error('Method not implemented.');
    }

    info(message?: any, ...optionalParams: any[]) {
        throw new Error('Method not implemented.');
    }

    debug(toLog: string) {
        console.log("will send to log: ", toLog)
    }

    setClassName(className: string): void {
    }


}

class RVMethodObserver implements IMethodObserver {
    @GetLogger(RVLogger)
    private logger: ILogger

    static clientId: string = null
    static logNumber: number = 0
    static buffer: any[] = []
    static registered:boolean = false

    private arguments: string = ""
    private result: string = ""
    private startTime: number = 0
    private executionTimeMillis = 0
    private methodName: string = ""

    registerForUnload(): void {
        if(RVMethodObserver.registered) {
            return;
        }
        RVMethodObserver.registered = true
        window.addEventListener("beforeunload", (()=>{
            console.log("flushing buffer...")
            this.flushBuffer()
        }))
    }

    methodNameCalled(methodName: string): void {
        this.methodName = methodName
    }

    methodCalled(that, ...args: any[]): void {
        let argObjects: any[] = args[0]

        this.arguments = argObjects.map((obj) => {
            return JSON.stringify(obj)
        }).join(", ")
        this.startTime = new Date().getTime()
    }

    methodReturns(that, ...args: any[]): void {
        this.executionTimeMillis = new Date().getTime() - this.startTime
        this.result = args.join(", ")
        this.log()
    }

    log(): void {
        RVMethodObserver.logNumber++;
        if (RVMethodObserver.clientId === null) {
            RVMethodObserver.clientId = this.generateClientId()
        }
        let d = new Date()
        let date = ("0" + d.getDate()).slice(-2) + "." + ("0" + (d.getMonth() + 1)).slice(-2) + "." +
            d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":" + d.getSeconds() + ":" + d.getMilliseconds() + "" + d.getTimezoneOffset() / 60;

        let result = {
            timestamp: date,
            clientId: RVMethodObserver.clientId,
            logNumber: RVMethodObserver.logNumber,
            methodName: this.methodName,
            arguments: this.arguments,
            result: this.result,
            executionTimeMS: this.executionTimeMillis
        }
        this.logger.debug(JSON.stringify(result))
        //RVMethodObserver.buffer.push(JSON.stringify(result))
        RVMethodObserver.buffer.push(result)
        if (RVMethodObserver.buffer.length >= RVConfig.BATCH_SIZE) {
            console.log("send A", JSON.stringify(RVMethodObserver.buffer))
            console.log("send B", RVMethodObserver.buffer)
            this.flushBuffer()
        }
    }

    flushBuffer(): void {
        this.postToServer(JSON.stringify(RVMethodObserver.buffer))
        RVMethodObserver.buffer = []
    }

    postToServer(message: string): void {
        //var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
        let r
        if ((<any>window).XMLHttpRequest) {
            r = new XMLHttpRequest();
        } else {
            r = new ActiveXObject("Microsoft.XMLHTTP");
        }
        r.open("POST", RVConfig.SERVER_ENDPOINT, true)
        r.setRequestHeader("Content-type", "application/json")
        r.send(message)
    }

    // TODO find a better strategy to compute a unique client ID
    generateClientId(): string {
        let rand1 = Math.floor(Math.random() * 1000) + 1
        let rand2 = Math.floor(Math.random() * 1000) + 1
        return "" + new Date().getTime() + "_" + rand1 + "_" + rand2
    }
}

/*
 @RVMethod({
 withArgs:boolean,
 withResult: boolean,
 setLogger: ILogger,
 clientID: string,
 logNumber: number,
 execTime: number
 })
 */
export function RVMethod() {
    let obj = new RVMethodObserver()
    obj.registerForUnload()
    return ObserveMethod(obj)
}
{'fraction_non_alphanumeric': 0.13362336855189558, 'fraction_numerical': 0.00584213797389683, 'mean_word_length': 2.4651162790697674, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1272851', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2254, 'n_words': 517}