Skip to content

Instantly share code, notes, and snippets.

@AlexMocioi
AlexMocioi / IdGenerator
Created May 26, 2013 19:23
Generator numar unic crescator
func idGenerator() chan int {
ids := make(chan int)
go func() {
id := 0 //intializare cu ultimul nr din baza de date
for {
id++
ch <- id
}
}()
return ids
@AlexMocioi
AlexMocioi / Indexer_allDB
Created May 28, 2013 11:20
Reindex views from all design docs, couch db
VIEWS=`curl --request GET -s "http://$server:5984/$database/_all_docs?startkey=%22_design/%22&endkey=%22_design0%22" | awk '{FS="[\":\"]+";if($2 == "id") print $3}'`
for VIEW in $VIEWS
do
#echo "index view: ${VIEW}"
echo "curl -X GET -s \"http://$server:5984/$database/_design/{$urls}\" >/dev/null 2>&1" >> $filename
done
@AlexMocioi
AlexMocioi / dbConnEx
Created June 10, 2013 14:50
Exemplu de conexiune la postgresql si couchdb
<?php
// Connecting, selecting database
$dbconn = pg_connect("host= port= dbname= user= password=")
or die('Could not connect: ' . pg_last_error());
//Tre sa fac validarea asta doar pentru TC.1
$crotalie = 'RO03000021724';
// Performing SQL query
$query = 'select apt.cod_crotalie
from apia_bd2010.application_tag apt
@AlexMocioi
AlexMocioi / couch.js
Created June 15, 2013 17:54
eu am facut in felul urmator, este experimental pentru moment dar functioneaza: 1 trebuie sa fii conectat prin vpn sau in reteaua de la iqm, mergi la adresa: http://172.16.0.52:5984/bd2013/internal_utils/index.html 2. deschizi consola navigatorului (F12) 3. totul este rulat din consola cu navigatorul pornit pentr multe dosare (>500) se poate sa …
$.couch.urlPrefix = "http://localhost:5984";
$.couch.info({
success: function(data) {
console.log(data);
}
});
$.couch.db("bd2013").info({
success: function(data) {
console.log(data);
@AlexMocioi
AlexMocioi / HTTP Handler
Created July 15, 2013 04:10
Describe how a http handler is treated in server.go
func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
done := make(chan bool, 1)
tw := &timeoutWriter{w: w}
go func() {
h.handler.ServeHTTP(tw, r)
done <- true
}()
select {
case <-done:
return
@AlexMocioi
AlexMocioi / PProf
Created July 15, 2013 09:09
Profiling GO web app
in header-ul cu import trebuie adaugata linia:
_ "net/http/pprof"
dupa care se pot accesa urmatoarele url-uri (sunt accesibile si din afara):
http://localhost:8081/debug/pprof/ - centralizatorul cu situatia de facto
http://localhost:8081/pprof/heap
http://localhost:8081/pprof/profile
http://localhost:8081/pprof/block
@AlexMocioi
AlexMocioi / FileLOG
Created July 16, 2013 00:58
Describe how to log to specific file go app output
func init() {
logf, err := os.OpenFile("log/mid_loop_"+ (time.Now()).Format("2006-01-02_15-04-05") +".log", os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
log.Fatalln(err)
}
log.SetOutput(logf)
...
}
pe exit inchide go filehandler sau se poate pune explicit in main
@AlexMocioi
AlexMocioi / Jquery one
Created July 16, 2013 22:01
One time binding event on element
$('#divLeduriComunicatie').one('cancel_GenerarePDF', function() {
...
});
Asta înseamnă că la primul event de tipul ăla, se va executa funcția și se va face automat și unbind, asta e bine că nu mai uiți să faci unbound!
http://api.jquery.com/one/
the handler is removed after the first time the event occurs at the delegated element
@AlexMocioi
AlexMocioi / main.go
Created December 18, 2013 00:34 — forked from tonyhb/main.go
Convert struct to map
package main
import (
"fmt"
"net/url"
"reflect"
"strconv"
)
type Person struct {
@AlexMocioi
AlexMocioi / AmprentaDigitalaDocCouch.js
Created January 18, 2014 08:36
Funcție map-reduce pe CouchDB, face SHA256 pe document:
//Map
function(doc) {
// Machetă document complex
if ( doc.tip=='MD' ) {
var ss = ''+doc.macheta+doc.prd+doc['_id']+doc['_rev'];
for (var i=0,n=doc.valori.length; i<n; i++) {
for (var j=0, m=doc.valori[i].registre.length; j<m; j++) {
var k = doc.valori[i].registre[j];
ss += doc.valori[i].valoare+k[0]+k[1]+k[2]+k[3];
}